Limit Order (Simple)
Limit Order (Simple Order Algo) Overview:Function to send a limit order, buy or sell.
Buy Limit order - pass the highest price you are willing to pay
Sell Limit order - pass the lowest price you are willing sell for
Examplesorder.send(self.symbol, 'buy', 10, type='LMT', price=1125)
order.send(self.symbol, 'sell', 10, type='LMT', price=1130)
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 Limit order type. | LMT |
price | No | The limit price for the order. | 193.27 |
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.
# LMT Order - CQ LITE & ELITE - Limit Order - Place the order right away, and fill the order with price better than "price"
# Run on 1/18/2018. We test with GOOG, we start checking at 12:50 with the GOOG price around 127.5
# Before the end of the day it will reach down under 1125 (13:10) and up over 1130 (14:22)
from cloudquant.interfaces import Strategy
class New_Order_Method_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
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:
self.order_handle_b = order.send(self.symbol, 'buy', 10, type='LMT', price=1125) # Limit buy order, pass the highest price you are willing to pay
print("{} - Buy Limit order sent @ $1125 {}".format(time_string ,self.symbol))
self.order_handle_s = order.send(self.symbol, 'sell', 10, type='LMT', price=1130) # Limit sell order, pass the lowest price you are willing sell for
print("{} - Sell Limit order sent @ $1130 {}".format(time_string ,self.symbol))
self.status = 1
# Note : on_fill will only be called for CQ Elite users
def on_fill(self, event, md, order, service, account):
if self.status ==1 or self.status ==2:
print("{} - Filled, price{:8.2f}".format(service.time_to_string(event.timestamp, format='%H:%M:%S'), event.price))
self.status += 1 # increment status for each fill
if self.status == 3 :
print("Both Filled")
service.terminate()
Console
12:50:01 - prevbarlow 1127.51 bid 1127.51 last 1127.55 ask 1127.99 prevbarhigh 1127.98 12:50:01 - Buy Limit order sent @ $1125 GOOG 12:50:01 - Sell Limit order sent @ $1130 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 ... 13:09:05 - prevbarlow 1125.23 bid 1125.22 last 1125.82 ask 1125.82 prevbarhigh 1126.23 13:10:03 - prevbarlow 1125.18 bid 1125.01 last 1125.20 ask 1125.82 prevbarhigh 1125.71 13:10:38 - Filled, price 1125.00 13:11:05 - prevbarlow 1124.15 bid 1124.10 last 1124.95 ask 1125.80 prevbarhigh 1125.66 13:12:01 - prevbarlow 1124.36 bid 1124.38 last 1124.78 ask 1125.64 prevbarhigh 1125.70 ... 14:23:02 - prevbarlow 1128.78 bid 1129.10 last 1129.54 ask 1129.99 prevbarhigh 1129.70 14:24:00 - prevbarlow 1128.67 bid 1129.01 last 1129.00 ask 1129.39 prevbarhigh 1129.99 14:24:07 - Filled, price 1130.00 Both Filled