|
| 1 | +--- |
| 2 | + |
| 3 | +sidebar_position: 1 |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +# Market data sources |
| 8 | +Algorithmic trading needs quick access to real-time data and effective data manipulation for successful analysis. |
| 9 | +To meet these needs, the framework provides a data object that can be used in your trading strategies. |
| 10 | + |
| 11 | +For data availability, we use a push-based approach. This means we send the desired information directly as |
| 12 | +an argument to each trading strategy handler function or trading strategy class. |
| 13 | +It's easy to use – just annotate your handler with the information you need. |
| 14 | + |
| 15 | +Here is an example of a handler that uses the `TICKER` data object: |
| 16 | + |
| 17 | +```python |
| 18 | +# A ticker market data source for the BTC/EUR symbol on the bitvavo exchange |
| 19 | +bitvavo_ticker_btc_eur = CCXTTickerMarketDataSource( |
| 20 | + identifier="BTC-ticker", |
| 21 | + market="BITVAVO", |
| 22 | + symbol="BTC/EUR", |
| 23 | +) |
| 24 | + |
| 25 | +class MyTradingStrategy(TradingStrategy): |
| 26 | + time_unit = TimeUnit.SECOND |
| 27 | + interval = 5 |
| 28 | + market_data_sources = ["BTC-ticker"] # Registering the market data source by using its identifier |
| 29 | + |
| 30 | + def apply_strategy(self, algorithm: Algorithm, data: dict): |
| 31 | + print(data) |
| 32 | + |
| 33 | +# Make sure to register your market data sources with the app |
| 34 | +app.add_trading_strategy(MyTradingStrategy) |
| 35 | +app.add_market_data_source(bitvavo_ticker_btc_eur) |
| 36 | +``` |
| 37 | + |
| 38 | +By doing so your handler function parameter data will be assigned a data Object containing ticker for BTC/EUR from |
| 39 | +the bitvavo exchange under the key "BTC-ticker". |
| 40 | + |
| 41 | +## Accessing data |
| 42 | +You can easily access the data object by using the `identifier` attribute of your MarketDataSource object. |
| 43 | +The following code snippet shows how to access the data object: |
| 44 | + |
| 45 | +:::tip |
| 46 | +The data object that is passed in your trading strategy is a dictionary. This allows you to access multiple data objects |
| 47 | +in your trading strategy. The key of the dictionary is the identifier of the market data source. |
| 48 | +::: |
| 49 | + |
| 50 | +```python |
| 51 | +# A ticker market data source for the BTC/EUR symbol on the bitvavo exchange |
| 52 | +bitvavo_ticker_btc_eur = CCXTTickerMarketDataSource( |
| 53 | + identifier="BTC-ticker", |
| 54 | + market="BITVAVO", |
| 55 | + symbol="BTC/EUR", |
| 56 | +) |
| 57 | + |
| 58 | +class MyTradingStrategy(TradingStrategy): |
| 59 | + time_unit = TimeUnit.SECOND |
| 60 | + interval = 5 |
| 61 | + market_data_sources = ["BTC-ticker"] # Registering the market data source by using its identifier |
| 62 | + |
| 63 | + def apply_strategy(self, algorithm: Algorithm, data): |
| 64 | + ticker_data = data["BTC-ticker"] # Accessing the data object directly by using the identifier |
| 65 | + |
| 66 | +# Make sure to register your market data sources with the app |
| 67 | +app.add_trading_strategy(MyTradingStrategy) |
| 68 | +app.add_market_data_source(bitvavo_ticker_btc_eur) |
| 69 | +``` |
| 70 | + |
| 71 | + |
| 72 | +## CCXT market data sources |
| 73 | +The framework comes out of the box with support for the [ccxt](https://github.com/ccxt/ccxt). |
| 74 | +This allows you the use the following ccxt market data sources: |
| 75 | + |
| 76 | +- CCXTTickerMarketDataSource |
| 77 | +- CCXTOHLCVMarketDataSource |
| 78 | +- CCXTOrderBookMarketDataSource |
| 79 | + |
| 80 | +### CCXTTickerMarketDataSource |
| 81 | +The CCXTTickerMarketDataSource is used to get the latest ticker data for a symbol. It is based |
| 82 | +on the popular [ccxt](https://github.com/ccxt/ccxt) library. |
| 83 | + |
| 84 | +```python |
| 85 | +from investing_algorithm_framework import CCXTTickerMarketDataSource, TradingStrategy, \ |
| 86 | + Algorithm, TimeUnit |
| 87 | + |
| 88 | +# A ohlcv market data source for the BTC/EUR symbol on the BITVAVO exchange |
| 89 | +bitvavo_ticker_btc_eur = CCXTTickerMarketDataSource( |
| 90 | + identifier="BTC-ticker", |
| 91 | + market="BITVAVO", |
| 92 | + symbol="BTC/EUR", |
| 93 | +) |
| 94 | + |
| 95 | +class MyTradingStrategy(TradingStrategy): |
| 96 | + time_unit = TimeUnit.SECOND # The time unit of the strategy |
| 97 | + interval = 5 # The interval of the strategy, runs every 5 seconds |
| 98 | + # Registering the market data source |
| 99 | + market_data_sources = [bitvavo_ticker_btc_eur] |
| 100 | + |
| 101 | + def apply_strategy(self, algorithm: Algorithm, market_data: Dict[str, Any]): |
| 102 | + print(market_data[bitvavo_ticker_btc_eur.get_identifier()]) |
| 103 | +``` |
| 104 | + |
| 105 | +### CCXTOHLCVMarketDataSource |
| 106 | +The CCXTOHLCVMarketDataSource is used to get candle stick/OHLCV data for a symbol. It is based |
| 107 | +on the popular [ccxt](https://github.com/ccxt/ccxt) library. |
| 108 | + |
| 109 | +:::info |
| 110 | +For ohlcv data you need to specify a start date, and/or an end date. |
| 111 | +If you don't specify an end date, the framework will use the current date as the end date. The daterange between |
| 112 | +the start and end date is used to determine the number of candlesticks in your ohlcv data. E.g. if you |
| 113 | +specify a start date of `start_date=datetime.utcnow() - timedelta(days=17)` and a timeframe of 2h, the framework will |
| 114 | +fetch 216 candlesticks (17 days * 12 candlesticks per day). Keep in mind that by leveraging a function like `datetime.utcnow()` |
| 115 | +you will get the current date in UTC time everytime the market data source is used. This allows you to get the latest data |
| 116 | +everytime the strategy runs. |
| 117 | +::: |
| 118 | + |
| 119 | +```python |
| 120 | +from investing_algorithm_framework import CCXTOHLCVMarketDataSource, TradingStrategy, \ |
| 121 | + Algorithm, TimeUnit |
| 122 | + |
| 123 | +# A order book market data source for the BTC/EUR symbol on the BITVAVO exchange |
| 124 | +bitvavo_btc_eur_ohlcv_2h = CCXTTickerMarketDataSource( |
| 125 | + identifier="BTC-ohlcv-2h", |
| 126 | + market="BITVAVO", |
| 127 | + symbol="BTC/EUR", |
| 128 | +) |
| 129 | + |
| 130 | +class MyTradingStrategy(TradingStrategy): |
| 131 | + time_unit = TimeUnit.SECOND # The time unit of the strategy |
| 132 | + interval = 5 # The interval of the strategy, runs every 5 seconds |
| 133 | + # Registering the market data source |
| 134 | + market_data_sources = [bitvavo_btc_eur_ohlcv_2h] |
| 135 | + |
| 136 | + def apply_strategy(self, algorithm: Algorithm, market_data: Dict[str, Any]): |
| 137 | + print(market_data[bitvavo_btc_eur_ohlcv_2h.get_identifier()]) |
| 138 | +``` |
| 139 | + |
| 140 | +### CCXTOrderBookMarketDataSource |
| 141 | +The CCXTOrderBookMarketDataSource is used to get order book data for a symbol. It is based |
| 142 | +on the popular [ccxt](https://github.com/ccxt/ccxt) library. |
| 143 | + |
| 144 | + |
| 145 | +```python |
| 146 | +from investing_algorithm_framework import CCXTOrderBookMarketDataSource, TradingStrategy, \ |
| 147 | + Algorithm, TimeUnit |
| 148 | + |
| 149 | +# A ticker market data source for the BTC/EUR symbol on the BITVAVO exchange |
| 150 | +bitvavo_btc_eur_order_book = CCXTOrderBookMarketDataSource( |
| 151 | + identifier="BTC-order-book", |
| 152 | + market="BITVAVO", |
| 153 | + symbol="BTC/EUR", |
| 154 | +) |
| 155 | + |
| 156 | +class MyTradingStrategy(TradingStrategy): |
| 157 | + time_unit = TimeUnit.SECOND # The time unit of the strategy |
| 158 | + interval = 5 # The interval of the strategy, runs every 5 seconds |
| 159 | + # Registering the market data source |
| 160 | + market_data_sources = [bitvavo_btc_eur_order_book] |
| 161 | + |
| 162 | + def apply_strategy(self, algorithm: Algorithm, market_data: Dict[str, Any]): |
| 163 | + print(market_data[bitvavo_btc_eur_order_book.get_identifier()]) |
| 164 | +``` |
| 165 | + |
0 commit comments