1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| def on_5min_bar(self, bar: BarData): for orderid in self.vt_orderids: self.cancel_order(orderid) self.vt_orderids.clear()
am = self.am am.update_bar(bar) if not am.inited: return
self.kk_up, self.kk_down = am.keltner(self.kk_length, self.kk_dev)
if self.pos == 0: self.intra_trade_high = bar.high_price self.intra_trade_low = bar.low_price self.send_oco_order(self.kk_up, self.kk_down, self.fixed_size)
elif self.pos > 0: self.intra_trade_high = max(self.intra_trade_high, bar.high_price) self.intra_trade_low = bar.low_price
vt_orderids = self.sell(self.intra_trade_high * (1 - self.trailing_percent / 100), abs(self.pos), True) self.vt_orderids.extend(vt_orderids)
elif self.pos < 0: self.intra_trade_high = bar.high_price self.intra_trade_low = min(self.intra_trade_low, bar.low_price)
vt_orderids = self.cover(self.intra_trade_low * (1 + self.trailing_percent / 100), abs(self.pos), True) self.vt_orderids.extend(vt_orderids)
self.put_event()
def on_trade(self, trade: TradeData): if self.pos != 0: if self.pos > 0: for short_orderid in self.short_vt_orderids: self.cancel_order(short_orderid)
elif self.pos < 0: for buy_orderid in self.long_vt_orderids: self.cancel_order(buy_orderid)
for orderid in (self.long_vt_orderids + self.short_vt_orderids): if orderid in self.vt_orderids: self.vt_orderids.remove(orderid)
self.put_event()
def send_oco_order(self, buy_price, short_price, volume): self.long_vt_orderids = self.buy(buy_price, volume, True) self.short_vt_orderids = self.short(short_price, volume, True)
self.vt_orderids.extend(self.long_vt_orderids) self.vt_orderids.extend(self.short_vt_orderids)
|