[Mod] change logic of grid algo
This commit is contained in:
parent
23792474b1
commit
ad36f0ead4
@ -91,8 +91,6 @@ def run_parent():
|
||||
):
|
||||
trading = True
|
||||
|
||||
trading = True
|
||||
|
||||
# Start child process in trading period
|
||||
if trading and child_process is None:
|
||||
print("启动子进程")
|
||||
|
@ -13,18 +13,15 @@ class GridAlgo(AlgoTemplate):
|
||||
default_setting = {
|
||||
"vt_symbol": "",
|
||||
"price": 0.0,
|
||||
"trade_count": 0.0,
|
||||
"step_price": 0.0,
|
||||
"step_volume": 0,
|
||||
"interval": 0,
|
||||
"interval": 10,
|
||||
}
|
||||
|
||||
variables = [
|
||||
"last_pos",
|
||||
"pos",
|
||||
"timer_count",
|
||||
"vt_orderid",
|
||||
"traded",
|
||||
"count"
|
||||
"vt_orderid"
|
||||
]
|
||||
|
||||
def __init__(
|
||||
@ -39,17 +36,14 @@ class GridAlgo(AlgoTemplate):
|
||||
# Parameters
|
||||
self.vt_symbol = setting["vt_symbol"]
|
||||
self.price = setting["price"]
|
||||
self.trade_count = setting["trade_count"]
|
||||
self.step_price = setting["step_price"]
|
||||
self.step_volume = setting["step_volume"]
|
||||
self.interval = setting["interval"]
|
||||
|
||||
# Variables
|
||||
self.vt_orderid = ""
|
||||
self.count = 0
|
||||
self.traded = 0
|
||||
self.last_pos = 0
|
||||
self.timer_count = 0
|
||||
self.vt_orderid = ""
|
||||
self.pos = 0
|
||||
self.last_tick = None
|
||||
|
||||
self.subscribe(self.vt_symbol)
|
||||
@ -74,12 +68,19 @@ class GridAlgo(AlgoTemplate):
|
||||
if self.vt_orderid:
|
||||
self.cancel_all()
|
||||
|
||||
# Calculate target volume to buy
|
||||
# Calculate target volume to buy and sell
|
||||
target_buy_distance = (
|
||||
self.price - self.last_tick.ask_price_1) / self.step_price
|
||||
target_buy_position = math.floor(
|
||||
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
|
||||
if target_buy_volume > 0:
|
||||
@ -88,22 +89,17 @@ class GridAlgo(AlgoTemplate):
|
||||
self.last_tick.ask_price_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
|
||||
if target_sell_volume > 0:
|
||||
elif target_sell_volume > 0:
|
||||
self.vt_orderid = self.sell(
|
||||
self.vt_symbol,
|
||||
self.last_tick.bid_price_1,
|
||||
min(target_sell_volume, self.last_tick.bid_volume_1)
|
||||
)
|
||||
|
||||
# Update UI
|
||||
self.put_variables_event()
|
||||
|
||||
def on_order(self, order: OrderData):
|
||||
""""""
|
||||
if not order.is_active():
|
||||
@ -111,21 +107,10 @@ class GridAlgo(AlgoTemplate):
|
||||
self.put_variables_event()
|
||||
|
||||
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:
|
||||
pos += trade.volume
|
||||
self.pos += trade.volume
|
||||
else:
|
||||
pos -= trade.volume
|
||||
return pos
|
||||
self.pos -= trade.volume
|
||||
|
||||
self.put_variables_event()
|
||||
|
@ -15,7 +15,6 @@ NAME_DISPLAY_MAP = {
|
||||
"display_volume": "挂出数量",
|
||||
"stop_price": "触发价格",
|
||||
"price_add": "委托超价",
|
||||
"trade_count": "交易次数限制",
|
||||
"step_price": "网格交易间距",
|
||||
"step_volume": "网格交易数量",
|
||||
"order_type": "类型",
|
||||
|
Loading…
Reference in New Issue
Block a user