Market If Touched (MIT) Orders (Simple Order Algo)
NOTE : As the MIT is monitoring trade by trade you must not clear event triggers ie service.clear_event_triggers()
Example order.send(self.symbol, 'buy', 10, type='MIT', stop=1125)
Not for use with live trading
This order method is for use with backtesting only. It isn't supported in live trading.
ParametersArgument | Required | Description | Example(s) |
---|---|---|---|
symbol | Yes | The symbol for the order. | QQQ |
side | Yes | The "buy" or "sell" indicator. | buy, sell |
qty | Yes | The order quantity. | 100 |
type | Yes | The order type. | MIT |
stop | Yes | The trigger price | 193.00 |
start_time | No | The time that the order is to be sent. Defaults to now. Only available for Elite. | md.market_open_time + service.time_interval(minute=5) |
# Copyright Cloudquant, LLC. All right reserved.
# MIT CQ ELITE ONLY = Market If Touched
# Run on 1/18/2018. We test with GOOG, we start checking at 12:50 with the GOOG price around 127.5
from cloudquant.interfaces import Strategy
class New_Order_Method_Market_If_Touched_MIT(Strategy):
@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
return symbol == 'GOOG'
def on_start(self, md, order, service, account):
self.status = 0
def on_minute_bar(self, event, md, order, service, account, bar):
if event.timestamp >= service.time(12,50):
time_string =service.time_to_string(event.timestamp, format='%H:%M:%S') # going to print this a few times so no point repeatedly calling it and code is easier to read.
mbar = bar.minute(start=-1)
mlow = mbar.low[0]
mhigh = mbar.high[0]
print("{} - prevbarlow{:8.2f} bid{:8.2f} last{:8.2f} ask{:8.2f} prevbarhigh{:8.2f}".format(time_string, mlow,md.L1.bid, md.L1.last, md.L1.ask,mhigh))
if self.status==0:
order.send(self.symbol, 'buy', 10, type='MIT', stop=1125) # Market If Price is Touched. Send a Buy Market order if the price comes down to the price specified.
print("{} - Send Market if Touched order at 1125 {}, most recent print{:8.2f}".format(time_string ,self.symbol,md.L1.last))
self.status = 1
def on_fill(self, event, md, order, service, account):
print("{} - Filled @{:8.2f} : bid{:8.2f} last{:8.2f} ask{:8.2f}".format(service.time_to_string(event.timestamp, format='%H:%M:%S'), event.price,md.L1.bid,md.L1.last,md.L1.ask))
Console
12:50:01 - prevbarlow 1127.51 bid 1127.51 last 1127.55 ask 1127.99 prevbarhigh 1127.98 12:50:01 - Send Market if Touched order at 1125 GOOG, most recent print 1127.55 12:51:00 - prevbarlow 1127.51 bid 1127.58 last 1127.89 ask 1127.93 prevbarhigh 1127.97 12:52:00 - prevbarlow 1127.28 bid 1127.28 last 1127.30 ask 1127.82 prevbarhigh 1127.75 ... 13:17:04 - prevbarlow 1125.70 bid 1125.23 last 1125.97 ask 1126.65 prevbarhigh 1126.73 13:18:01 - prevbarlow 1125.17 bid 1125.18 last 1125.49 ask 1125.94 prevbarhigh 1126.17 13:18:40 - Filled @ 1125.00 : bid 1124.37 last 1125.00 ask 1125.00 13:19:02 - prevbarlow 1124.32 bid 1124.32 last 1124.86 ask 1124.85 prevbarhigh 1125.79 13:20:00 - prevbarlow 1123.96 bid 1123.89 last 1124.46 ask 1125.00 prevbarhigh 1124.85 ... 15:58:00 - prevbarlow 1128.88 bid 1129.73 last 1129.86 ask 1130.00 prevbarhigh 1130.00 15:59:00 - prevbarlow 1129.32 bid 1129.33 last 1129.33 ask 1129.79 prevbarhigh 1130.09