from vnpy.trader.constant import Direction from vnpy.trader.object import TradeData, OrderData, TickData from vnpy.trader.engine import BaseEngine from vnpy.trader.constant import OrderType, Offset, Direction from ..template import AlgoTemplate import math class MyGridAlgo(AlgoTemplate): """""" display_name = "My Grid Tradding 网格交易 " default_setting = { "step_price": 1.0, "step_volume": 1, "interval": 1, "grid_up":100, "grid_down":50 } variables = [ "pos", "long_pos", "short_pos", "timer_count", "vt_orderid", "grid_bid_base", "grid_ask_base" ] def __init__( self, algo_engine: BaseEngine, algo_name: str, vt_symbol: str, direction: str, offset: str, price: float, volume: float, setting: dict ): """""" super().__init__(algo_engine, algo_name,vt_symbol, direction, offset, price, volume, setting) # Parameters self.step_price = setting["step_price"] self.step_volume = setting["step_volume"] self.interval = setting["interval"] self.grid_up=setting['grid_up'] self.grid_down=setting['grid_down'] self.grid_bid_base=0 self.grid_ask_base=0 # Variables self.timer_count = 0 self.vt_orderid = "" self.pos = 0 self.long_pos=0 self.short_pos=0 self.last_tick = None self.put_event() def on_tick(self, tick: TickData): """""" self.last_tick = tick def on_timer(self): """""" if not self.last_tick: return self.timer_count += 1 if self.timer_count < self.interval: self.put_event return self.timer_count = 0 if self.vt_orderid: self.cancel_all() bid_diff = self.last_tick.bid_price_1 - self.grid_bid_base;##平多或者开空的价差(买入价价差) ask_diff = self.last_tick.ask_price_1 - self.grid_ask_base;##开多或者平空的价差(卖出价价差) # 市场下跌时买多 if ask_diff <= -1* self.step_price: self.grid_ask_base = self.last_tick.ask_price_1 self.grid_bid_base= self.last_tick.bid_price_1 if self.grid_ask_base >=self.grid_down and self.grid_ask_base<=self.grid_up: self.write_log("下跌开多") ##下跌时加多仓(加多仓位) self.vt_orderid= self.buy( self.last_tick.ask_price_1, self.step_volume, OrderType.LIMIT, Offset.OPEN ) ##下跌时平空仓(平空获利) self.buy( self.last_tick.ask_price_1, self.step_volume, OrderType.LIMIT, Offset.CLOSE ) # 市场上涨时平多 elif bid_diff >= self.step_price: self.write_log("上涨平多") self.grid_bid_base = self.last_tick.bid_price_1 self.grid_ask_base=self.last_tick.ask_price_1 if self.grid_bid_base >=self.grid_down and self.grid_bid_base<=self.grid_up: ##上涨时,平多仓 self.sell( self.last_tick.bid_price_1, self.step_volume, OrderType.LIMIT, Offset.CLOSE ) ##上涨时开空仓 self.sell( self.last_tick.bid_price_1, self.step_volume, OrderType.LIMIT, Offset.OPEN ) # Update UI self.put_event() def on_order(self, order: OrderData): """""" if not order.is_active(): self.vt_orderid = "" self.put_event def on_trade(self, trade: TradeData): """""" if trade.direction == Direction.LONG: if trade.offset==Offset.OPEN: self.long_pos += trade.volume self.pos += trade.volume else: self.short_pos -= trade.volume self.pos -= trade.volume else: if trade.offset==Offset.OPEN: self.short_pos += trade.volume self.pos -= trade.volume else: self.long_pos -= trade.volume self.pos -=trade.volume self.put_event
标签:VNPY,bid,self,price,算法,base,grid,tick,交易 From: https://www.cnblogs.com/ip99/p/17648583.html