Mariner Backtesting - order.send() Stop Orders (Simple)

STP Orders

Stop Market Order (Simple Order Algo) Overview:

Function to input a Stop Market Order.

Buy Stop - when the ask is higher than "stop" it will send a market order

Sell Stop - when the bid is lower than "stop" it will send a market order

As a "Market order" is submitted, this means that you are not guaranteed to fill AT the "stop" price.

NOTE : As the stop is monitoring trade by trade you must not clear event triggers ie service.clear_event_triggers()

Example

order.send(self.symbol, 'sell', 10, type='STP', stop=1125) #

Not for use with live trading

This order method is for use with backtesting only. It isn't supported in live trading.

Parameters
Argument 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 order type 'STP'
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)
Working Example: Link
# Copyright Cloudquant, LLC. All right reserved.
# STP Order - CQ LITE & ELITE - Stop Order - Place a market order when the ask is greater than or equal to "stop"
# 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

from cloudquant.interfaces import Strategy
class New_Order_Method_Stop_Market(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: 
                self.order_handle_b = order.send(self.symbol, 'buy', 10, type='STP', stop=1130)  # Buy  Stop - when the ask is higher than "stop" it will send a market order
                print("{} -  Buy  Stop order sent {} @ {}".format(time_string ,self.symbol,1130))
                self.order_handle_s = order.send(self.symbol, 'sell', 10, type='STP', stop=1125) # Sell Stop - when the bid is lower than "stop" it will send a market order
                print("{} -  Sell Stop order sent {} @ {}".format(time_string ,self.symbol,1125))
                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
        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  Stop order sent GOOG @ 1130
12:50:01 -  Sell Stop order sent GOOG @ 1125
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:08:01 -  prevbarlow 1125.63  bid 1126.00  last 1126.29  ask 1126.23  prevbarhigh 1126.34
13:09:05 -  prevbarlow 1125.23  bid 1125.22  last 1125.82  ask 1125.82  prevbarhigh 1126.23
13:09:52 -  Filled,  price 1125.02
13:10:03 -  prevbarlow 1125.18  bid 1125.01  last 1125.20  ask 1125.82  prevbarhigh 1125.71
13:11:05 -  prevbarlow 1124.15  bid 1124.10  last 1124.95  ask 1125.80  prevbarhigh 1125.66
...
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:31 -  Filled,  price 1129.77
Both Filled