{"id":15032065,"url":"https://github.com/ivopetiz/algotrading","last_synced_at":"2025-04-08T12:09:34.271Z","repository":{"id":44328304,"uuid":"140492255","full_name":"ivopetiz/algotrading","owner":"ivopetiz","description":"Algorithmic trading framework for cryptocurrencies.","archived":false,"fork":false,"pushed_at":"2024-07-06T00:34:03.000Z","size":1005,"stargazers_count":1241,"open_issues_count":8,"forks_count":187,"subscribers_count":33,"default_branch":"master","last_synced_at":"2025-04-01T11:04:09.332Z","etag":null,"topics":["algorithmic-trading","algotrading","backtest","backtesting-trading-strategies","bot","crypto","crypto-algotrading","cryptocurrencies","cryptocurrency","cryptocurrency-exchanges","framework","hft","hft-trading","high-frequency-trading","realtime","trading","trading-algorithms","trading-api","trading-bot","trading-simulator"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ivopetiz.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-07-10T22:10:41.000Z","updated_at":"2025-03-31T05:54:49.000Z","dependencies_parsed_at":"2024-04-13T11:34:04.257Z","dependency_job_id":"36a5a8fb-a30a-4fb5-88c7-3c5331fee6f5","html_url":"https://github.com/ivopetiz/algotrading","commit_stats":{"total_commits":192,"total_committers":5,"mean_commits":38.4,"dds":"0.23958333333333337","last_synced_commit":"73ce60420bd1266c61ab4d25ae260100bd20e0c7"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivopetiz%2Falgotrading","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivopetiz%2Falgotrading/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivopetiz%2Falgotrading/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivopetiz%2Falgotrading/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ivopetiz","download_url":"https://codeload.github.com/ivopetiz/algotrading/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247838444,"owners_count":21004580,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithmic-trading","algotrading","backtest","backtesting-trading-strategies","bot","crypto","crypto-algotrading","cryptocurrencies","cryptocurrency","cryptocurrency-exchanges","framework","hft","hft-trading","high-frequency-trading","realtime","trading","trading-algorithms","trading-api","trading-bot","trading-simulator"],"created_at":"2024-09-24T20:17:14.740Z","updated_at":"2025-04-08T12:09:34.246Z","avatar_url":"https://github.com/ivopetiz.png","language":"Python","funding_links":[],"categories":["By Industry"],"sub_categories":["Blockchain"],"readme":"# Crypto AlgoTrading Framework\n\n[![Codacy Badge](https://app.codacy.com/project/badge/Grade/dce868084d344fafa498bf3ff7bf2d81)](https://www.codacy.com/gh/ivopetiz/algotrading/dashboard?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=ivopetiz/algotrading\u0026amp;utm_campaign=Badge_Grade)\n[![Codacy Security Scan](https://github.com/ivopetiz/algotrading/actions/workflows/codacy.yml/badge.svg)](https://github.com/ivopetiz/algotrading/actions/workflows/codacy.yml)\n[![Build Status](https://travis-ci.com/ivopetiz/algotrading.svg?branch=master)](https://travis-ci.com/ivopetiz/algotrading)\n[![Coverage Status](https://coveralls.io/repos/github/ivopetiz/algotrading/badge.svg?branch=master)](https://coveralls.io/github/ivopetiz/algotrading?branch=master)\n\n## Algorithmic trading framework for cryptocurrencies in Python\n\nAlgotrading Framework is a repository with tools to build and run working trading bots, backtest strategies, assist on trading, define simple stop losses and trailing stop losses, etc. This framework work with data directly from Crypto exchanges API, from a DB or CSV files. Can be used for data-driven and event-driven systems. Made exclusively for crypto markets for now and written in Python.\n\n---\n\n## Follow this [Quickstart Guide](quickstart_guide.md) if you want to start right away\n\n---\n\n[A Medium story dedicated to this project](https://ivopetiz.medium.com/my-algorithmic-trading-project-f5d2eb2dbb5a)\n\n---\n\n## Index\n\n* [Operating modes](#operating-modes)\n  * [Realtime](#realtime)\n  * [Tick-by-tick](#tick-by-tick)\n  * [Backtest](#backtest)\n\n* [How to start](#how-to-start)\n  * [Pre Requesites](#pre-requesites)\n\n  * [Collecting data](#collecting-data)\n    * [Database](#batabase)\n    * [Script](#script)\n\n* [Entry functions](#entry-functions)\n  * [cross SMA](#cross-sma)\n\n* [Exit functions](#exit-functions)\n  * [cross SMA](#cross-sma)\n\n* [Plot data](#plot-data)\n\n* [Logs](#logs)\n\n* [Examples](#examples)\n  * [Realtime alert for volume increasing](#realtime-alert-for-volume-increasing)\n  * [Backtest a strategy on BTC-DGB pair using SMA](#backtest-a-strategy-on-BTC-DGB-pair-using-sma)\n\n* [Pypy](#pypy)\n\n* [TODO](#todo)\n\n* [Additional info](#additional-info)\n\n---\n\n## Operating modes\n\nFramework has three operating modes:\n\n* Realtime -- Trades with real data in real time, with real money or in simulation mode.\n* Tick-by-tick -- Testing strategies in real time frames, so user can follow its entries and exits strategies.\n* Backtest -- Backtesting strategies and presenting the results.\n\n### Realtime\n\nIn realtime, Trading Bot operates in real-time, with live data from exchanges APIs. It doesn't need pre-stored data or DB to work. In this mode, a bot can trade real money, simulate or alert the user when its time to buy or sell, based on entry and exit strategies defined by the user. Can also simulate users strategies and present the results in real-time.\n\n### Tick-by-tick\n\nTick-by-tick mode allows users to check strategies in a visible timeframe, to better check entries and exit points or to detect strategies faults or new entry and exit points. Use data from CSV files or DB.\n\n### Backtest\n\nAllows users to backtest strategies, with previously stored data. Can also plot trading data showing entry and exit points for implemented strategies.\n\n## How to start\n\n### Pre requisites\n\nTo get algotrading fully working is necessary to install some packages and Python libs, as IPython, Pandas, Matplotlib, Numpy, Python-Influxdb and Python-tk. \nOn Linux machines these packages could be installed with:\n\n```bash\npip install -r requirements.txt\n```\n\n### Collecting data\n\nThe first step is to collect data. To get markets data is necessary to run a DB, to get and manage all data or save the data to CSV files. \nThere are two options:\n\n* Install and configure a database.\n* Run a script to collect data and save it to CSV files.\n\n#### Database\n\nTrading Bot is ready to operate with InfluxDB, but can work with other databases, with some small changes.\n\nTo install, configure and use a InfluxDB database, you can clone this repository:\nhttps://github.com/ivopetiz/crypto-database\n\n#### Script\n\nIf you don't want to install and manage any databases and simply want to get data to CSV files you can use the script in this Gist:\nhttps://gist.github.com/ivopetiz/051eb8dcef769e655254df21a093831a\n\n*Using a database is the best option, once you can analyse and plot data using DB tools, as Chronograf, and can always extract data to CSV if needed.*\n\n## Entry functions\n\nEntry functions aggregate all strategies to enter in a specific market. Once data fill all the requisites to enter a specific market, an action is taken. \nUsers can use one or several functions in the same call, to fill the requisites and enter market/markets. \nFunctions should return *True*, if the available data represent an entry point for the user. If not, the return needs to be *False*.\n\u003centry.py\u003e should aggregate all users entry functions.\n\n### Example\n\n#### cross SMA\n\nFunction \u003ccross_smas\u003e will return *True* if first SMA cross the second one. If not will return *False*.\n\n```python\ndef cross_smas(data, smas=[5, 10]):\n    '''\n    Checks if it's an entry point based on crossed smas.\n    '''\n    if data.Last.rolling(smas[0]).mean().iloc[-1] \u003e \\\n       data.Last.rolling(smas[1]).mean().iloc[-1] and \\\n       data.Last.rolling(smas[0]).mean().iloc[-2] \u003c \\\n       data.Last.rolling(smas[1]).mean().iloc[-2]:\n        return True\n\n    return False\n```\n\n## Exit functions\n\nExit functions have all functions responsible for exit strategies. When a user is in the market, and data met exit criteria, the bot will exit the market. \nExit functions can be used with other exit functions, to cover more situations, as used in entry functions. \nStop loss and trailing stop loss are also implemented, to exit markets in case of an unexpected price drop. Functions should return *True*, if the available data represent an exit point for the user. If not, the return needs to be *False*.\n\u003cexit.py\u003e should aggregate all users' exit functions.\n\nExample\n---\n\n### cross SMA\n\nFunction \u003ccross_smas\u003e will return *True* if first SMA cross the second one. If not will return *False*.\n\n```python\ndef cross_smas(data, smas=[10, 20]):\n    '''\n    Checks if it's an exit point based on crossed smas.\n    '''\n    if data.Last.rolling(smas[0]).mean().iloc[-1] \u003c \\\n       data.Last.rolling(smas[1]).mean().iloc[-1] and \\\n       data.Last.rolling(smas[0]).mean().iloc[-2] \u003e \\\n       data.Last.rolling(smas[1]).mean().iloc[-2]:\n        return True\n\n    return False\n```\n\n## Plot data\n\nIt's possible to plot entry and exit points, among market data, using Matplotlib lib for Python with the option *plot=True* on function call.\n\n## Log\n\nCan log entry and exit points in order to evaluate strategies, presenting P\u0026L for specific markets and total.\n\n## Examples\n\nHere are some examples of how to use this framework.\n\n### Realtime alert for volume increasing\n\nTo get an alert when a market doubles its volume:\n\n```python\nfrom cryptoalgotrading.cryptoalgotrading import realtime\n\ndef alert_volume_x2(data):\n    if pd.vol.iloc[-1] \u003e pd.vol.iloc[-2]*2:\n        return True\n    return False\n\nrealtime([], alert_volume_x2, interval='10m')\n```\n\n*alert_volume_x2* checks the value of actual market volume and compare it with the last time frame volume value, alerting user when actual market volume is bigger than last time frame volume value multiplied by 2. Can add functions live on IPython for example of add them to entry and exit python files.\n\n### Backtest a strategy on BTC-DGB pair using SMA\n\nTo backtest a cross simple moving average strategy in a specific market and plot the entry points:\n\n```python\nfrom cryptoalgotrading.cryptoalgotrading import backtest\nimport cryptoalgotrading.entry as entry\n\nbacktest([\"BTC-XRP\"], entry.cross_smas, smas=[15,40], interval='10m', from_file=True, plot=True)\n```\n\nBased on market data available for BTC_XRP pair, code above can present an output like this:\n\n![testing strategy on BTC-XRP pair data.](figs/fig2_xrp.png)\n\nThe figure has three charts. The chart on top presents on top BTC-XRP data from a certain period, with its Bollinger bands and 3 SMA lines. Green points represent the entry points for the defined strategy. In the middle is a chart representing volume data and at the bottom is represented the number of selling orders among time. All these fields and charts are configurable on *plot* function.\n\nCan also add exit points by adding an exit function or functions to backtest function. \nIt is possible to enter multiple entries and exit functions to backtest, to define different entry and exit positions.\n\nBoth functions are available on entry.py and exit.py as example.\n\nIn finance.py are some functions which could be useful to implement some strategies.\n\n## Pypy\n\nThis Crypto AlgoTrading Framework can be used with Pypy, but the results will not be great, during the use of Pandas and Numpy libs.\n\n## TODO\n\n* [x] Python 3 implementation\n* [ ] Cython version\n* [ ] Pure Python for Pypy (without Pandas and Numpy dependency)\n\n## Additional Info\n\nAPI Key is just needed in case of buy/sell operations. For backtest, tick-by-tick and realtime alert implementations API Key can be left empty.\n\nBuy and sell options are commented and should only be used if you know what you are doing.\n\nIf you are interested in using this bot and don't have an account on Binance Exchange yet, please help me, creating  an account through my referral code here: https://accounts.binance.com/en/register?ref=17181609 \n\n---\n\n\u003cp align=\"center\"\u003eUSE THIS AT YOUR OWN RISK.\u003c/p\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivopetiz%2Falgotrading","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fivopetiz%2Falgotrading","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivopetiz%2Falgotrading/lists"}