backtestify


Namebacktestify JSON
Version 0.1.6 PyPI version JSON
download
home_pagehttps://github.com/EladioRocha/backtestify
SummaryA package for backtesting trading strategies
upload_time2023-06-06 17:28:06
maintainer
docs_urlNone
authorEladio Rocha Vizcaino
requires_python
licenseApache License 2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # backtestify

## Table of Contents
- [backtestify](#backtestify)
  - [Table of Contents](#table-of-contents)
  - [Introduction](#introduction)
  - [Installation](#installation)
  - [Usage](#usage)
  - [Contributing](#contributing)
  - [License](#license)

## Introduction
While 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.

## Installation
pip install backtestify

## Usage
A simple example of how to use the package is shown below. More examples will be added in the future in the examples folder.

```python
import pandas as pd
from backtestify import Strategy, SignalEvent, SignalType, Backtester, Account, CFD, InstrumentType
import MetaTrader5 as mt5
import datetime
import talib as ta

def get_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.26

    return rates_frame

class RSIStrategy(Strategy):
    def __init__(self, market_info, rsi_period):
        self.rsi_period = rsi_period
        self.previous_trade_signal = None
        market_info["rsi"] = ta.RSI(market_info["close"], timeperiod=rsi_period)

        super().__init__(market_info)

    def on_tick(self, history):
        try:

            current = history.iloc[-1]

            if current["rsi"] is None:
                return

            if current["rsi"] < 30 and self.previous_trade_signal != SignalType.BUY:
                self.previous_trade_signal = SignalType.BUY
                return [
                    SignalEvent(signal=SignalType.EXIT),
                    SignalEvent(signal=SignalType.BUY)
                ]

            if current["rsi"] > 70 and self.previous_trade_signal != SignalType.SELL:
                self.previous_trade_signal = SignalType.SELL
                return [
                    SignalEvent(signal=SignalType.EXIT),
                    SignalEvent(signal=SignalType.SELL)
                ]

        except Exception as e:
            pass

cfd = 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.results
```

## Contributing

For any bug reports or recommendations, please visit our [issue tracker](https://github.com/EladioRocha/backtestify/issues) and 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!

## License
[Apache 2.0](https://choosealicense.com/licenses/apache-2.0/)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/EladioRocha/backtestify",
    "name": "backtestify",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Eladio Rocha Vizcaino",
    "author_email": "eladio.rocha99@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/26/ee/94be11364b46a6763d98fea8e54835df6ebe297644eb6ed563adc7f20510/backtestify-0.1.6.tar.gz",
    "platform": null,
    "description": "# backtestify\n\n## Table of Contents\n- [backtestify](#backtestify)\n  - [Table of Contents](#table-of-contents)\n  - [Introduction](#introduction)\n  - [Installation](#installation)\n  - [Usage](#usage)\n  - [Contributing](#contributing)\n  - [License](#license)\n\n## Introduction\nWhile 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.\n\nWhile 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.\n\n## Installation\npip install backtestify\n\n## Usage\nA simple example of how to use the package is shown below. More examples will be added in the future in the examples folder.\n\n```python\nimport pandas as pd\nfrom backtestify import Strategy, SignalEvent, SignalType, Backtester, Account, CFD, InstrumentType\nimport MetaTrader5 as mt5\nimport datetime\nimport talib as ta\n\ndef get_asset_info_mt5(symbol, timeframe=mt5.TIMEFRAME_D1, n=1000):\n    mt5.initialize()\n    utc_from = datetime.datetime.now()\n    rates = mt5.copy_rates_from(symbol, timeframe, utc_from, n)\n    rates_frame = pd.DataFrame(rates)\n\n    columns = ['time', 'open', 'high', 'low', 'close', 'tick_volume']\n    rates_frame = rates_frame[columns]\n\n    rates_frame = rates_frame.set_index('time')\n    rates_frame.rename(columns={'tick_volume': 'volume'}, inplace=True)\n    rates_frame.index.name = 'timestamp'\n\n    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.\n    rates_frame['swap_short'] = 4.26\n\n    return rates_frame\n\nclass RSIStrategy(Strategy):\n    def __init__(self, market_info, rsi_period):\n        self.rsi_period = rsi_period\n        self.previous_trade_signal = None\n        market_info[\"rsi\"] = ta.RSI(market_info[\"close\"], timeperiod=rsi_period)\n\n        super().__init__(market_info)\n\n    def on_tick(self, history):\n        try:\n\n            current = history.iloc[-1]\n\n            if current[\"rsi\"] is None:\n                return\n\n            if current[\"rsi\"] < 30 and self.previous_trade_signal != SignalType.BUY:\n                self.previous_trade_signal = SignalType.BUY\n                return [\n                    SignalEvent(signal=SignalType.EXIT),\n                    SignalEvent(signal=SignalType.BUY)\n                ]\n\n            if current[\"rsi\"] > 70 and self.previous_trade_signal != SignalType.SELL:\n                self.previous_trade_signal = SignalType.SELL\n                return [\n                    SignalEvent(signal=SignalType.EXIT),\n                    SignalEvent(signal=SignalType.SELL)\n                ]\n\n        except Exception as e:\n            pass\n\ncfd = CFD(\n    instrument_type=InstrumentType.STOCK,\n    lot_size=1,\n    entry_lots=1,\n    commission=0,\n    point_value=1,\n    leverage=100,\n    point=0.01,\n    spread=5,\n    period=mt5.TIMEFRAME_D1,\n    pips=0.01,\n)\ndf = get_asset_info_mt5(\"SPX500\")\n\nsma = RSIStrategy(df, 14)\naccount = Account(10000)\nbacktester = Backtester(sma, cfd, account)\nbacktester.run()\n\nbacktester.results\n```\n\n## Contributing\n\nFor any bug reports or recommendations, please visit our [issue tracker](https://github.com/EladioRocha/backtestify/issues) and create a new issue. If you're reporting a bug, it would be great if you can provide a minimal reproducible example.\n\nThank you for your contribution!\n\n## License\n[Apache 2.0](https://choosealicense.com/licenses/apache-2.0/)\n",
    "bugtrack_url": null,
    "license": "Apache License 2.0",
    "summary": "A package for backtesting trading strategies",
    "version": "0.1.6",
    "project_urls": {
        "Bug Tracker": "https://github.com/EladioRocha/backtestify/issues",
        "Homepage": "https://github.com/EladioRocha/backtestify"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1cb67f838acc26afd9c995429eb19580209f2e04d3e7e38df667c460e4e5ccdc",
                "md5": "3a396907477b23dcd8589d50bf0ecd4a",
                "sha256": "21a7bf58455944b471e0b2f1f90fdae7cdfdaa5c30268c756d367e2552bf9e6e"
            },
            "downloads": -1,
            "filename": "backtestify-0.1.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3a396907477b23dcd8589d50bf0ecd4a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 18484,
            "upload_time": "2023-06-06T17:28:01",
            "upload_time_iso_8601": "2023-06-06T17:28:01.203254Z",
            "url": "https://files.pythonhosted.org/packages/1c/b6/7f838acc26afd9c995429eb19580209f2e04d3e7e38df667c460e4e5ccdc/backtestify-0.1.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "26ee94be11364b46a6763d98fea8e54835df6ebe297644eb6ed563adc7f20510",
                "md5": "48a772604c34238d0bb7bdf4bd0c26dc",
                "sha256": "ddf855a7e8456c797de6a8ddd7bc63d9603f14a1138a1f490ac28f7dc6a3663f"
            },
            "downloads": -1,
            "filename": "backtestify-0.1.6.tar.gz",
            "has_sig": false,
            "md5_digest": "48a772604c34238d0bb7bdf4bd0c26dc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 16162,
            "upload_time": "2023-06-06T17:28:06",
            "upload_time_iso_8601": "2023-06-06T17:28:06.710991Z",
            "url": "https://files.pythonhosted.org/packages/26/ee/94be11364b46a6763d98fea8e54835df6ebe297644eb6ed563adc7f20510/backtestify-0.1.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-06 17:28:06",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "EladioRocha",
    "github_project": "backtestify",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "backtestify"
}
        
Elapsed time: 0.07419s