Mariner Backtesting - Advanced Read service.read_file

Reading a file from your private User Data

CSV Application Overview:

Using the csv data from the previous lesson, advanced_write, we will analyze the trade information.

What Does This Strategy Do?:

This script opens the TradeInfo file in the csv format and compares the trade results to identify particular defining characteristics.

The TradeInfo file used in this example:

EntryPrice,EntryTime,ExitPrice,ExitTime,Profit,Symbol
68.0199966431,1486650905000001,68.3700027466,1486651380315000,35.0006103516,WMT
45.2700004578,1486652345006700,45.6399993896,1486653121291000,36.9998931885,AAL
65.0400009155,1486651025000001,65.3600006104,1486655040556000,31.9999694824,TGT
63.5400009155,1486651445000001,63.8499984741,1486656480869000,30.9997558594,MSFT

This file was generated by running the advanced_write example script.

Using the Data

Below, the TradeInfo file is read as a csv, and a dictionary is created for each line in the file (besides the header).

 data = service.read_file('TradeInfo', 'csv')

Then each row is compared. If the row meets the requirement, for example if it is the most profitable,largest_profit is set to the row.

 if row['Profit'] > largest_profit['Profit']:
    largest_profit = row
Working Example:

Advanced read_file Application

# Copyright Cloudquant, LLC. All right reserved.
from cloudquant.interfaces import Strategy

class AdvReadExample(Strategy):

    @classmethod
    def on_strategy_start(cls, md, service, account):

        # read the TradeInfo file as a dictionary by setting the format to csv
        data = service.read_file('TradeInfo', 'csv')

        # create variables for values, and the index of the corresponding value

        largest_profit = {'Profit': -1}
        # choose a large number so that the first profit replaces this temporary value
        smallest_profit = {'Profit': 99999999999999999}
        earliest_entry = {'EntryTime': 99999999999999999}
        latest_entry = {'EntryTime': -1}
        earliest_exit = {'ExitTime': 99999999999999999}
        latest_exit = {'ExitTime':-1}
        longest_position = {'EntryTime':1, 'ExitTime':2}
        shortest_position = {'EntryTime': 1, 'ExitTime': 99999999999999999}

        print 'Data Rows:\n'
        for index, row in enumerate(data):
            print row

            # Do comparisons for each value
            # compare the current row's values with the saved times/prices

            if row['Profit'] > largest_profit['Profit']:
                largest_profit = row

            if row['Profit'] < smallest_profit['Profit']:
                smallest_profit = row

            if row['EntryTime'] < earliest_entry['EntryTime']:
                earliest_entry = row

            if row['EntryTime'] > latest_entry['EntryTime']:
                latest_entry = row

            if row['ExitTime'] < earliest_exit['ExitTime']:
                earliest_exit = row

            if row['ExitTime'] > latest_exit['ExitTime']:
                latest_exit = row

            if row['ExitTime'] - row['EntryTime'] > longest_position['ExitTime'] - longest_position['EntryTime']:
                longest_position = row

            if row['ExitTime'] - row['EntryTime'] < shortest_position['ExitTime'] - shortest_position['EntryTime']:
                shortest_position = row

        # print trade information
        print '\n\nTradeInfo Analysis\n--------------------------------\n'

        print 'Most Profitable Symbol:\t', largest_profit['Symbol'], '\t', largest_profit['Profit']
        print 'Least Profitable Trade:\t', smallest_profit['Symbol'], '\t', smallest_profit['Profit']
        print 'Earliest Position Entry:\t', earliest_entry['Symbol'], '\t', service.time_to_string(earliest_entry['EntryTime'], '%H:%M:%S')
        print 'Latest Position Entry:\t', latest_entry['Symbol'], '\t', service.time_to_string(latest_entry['EntryTime'], '%H:%M:%S')
        print 'Earliest Exit:\t', earliest_exit['Symbol'], '\t', service.time_to_string(earliest_exit['ExitTime'], '%H:%M:%S')
        print 'Latest Exit:\t', latest_exit['Symbol'], '\t', service.time_to_string(latest_exit['ExitTime'], '%H:%M:%S')
        print 'Longest Position Duration:\t', longest_position['Symbol'], '\t',\
            service.time_to_string(longest_position['EntryTime'], '%H:%M:%S'), 'to',  service.time_to_string(longest_position['ExitTime'], '%H:%M:%S')
        print 'Shortest Position Duration:\t', shortest_position['Symbol'], '\t',\
            service.time_to_string(shortest_position['EntryTime'], '%H:%M:%S'), 'to',  service.time_to_string(shortest_position['ExitTime'], '%H:%M:%S')

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        # Prevent any symbols from backtesting to save time
        return False

Console

Data Rows:

{'EntryPrice': 68.0199966431, 'Profit': 35.000610351599995, 'Symbol': 'WMT', 'ExitTime': 1486651380315000, 'EntryTime': 1486650905000001, 'ExitPrice': 68.3700027466}
{'EntryPrice': 45.270000457799995, 'Profit': 36.999893188499996, 'Symbol': 'AAL', 'ExitTime': 1486653121291000, 'EntryTime': 1486652345006700, 'ExitPrice': 45.6399993896}
{'EntryPrice': 65.0400009155, 'Profit': 31.999969482399997, 'Symbol': 'TGT', 'ExitTime': 1486655040556000, 'EntryTime': 1486651025000001, 'ExitPrice': 65.3600006104}
{'EntryPrice': 63.5400009155, 'Profit': 30.9997558594, 'Symbol': 'MSFT', 'ExitTime': 1486656480869000, 'EntryTime': 1486651445000001, 'ExitPrice': 63.8499984741}


TradeInfo Analysis
--------------------------------

Most Profitable Symbol: AAL     36.9998931885
Least Profitable Trade: MSFT    30.9997558594
Earliest Position Entry:    WMT     09:35:05
Latest Position Entry:  AAL     09:59:05
Earliest Exit:  WMT     09:43:00
Latest Exit:    MSFT    11:08:00
Longest Position Duration:  MSFT    09:44:05 to 11:08:00
Shortest Position Duration: WMT     09:35:05 to 09:43:00

As you can see, writing and reading csv files can be used to review and analyze your trade data. There are many other factors that you can analyze in your own strategies, such as the spreads of the symbols, volume or volatility, to name a few. Analyzing your trading data can help you pick up on beneficial trends in your successful strategies.