Mariner Backtesting - Strategy.on_fill()

Order Fill Event

Called whenever a position associated with this strategy's account changes.

Interface:
 def on_fill(self,
            event,
            md,
            order,
            service,
            account):
    pass
Data Attributes for event:
Name Type Information
account_id integer The account id of the order event. Will be the same as account id of the script.
actual_direction string Either increase, reduce, or reverse. This represents the actual change to the accounts position. Can be debugging order behavior
clordid string Client Order Id in FIX. Is generated when the order is sent. Will be the same on executions.
collect object is a json dictionary of information collected at the time of the order.
expected_direction enum Either increase, reduce, or reverse. This will be set when the order is first placed, and is a prediction of whether your order will increase, reduce, or reverse your posistion. This can obviously change as it's possible you have orders in flight, that haven't Filled yet. Can be usefull in debugging order behavior
fill_id integer fill_id tells when the order was filled during the simulation day. The first fill of the day will have fill_id == 1. Each fill_id is unique per simulation day.
instruction_id string Unique GUID for the instance of the script that generated the initial order. Will be None if the initial order wasn't started from same machine (e.g. from trading client)
intent string The intent attribute you set in the order that generated this event.
kind integer Event enum (8) representing the kind of event. *LIVE AND FORWARD ONLY*, represents the type of event (trade, quote, news, imbalance etc)
line_number string line_number is the line in your strategy script where the order was placed that generated this event. Can be used to help debug order behavior
order_algorithm string The order_algorithm GUID you set in the order that generated this event. This can be useful in debugging order behavior.
order_id string Unique GUID of order being filled (very similar clordid). Will be None if the initial order wasn't started from same machine (e.g. from trading client)
price float Price of the fill (not the order).
script_class_name string script_class_name represents the name of your Strategy class that placed the order that generated this event. Can be used to help debug order behavior
script_id string GUID of the script. Will be None if the initial order wasn't started from same machine (e.g. from trading client)
shares integer Shares being filled. Not necessarily the original order size (unless the entire order is filled in one execution).
state string Order Message's States
symbol string Event Symbol. In most cases symbol is obvious (self.symbol, md.symbol). If the script has subscribed to multiple symbols events, then event.symbol should be used to identify the symbol.
timestamp muts Timestamp of the event.
user_key string user_key set on the order. Can be used to identify trades that go together (pairs/batch/strategy). account object can be filtered on user_key
Working Example:
from cloudquant.interfaces import Strategy


class OnFillExample(Strategy):

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        # when starting as a scan ... only AAL will qualify
        return symbol == "AAL"

    def on_start(self, md, order, service, account):
        #print start time
        print(self.symbol + "\n" + service.time_to_string(service.system_time) + "\n \n")

        # instance varialbe
        self.do_once = True

    def on_trade(self, event, md, order, service, account):
        # test if self.do_once is True and the event timestamp is after market open
        if self.do_once and event.timestamp > md.market_open_time:

            # send order
            order.algo_buy(self.symbol, "market", intent="init", order_quantity=100)

            #  change instance variable to False
            self.do_once = False

    def on_fill(self, event, md, order, service, account):
        #  the time of the fill - human readable
        print(service.time_to_string(event.timestamp)+ '\n\n' )

        # print the event object
        print(event)

Console

2016-08-08 09:23:14.962000

2016-08-08 09:30:00.315000

FillEvent(instruction_id='3175a456-5c20-41fe-88bf-2ee97b232536', script_id=u'9b18a6b5-5eaa-4fbc-be0d-1ea3cc805ab9', user_key='3175a456-5c20-41fe-88bf-2ee97b232536', order_id='0000000000000001', state='F', price=34.220001220703125, shares=100, timestamp=1470663000315000, symbol='AAL', account_number=0, clordid=None, collect=None, kind=8)