{"id":18567114,"url":"https://github.com/diovisgood/intraday","last_synced_at":"2025-04-10T05:32:39.196Z","repository":{"id":57756603,"uuid":"418995223","full_name":"diovisgood/intraday","owner":"diovisgood","description":"Gym environment which simulates intraday trading","archived":false,"fork":false,"pushed_at":"2022-02-09T08:36:50.000Z","size":1963,"stargazers_count":27,"open_issues_count":0,"forks_count":12,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-24T16:46:47.349Z","etag":null,"topics":["candle","candlestick","candlestick-chart","environment","gym","gym-environment","intraday","reinforcement-learning","reinforcement-learning-algorithms","stream","trades","trading","trading-bot","trading-strategies"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/diovisgood.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-19T16:00:09.000Z","updated_at":"2024-08-12T20:17:22.000Z","dependencies_parsed_at":"2022-08-23T19:11:01.021Z","dependency_job_id":null,"html_url":"https://github.com/diovisgood/intraday","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diovisgood%2Fintraday","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diovisgood%2Fintraday/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diovisgood%2Fintraday/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diovisgood%2Fintraday/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/diovisgood","download_url":"https://codeload.github.com/diovisgood/intraday/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248163252,"owners_count":21057894,"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":["candle","candlestick","candlestick-chart","environment","gym","gym-environment","intraday","reinforcement-learning","reinforcement-learning-algorithms","stream","trades","trading","trading-bot","trading-strategies"],"created_at":"2024-11-06T22:25:29.298Z","updated_at":"2025-04-10T05:32:36.335Z","avatar_url":"https://github.com/diovisgood.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ca rel=\"license\" href=\"http://creativecommons.org/licenses/by-nc-sa/4.0/\"\u003e\n\u003cimg alt=\"Creative Commons License\" style=\"border-width:0\" src=\"https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png\" /\u003e\u003c/a\u003e\n\u003cbr /\u003e\n\u003cspan xmlns:dct=\"http://purl.org/dc/terms/\" property=\"dct:title\"\u003e\nIntraday Exchange Gym Environment\u003c/span\u003e by\n\u003ca xmlns:cc=\"http://creativecommons.org/ns#\" href=\"https://github.com/diovisgood/intraday\" property=\"cc:attributionName\" rel=\"cc:attributionURL\"\u003ePavel B. Chernov\u003c/a\u003e\nis licensed under a \u003ca rel=\"license\" href=\"http://creativecommons.org/licenses/by-nc-sa/4.0/\"\u003e\nCreative Commons Attribution-NonCommercial-ShareAlike 4.0 International License\u003c/a\u003e\n\n# Intraday\n\nThis package provides gym compatible environment to **simulate intraday trading**\nbased on stream of trades, either historical or real-time.\n\n![gif animation of trained agent](doc/render_ethusdt_trained.gif)\n\nThis project was inspired by [TensorTrade](https://github.com/tensortrade-org/tensortrade),\nbut it was written from scratch, and it is a completely original source code.\n\nThe main idea was to go deeper from candles to the actual stream of trades.\nBecause candles lose a lot of information, for example:\n\n- How many trades during a period were initiated by buyers or by sellers?\n- At what price did most of the trades happen during a period?\n- At what price there were almost no activity?\n- Were there many trades with small amounts or less trades with big amounts?\n- etc.\n\n## Installation\n\n1. Download package\n2. Cd into directory\n3. Install package\n\n```bash\ngit clone https://github.com/diovisgood/intraday.git\ncd intraday\npython setup.py install\n```\n\n### Quick start\n\nHere is a simple script to run:\n\n```python\nfrom datetime import date, timedelta\nfrom intraday.providers import BinanceArchiveProvider\nfrom intraday.processor import IntervalProcessor\nfrom intraday.features import EMA, Copy, PriceEncoder\nfrom intraday.actions import BuySellCloseAction\nfrom intraday.rewards import BalanceReward\nfrom intraday.env import SingleAgentEnv\n\nprovider = BinanceArchiveProvider(data_dir='.', symbol='ETHUSDT',\n                                  date_from=date(2018, 5, 1), date_to=date(2018, 5, 31))\nprocessor = IntervalProcessor(method='time', interval=5*60)\nperiod = 1000\natr_name = f'ema_{period}_true_range'\nfeatures_pipeline = [\n    PriceEncoder(source='close', write_to='both'),\n    EMA(period=period, source='true_range', write_to='frame'),\n    Copy(source=['volume'])\n]\naction_scheme = BuySellCloseAction()\nreward_scheme = BalanceReward(norm_factor=atr_name)\nenv = SingleAgentEnv(\n    provider=provider,\n    processor=processor,\n    features_pipeline=features_pipeline,\n    action_scheme=action_scheme,\n    reward_scheme=reward_scheme,\n    initial_balance=1000000,\n    warm_up_time=timedelta(hours=1)\n)\n\nstate = env.reset()\nwhile True:\n    env.render('human')\n    print(state)\n    action = action_scheme.get_random_action()\n    state, reward, done, frame = env.step(action)\n    if done:\n        break\n        \nenv.close()\n```\n\nYou should see something like this:\n\n![gif animation of chart window](doc/render_ethusdt_untrained.gif)\n\n### Screen explained\n\nHere is some explaination of different values on this screen:\n\n![screenshot with notes](doc/render_01.png)\n\nNote that in the code above agent performed random actions,\nthat is why balance chart is constantly decreasing.\n\nBy the way, notice different colors on the balance line?\nEach color describes a position agent was at that moment:\n\n- Green: agent was in long position.\n- Red: agent was in short position.\n- Black: agent was not in position.\n\n## Advantages\n\nThis project has several advantages over other trading environments:\n\n1. Simulates order *delays*, like in the real life.\n2. Takes into account bid-ask *spread*, when executing market orders.\n3. Some limit orders are not executed even if price *touches* them, like in the real life. (Read below for explanation).\n4. Supports *multiple agents* running the same episode.\n5. Supports *idle penalty* to stimulate agents to perform actions and not just hold the initial balance.\n6. Liquidates agent account when it goes *bankrupt* (like in the real life).\n7. Supports adjustable *commissions* either a constant value or a callback function.\n\nIt provides you with:\n\n- Gym compatible environment.\n- Real exchange simulation under the hood.\n- Different methods to split stream of trades into frames:\n  - by time interval (as usual)\n  - by traded volume\n  - by number of trades (a.k.a. ticks)\n- Different actions schemes, including the most popular one: `{Buy, Sell, Close}`.\n- Support for many popular order types (it means you can easily write any desired action scheme):\n  - MarketOrder\n  - LimitOrder\n  - StopOrder\n  - TrailingStopOrder\n  - TakeProfitOrder\n- Support for different reward schemes.\n  The most obvious one is: `BalanceReward`. Also there is: `ConstantReward`.\n- Ability to train agent on different assets simultaneously.\n  In this case the reward agent receives when trading different assets highly depends on their price.\n  For example: `BTCUSD` price can move $4000 per day, while for `ETHUSD` it has range of about $100 per day.\n  Hence the rewards based on balance will differ more than 10 times,\n  making rewards from `BTCUSD` more valuable than those of `ETHUSD`, which is wrong!\n  To solve this problem you can automatically divide balance reward with computed\n  [**ATR**](https://en.wikipedia.org/wiki/Average_true_range) value.\n  Thus making rewards from different assets to be in the same range.\n  So it would be possible to train agent on different assets with different prices simultaneously.\n- Support for many popular features and indicators:\n  - ADL\n  - CMF\n  - Cumulative Sum\n  - Efficiency Ratio\n  - EMA\n  - EOM\n  - Fractals\n  - Fractal Dimensions\n  - Heiken Ashi\n  - KAMA\n  - OBV\n  - Parabolic SAR\n  - etc.\n- Two Binance providers: `BinanceArchiveProvider` and `BinanceKlines`.\n- Trades simulation, if for some reason you only have candles.\n- Enhanced evaluation of agent trading performance:\n  - roi\n  - sharpe ratio\n  - profit factor\n  - sortino ratio\n  - etc.\n\n## Details\n\nThis project tries to simulate trading environment as realistically as possible.\nA lot of trained models seem to perform good during training, but often fail to show\nany positive result in real trading. Mainly because they ignore following issues.\n\n### Order delays from agent to exchange\n\nThere is always a delay between the moment agent makes its decision and the\nmoment order reaches an exchange, due to network or exchange delays.\nTypically, about 1..3 seconds.\nThis package allows you to specify it explicitly in `agent_order_delay` argument.\n\n### Order delays from broker to exchange or inside exchange\n\nFor some kind of orders like: `TakeProfitOrder` or `StopOrder` there is a small internal delay\nbetween the moment broker/exchange server decides to execute this order by placing the `MarketOrder`\nand the moment when `MarketOrder` reaches an exchange. Typically, about 0.5...1 seconds.\nThis package allows you to specify it explicitly in `broker_order_delay` argument.\n\n### Realistic bid-ask spread\n\nWhen agent wants to buy or sell immediately it issues a `MarketOrder`.\nThis order should be executed by the best price which is available there in the order book.\nBut there is always a gap between best bid and best ask price, which is often ignored.\n\nImagine a situation: the last trade was a `sell at a price 103.5`,\nand agent decides it should buy immediately.\nBut it can't buy at this price, since it was a sell trade, which was executed at the **best bid price**.\nWhile buy order will be executed at **best ask price**.\nThe best ask price is typically higher than best bid price, say, for example: 104.0 (thus the spread is: 0.5)\n\nWe don't have order book since this is a simulated Exchange environment.\nBut this package analyzes the stream of buy and sell trades and estimates the mean and std\nvalues for the bid-ask spread. It then uses the upper estimation of spread to choose\nthe realistic price for `MarketOrder` to be executed.\n\n### Limit orders realistic execution\n\nAnother issue is with limit orders. Sometimes limit orders are not executed even if price *touches* them.\nSimply because they were last in the order book and there were not enough corresponding market buy(sell)\norders to fulfill them.\nThis package lets you specify explicitly the probability for limit order to be executed in such scenario,\nvia `order_luck` argument.\n\n### Support for multiple agents\n\nUnlike most gym environments this package is able to run multiple agents on the same episode.\nIt does this by allocating different virtual broker's accounts for each agent.\nSo each agent has its own `initial_balance` and can perform its own trades.\nThis can be useful for some optimization algorithms like: Evolutions Strategies, CMA-ES, etc.\n\n### Support for idle penalty\n\nWhen trained in complicated stochastic environments agents often tend to **do nothing**.\nThis is a simple way to save its life or money balance.\nA simple solution to the problem is to introduce some penalty for agent for being idle.\n\nThus, its balance will slightly decrease on each step even if it did not open long or short position.\nThis decrease is equal to the price range of the current frame multiplied by `idle_penalty` parameter.\n\n### Liquidation of agent account when it goes bankrupt\n\nIn reality, in most cases, exchange will block your account if your balance becomes negative.\nThis means you can no longer buy or sell assets.\n\nMaybe this is not very useful for the aim of agent training.\nI suggest a better approach would be to let it make mistakes at the beginning of learning.\nAnd after some time, as it matures, bring some real constraints.\n\nThis could easily be achieved by specifying very large `initial_balance` at the start of training.\nAnd then reduce it to some realistic value over time.\n\n### Adjustable commissions\n\nMany researchers successfully train their models to trade different assets without commission.\nThen, of course, these models fail in the real world.\nBecause if there are no commissions in training environments agents may learn to perform a lot of trades.\nWhich will lead to a great losses in the real world.\n\nThis package allows you to specify either a fixed commission, or a callback function.\nFor example:\n\n```python\ndef binance_commission(operation: str, amount: float, price: float) -\u003e float:\n    return 0.0004 * abs(amount) * price\n```\n\nNote: operation in a string, one of: `{'B', 'S'}`\n\n### Support for any kind of data providers\n\n`BinanceArchiveProvider` - automatically downloads monthly trades archives from [binance.com](binance.com)\nand converts them into `.feather` file format for faster loading.\nAll you need to do is to specify symbol name, for example: 'BTCUSDT', and dates range.\n\n`BinanceKLines` - automatically downloads monthly candles archives from [binance.com](binance.com)\nand also converts them into `.feather` file format for faster loading.\nKLine is a binance candle with some additional fields.\nIf you want to investigate large time intervals: say 30 minutes or 1 hour - processing trades archives\nbecomes **too slow**. In this case you may want to use 1 minute klines as your data source.\nNote, that in this case you should not rely on features which analyze trades,\nbecause trades are *simulated* in case of klines or candles.\n\n`MoexArchiveProvider` - provider which works with raw stream of trades from Moscow Exchange\nsaved in a special binary format: `*.trades.gz`.\nIn this format each file represents one trading session (one day).\nJust ignore it if you don't access to these archives.\n\n`SineProvider` - Sine wave generator with adjustable noise.\nIf you want to train a trading bot, this sine generator\nwould be a good test for it.\nIf your algorithm fails to learn to make profit even on such simple data,\nit will never find any profit in a real market data.\n\n\n## TODO\n\n1. Not all `features/*.py` have corresponding unit-tests. Ideally, each feature should have good unit-testing.\n2. It would be nice to have support for other major exchanges like Coinbase.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiovisgood%2Fintraday","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdiovisgood%2Fintraday","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiovisgood%2Fintraday/lists"}