1. Support Center
  2. Mariner Backtesting

Mariner Backtesting - CQ Elite 2

CloudQuant Elite 2

In this tutorial, we will buy shares using the order.algo_buy() method, and sell them in on_fill() using order.algo_sell()

on_trade
Code Used in This Tutorial:
 # Copyright Cloudquant, LLC. All right reserved.
from cloudquant.interfaces import Strategy


class first_script(Strategy):
    # note that this doesn't start with "self" because it's a @staticmethod
    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol in ['AAPL', 'MSFT']

    # called at the beginning of each instance
    def on_start(self, md, order, service, account):
        print('Current Symbols:\t' + self.symbol)
        print('Current Time:\t' + service.time_to_string(service.system_time) + '\n')
        self.trade_count = 0
        self.buy_id = order.algo_buy(symbol=self.symbol, algorithm='market', intent='init', order_quantity=100)
        print('Order ' + str(self.buy_id) + ' for 100 shares of ' + self.symbol + ' has been placed.')

    def on_trade(self, event, md, order, service, account):
        self.trade_count += 1

    def on_fill(self, event, md, order, service, account):
        if self.buy_id == event.order_id:
            order.algo_sell(symbol=self.symbol, algorithm='market', intent='exit')
            print('\nAll shares of ' + self.symbol + 'have been sold.')

    def on_finish(self, md, order, service, account):
        print('\n\non_finish\tCurrent Symbols:\t' + self.symbol)
        print('on_finish\tCurrent Time:\t' + service.time_to_string(service.system_time) + '\n')
        print('trade_count:\t' + str(self.trade_count))
Console

The console output from running our code:

 Current Symbols:    AAPL
Current Time:   2016-11-01 09:30:00.000000

Order 0000000000000001 for 100 shares of AAPL has been placed.
Current Symbols:    MSFT
Current Time:   2016-11-01 09:30:00.000000

Order 0000000000000002 for 100 shares of MSFT has been placed.

All shares of AAPLhave been sold.

All shares of MSFThave been sold.


on_finish   Current Symbols:    AAPL
on_finish   Current Time:   2016-11-01 16:00:00.000000

trade_count:    234393


on_finish   Current Symbols:    MSFT
on_finish   Current Time:   2016-11-01 16:00:00.000000

trade_count:    134053