├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── examples ├── __init__.py ├── sample1 │ ├── __init__.py │ ├── final_dataset.csv │ ├── main.py │ └── sample_dataset.csv └── sample2 │ ├── README.md │ ├── __init__.py │ ├── data.csv │ ├── main.py │ ├── result │ └── .gitkeep │ └── signal_generator.py ├── logo.png ├── main.py ├── pyproject.toml ├── requirements.txt ├── sample_dataset.png ├── setup.py └── src ├── __init__.py └── signal_backtester ├── __init__.py ├── base ├── __init__.py ├── base_backtester.py └── base_strategy.py ├── core ├── __init__.py ├── operations.py ├── picker.py └── runner.py ├── schemas ├── __init__.py └── models.py └── strategies ├── __init__.py ├── one_sided.py └── two_sided.py /.gitignore: -------------------------------------------------------------------------------- 1 | venv 2 | *.pyc 3 | 4 | /__pycache__ 5 | 6 | dist 7 | *.egg-info -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Signal Backtester 2 | We love your input! We want to make contributing to this project as easy and transparent as possible, whether it's: 3 | - Reporting a bug 4 | - Discussing the current state of the code 5 | - Submitting a fix 6 | - Proposing new features 7 | - Becoming a maintainer 8 | ## We Develop with Github 9 | We use github to host code, to track issues and feature requests, as well as accept pull requests. 10 | ### Issues 11 | Issues should be used to report problems with the library, request a new feature, or to discuss potential changes before a PR is created. When you create a new Issue, a template will be loaded that will guide you through collecting and providing the information we need to investigate. 12 | 13 | If you find an Issue that addresses the problem you're having, please add your own reproduction information to the existing issue rather than creating a new one. Adding a [reaction](https://github.blog/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) can also help be indicating to our maintainers that a particular problem is affecting more than just the reporter. 14 | ### Pull Requests 15 | PRs to our libraries are always welcome and can be a quick way to get your fix or improvement slated for the next release. In general, PRs should: 16 | - Only fix/add the functionality in question **OR** address wide-spread whitespace/style issues, not both. 17 | - Add unit or integration tests for fixed or changed functionality (if a test suite already exists). 18 | - Address a single concern in the least number of changed lines as possible. 19 | - Include documentation in the repo or on our [docs site](https://auth0.com/docs). 20 | - Be accompanied by a complete Pull Request template (loaded automatically when a PR is created). 21 | For changes that address core functionality or would require breaking changes (e.g. a major release), it's best to open an Issue to discuss your proposal first. This is not required but can save time creating and reviewing changes. 22 | 23 | In general, we follow the ["fork-and-pull" Git workflow](https://github.com/susam/gitpr) 24 | 25 | 1. Fork the repository to your own Github account 26 | 2. Clone the project to your machine 27 | 3. Create a branch locally with a succinct but descriptive name 28 | 4. Commit changes to the branch 29 | 5. Following any formatting and testing guidelines specific to this repo 30 | 6. Push changes to your fork 31 | 7. Open a PR in our repository and follow the PR template so that we can efficiently review the changes. 32 | ## Report bugs using Github's [issues](https://github.com/xibalbas/signal_backtester/issues) 33 | We use GitHub issues to track public bugs. Report a bug by [opening a new issue](); it's that easy! 34 | ## Write bug reports with detail, background, and sample code 35 | [This is an example](http://stackoverflow.com/q/12488905/180626) of a bug report I wrote, and I think it's not a bad model. Here's [another example from Craig Hockenberry](http://www.openradar.me/11905408), an app developer whom I greatly respect. 36 | 37 | **Great Bug Reports** tend to have: 38 | - A quick summary and/or background 39 | - Steps to reproduce 40 | - Be specific! 41 | - Give sample code if you can. [My stackoverflow question](http://stackoverflow.com/q/12488905/180626) includes sample code that *anyone* with a base R setup can run to reproduce what I was seeing 42 | - What you expected would happen 43 | - What actually happens 44 | - Notes (possibly including why you think this might be happening, or stuff you tried that didn't work) 45 | 46 | People *love* thorough bug reports. I'm not even kidding. -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Ali Moradi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Alt text](logo.png) 2 | ![GitHub top language](https://img.shields.io/github/languages/top/xibalbas/signal_backtester) 3 | ![GitHub repo size](https://img.shields.io/github/repo-size/xibalbas/signal_backtester) 4 | ![PyPI](https://img.shields.io/pypi/v/signal-backtester) 5 | # Signal Backtester 6 | a tiny backtester Based on [Backtesting](https://pypi.org/project/Backtesting/) Lib . 7 | easiest way to backtest your generated signal. 8 | just need a csv file contain candleStick informations. OHLCV + signal 9 | see [Dataset Structure](https://github.com/xibalbas/signal_backtester#dataset-structure) for more information 10 | # why ? 11 | some time writing good backtest for a strategy is not too easy . and you may have some challenge with backtest libraries. 12 | 13 | so i decided to make a seprate repo for backtesting in easiest way. 14 | what you need is a csv file contain `signal` column . for buy signal you should put `2`, and for sell signal you put `1`. 15 | 16 | and good news is you did not need to write strategy for how trade we wrote it before you just choose yours and finish you did it :)) 17 | see [Strategy](https://github.com/xibalbas/signal_backtester#strategy) guide. 18 | # Quick Start 19 | ## installation 20 | ```bash 21 | pip install signal-backtester 22 | ``` 23 | ## Usage 24 | ```python 25 | from signal_backtester import SignalBacktester 26 | 27 | # address of your dataset file 28 | # columns should include "Open, High, Low, Close, Volume, signal" 29 | 30 | backtest = SignalBacktester( 31 | dataset="/home/xibalbas/sample.csv", 32 | strategy='two_side_sl_tp_reversed', 33 | cash=1000, 34 | commission=0.0005, # equal 0.05 % 35 | percent_of_portfolio=99, 36 | stop_loss=1, 37 | take_profit=10, 38 | trailing_stop=3, # if you are using trailing stop 39 | time_frame='30m', 40 | output_path='.' # path of result files 41 | ) 42 | 43 | backtest.run() 44 | ``` 45 | * also you can see this [example](https://github.com/xibalbas/signal_backtester/tree/master/examples/sample2) for how generate signal . and how backtest generated signal 46 | # strategy 47 | 48 | available strategy to use are : 49 | - **two_side_sl_tp_reversed** 50 | 51 | this strategy open position in both side `buy` and `sell`. it close position with `stoploss` or `take profit` 52 | also if you have an open `buy position` and you give a sell signal we close your last position an open new one 53 | - **two_side_sl_trailing_reversed** 54 | 55 | this strategy open position in both side `buy` and `sell`. it close position with `stoploss`. your stop loss is dynamic if price change your stop loss will change . 56 | also if you have an open `buy position` and you give a sell signal we close your last position an open new one 57 | - **one_side_buy_sl_tp** 58 | 59 | this strategy open position just in one side `buy`. it close position with `stoploss` or `take profit` 60 | - **one_side_sell_sl_tp** 61 | 62 | this strategy open position just in one side `sell`. it close position with `stoploss` or `take profit` 63 | - **one_side_buy_sl_trailing** 64 | 65 | this strategy open position just in one side `buy`. it close position with `stoploss`. your stop loss is dynamic if price change your stop loss will change . 66 | - **one_side_sell_sl_trailing** 67 | 68 | this strategy open position just in one side `sell`. it close position with `stoploss`. your stop loss is dynamic if price change your stop loss will change . 69 | # dataset structure 70 | your data set structure should be like this table 71 | 72 | your buy signals should generate as 2 73 | and your sell signals should generate as 1 74 | 75 | 76 | you must have this columns in your dataset 77 | * Date 78 | * Open 79 | * High 80 | * Low 81 | * Close 82 | * Volume 83 | * signal 84 | 85 | ![Alt text](sample_dataset.png) 86 | # Contributing 87 | see contributing guide [here](https://github.com/xibalbas/signal_backtester/blob/master/CONTRIBUTING.md) 88 | # License 89 | `signal_backtester` is freely available under the MIT [license](https://github.com/xibalbas/signal_backtester/blob/master/LICENSE). 90 | -------------------------------------------------------------------------------- /examples/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alm0ra/signal_backtester/8eaa52ecad22419b29b0e0e34eaadfea83f4e4b9/examples/__init__.py -------------------------------------------------------------------------------- /examples/sample1/__init__.py: -------------------------------------------------------------------------------- 1 | """Sample 2 | Sample 1 3 | more description 4 | """ 5 | -------------------------------------------------------------------------------- /examples/sample1/main.py: -------------------------------------------------------------------------------- 1 | """example of backtester 2 | example of backtester 3 | """ 4 | 5 | from signal_backtester import SignalBacktester 6 | 7 | 8 | dataset_address = "./sample_dataset.csv" 9 | 10 | backtest = SignalBacktester( 11 | dataset=dataset_address, 12 | strategy="two_side_sl_tp_reversed", 13 | cash=1000, 14 | commission=0.0005, 15 | percent_of_portfolio=99, 16 | stop_loss=1, 17 | take_profit=10, 18 | trailing_stop=3, 19 | time_frame="30m", 20 | ) 21 | 22 | backtest.run() 23 | -------------------------------------------------------------------------------- /examples/sample2/README.md: -------------------------------------------------------------------------------- 1 | # sample 2 2 | 3 | ## EMA Cross Strategy 4 | 5 | first you can run signal generator 6 | it will generate a out out data set call `final_dataset.csv` 7 | 8 | ```bash 9 | python signal_generator.py 10 | ``` 11 | 12 | `Notice: you may need to install TA-LIB library` 13 | 14 | ## backtest generated signals 15 | 16 | ```bash 17 | python main.py 18 | ``` 19 | 20 | and result will save in `result` directory -------------------------------------------------------------------------------- /examples/sample2/__init__.py: -------------------------------------------------------------------------------- 1 | """Sample 2 | Sample 2 3 | more description 4 | """ 5 | -------------------------------------------------------------------------------- /examples/sample2/main.py: -------------------------------------------------------------------------------- 1 | """example of backtester 2 | example of backtester 3 | """ 4 | from signal_backtester import SignalBacktester 5 | 6 | # dataset addres 7 | dataset_address = "./final_dataset.csv" 8 | 9 | # make an object with your backtest config 10 | backtest = SignalBacktester( 11 | dataset=dataset_address, 12 | strategy="two_side_sl_tp_reversed", 13 | cash=100000, 14 | commission=0.0005, 15 | percent_of_portfolio=99, 16 | stop_loss=1, 17 | take_profit=2, 18 | trailing_stop=3, 19 | output_path="./result", # path of result files 20 | ) 21 | 22 | 23 | # run your backtest 24 | backtest.run() 25 | -------------------------------------------------------------------------------- /examples/sample2/result/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alm0ra/signal_backtester/8eaa52ecad22419b29b0e0e34eaadfea83f4e4b9/examples/sample2/result/.gitkeep -------------------------------------------------------------------------------- /examples/sample2/signal_generator.py: -------------------------------------------------------------------------------- 1 | import talib # notice you can install talib manually 2 | import pandas as pd 3 | 4 | 5 | def cross_EMA_signals(df, fast_period, slow_period): 6 | # generate a zero list for signals and we turn it to 1 if we had 7 | # a sell signal also we turn it to 2 if we had buy signal 8 | signal = [0] * len(df) 9 | 10 | # calculate fast and slow of exp moving average 11 | df["fast"] = talib.EMA(df.Close, timeperiod=fast_period) 12 | df["slow"] = talib.EMA(df.Close, timeperiod=slow_period) 13 | 14 | # loop on dataframe and looking for signals 15 | for idx in range(len(df)): 16 | if idx > slow_period: 17 | 18 | # Condition 1 (buy signal Cross EMA UP) 19 | if ( 20 | df.iloc[idx - 1].fast < df.iloc[idx - 1].slow 21 | and df.iloc[idx].fast > df.iloc[idx].slow 22 | ): 23 | # buy signal (change signal amount ) 24 | signal[idx] = 2 25 | 26 | # Condition 2 (sell signal Cross EMA DOWN) 27 | if ( 28 | df.iloc[idx - 1].fast > df.iloc[idx - 1].slow 29 | and df.iloc[idx].fast < df.iloc[idx].slow 30 | ): 31 | # sell signal (change signal amount ) 32 | signal[idx] = 1 33 | 34 | # save generated signal time frame 35 | df["signal"] = signal 36 | df.to_csv("./final_dataset.csv") 37 | 38 | 39 | # run 40 | df = pd.read_csv("./data.csv") 41 | 42 | if __name__ == "__main__": 43 | cross_EMA_signals(df, 15, 30) 44 | -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alm0ra/signal_backtester/8eaa52ecad22419b29b0e0e34eaadfea83f4e4b9/logo.png -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alm0ra/signal_backtester/8eaa52ecad22419b29b0e0e34eaadfea83f4e4b9/main.py -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = [ 3 | "setuptools>=42", 4 | "wheel" 5 | ] 6 | build-backend = "setuptools.build_meta" -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Backtesting==0.3.3 2 | bokeh==2.4.2 3 | Jinja2==3.0.3 4 | MarkupSafe==2.1.0 5 | numpy==1.22.2 6 | packaging==21.3 7 | pandas==1.4.1 8 | Pillow==9.0.1 9 | pydantic==1.9.0 10 | pyparsing==3.0.7 11 | python-dateutil==2.8.2 12 | pytz==2021.3 13 | PyYAML==6.0 14 | six==1.16.0 15 | tornado==6.1 16 | typing-extensions==4.1.1 17 | -------------------------------------------------------------------------------- /sample_dataset.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alm0ra/signal_backtester/8eaa52ecad22419b29b0e0e34eaadfea83f4e4b9/sample_dataset.png -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import setuptools 2 | 3 | with open("README.md", "r", encoding="utf-8") as fh: 4 | long_description = fh.read() 5 | 6 | setuptools.setup( 7 | name="signal_backtester", 8 | version="1.0.4", 9 | author="Ali moradi", 10 | author_email="ali.mrd318@gmail.com", 11 | description="tiny library for fast backtest on generated signals", 12 | long_description=long_description, 13 | long_description_content_type="text/markdown", 14 | url="https://github.com/xibalbas/signal_backtester", 15 | project_urls={ 16 | "Bug Tracker": "https://github.com/xibalbas/signal_backtester/issues", 17 | }, 18 | classifiers=[ 19 | "Programming Language :: Python :: 3", 20 | "License :: OSI Approved :: MIT License", 21 | "Operating System :: OS Independent", 22 | ], 23 | package_dir={"": "src"}, 24 | packages=setuptools.find_packages(where="src"), 25 | install_requires=["backtesting", "pydantic"], 26 | python_requires=">=3.7", 27 | ) 28 | -------------------------------------------------------------------------------- /src/__init__.py: -------------------------------------------------------------------------------- 1 | """src 2 | src Package 3 | more description 4 | """ 5 | -------------------------------------------------------------------------------- /src/signal_backtester/__init__.py: -------------------------------------------------------------------------------- 1 | """Signal Backtester 2 | Signal Backtester Package 3 | more description 4 | """ 5 | 6 | from signal_backtester.core.runner import SignalBacktester 7 | 8 | 9 | __all__ = ["SignalBacktester"] 10 | -------------------------------------------------------------------------------- /src/signal_backtester/base/__init__.py: -------------------------------------------------------------------------------- 1 | """Base 2 | Base Package 3 | more description 4 | """ 5 | -------------------------------------------------------------------------------- /src/signal_backtester/base/base_backtester.py: -------------------------------------------------------------------------------- 1 | from backtesting import Backtest 2 | import pandas as pd 3 | 4 | 5 | class BackTestingDataset: 6 | name = None 7 | data = None 8 | backtest = None 9 | 10 | def __init__(self, name, data): 11 | self.name = name 12 | self.data = data 13 | 14 | 15 | class BackTestingBackTest: 16 | """ 17 | Backtesting class 18 | written for backtest several dataset on a strategy 19 | it run engine of backtest on datasets and return a plot 20 | also you can optimize your strategy. 21 | """ 22 | 23 | datasets = [] 24 | 25 | def __init__(self, datasets, strategy, **kwargs): 26 | for dataset in datasets: 27 | dataset.backtest = Backtest(dataset.data, strategy=strategy, **kwargs) 28 | self.datasets = datasets 29 | 30 | def run(self, **kwargs): 31 | results = [dataset.backtest.run(**kwargs) for dataset in self.datasets] 32 | dataframe_results = pd.DataFrame(results).transpose() 33 | return dataframe_results 34 | 35 | def plot(self, **kwargs): 36 | # plot result with backtesing plot module 37 | [dataset.backtest.plot(**kwargs) for dataset in self.datasets] 38 | return True 39 | 40 | # def optimize(self, **kwargs): 41 | # """ 42 | # optimization module 43 | # for optimizing your strategy, get best result & best config 44 | # """ 45 | # optimize_args = {"return_heatmap": True, **kwargs} 46 | # heatmaps = [] 47 | 48 | # for dataset in self.datasets: 49 | # _best_stats, heatmap = dataset.backtest.optimize(**optimize_args) 50 | # heatmaps.append(heatmap) 51 | 52 | # return pd.DataFrame(heatmaps) 53 | -------------------------------------------------------------------------------- /src/signal_backtester/base/base_strategy.py: -------------------------------------------------------------------------------- 1 | from backtesting import Strategy 2 | from abc import abstractmethod 3 | import pandas as pd 4 | 5 | 6 | class BacktestingBaseStrategy(Strategy): 7 | """ 8 | Base Strategy class 9 | it prepare a final report of orders by 'save_trades' module 10 | a csv report contain of some information of each trade 11 | will prepare here 12 | also it determines when strategy stops and how many candle 13 | pass from next() function 14 | """ 15 | 16 | def init(self): 17 | super().init() 18 | self.order_csv_report = None 19 | self.dataframe_count = len(self.data) 20 | 21 | def save_trades(self, closed_trades): 22 | # make a dataframe 23 | self.order_csv_report = pd.DataFrame( 24 | columns=[ 25 | "entry_price", 26 | "entry_time", 27 | "exit_price", 28 | "exit_time", 29 | "side", 30 | "pl", 31 | "pl_pct", 32 | "size", 33 | "value", 34 | "status", 35 | ] 36 | ) 37 | # append information od each trades 38 | for trade in closed_trades: 39 | pl_pct = trade.pl_pct * 100 40 | pl_pct_round = round(pl_pct, 2) 41 | order_report = { 42 | "entry_price": trade.entry_price, 43 | "entry_time": trade.entry_time, 44 | "exit_price": trade.exit_price, 45 | "exit_time": trade.exit_time, 46 | "pl": trade.pl, 47 | "pl_pct": str(pl_pct_round) + " %", 48 | "size": trade.size, 49 | "value": trade.value, 50 | } 51 | if trade.is_long: 52 | order_report["side"] = "BUY" 53 | if trade.is_short: 54 | order_report["side"] = "SELL" 55 | 56 | if trade.tp == trade.exit_price: 57 | order_report["status"] = "TP trigger" 58 | elif trade.sl == trade.exit_price: 59 | order_report["status"] = "SL trigger" 60 | else: 61 | order_report["status"] = "closed" 62 | 63 | self.order_csv_report = self.order_csv_report.append( 64 | order_report, ignore_index=True 65 | ) 66 | # make an out put 67 | self.order_csv_report.to_csv(self.order_report_path, index=False) 68 | 69 | def next(self): 70 | super().next() 71 | if self.data._Data__i > self.dataframe_count - 1: 72 | self.stop() 73 | 74 | @abstractmethod 75 | def stop(self): 76 | # this method call when backtest is finished 77 | pass 78 | -------------------------------------------------------------------------------- /src/signal_backtester/core/__init__.py: -------------------------------------------------------------------------------- 1 | """Core 2 | Core Package 3 | more description 4 | """ 5 | -------------------------------------------------------------------------------- /src/signal_backtester/core/operations.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | import pandas as pd 3 | 4 | 5 | def time_format_picker(timeframe): 6 | """ 7 | format of time for each time frame for 8 | changing time from timestamp 9 | """ 10 | time_dict = { 11 | "1d": "%Y-%m-%d", 12 | "4h": "%Y-%m-%d %H:%M", 13 | "1h": "%Y-%m-%d %H:%M", 14 | "30m": "%Y-%m-%d %H:%M", 15 | "15m": "%Y-%m-%d %H:%M", 16 | "5m": "%Y-%m-%d %H:%M", 17 | "1m": "%Y-%m-%d %H:%M", 18 | } 19 | return time_dict.get(timeframe, None) 20 | 21 | 22 | def timestamp_changer(ts, time_format): 23 | # change timestamp to time format 24 | return datetime.datetime.fromtimestamp((ts // 1000)).strftime(time_format) 25 | 26 | 27 | def prepare_data(time_frame, data_frame_signal): 28 | # change format of Date from timestamp to usual mode 29 | time_format = time_format_picker(time_frame) 30 | data_frame_signal["Date"] = pd.to_datetime( 31 | [timestamp_changer(ts, time_format) for ts in data_frame_signal["Date"]] 32 | ) 33 | # make Date column as index because Backtesting give Date as index 34 | data_frame_signal = data_frame_signal.set_index("Date") 35 | 36 | return data_frame_signal 37 | -------------------------------------------------------------------------------- /src/signal_backtester/core/picker.py: -------------------------------------------------------------------------------- 1 | from signal_backtester.strategies.one_sided import ( 2 | BuyOneSidedSlTpCloseOpposite, 3 | SellOneSidedSlTpCloseOpposite, 4 | BuyOneSidedSlTrailingCloseOpposite, 5 | SellOneSidedSlTrailingCloseOpposite, 6 | ) 7 | from signal_backtester.strategies.two_sided import ( 8 | TwoSidedSlTpReverse, 9 | TwoSidedSlTrailingReverse, 10 | ) 11 | 12 | STRATEGIES = { 13 | # two-sided strategies 14 | "two_side_sl_tp_reversed": TwoSidedSlTpReverse, 15 | # stop trailing 16 | "two_side_sl_trailing_reversed": TwoSidedSlTrailingReverse, 17 | # one-sided strategies 18 | "one_side_buy_sl_tp": BuyOneSidedSlTpCloseOpposite, 19 | "one_side_sell_sl_tp": SellOneSidedSlTpCloseOpposite, 20 | # stop-trailing 21 | "one_side_buy_sl_trailing": BuyOneSidedSlTrailingCloseOpposite, 22 | "one_side_sell_sl_trailing": SellOneSidedSlTrailingCloseOpposite, 23 | } 24 | -------------------------------------------------------------------------------- /src/signal_backtester/core/runner.py: -------------------------------------------------------------------------------- 1 | from signal_backtester.base.base_backtester import ( 2 | BackTestingBackTest, 3 | BackTestingDataset, 4 | ) 5 | from signal_backtester.core.picker import STRATEGIES 6 | from signal_backtester.schemas.models import InputValidatorBase 7 | from signal_backtester.core.operations import prepare_data 8 | import pandas as pd 9 | 10 | 11 | class SignalBacktester: 12 | """ 13 | Signal Baktester moudle 14 | 15 | it uses Backtesting Lib (https://kernc.github.io/backtesting.py/) 16 | you must have this columns in your dataset 17 | - Date (timestamp is prefered) 18 | - Open 19 | - High 20 | - Low 21 | - Close 22 | - Volume 23 | - signal (1 for sell signal)(2 for buy signal) 24 | """ 25 | 26 | def __init__( 27 | self, 28 | dataset, 29 | strategy, 30 | cash, 31 | commission, 32 | percent_of_portfolio, 33 | data_name="data", 34 | stop_loss=None, 35 | take_profit=None, 36 | trailing_stop=None, 37 | time_frame="5m", 38 | output_path=".", 39 | ): 40 | # validate input fields with a pydantic model 41 | self.fields = InputValidatorBase( 42 | cash=cash, 43 | commission=commission, 44 | stop_loss=stop_loss, 45 | take_profit=take_profit, 46 | trailing_stop=trailing_stop, 47 | percent_of_portfolio=percent_of_portfolio, 48 | dataset=dataset, 49 | strategy=strategy, 50 | time_frame=time_frame, 51 | ) 52 | self.data_name = data_name 53 | self.out_path = output_path 54 | 55 | def read_dataset(self): 56 | return pd.read_csv(self.fields.dataset) 57 | 58 | def run(self): 59 | # read dataset and prepare data for backtesting lib 60 | dataframe = self.read_dataset() 61 | dataframe = prepare_data(self.fields.time_frame, dataframe) 62 | 63 | # define signal column as an indicator 64 | def SIGNAL(): 65 | return dataframe.signal 66 | 67 | params = { 68 | "stop_loss": self.fields.stop_loss, 69 | "take_profit": self.fields.take_profit, 70 | "stop_trailing_amount": self.fields.trailing_stop, 71 | "indicator": SIGNAL, 72 | "order_report_path": f"{self.out_path}/order_report.csv", 73 | "percent_of_portfolio": self.fields.percent_of_portfolio, 74 | } 75 | 76 | datasets = [ 77 | BackTestingDataset(self.data_name, dataframe), 78 | ] 79 | 80 | backtest = BackTestingBackTest( 81 | datasets, 82 | strategy=STRATEGIES.get(self.fields.strategy), 83 | cash=self.fields.cash, 84 | commission=self.fields.commission, 85 | exclusive_orders=True, 86 | trade_on_close=False, 87 | ) 88 | 89 | # final csv report 90 | report = backtest.run(params=params) 91 | report_path = f"{self.out_path}/final_report.csv" 92 | report.to_csv(report_path) 93 | 94 | # final html report 95 | backtest.plot( 96 | filename=f"{self.out_path}/final_report.html", 97 | plot_equity=True, 98 | plot_return=True, 99 | plot_pl=True, 100 | plot_volume=True, 101 | plot_drawdown=True, 102 | smooth_equity=True, 103 | relative_equity=True, 104 | superimpose=True, 105 | resample=True, 106 | reverse_indicators=False, 107 | show_legend=True, 108 | open_browser=False, 109 | ) 110 | -------------------------------------------------------------------------------- /src/signal_backtester/schemas/__init__.py: -------------------------------------------------------------------------------- 1 | """Schemas 2 | Schemas Package 3 | more description 4 | """ 5 | -------------------------------------------------------------------------------- /src/signal_backtester/schemas/models.py: -------------------------------------------------------------------------------- 1 | from pydantic import BaseModel, Field, root_validator 2 | from typing import Union 3 | from typing_extensions import Literal 4 | import pandas as pd 5 | 6 | 7 | class InputValidatorBase(BaseModel): 8 | """ 9 | Validate input fields model 10 | validation of dataset will checks: 11 | - if dataset is csv file 12 | - if dataset exist 13 | - if some column exist in dataset 14 | """ 15 | 16 | cash: int = Field(gt=0) 17 | commission: float = Field(gt=0, lt=0.1) 18 | stop_loss: float = Field(gt=0) 19 | take_profit: float = Field(gt=0) 20 | trailing_stop: float = Field(gt=0) 21 | percent_of_portfolio: int = Field(gt=0, lt=100) 22 | dataset: str 23 | strategy: Union[ 24 | Literal["two_side_sl_tp_reversed"], 25 | Literal["two_side_sl_trailing_reversed"], 26 | Literal["one_side_sell_sl_tp"], 27 | Literal["one_side_buy_sl_tp"], 28 | Literal["one_side_buy_sl_trailing"], 29 | Literal["one_side_sell_sl_trailing"], 30 | ] 31 | 32 | time_frame: Union[ 33 | Literal["1m"], 34 | Literal["5m"], 35 | Literal["15m"], 36 | Literal["30m"], 37 | Literal["1h"], 38 | Literal["4h"], 39 | Literal["1d"], 40 | ] 41 | 42 | @root_validator(pre=True) 43 | def do_validation(cls, values): 44 | try: 45 | # check if dataset is a csv file 46 | if ".csv" not in values["dataset"]: 47 | raise ValueError("[dataset] file should be a .csv file") 48 | 49 | # check if exist and you can make a dataframe 50 | df = pd.read_csv(values["dataset"]) 51 | 52 | # check if column exist 53 | available_columns = [ 54 | "Date", 55 | "Open", 56 | "High", 57 | "Low", 58 | "Close", 59 | "Volume", 60 | "signal", 61 | ] 62 | 63 | for column in available_columns: 64 | if column not in df.columns: 65 | raise ValueError(f"[dataset must contain {column} column]") 66 | 67 | except FileNotFoundError: 68 | raise ValueError("[dataset] file Does not exist") 69 | 70 | return values 71 | -------------------------------------------------------------------------------- /src/signal_backtester/strategies/__init__.py: -------------------------------------------------------------------------------- 1 | """Strategies 2 | Strategies folder package 3 | contain 4 | - one_sided.py 5 | - two_sided.py 6 | """ 7 | -------------------------------------------------------------------------------- /src/signal_backtester/strategies/one_sided.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from signal_backtester.base.base_strategy import BacktestingBaseStrategy 3 | 4 | 5 | class BuyOneSidedSlTpCloseOpposite(BacktestingBaseStrategy): 6 | """ 7 | 1 Side (long) With Stoploss & TakeProfit and reverse 8 | this strategy can open position on 2 side 9 | it set stop loss and take profit if hit will close the position 10 | if you gave opposit signal it will close your last position and open 11 | new position 12 | """ 13 | 14 | params = None 15 | 16 | def init(self): 17 | super().init() 18 | self.signal_indicator = self.I(self.params.get("indicator")) 19 | self.order_report_path = self.params.get("order_report_path") 20 | 21 | def next(self): 22 | super().next() 23 | close_price = self.data.Close[-1] 24 | for trade in self.trades: 25 | if trade.is_long: 26 | if self.signal_indicator == 1: 27 | trade.close() 28 | 29 | if self.signal_indicator == 2 and len(self.trades) == 0: 30 | self.buy( 31 | sl=close_price - (self.params.get("stop_loss") * close_price / 100), 32 | tp=close_price + (self.params.get("take_profit") * close_price / 100), 33 | ) 34 | 35 | def stop(self): 36 | self.save_trades(self.closed_trades) 37 | 38 | 39 | class SellOneSidedSlTpCloseOpposite(BacktestingBaseStrategy): 40 | """ 41 | 1 Side (Short) With Stoploss & TakeProfit and reverse 42 | this strategy can open position on 2 side 43 | it set stop loss and take profit if hit will close the position 44 | if you gave opposit signal it will close your last position and open 45 | new position 46 | """ 47 | 48 | params = None 49 | 50 | def init(self): 51 | super().init() 52 | self.signal_indicator = self.I(self.params.get("indicator")) 53 | self.order_report_path = self.params.get("order_report_path") 54 | 55 | def next(self): 56 | super().next() 57 | close_price = self.data.Close[-1] 58 | for trade in self.trades: 59 | if not trade.is_long: 60 | if self.signal_indicator == 2: 61 | trade.close() 62 | 63 | if self.signal_indicator == 1 and len(self.trades) == 0: 64 | self.sell( 65 | sl=close_price + (self.params.get("stop_loss") * close_price / 100), 66 | tp=close_price - (self.params.get("take_profit") * close_price / 100), 67 | ) 68 | 69 | def stop(self): 70 | self.save_trades(self.closed_trades) 71 | 72 | 73 | class BuyOneSidedSlTrailingCloseOpposite(BacktestingBaseStrategy): 74 | """ 75 | 1 Side (long) trail stop and reverse 76 | this strategy can open position on 1 side and it just have a trail stop 77 | if you gave opposit signal it will close your last position and open 78 | new position 79 | """ 80 | 81 | params = None 82 | 83 | def init(self): 84 | super().init() 85 | self.signal_indicator = self.I(self.params.get("indicator")) 86 | self.order_report_path = self.params.get("order_report_path") 87 | self.stop_trailing_amount = self.params.get("stop_trailing_amount") 88 | 89 | def next(self): 90 | super().next() 91 | close_price = self.data.Close[-1] 92 | stop_trail_long = close_price - (close_price * self.stop_trailing_amount / 100) 93 | 94 | for trade in self.trades: 95 | if trade.is_long: 96 | trade.sl = max(trade.sl or -np.inf, stop_trail_long) 97 | if self.signal_indicator == 1: 98 | trade.close() 99 | 100 | if self.signal_indicator == 2 and len(self.trades) == 0: 101 | self.buy(sl=stop_trail_long) 102 | 103 | def stop(self): 104 | self.save_trades(self.closed_trades) 105 | 106 | 107 | class SellOneSidedSlTrailingCloseOpposite(BacktestingBaseStrategy): 108 | """ 109 | 1 Side (short) trail stop and reverse 110 | this strategy can open position on 1 side and it just have a trail stop 111 | if you gave opposit signal it will close your last position and open 112 | new position 113 | """ 114 | 115 | params = None 116 | 117 | def init(self): 118 | super().init() 119 | self.signal_indicator = self.I(self.params.get("indicator")) 120 | self.order_report_path = self.params.get("order_report_path") 121 | self.stop_trailing_amount = self.params.get("stop_trailing_amount") 122 | 123 | def next(self): 124 | super().next() 125 | close_price = self.data.Close[-1] 126 | stop_trail_short = close_price + (close_price * self.stop_trailing_amount / 100) 127 | for trade in self.trades: 128 | if not trade.is_long: 129 | trade.sl = min(trade.sl or np.inf, stop_trail_short) 130 | 131 | if self.signal_indicator == 2: 132 | trade.close() 133 | 134 | if self.signal_indicator == 1 and len(self.trades) == 0: 135 | self.sell(sl=stop_trail_short) 136 | 137 | def stop(self): 138 | self.save_trades(self.closed_trades) 139 | -------------------------------------------------------------------------------- /src/signal_backtester/strategies/two_sided.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from signal_backtester.base.base_strategy import BacktestingBaseStrategy 3 | 4 | 5 | class TwoSidedSlTpReverse(BacktestingBaseStrategy): 6 | """ 7 | 2 Side (long or short) With Stoploss & TakeProfit and reverse 8 | this strategy can open position on 2 side 9 | it set stop loss and take profit if hit will close the position 10 | if you gave opposit signal it will close your last position and open 11 | new position 12 | """ 13 | 14 | params = None 15 | 16 | def init(self): 17 | super(TwoSidedSlTpReverse, self).init() 18 | self.signal_indicator = self.I(self.params.get("indicator")) 19 | self.order_report_path = self.params.get("order_report_path") 20 | 21 | def next(self): 22 | super(TwoSidedSlTpReverse, self).next() 23 | close_price = self.data.Close[-1] 24 | for trade in self.trades: 25 | if trade.is_long: 26 | if self.signal_indicator == 1: 27 | trade.close() 28 | self.sell( 29 | sl=close_price 30 | + (self.params.get("stop_loss") * close_price / 100), 31 | tp=close_price 32 | - (self.params.get("take_profit") * close_price / 100), 33 | ) 34 | 35 | else: 36 | if self.signal_indicator == 2: 37 | trade.close() 38 | self.buy( 39 | sl=close_price 40 | - (self.params.get("stop_loss") * close_price / 100), 41 | tp=close_price 42 | + (self.params.get("take_profit") * close_price / 100), 43 | ) 44 | 45 | if self.signal_indicator == 2 and len(self.trades) == 0: 46 | self.buy( 47 | sl=close_price - (self.params.get("stop_loss") * close_price / 100), 48 | tp=close_price + (self.params.get("take_profit") * close_price / 100), 49 | ) 50 | 51 | elif self.signal_indicator == 1 and len(self.trades) == 0: 52 | self.sell( 53 | sl=close_price + (self.params.get("stop_loss") * close_price / 100), 54 | tp=close_price - (self.params.get("take_profit") * close_price / 100), 55 | ) 56 | 57 | def stop(self): 58 | self.save_trades(self.closed_trades) 59 | 60 | 61 | class TwoSidedSlTrailingReverse(BacktestingBaseStrategy): 62 | """ 63 | 2 Side (long or short) trail stop and reverse 64 | this strategy can open position on 2 side and it just have a trail stop 65 | if you gave opposit signal it will close your last position and open 66 | new position 67 | """ 68 | 69 | params = None 70 | 71 | def init(self): 72 | super(TwoSidedSlTrailingReverse, self).init() 73 | self.signal_indicator = self.I(self.params.get("indicator")) 74 | self.order_report_path = self.params.get("order_report_path") 75 | self.stop_trailing_amount = self.params.get("stop_trailing_amount") 76 | 77 | def next(self): 78 | super(TwoSidedSlTrailingReverse, self).next() 79 | close_price = self.data.Close[-1] 80 | stop_trail_long = close_price - (close_price * self.stop_trailing_amount / 100) 81 | stop_trail_short = close_price + (close_price * self.stop_trailing_amount / 100) 82 | 83 | for trade in self.trades: 84 | if trade.is_long: 85 | trade.sl = max(trade.sl or -np.inf, stop_trail_long) 86 | if self.signal_indicator == 1: 87 | trade.close() 88 | self.sell(sl=stop_trail_short) 89 | else: 90 | trade.sl = min(trade.sl or np.inf, stop_trail_short) 91 | if self.signal_indicator == 2: 92 | trade.close() 93 | self.buy(sl=stop_trail_long) 94 | 95 | if self.signal_indicator == 2 and len(self.trades) == 0: 96 | self.buy(sl=stop_trail_long) 97 | 98 | elif self.signal_indicator == 1 and len(self.trades) == 0: 99 | self.sell(sl=stop_trail_short) 100 | 101 | def stop(self): 102 | self.save_trades(self.closed_trades) 103 | --------------------------------------------------------------------------------