# dataframe for a historical instrument feature (basis in this case). The index is the timestamps # atmost upto lookback data points. The columns of this dataframe are the stocks/instrumentIds. lookbackInstrumentValue = lookbackInstrumentFeatures.getFeatureDf('F5') # The last row of the previous dataframe gives the last calculated value for that feature (basis in this case) # This returns a series with stocks/instrumentIds as the index. currentValue = lookbackInstrumentValue.iloc[-1] if param1Value == 'value1': return currentValue * 0.1 else: return currentValue * 0.5 if __name__ == "__main__": if updateCheck(): print( 'Your version of the auquan toolbox package is old. Please update by running the following command:' ) print('pip install -U auquan_toolbox') else: tf = MyTradingFunctions() tsParams = MyTradingParams(tf) tradingSystem = TradingSystem(tsParams) # Set onlyAnalyze to True to quickly generate csv files with all the features # Set onlyAnalyze to False to run a full backtest # Set makeInstrumentCsvs to False to not make instrument specific csvs in runLogs. This improves the performance BY A LOT tradingSystem.startTrading(onlyAnalyze=False, shouldPlot=True, makeInstrumentCsvs=True)
param1Value = featureParams['param1'] # A holder for the all the instrument features lookbackInstrumentFeatures = instrumentManager.getLookbackInstrumentFeatures() # dataframe for a historical instrument feature (basis in this case). The index is the timestamps # atmost upto lookback data points. The columns of this dataframe are the stocks/instrumentIds. lookbackInstrumentValue = lookbackInstrumentFeatures.getFeatureDf('Adj Close') # The last row of the previous dataframe gives the last calculated value for that feature (basis in this case) # This returns a series with stocks/instrumentIds as the index. currentValue = lookbackInstrumentValue.iloc[-1] if param1Value == 'value1': return currentValue * 0.1 else: return currentValue * 0.5 if __name__ == "__main__": if updateCheck(): print('Your version of the auquan toolbox package is old. Please update by running the following command:') print('pip install -U auquan_toolbox') else: tsParams = MyTradingParams() tradingSystem = TradingSystem(tsParams) # Set onlyAnalyze to True to quickly generate csv files with all the features # Set onlyAnalyze to False to run a full backtest # Set makeInstrumentCsvs to False to not make instrument specific csvs in runLogs. This improves the performance BY A LOT tradingSystem.startTrading(onlyAnalyze=False, shouldPlot=True, makeInstrumentCsvs=True, createResultDict=False)
def run_plain_momentum(): tsParams = PlainMomParams() tradingSystem = TradingSystem(tsParams) results = tradingSystem.startTrading() return results
currentMarketFeatures['ma_r%s_90' % i] ) / currentMarketFeatures['sdev_r%s_90' % i] else: z_score[i] = 0 instrument = instrumentManager.getInstrument(instrumentIds[0]) #z_score = z_score + instrument.getDataDf()['position']/20000 if currentMarketFeatures['correl_r%s_90' % i] < 0.5: z_score[i] = 0 if z_score[i] > 1: prediction[PAIRIDS[i][0]] = 0 prediction[PAIRIDS[i][1]] = 1 elif z_score[i] < -1: prediction[PAIRIDS[i][0]] = 1 prediction[PAIRIDS[i][1]] = 0 elif (z_score[i] > 0.5) or (z_score[i] < -0.5): prediction[PAIRIDS[i][0]] = 0.75 prediction[PAIRIDS[i][1]] = 0.25 else: prediction[PAIRIDS[i][0]] = 0.5 prediction[PAIRIDS[i][1]] = 0.5 return prediction if __name__ == "__main__": tsParams = MyTradingParams() tradingSystem = TradingSystem(tsParams) tradingSystem.startTrading(onlyAnalyze=False)
ma90 = ma90Data.iloc[-1] # Calculate Hurst Exponent hurst = ma90Data.apply(hurst_f, axis=0) # Go long if Hurst > 0.5 and both long term and short term momentum are positive predictions[(hurst > 0.5) & (mom30 > 0) & (mom10 > 0)] = 1 # Go short if Hurst > 0.5 and both long term and short term momentum are negative predictions[(hurst > 0.5) & (mom30 <= 0) & (mom10 <= 0)] = 0 # Get out of position if Hurst > 0.5 and long term momentum is positive while short term is negative predictions[(hurst > 0.5) & (mom30 > 0) & (mom10 <= 0)] = 0.5 # Get out of position if Hurst > 0.5 and long term momentum is negative while short term is positive predictions[(hurst > 0.5) & (mom30 <= 0) & (mom10 > 0)] = 0.5 # Get out of position if Hurst < 0.5 predictions[hurst <= 0.5] = 0.5 else: # If no sufficient data then don't take any positions predictions.values[:] = 0.5 return predictions def updateCount(self): self.count = self.count + 1 tf = MyTradingFunctions() tsParams = MomentumTradingParams(tf) tradingSystem = TradingSystem(tsParams) results = tradingSystem.startTrading()
# dataframe for a historical instrument feature (basis in this case). The index is the timestamps # atmost upto lookback data points. The columns of this dataframe are the symbols/instrumentIds. lookbackInstrumentValue = lookbackInstrumentFeatures.getFeatureDf('symbolVWAP') # The last row of the previous dataframe gives the last calculated value for that feature (basis in this case) # This returns a series with symbols/instrumentIds as the index. currentValue = lookbackInstrumentValue.iloc[-1] if param1Value == 'value1': return currentValue * 0.1 else: return currentValue * 0.5 if __name__ == "__main__": if updateCheck(): print('Your version of the auquan toolbox package is old. Please update by running the following command:') print('pip install -U auquan_toolbox') else: print('Loading your config dicts and prediction function') tf = MyTradingFunctions() print('Loaded config dicts and prediction function, Loading Problem Params') tsParams1 = MyTradingParams(tf, 'Revenue(Y)') tradingSystem = TradingSystem(tsParams1) # s=MyTradingFunctions() # s.getInstrumentIds() results1 = tradingSystem.startTrading(onlyAnalyze=False, shouldPlot=False, makeInstrumentCsvs=False) # tsParams2 = MyTradingParams(tf, 'Income(Y)') # tradingSystem = TradingSystem(tsParams2) # results2 = tradingSystem.startTrading(onlyAnalyze=False, shouldPlot=False, makeInstrumentCsvs=False) print('Score: %0.3f'%((results1['score'])))#+results2['score'])/2))
instrument: Instrument for which this feature is being calculated instrumentManager: A holder for all the instruments ''' @classmethod def computeForInstrument(cls, featureParams, featureKey, currentFeatures, instrument, instrumentManager): # Current Book Data (dictionary) for the Instrument. This is the last update to the instrument bookData = instrument.getCurrentBookData() # Custom parameter which can be used as input to computation of this feature param1Value = featureParams['param1'] # dataframe for historical instrument features. The last row of this data frame # would contain the features which are being calculated in this update cycle or for this time. # The second to last row (if exists) would have the features for the previous # time update. Columns will be featureKeys for different features lookbackInstrumentFeaturesDf = instrument.getDataDf() return 0 if __name__ == "__main__": if updateCheck(): print 'Your version of the auquan toolbox package is old. Please update by running the following command:' print 'pip install -U auquan_toolbox' problem1Solver = Problem1Solver() tsParams = FairValueTradingParams(problem1Solver) tradingSystem = TradingSystem(tsParams) # Set onlyAnalyze to True to quickly generate csv files with all the features # Set onlyAnalyze to False to run a full backtest tradingSystem.startTrading(onlyAnalyze=False, shouldPlot=False)
def run_hurst_momentum(): tsParams = HurstMomParams() tradingSystem = TradingSystem(tsParams) results = tradingSystem.startTrading() return results
def run_hurst_pairs(): tsParams = HurstPairParams() tradingSystem = TradingSystem(tsParams) result = tradingSystem.startTrading() return result
def run_plain_pairs(): tsParams = PlainPairParams() tradingSystem = TradingSystem(tsParams) result = tradingSystem.startTrading() return result