service.write_file()
Write Csv Overview:In this lesson, you will learn how to write files to user_data using service.write_file().
Interface service.write_file('reject_data', temp_string, mode='overwrite')
This method call opens the reject_data file and overwrites the previous text with temp_string.
Basic use case:Log trade information from on_trade, on_fill, or on_reject.
Below is the information logged in this example from on_reject.
temp_string = "\n%s,%s,%s,%s,%s,%s" % (event.script_class_name, event.symbol, event.state,
event.intent, event.order_algorithm, event.reason.replace(",", " "))
service.write_file('reject_data', temp_string)
Comma separated values(csv) from On_reject event information are written to file for later use.
Working Example:Writing on_reject information in csv format.
# Copyright Cloudquant, LLC. All right reserved.
from cloudquant.interfaces import Strategy
class WriteCsvExample(Strategy):
@classmethod
def on_strategy_start(cls, md, service, account):
# create a global variable
cls.only_once = True
@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
# only run script on AAL and AAPL
return (symbol == "AAL" or symbol == "AAPL")
def on_start(self, md, order, service, account):
# only do once per run of the script
if self.__class__.only_once == True:
# set variable to False
self.__class__.only_once = False
# set up the csv dictionary. List the names of each of the items with a comma seperating them.
temp_string = "%s,%s,%s,%s,%s,%s" % ('script_name', 'symbol', 'state', 'intent', 'order_algorithm', 'reason')
# replace the text in reject_data with temp_string
service.write_file('reject_data', temp_string, mode='overwrite')
print(self.symbol + "\n" + service.time_to_string(service.system_time) + "\n")
# send 2 simultaneous orders to with allow_multiple_pending set to False to trigger a reject
order.algo_buy(self.symbol, algorithm='market', intent='init', order_quantity=100)
order.algo_buy(self.symbol, algorithm='market', intent='increase', order_quantity=100)
# sell shares you don't have with intent "exit" to trigger a reject
order.algo_sell(self.symbol, "market", intent="exit")
def on_reject(self, event, md, order, service, account):
print("\n\nin on_reject\n\n")
print event
# replace all commas in event.reason to avoid an error with reading as csv
# create a temporary variable to hold the string that will be written to file
# all values separated by commas
temp_string = "%s,%s,%s,%s,%s,%s" % (
event.script_class_name, event.symbol, event.state, event.intent, event.order_algorithm, event.reason.replace(",", " "))
service.write_file('reject_data', temp_string)
Console
Note: Not actual console, but the user_data file reject_data
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 example shows you how to write files as csv. In the next lesson you will learn how to read the csv files as dictionaries.