[Mod] change logic of grid algo

This commit is contained in:
vn.py 2019-06-14 16:03:45 +08:00
parent 23792474b1
commit ad36f0ead4
3 changed files with 22 additions and 40 deletions

View File

@ -91,8 +91,6 @@ def run_parent():
): ):
trading = True trading = True
trading = True
# Start child process in trading period # Start child process in trading period
if trading and child_process is None: if trading and child_process is None:
print("启动子进程") print("启动子进程")

View File

@ -13,18 +13,15 @@ class GridAlgo(AlgoTemplate):
default_setting = { default_setting = {
"vt_symbol": "", "vt_symbol": "",
"price": 0.0, "price": 0.0,
"trade_count": 0.0,
"step_price": 0.0, "step_price": 0.0,
"step_volume": 0, "step_volume": 0,
"interval": 0, "interval": 10,
} }
variables = [ variables = [
"last_pos", "pos",
"timer_count", "timer_count",
"vt_orderid", "vt_orderid"
"traded",
"count"
] ]
def __init__( def __init__(
@ -39,17 +36,14 @@ class GridAlgo(AlgoTemplate):
# Parameters # Parameters
self.vt_symbol = setting["vt_symbol"] self.vt_symbol = setting["vt_symbol"]
self.price = setting["price"] self.price = setting["price"]
self.trade_count = setting["trade_count"]
self.step_price = setting["step_price"] self.step_price = setting["step_price"]
self.step_volume = setting["step_volume"] self.step_volume = setting["step_volume"]
self.interval = setting["interval"] self.interval = setting["interval"]
# Variables # Variables
self.vt_orderid = ""
self.count = 0
self.traded = 0
self.last_pos = 0
self.timer_count = 0 self.timer_count = 0
self.vt_orderid = ""
self.pos = 0
self.last_tick = None self.last_tick = None
self.subscribe(self.vt_symbol) self.subscribe(self.vt_symbol)
@ -74,12 +68,19 @@ class GridAlgo(AlgoTemplate):
if self.vt_orderid: if self.vt_orderid:
self.cancel_all() self.cancel_all()
# Calculate target volume to buy # Calculate target volume to buy and sell
target_buy_distance = ( target_buy_distance = (
self.price - self.last_tick.ask_price_1) / self.step_price self.price - self.last_tick.ask_price_1) / self.step_price
target_buy_position = math.floor( target_buy_position = math.floor(
target_buy_distance) * self.step_volume target_buy_distance) * self.step_volume
target_buy_volume = target_buy_position - self.last_pos target_buy_volume = target_buy_position - self.pos
# Calculate target volume to sell
target_sell_distance = (
self.price - self.last_tick.bid_price_1) / self.step_price
target_sell_position = math.ceil(
target_sell_distance) * self.step_volume
target_sell_volume = self.pos - target_sell_position
# Buy when price dropping # Buy when price dropping
if target_buy_volume > 0: if target_buy_volume > 0:
@ -88,22 +89,17 @@ class GridAlgo(AlgoTemplate):
self.last_tick.ask_price_1, self.last_tick.ask_price_1,
min(target_buy_volume, self.last_tick.ask_volume_1) min(target_buy_volume, self.last_tick.ask_volume_1)
) )
# Calculate target volume to sell
target_sell_distance = (
self.price - self.last_tick.bid_price_1) / self.step_price
target_sell_position = math.ceil(
target_sell_distance) * self.step_volume
target_sell_volume = self.last_pos - target_sell_position
# Sell when price rising # Sell when price rising
if target_sell_volume > 0: elif target_sell_volume > 0:
self.vt_orderid = self.sell( self.vt_orderid = self.sell(
self.vt_symbol, self.vt_symbol,
self.last_tick.bid_price_1, self.last_tick.bid_price_1,
min(target_sell_volume, self.last_tick.bid_volume_1) min(target_sell_volume, self.last_tick.bid_volume_1)
) )
# Update UI
self.put_variables_event()
def on_order(self, order: OrderData): def on_order(self, order: OrderData):
"""""" """"""
if not order.is_active(): if not order.is_active():
@ -111,21 +107,10 @@ class GridAlgo(AlgoTemplate):
self.put_variables_event() self.put_variables_event()
def on_trade(self, trade: TradeData): def on_trade(self, trade: TradeData):
""""""
self.last_pos = self.update_last_pos(self.last_pos, trade)
self.traded += trade.volume
self.count += 1
if self.count >= self.trade_count:
self.write_log(f"已交易数量:{self.traded},总成交次数:{self.count}")
self.stop()
else:
self.put_variables_event()
def update_last_pos(self, pos, trade: TradeData):
"""""" """"""
if trade.direction == Direction.LONG: if trade.direction == Direction.LONG:
pos += trade.volume self.pos += trade.volume
else: else:
pos -= trade.volume self.pos -= trade.volume
return pos
self.put_variables_event()

View File

@ -15,7 +15,6 @@ NAME_DISPLAY_MAP = {
"display_volume": "挂出数量", "display_volume": "挂出数量",
"stop_price": "触发价格", "stop_price": "触发价格",
"price_add": "委托超价", "price_add": "委托超价",
"trade_count": "交易次数限制",
"step_price": "网格交易间距", "step_price": "网格交易间距",
"step_volume": "网格交易数量", "step_volume": "网格交易数量",
"order_type": "类型", "order_type": "类型",