Package: tradingene
Tradingene: A Package For Backtesting Trading AlgorithmsSee full documentationhereThe Tradingene package turns your computer into a tool for developing and backtesting trading strategies you write in the Python programming language. Having been developed and backtested, these ones can be then easily adapted for live trading at theTradingene Platform.InstallationTradingne can be installed via pip for python3:pip3 install tradingeneGetting StartedAn algorithm performs a trading logic that is implemented in a user defined function. If we want to test profitability of this logic we have to perform a backtest which consists of a series of consecutive calls of this (user defined) function.Suppose we came up with the following trading logic:open a long position if the closing price of a bar is greater than the open price;open a short position if the closing price of a bar is less than the open price;do not make any changes to the position otherwise.To start coding we need to define thenameand theregimeof the algorithm as well as thestart_dateand theend_dateof the backtesting period:Setting parametersfromdatetimeimportdatetimefromtradingene.algorithm_backtest.tngimportTNGfromtradingene.backtest_statisticsimportbacktest_statisticsasbsname="Cornucopia"regime="SP"start_date=datetime(2018,9,1)end_date=datetime(2018,10,1)After that we are ready to create an instance of theTNGclass. The instance namedalgwill contain all the methods required for backtesting.alg=TNG(name,regime,start_date,end_date)See  more on initialization.Next we are able to specify an instrument and timeframe (measured in minutes) that we will use in our backtest:alg.addInstrument("btcusd")alg.addTimeframe("btcusd",1440)See  more on adding instruments and timeframes.Implementing trading logicIn the next step we will code theonBar()function that will implement our trading logic:defonBar(instrument):ifinstrument.open[1]>instrument.close[1]:# If the price moved down we take a short positionalg.sell()elifinstrument.open[1]<instrument.close[1]:# If the price moved up we take a long positionalg.buy()else:# If the price did not change then do nothingpassTheinstrumentvariable contains price values as well as the values of specified technical indicators.See more on onBar function.Now we are ready to run a backtest:alg.run_backtest(onBar)Results of backtestAfter the backtest is complete we may retrieve the statistics to estimate the performance of our algorithm:stats=bs.BacktestStatistics(alg)stats.backtest_results(plot=True,filename="backtest_stats")With these lines of code we make the backtest statistics formatted into an html page namedbacktest_stats.html. This page also shows us a cumulative profit plot, just like the one presented below:See more on backtest statistics.Machine learning and loading dataA powerful feature of theTradingenepackage is the ability to load, recalculate and convert data into a form suitable to train machine learning models with. That's why an algotrader can easily create and backtest "learning" trading robots.With a series of our mini-lessons you'll learn how to use neural networks for solvingclassificationandregressionproblems (with respect to the challenges of trading) as well as how to engage anSVMetc.See more onloading data.