Reading a file from your private user data
Read Csv Overview:In this lesson, you will learn how to read csv files as dictionaries, using service.read_file(), to read a user_data file.
Interface data = service.read_file('reject_data', 'csv')
The above method call opens the reject_data file and creates a dictionary for each line in the file(besides the first). The top line of reject_data will be the names for all the dictionaries, and all subsequent lines will be values.
When reading csv files, make sure your data contains no commas. Values containing commas cause your script to fail.
Reading a CSVThis is the reject_data file:
script_name,symbol,state,intent,order_algorithm,reason write_csv,AAL,L,increase,market,multiple pending orders not allowed while allow_multiple_pending is false write_csv,AAL,L,exit,market,when intent is exit current_position cannot be 0 write_csv,AAPL,L,increase,market,multiple pending orders not allowed while allow_multiple_pending is false write_csv,AAPL,L,exit,market,when intent is exit current_position cannot be 0
This is what the CSV object holds after calling the method: service.read_file('reject_data', 'csv').
{'order_algorithm': 'market', 'symbol': 'AAL', 'state': 'L', 'script_name': 'write_csv', 'reason': 'multiple pending orders not allowed while allow_multiple_pending is false', 'intent': 'increase'} {'order_algorithm': 'market', 'symbol': 'AAL', 'state': 'L', 'script_name': 'write_csv', 'reason': 'when intent is exit current_position cannot be 0', 'intent': 'exit'} {'order_algorithm': 'market', 'symbol': 'AAPL', 'state': 'L', 'script_name': 'write_csv', 'reason': 'multiple pending orders not allowed while allow_multiple_pending is false', 'intent': 'increase'} {'order_algorithm': 'market', 'symbol': 'AAPL', 'state': 'L', 'script_name': 'write_csv', 'reason': 'when intent is exit current_position cannot be 0', 'intent': 'exit'}Working Example:
Reading reject_data as dictionaries.
Note: the reject_data file used in the example is the above file.
# Copyright Cloudquant, LLC. All right reserved.
from cloudquant.interfaces import Strategy
class ReadCsvExample(Strategy):
@classmethod
def on_strategy_start(cls, md, service, account):
# read the reject_data file as a dictionary by setting the format to csv
data = service.read_file('reject_data', 'csv')
# print the data
print("Rejection data:\n\n")
# create a variable to track number of rejections
reject_count = 1
# for each reject dictionary in data
for reject in data:
# print reject count and increment variable
print("Reject " + str(reject_count) + ":\n")
reject_count +=1
print("There was a rejection in the " + reject["script_name"] + " script. The " + reject["symbol"] + " " +
reject["order_algorithm"] + " order intending to " + reject["intent"] + " failed because " + reject["reason"] + ".\n\n" )
# print all the data
print("\nRejection Dictionaries:\n\n")
for row in data:
print row
@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
# Prevent any symbols from backtesting to save time
return False
Console
Rejection data: Reject 1: There was a rejection in the write_csv script. The AAL market order intending to increase failed because multiple pending orders not allowed while allow_multiple_pending is false. Reject 2: There was a rejection in the write_csv script. The AAL market order intending to exit failed because when intent is exit current_position cannot be 0. Reject 3: There was a rejection in the write_csv script. The AAPL market order intending to increase failed because multiple pending orders not allowed while allow_multiple_pending is false. Reject 4: There was a rejection in the write_csv script. The AAPL market order intending to exit failed because when intent is exit current_position cannot be 0. Rejection Dictionaries: {'order_algorithm': 'market', 'symbol': 'AAL', 'state': 'L', 'script_name': 'write_csv', 'reason': 'multiple pending orders not allowed while allow_multiple_pending is false', 'intent': 'increase'} {'order_algorithm': 'market', 'symbol': 'AAL', 'state': 'L', 'script_name': 'write_csv', 'reason': 'when intent is exit current_position cannot be 0', 'intent': 'exit'} {'order_algorithm': 'market', 'symbol': 'AAPL', 'state': 'L', 'script_name': 'write_csv', 'reason': 'multiple pending orders not allowed while allow_multiple_pending is false', 'intent': 'increase'} {'order_algorithm': 'market', 'symbol': 'AAPL', 'state': 'L', 'script_name': 'write_csv', 'reason': 'when intent is exit current_position cannot be 0', 'intent': 'exit'}
This is a basic example of how reading CSV files can be used. For a more advanced example, see Advanced Read