Stop Limit Orders
Stop Limit Order (Simple Order Algo) Overview:Function to input a Stop Limit Order.
Stop Limit Buy - When the ask is higher than the "stop" it will send a Limit Buy order to buy for no more than the "price".
Stop Limit Sell - When the bid is lower than the "stop" it will send a Limit Sell order to sell for no less than the "price".
NOTE : As the stop is monitoring trade by trade you must not clear event triggers ie service.clear_event_triggers()
Exampleorder.send(self.symbol, 'buy', 10, type='STL', stop=1130,price=1129.2)
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 Stop Limit order type. Defaults to market. | STL |
price | Yes | The limit price for the order. | 193.27 |
stop | Yes | The stop (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.
# STL Order with Limit - CQ LITE & ELITE
# Run on 1/18/2018 with GOOG as the symbol for testing purposes. We start checking at 12:50 with the GOOG price around 127.5
# Buy - When the ask is higher than the "stop" it will send a Limit Buy order to buy for no more than the "price"
# Sell - When the bid is lower than the "stop" it will send a Limit Sell order to sell for no less than the "price"
from cloudquant.interfaces import Strategy
class New_Order_Method_Stop_Limit(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
self.order_handle = 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:
# Buy Stop Limit - when the ask is higher than the "stop" it will send a Limit order to buy for no more than the "price"
self.order_handle_b = order.send(self.symbol, 'buy', 10, type='STL', stop=1130,price=1129.2)
print("{} - Buy Stop Limit order will be triggered at $1130 to pay no more than $1129.2 {}".format(time_string ,self.symbol))
self.status = 1
if self.status==2:
# Sell Stop Limit - When the bid is lower than the "stop" it will send a Limit Sell order to sell for no less than the "price"
self.order_handle_b = order.send(self.symbol, 'sell', 10, type='STL', stop=1131,price=1131.2)
print("{} - Sell Stop Limit order will be triggered at $1131 to sell for no less than $1131.2 {}".format(time_string ,self.symbol))
self.status = 3
# Note : on_fill will only be called for CQ Elite users
def on_fill(self, event, md, order, service, account):
if self.status == 1:
print("{} - Filled Stop Buy Limit, price{:8.2f}".format(service.time_to_string(event.timestamp, format='%H:%M:%S'), event.price))
self.status = 2
if self.status == 3:
print("{} - Filled Stop Sell Limit, price{:8.2f}".format(service.time_to_string(event.timestamp, format='%H:%M:%S'), event.price))
self.status = 4
Console
12:50:01 - prevbarlow 1127.51 bid 1127.51 last 1127.55 ask 1127.99 prevbarhigh 1127.98 12:50:01 - Buy Stop Limit order will be triggered at $1130 to pay no more than $1129.2 GOOG 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 ... 14:21:05 - prevbarlow 1127.76 bid 1127.76 last 1127.76 ask 1128.49 prevbarhigh 1128.50 14:22:04 - prevbarlow 1128.34 bid 1128.79 last 1129.00 ask 1129.95 prevbarhigh 1129.00 14:22:57 - Filled Stop Buy Limit, price 1129.20 14:23:02 - prevbarlow 1128.78 bid 1129.10 last 1129.54 ask 1129.99 prevbarhigh 1129.70 14:23:02 - Sell Stop Limit order will be triggered at $1131 to sell for no less than $1131.2 GOOG 14:24:00 - prevbarlow 1128.67 bid 1129.01 last 1129.00 ask 1129.39 prevbarhigh 1129.99 14:25:02 - prevbarlow 1129.04 bid 1129.27 last 1129.25 ask 1129.75 prevbarhigh 1130.00 ... 15:17:04 - prevbarlow 1130.13 bid 1130.13 last 1130.22 ask 1130.75 prevbarhigh 1130.75 15:18:03 - prevbarlow 1130.02 bid 1130.38 last 1130.68 ask 1130.82 prevbarhigh 1130.83 15:18:48 - Filled Stop Sell Limit, price 1131.20 15:19:05 - prevbarlow 1130.52 bid 1131.06 last 1131.33 ask 1131.71 prevbarhigh 1131.33 15:20:00 - prevbarlow 1131.09 bid 1131.18 last 1131.19 ask 1131.50 prevbarhigh 1131.71