Mariner Backtesting - register_extra_data_sources()

(system method) Overview:

Method to register formatted user data to event stream. The user file needs to be in a csv format using '|' as the delimiter, and the first row of the file needs to be a header with first two columns being 'timestamp' and 'symbol'. The returned value is a dictionary with keys being the file name and values being the corresponding callback method name.

Interface:
 def register_extra_data_sources(self,
                                cls,
                                md,
                                service,
                                account):
    pass
Parameters
Name Type Default Information
cls object required cls is how to read or assign class variables in a classmethod (compared to reading/assigning class variable in an instance method self.__class__.CLASS_VARIABLE).
md Market required market data object
service Service required service object
account Account required account object
Remarks:
  • The values in the 'timestamp' column need to be in a standard format, e.g. 2017-02-17T03:25:21.000
  • The records in a file with timestamp prior to simulation start time or after simulation end time will be ignored by CloudQuant. If the symbol name of a record is not defined in is_symbol_qualified, the record will also be ignored.
  • The records also need to be sorted chronologically from the earliest timestamp to the latest timestamp.
  • The register_extra_data_sources() as a class method, is called once after on_strategy_start() and is_symbol_qualified(), but before on_start().
  • Group data is also available by passing a tuple as the key to the dictionary: (group_name, file_name)
Working Example:

Register to event stream the user defined news sentiment data, and social sentiment data.

from cloudquant.interfaces import Strategy
import pandas as pd
import datetime
from StringIO import StringIO


class DataStreamingExample(Strategy):
    # add user-defined sentiment data to event stream
    str_today = None

    @classmethod
    def on_strategy_start(cls, md, service, account):
        print('on strategy start:')
        if (cls.str_today != service.time_to_string(service.system_time, '%Y-%m-%d')):
            # please use this date format: yyyy-mm-dd
            cls.str_today = service.time_to_string(service.system_time, '%Y-%m-%d')
            print('today is %s' % cls.str_today)

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol == 'AAPL'

    @classmethod
    def register_extra_data_sources(cls, md, service, account):
        # my_file_string_for_register = 'timestamp|symbol|my_score|my_confidence|my_wire\n%sT10:00:00.000|AAPL|1|100|1234'%cls.str_today
        # service.write_file('my_sentiment_feed.csv',my_file_string_for_register,mode='overwrite')
        my_news_feed = 'my_sentiment_feed.csv'
        today_news_feed = 'ET_news_full_sentiment_story_%s.csv' % cls.str_today
        today_social_feed = 'ET_social_full_sentiment_story_%s.csv' % cls.str_today
        return {my_news_feed: 'my_sentiment_callback', \
                ('sentiment', today_news_feed): 'news_sentiment_callback', \
                ('sentiment', today_social_feed): 'social_sentiment_callback' \
                }

    def my_sentiment_callback(self, event, md, order, service, account):
        print('\nMy sentiment for %s at %s:' % (self.symbol, service.time_to_string(service.system_time)))
        print('Sentiment score is: %d, confidence is: %d, source (wire number) is %d' % (
        int(event.field['my_score']), int(event.field['my_confidence']), int(event.field['my_wire'])))

    def news_sentiment_callback(self, event, md, order, service, account):
        print('\nNews sentiment for %s at %s' % (self.symbol, service.time_to_string(service.system_time)))
        print('Sentiment score is: %d, confidence is: %d, source (wire number) is %d' % (
        int(event.field['score']), int(event.field['confidence']), int(event.field['wire'])))

    def social_sentiment_callback(self, event, md, order, service, account):
        print('\nSocial sentiment for %s at %s' % (self.symbol, service.time_to_string(service.system_time)))
        print('Sentiment score is: %d, confidence is: %d, source (wire number) is %d' % (
        int(event.field['score']), int(event.field['confidence']), int(event.field['wire'])))

Console

on strategy start:
today is 2016-11-29

Social sentiment for AAPL at 2016-11-29 09:59:01.825000
Sentiment score is: 0, confidence is: 84, source (wire number) is 2318

Social sentiment for AAPL at 2016-11-29 09:59:02.121000
Sentiment score is: 0, confidence is: 92, source (wire number) is 2318

Social sentiment for AAPL at 2016-11-29 09:59:05.304000
Sentiment score is: 0, confidence is: 67, source (wire number) is 2318

News sentiment for AAPL at 2016-11-29 09:59:41.352000
Sentiment score is: 0, confidence is: 70, source (wire number) is 1819

Social sentiment for AAPL at 2016-11-29 09:59:41.794000
Sentiment score is: 0, confidence is: 95, source (wire number) is 3446

Social sentiment for AAPL at 2016-11-29 09:59:55.414000
Sentiment score is: 0, confidence is: 39, source (wire number) is 2318

My sentiment for AAPL at 2016-11-29 10:00:00.000000:
Sentiment score is: 1, confidence is: 100, source (wire number) is 1234

Social sentiment for AAPL at 2016-11-29 10:00:02.430000
Sentiment score is: 0, confidence is: 85, source (wire number) is 2318

Social sentiment for AAPL at 2016-11-29 10:00:05.335000
Sentiment score is: 0, confidence is: 65, source (wire number) is 2318

Example file format

timestamp,symbol,my_score,my_confidence,my_wire
AAPL,2016-11-29T09:59:01.825000,0,84,2318
AAPL,2016-11-29T09:59:02.121000,0,92,2318
AAPL,2016-11-29T09:59:05.304000,0,67,2318
AAPL,2016-11-29T09:59:41.352000,0,70,1819
AAPL,2016-11-29T09:59:41.794000,0,95,3446
AAPL,2016-11-29T09:59:55.414000,0,39,2318
AAPL,2016-11-29T10:00:00.000000,1,100,1234
AAPL,2016-11-29T10:00:02.430000,0,85,2318
AAPL,2016-11-29T10:00:05.335000,0,65,2318