Strategy Start Event
on_strategy_start() is called on the first instantiation for the class.
Interface: @classmethod
def on_strategy_start(cls,
md,
service,
account):
pass
Name | Type | Default | Information |
---|---|---|---|
md | Market | required | market data object |
service | Service | required | service object |
account | Account | required | account object |
- Test script uses data in StatData in is_symbol_qualified and its data to qualify each symbol.
symbol,company_name,security_type,primary_exchange,sector,industry,previous_day_volume,adv10,adv30,previous_close,beta,beta2
APD,AIR PRODS & CHEMS INC,OrdinaryShares,N,Basic Material,Chemicals-Specialty,858082.0,996092,898543,155.89,1.208,1.23919
HW,HEADWATERS INC,OrdinaryShares,N,Consumer Cyclical,Bldg-Constr Prds/Misc,438424.0,517816,609371,18.6,1.7849,1.74121
HOG,HARLEY DAVIDSON INC,OrdinaryShares,N,Consumer Cyclical,Leisure-Products,1930811.0,2424363,2488764,52.33,1.1354,1.80105
USB,US BANCORP DEL,OrdinaryShares,N,Financial,Banks-Super Regional,7203005.0,5555187,5327973,43.84,0.7962,1.34894
ULTA,ULTA SALON COSMETCS & FRAG I,OrdinaryShares,Q,Retail,Retail-Specialty,1246789.0,1522229,802796,254.09,0.6278,0.73014
ULTI,ULTIMATE SOFTWARE GROUP INC,OrdinaryShares,Q,Technology,Computer Sftwr-Enterprse,121181.0,218459,247742,211.96001,1.2676,1.32008
UBSH,UNION BANKSHARES CORP NEW,OrdinaryShares,Q,Financial,Banks-Southeast,121299.0,132288,111614,27.1,1.1489,1.45813
ULTR,ULTRAPETROL BAHAMAS LTD,OrdinaryShares,Q,Transportation,Transportation-Ship,99678.0,152145,286023,0.331,2.667,3.50239
UVV,UNIVERSAL CORP VA,OrdinaryShares,N,Consumer Staple,Tobacco,67094.0,101279,149420,60.64,1.1796,0.57873
UHAL,AMERCO,OrdinaryShares,Q,Capital Equipment,Comml Svcs-Leasing,66608.0,63376,62815,336.97,1.527,1.05692
from cloudquant.interfaces import Strategy
class StatCheck(Strategy):
@classmethod
def on_strategy_start(cls, md, service, account):
# create a list of dictionaries from the user_data file
# the headers will be used as the keys
# each row will have its own dictionary
temp_file = service.read_file('StatData', format='csv')
# populate class variable (dictionary)
cls.stats = {}
# for each dictionary created for each row:
for row in temp_file:
# save the data from the file to a class variable based on symbol
# access the symbol from the 'symbol' key in row, row['symbol']
cls.stats[row['symbol']] = row
# print the cls.stats keys, which are symbols
print 'symbol list:', cls.stats.keys(), '\n\n'
@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
# qualify all of the symbols from the file
return symbol in cls.stats.keys()
# called at the beginning of each instance
def on_start(self, md, order, service, account):
# print the symbol
print 'in on_start for ' + self.symbol
# print the data for the current symbol
print self.__class__.stats[self.symbol]
# check the previous close on file vs the current previous close
# access the previous close from the dictionary
print 'Previous Close:', md.stat.prev_close, '\tPrevious Close From File:', \
self.__class__.stats[self.symbol]['previous_close'], '\n'
service.terminate()
Console
symbol list: ['UBSH', 'HOG', 'ULTA', 'UHAL', 'USB', 'ULTI', 'HW', 'UVV', 'ULTR', 'APD'] in on_start for APD {'sector': 'Basic Material', 'previous_close': 155.89, 'industry': 'Chemicals-Specialty', 'security_type': 'OrdinaryShares', 'beta': 1.208, 'primary_exchange': 'N', 'adv10': 996092, 'previous_day_volume': 858082.0, 'company_name': 'AIR PRODS & CHEMS INC', 'adv30': 898543, 'symbol': 'APD', 'beta2': 1.23919} Previous Close: 149.550003052 Previous Close From File: 155.89 in on_start for HOG {'sector': 'Consumer Cyclical', 'previous_close': 52.33, 'industry': 'Leisure-Products', 'security_type': 'OrdinaryShares', 'beta': 1.1354, 'primary_exchange': 'N', 'adv10': 2424363, 'previous_day_volume': 1930811.0, 'company_name': 'HARLEY DAVIDSON INC', 'adv30': 2488764, 'symbol': 'HOG', 'beta2': 1.80105} Previous Close: 60.8100013733 Previous Close From File: 52.33 in on_start for HW {'sector': 'Consumer Cyclical', 'previous_close': 18.6, 'industry': 'Bldg-Constr Prds/Misc', 'security_type': 'OrdinaryShares', 'beta': 1.7849, 'primary_exchange': 'N', 'adv10': 517816, 'previous_day_volume': 438424.0, 'company_name': 'HEADWATERS INC', 'adv30': 609371, 'symbol': 'HW', 'beta2': 1.74121} Previous Close: 23.5799999237 Previous Close From File: 18.6 in on_start for UBSH {'sector': 'Financial', 'previous_close': 27.1, 'industry': 'Banks-Southeast', 'security_type': 'OrdinaryShares', 'beta': 1.1489, 'primary_exchange': 'Q', 'adv10': 132288, 'previous_day_volume': 121299.0, 'company_name': 'UNION BANKSHARES CORP NEW', 'adv30': 111614, 'symbol': 'UBSH', 'beta2': 1.45813} Previous Close: 35.8600006104 Previous Close From File: 27.1 in on_start for UHAL {'sector': 'Capital Equipment', 'previous_close': 336.97, 'industry': 'Comml Svcs-Leasing', 'security_type': 'OrdinaryShares', 'beta': 1.527, 'primary_exchange': 'Q', 'adv10': 63376, 'previous_day_volume': 66608.0, 'company_name': 'AMERCO', 'adv30': 62815, 'symbol': 'UHAL', 'beta2': 1.05692} Previous Close: 372.059997559 Previous Close From File: 336.97 in on_start for ULTA {'sector': 'Retail', 'previous_close': 254.09, 'industry': 'Retail-Specialty', 'security_type': 'OrdinaryShares', 'beta': 0.6278, 'primary_exchange': 'Q', 'adv10': 1522229, 'previous_day_volume': 1246789.0, 'company_name': 'ULTA SALON COSMETCS & FRAG I', 'adv30': 802796, 'symbol': 'ULTA', 'beta2': 0.73014} Previous Close: 251.13999939 Previous Close From File: 254.09 in on_start for ULTI {'sector': 'Technology', 'previous_close': 211.96001, 'industry': 'Computer Sftwr-Enterprse', 'security_type': 'OrdinaryShares', 'beta': 1.2676, 'primary_exchange': 'Q', 'adv10': 218459, 'previous_day_volume': 121181.0, 'company_name': 'ULTIMATE SOFTWARE GROUP INC', 'adv30': 247742, 'symbol': 'ULTI', 'beta2': 1.32008} Previous Close: 190.399993896 Previous Close From File: 211.96001 in on_start for USB {'sector': 'Financial', 'previous_close': 43.84, 'industry': 'Banks-Super Regional', 'security_type': 'OrdinaryShares', 'beta': 0.7962, 'primary_exchange': 'N', 'adv10': 5555187, 'previous_day_volume': 7203005.0, 'company_name': 'US BANCORP DEL', 'adv30': 5327973, 'symbol': 'USB', 'beta2': 1.34894} Previous Close: 52.0400009155 Previous Close From File: 43.84 in on_start for UVV {'sector': 'Consumer Staple', 'previous_close': 60.64, 'industry': 'Tobacco', 'security_type': 'OrdinaryShares', 'beta': 1.1796, 'primary_exchange': 'N', 'adv10': 101279, 'previous_day_volume': 67094.0, 'company_name': 'UNIVERSAL CORP VA', 'adv30': 149420, 'symbol': 'UVV', 'beta2': 0.57873} Previous Close: 60.9000015259 Previous Close From File: 60.64Working Example 2:
- Iterate over md to create a symbol list.
from cloudquant.interfaces import Strategy
class CreatingSymbolList(Strategy):
@classmethod
def on_strategy_start(cls, md, service, account):
# initialize symbol list variable
cls.symbol_list = []
# iterate over the market data
for md_info in md:
symbol = md_info.symbol
# if the symbols' exchange is NYSE Arca and the average volume is over 20 million
if md_info.stat.exchange == 'P' and md_info.stat.avol > 20000000:
# add the symbol to the symbol list after it met the if condition
cls.symbol_list.append(symbol)
# print the length of the symbol list
print 'cls.symbol_list:', len(cls.symbol_list)
print cls.symbol_list, '\n\n'
@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
# qualifies all symbols in cls.symbol_list
return symbol in cls.symbol_list
def on_start(self, md, order, service, account):
# print the symbol, and the current method
print self.symbol + '\t' + 'in on_start'
service.terminate()
Console
cls.symbol_list: 11 ['SPY', 'DGAZ', 'GDX', 'JNUG', 'EEM', 'XLF', 'IWM', 'USO', 'NUGT', 'VXX', 'EFA'] DGAZ in on_start EEM in on_start EFA in on_start GDX in on_start IWM in on_start JNUG in on_start NUGT in on_start SPY in on_start USO in on_start VXX in on_start XLF in on_startRemarks:
- No return value.
- Has access to system objects: md, service and account.
- Class method - @classmethod.
- Use cls to access/assign class variables (compared to reading/assigning class variable in an instance method self.__class__.CLASS_VARIABLE).
- This is a good place to read in user data that can be accessed in in_symbol_universe()/is_symbol_qualified() or on_start().