Package: backtestify
backtestifyTable of ContentsbacktestifyTable of ContentsIntroductionInstallationUsageContributingLicenseIntroductionWhile there are already backtesting tools available, I aimed to create a tool that allows for more accurate backtesting specifically for Contract for Difference (CFD). My vision was to create a comprehensive tool that takes care of the entire backtesting process, where the trader can focus solely on strategy formulation and testing new strategies without needing to invest time in coding. This tool is envisioned to be user-friendly and fully open-source, similar to other strategy analysis builders available on the market, but with the added benefit of being completely free.While platforms like MetaTrader 5 allow for backtesting strategies using technical indicators, they often fall short when it comes to applying advanced techniques such as machine learning. Such processes can be complicated and tedious. By creating a backtester directly in Python, this process is significantly simplified, and it also opens up the possibility of utilizing all kinds of existing Python modules.Installationpip install backtestifyUsageA simple example of how to use the package is shown below. More examples will be added in the future in the examples folder.importpandasaspdfrombacktestifyimportStrategy,SignalEvent,SignalType,Backtester,Account,CFD,InstrumentTypeimportMetaTrader5asmt5importdatetimeimporttalibastadefget_asset_info_mt5(symbol,timeframe=mt5.TIMEFRAME_D1,n=1000):mt5.initialize()utc_from=datetime.datetime.now()rates=mt5.copy_rates_from(symbol,timeframe,utc_from,n)rates_frame=pd.DataFrame(rates)columns=['time','open','high','low','close','tick_volume']rates_frame=rates_frame[columns]rates_frame=rates_frame.set_index('time')rates_frame.rename(columns={'tick_volume':'volume'},inplace=True)rates_frame.index.name='timestamp'rates_frame['swap_long']=-7.84# This is the swap rate for SPX500. This can be found in the Market Watch window in MetaTrader 5 this could be different for a specific broker or instrument and take in consideration that this could be different depending on the day of the week.rates_frame['swap_short']=4.26returnrates_frameclassRSIStrategy(Strategy):def__init__(self,market_info,rsi_period):self.rsi_period=rsi_periodself.previous_trade_signal=Nonemarket_info["rsi"]=ta.RSI(market_info["close"],timeperiod=rsi_period)super().__init__(market_info)defon_tick(self,history):try:current=history.iloc[-1]ifcurrent["rsi"]isNone:returnifcurrent["rsi"]<30andself.previous_trade_signal!=SignalType.BUY:self.previous_trade_signal=SignalType.BUYreturn[SignalEvent(signal=SignalType.EXIT),SignalEvent(signal=SignalType.BUY)]ifcurrent["rsi"]>70andself.previous_trade_signal!=SignalType.SELL:self.previous_trade_signal=SignalType.SELLreturn[SignalEvent(signal=SignalType.EXIT),SignalEvent(signal=SignalType.SELL)]exceptExceptionase:passcfd=CFD(instrument_type=InstrumentType.STOCK,lot_size=1,entry_lots=1,commission=0,point_value=1,leverage=100,point=0.01,spread=5,period=mt5.TIMEFRAME_D1,pips=0.01,)df=get_asset_info_mt5("SPX500")sma=RSIStrategy(df,14)account=Account(10000)backtester=Backtester(sma,cfd,account)backtester.run()backtester.resultsContributingFor any bug reports or recommendations, please visit ourissue trackerand create a new issue. If you're reporting a bug, it would be great if you can provide a minimal reproducible example.Thank you for your contribution!LicenseApache 2.0