| Overall Statistics |
|
Total Trades 0 Average Win 0% Average Loss 0% Compounding Annual Return 0% Drawdown 0% Expectancy 0 Net Profit 0% Sharpe Ratio 0 Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0 Beta 0 Annual Standard Deviation 0 Annual Variance 0 Information Ratio 0 Tracking Error 0 Treynor Ratio 0 Total Fees $0.00 |
import pandas as pd
import numpy as np
class ModulatedResistanceContainmentField(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2018, 4, 20) # Set Start Date
self.SetEndDate(2018,8,28) # Set End Date
self.SetCash(100000) # Set Strategy Cash
# self.AddEquity("SPY", Resolution.Minute)
self.spy = self.AddEquity("SPY", Resolution.Daily).Symbol
# resolution for the data added to the universe
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.CoarseSelectionFunction,self.FineSelectionFunction)
self.symbols = []
self.volumeAvg = {}
self.stateData = {}
self.stocks_worst =[]
self.effectiveTradingDays ={}
self.effectiveTradingDaysTickers = {}
self.MaxCandidates=30
self.MaxBuyOrdersAtOnce=15
self.MyLeastPrice=1.15
self.MyMostPrice=1.49
self.MyFireSalePrice= self.MyLeastPrice
self.MyFireSaleAge=3
self.LowVar = 6
self.HighVar = 40
#self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen(self.spy, -45), Action(self.BeforeMarketOpen))
def CoarseSelectionFunction(self,coarce):
'''The stocks must have fundamental data
The stock must have positive previous-day close price and volume
'''
### filtered universe is changing every day. A specific symbol may be selected one day and out of universe the next day. So, the SMA indicators will not be ready until the accumulated
### effective trading days reach the specifed moving average length.
filtered = [x for x in coarce if (x.HasFundamentalData) and (x.Volume > 0) and (x.Price >= self.MyLeastPrice) and (x.Price <= self.MyMostPrice)]
first_filter_symbol = [x.Symbol for x in filtered]
# self.Debug('first filter %s' % len(first_filter_symbol))
self.Debug('%s length of coarse fundamental filter: %s' % (self.Time.date(),len(filtered)))
for cf in filtered:
if cf.Symbol not in self.stateData:
self.stateData[cf.Symbol] = SelectionData(cf.Symbol)
self.effectiveTradingDays[cf.Symbol] = 0 ### create a dictionary using filtered symbols as keys and initialize the value of effective trading days as 0
self.effectiveTradingDays[cf.Symbol] += 1 ### tracks the effective trading days for each symbol
# Updates the SymbolData object with current EOD price
avg = self.stateData[cf.Symbol]
avg.update(cf.EndTime, cf.AdjustedPrice,cf.DollarVolume)
values = list(filter(lambda x: x.is_ready, self.stateData.values()))
symbols = [x.symbol.Value for x in values]
self.Debug('%s length of values: %s' % (self.Time.date(),len(symbols)))
self.Debug(symbols)
### Changing symbols to tickers
for k, v in self.effectiveTradingDays.items():
self.effectiveTradingDaysTickers[k.Value] = v
self.Debug(self.effectiveTradingDaysTickers) ### Here you can check the effective trading days for all symbols
#volume_values = [x.mean_volume for x in values]
#pct_values = [x.percent_difference for x in values]
#self.Debug(self.Time)
#self.Debug(len(pct_values))
#self.Debug(pct_values)
#if pct_values:
# self.Debug(self.Time)
# self.Debug('volume')
# self.Debug(volume_values)
# self.Debug(len(volume_values))
# pct_diff = [x.percent_difference for x in values]
# self.Debug('pct_diff')
# self.Debug(pct_diff)
#self.Debug(len(values))
#if len(volume_values) > 0:
# vol_small_percentile = np.percentile(volume_values, self.LowVar)
## vol_large_percentile = np.percentile(volume_values, self.HighVar)
# self.Debug('small percentile volume is %s' % vol_small_percentile)
# self.Debug('large percentile volume is %s' % vol_large_percentile)
# The volume_filter is the list of items in the values object that has volume between the small percentile and large percentile
# volume_filter = [x for x in values if (x.self.volumeAvg > vol_small_percentile) and (x.self.volumeAvg < vol_large_percentile)]
# Sort the volume_filter by the percent_difference variable that is the ratio of (ShortAvg-LongAvg)/LongAvg
# stocks_by_perc_diff = sorted(volume_filter, key=lambda x:x.percent_difference)
# percent_diff = [x.percent_difference for x in stocks_by_perc_diff]
#self.Debug(self.Time)
# self.Debug('percent_diff %s' % percent_diff)
# self.Log(self.Time)
# self.Log(percent_diff)
# symbols = [x.symbol for x in stocks_by_perc_diff]
#self.Debug('length of symbols %s' % len(symbols))
# self.stock_worst = symbols[0:30]
# return [x.Symbol for x in self.stock_worst]
# perc_diff_sorted = sorted(values, key=lambda x:x.percent_difference)
# percent_diff = [x.percent_difference for x in perc_diff_sorted]
# self.Debug(self.Time)
# self.Debug('percent_diff %s' % percent_diff)
#symbols = [x.Symbol for x in values]
return [x.Symbol for x in filtered]
def FineSelectionFunction(self,fine):
fine_filter = [x.Symbol for x in fine if x.SecurityReference.IsPrimaryShare == 1 and x.SecurityReference.SecurityType == 'ST00000001' and x.CompanyReference.IsLimitedPartnership == 0 and
x.SecurityReference.IsDepositaryReceipt == 0 ]
self.symbols = [x for x in fine_filter]
# self.Debug('%s length fine fundamental filter %s' % (self.Time.date(),len(self.symbols)))
return self.symbols
class SelectionData(object):
def __init__(self, symbol):
self.symbol = symbol
self.ShortAvg = SimpleMovingAverage(3)
self.LongAvg = SimpleMovingAverage(45)
self.is_ready = False
self.percent_difference = 0
self.volume = SimpleMovingAverage(20)
self.mean_volume = 0
def update(self, time, price,volume):
### In "A and B and C" statement, when A is False, B and C will not be executed.
### Your strategy wants to update all the three SMA and then check if all of them are ready,
### So, the Update() methods should not be in the "and" statement condition.
self.LongAvg.Update(time,price)
self.ShortAvg.Update(time,price)
self.volume.Update(time,volume)
if self.LongAvg.IsReady and self.ShortAvg.IsReady and self.volume.IsReady:
# if self.LongAvg.Update(time,price) and self.ShortAvg.Update(time,price) and self.volume.Update(time,volume):
shortAvg = self.ShortAvg.Current.Value
longAvg = self.LongAvg.Current.Value
self.mean_volume = self.volume.Current.Value
self.percent_difference = (shortAvg - longAvg) / longAvg
self.is_ready = True
#self.Debug('se actualizaron los indicadores')
#self.is_ready = True
#shortAvg = self.ShortAvg.Current.Value
#longAvg = self.LongAvg.Current.Value
#self.percent_difference = (shortAvg - longAvg) / longAvg