Read a file for symbols that are in your own private file to only process your own symbols.
Hi All,
I was recently asked a question on how does someone read a file with a private list of symbols.
Step 1. Create your own file in the User Data tab.
I created a file that has my list of symbols. The file is called "secret_symbol_list.txt"
Each row of the file has a different symbol. Here is my "secret_symbol_list.txt"
IBM
ORCL
MSFT
SPY
FB
M
XOM
For those unfamiliar with the CloudQuant User Data Tab, this is what it looks like:
Step 2. Read the file in on_strategy_start
Not every algo has an on_strategy_start() method. I use this method to load up the symbols into a class list variable so that it is available to all instances as my algo runs.
@classmethod
def on_strategy_start(cls, md, service, account):
# read the user data file as a string
cls.symbol_list = []
symbols = service.read_file('secret_symbol_list.txt', format='raw')
# create a symbol list by splitting the lines into a list of symbols
cls.symbol_list = symbols.splitlines()
Step 3. Return True in is_symbol_qualified if the symbol is in your list
The is_symbol_qualified method lets the backtest simulation engine know if we are using this symbol for trading.
@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
# qualify only symbol in both the symbol list and in the entry stats file
if symbol in cls.symbol_list:
print("This symbol {} is in my list.".format(symbol))
return True
Full Working Script that uses this technique
from cloudquant.interfaces import Strategy
import talib
import numpy
#####################################################
# Simple Exponential Moving Average Script
# When yesterday's EMA is above the previous day's EMA
# then go long - else go short
class Simple_EMA(Strategy):
@classmethod
def on_strategy_start(cls, md, service, account):
# read the user data file as a string
cls.symbol_list = []
symbols = service.read_file('secret_symbol_list.txt', format='raw')
# create a symbol list by splitting the lines into a list of symbols
cls.symbol_list = symbols.splitlines()
@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
# qualify only symbol in both the symbol list and in the entry stats file
if symbol in cls.symbol_list:
print("This symbol {} is in my list.".format(symbol))
return True
def on_start(self, md, order, service, account): #
myBars = md.bar.daily(start=-21) # grab 21 bars, EMA uses 20, we want yesterdays and the day before so grab 21
close = myBars.close # pull out just the close prices
EMA = talib.MA(close,timeperiod=20,matype=1) # TALIB Moving Average matype=1 = Exponential Moving Average
EMAlist = numpy.ndarray.tolist(EMA) # TALIB returns a numpy array, lets turn that back into a basic list
#print self.symbol,EMAlist
if EMAlist[-1] > EMAlist[-2] : # if yesterday's EMA is above the previous day go long
order.send(self.symbol, 'buy', 100, type='MKT') # Market order, no price required
print("long entry ordered on {}".format(self.symbol))
if EMAlist[-1] < EMAlist[-2] : # if yesterday's EMA is below the previous day go short
order.send(self.symbol, 'sell', 100, type='MKT') # Market order, no price required
print("short entry ordered on {}".format(self.symbol))
def on_minute_bar(self, event, md, order, service, account, bar):
if event.timestamp > service.time(15,44): # at the end of the day, exit the position and terminate the script
if account[self.symbol].position.shares > 0:
order.send(self.symbol, 'sell', 100, type='MKT') # Market order, no price required
print("sell to cover ordered on {}".format(self.symbol))
elif account[self.symbol].position.shares < 0:
order.send(self.symbol, 'buy', 100, type='MKT') # Market order, no price required
print("buy to cover ordered on {}".format(self.symbol))
Tagged:
Comments
Also would be great to see a good example of working with files which contain list of trades. For example
AAPL,20180101 12:15:18.200,200,"buy","mkt"
AAPL,20180101 12:20:21.300,200,"sell","mkt"
AAPL,20180101 15:30:00.000,500,"buy","MOC"
Hi:
If you are using a CSV file, it is easier to have column headings.
For this example, I am going to use a file called signals.csv which contains the following data.
Reading in the data from my signals.csv file can be accomplished using the following line of code. The return of this will be a list containing dictionary data.
data = service.read_file('signals.csv', 'csv')
To See what is in data you can print it out by iterating through the list.
This produces the following prints:
Because this is a list of dictionaries you can now reference each of the elements by name. For example:
Timestamps, datetime, MUTS - dealing with problems of converting your timestamp.
My data file has a representation of the timestamp that looks like: 20180710 12:15:18.200
To process this we may need to convert the timestamp string into a datetime field. code that I used for that process. The following code handles this logic for me.
Full Working Script - To Replay a Signal file.