def RunTradingModelBuyHold(ticker: str, startDate: str, durationInYears: int, totalFunds: int, verbose: bool = False, saveHistoryToFile: bool = True, returndailyValues: bool = False): modelName = 'BuyHold' + '_' + ticker tm = TradingModel(modelName, ticker, startDate, durationInYears, totalFunds, verbose) if not tm.modelReady: print('Unable to initialize price history for model for ' + str(startDate)) if returndailyValues: return pandas.DataFrame() else: return totalFunds else: while not tm.ModelCompleted(): tm.ProcessDay() currentPrices = tm.GetPriceSnapshot() if not currentPrices == None: if tm.TraunchesAvailable( ) and tm.FundsAvailable() > currentPrices.high: tm.PlaceBuy(ticker, currentPrices.low, True) if tm.AccountingError(): break if returndailyValues: tm.CloseModel(verbose, saveHistoryToFile) return tm.GetdailyValue() #return daily value else: return tm.CloseModel(verbose, saveHistoryToFile) #return closing value
def RunTradingModelBuyHold(tm: TradingModel, ticker: str): currentPrices = tm.GetPriceSnapshot() if tm.verbose: print(currentPrices.snapShotDate, currentPrices.nextDayTarget) if not currentPrices == None: if tm.TranchesAvailable() > 0 and tm.FundsAvailable( ) > currentPrices.high: tm.PlaceBuy(ticker, currentPrices.low, True)
def RunTradingModelBuyHold(tm: TradingModel, ticker: str): #Baseline model, buy and hold currentPrices = tm.GetPriceSnapshot() if tm.verbose: print(currentPrices.snapShotDate, currentPrices.nextDayTarget) if not currentPrices == None: for i in range(tm._tranchCount): available, buyPending, sellPending, longPositions = tm.PositionSummary( ) if tm.TranchesAvailable() > 0 and tm.FundsAvailable( ) > currentPrices.high: tm.PlaceBuy(ticker, currentPrices.low, True) if available == 0: break
def RunTradingModelSeasonal(tm: TradingModel, ticker: str): SellMonth = 4 #April BuyMonth = 10 #October currentPrices = tm.GetPriceSnapshot() if not currentPrices == None: low = currentPrices.low high = currentPrices.high m = tm.currentDate.month available, buyPending, sellPending, longPositions = tm.PositionSummary( ) if m >= SellMonth and m <= BuyMonth: if longPositions > 0: tm.PlaceSell(ticker, high, True) else: if available > 0 and tm.FundsAvailable() > high: tm.PlaceBuy(ticker, low, True)
def RunTradingModelSeasonal(tm: TradingModel, ticker: str): #Buy in November, sell in May SellMonth = 5 BuyMonth = 11 currentPrices = tm.GetPriceSnapshot() if not currentPrices == None: low = currentPrices.low high = currentPrices.high m = tm.currentDate.month for i in range(tm._tranchCount): available, buyPending, sellPending, longPositions = tm.PositionSummary( ) if m >= SellMonth and m <= BuyMonth: if longPositions > 0: tm.PlaceSell(ticker, high, True) else: break else: if available > 0 and tm.FundsAvailable() > high: tm.PlaceBuy(ticker, low, True) else: break
def RunTradingModelSeasonal(ticker: str, startDate: str, durationInYears: int, totalFunds: int, verbose: bool = False, saveHistoryToFile: bool = True, returndailyValues: bool = False): modelName = 'Seasonal' + '_' + ticker tm = TradingModel(modelName, ticker, startDate, durationInYears, totalFunds, verbose) if not tm.modelReady: print('Unable to initialize price history for model for ' + str(startDate)) if returndailyValues: return pandas.DataFrame() else: return totalFunds else: while not tm.ModelCompleted(): tm.ProcessDay() currentPrices = tm.GetPriceSnapshot() if not currentPrices == None: low = currentPrices.low high = currentPrices.high m = tm.currentDate.month available, buyPending, sellPending, longPositions = tm.GetPositionSummary( ) if m >= 11 or m <= 4: #Buy if Nov through April, else sell if available > 0 and tm.FundsAvailable() > high: tm.PlaceBuy(ticker, low, True) else: if longPositions > 0: tm.PlaceSell(ticker, high, True) if tm.AccountingError(): break if returndailyValues: tm.CloseModel(verbose, saveHistoryToFile) return tm.GetdailyValue() #return daily value else: return tm.CloseModel(verbose, saveHistoryToFile) #return closing value
def RunTradingModelFirstHalfOfMonth(tm: TradingModel, ticker: str): #From Robert Ariel's observations, most gains are in the first half of the month BuyDay = 25 #Buy at the end of the month, after the 25th SellDay = 15 #Sell mid month, after the 15th currentPrices = tm.GetPriceSnapshot() if not currentPrices == None: low = currentPrices.low high = currentPrices.high d = tm.currentDate.day for i in range(tm._tranchCount): available, buyPending, sellPending, longPositions = tm.PositionSummary( ) if d >= BuyDay or d < 3: if available > 0 and tm.FundsAvailable() > high: tm.PlaceBuy(ticker, low, True) else: break elif d >= SellDay: if longPositions > 0: tm.PlaceSell(ticker, high, True) else: break else: break