blankly-finance / blankly

🚀 💸 Easily build, backtest and deploy your algo in just a few lines of code. Trade stocks, cryptos, and forex across exchanges w/ one package.
https://package.blankly.finance
GNU Lesser General Public License v3.0
2.09k stars 265 forks source link

KeylessExchange does not provide OHLCV data on bar event #136

Closed lomagno2003 closed 2 years ago

lomagno2003 commented 2 years ago

Description of Feature Request

First of all, congratz on your work on Blankly. I've also used Freqtrade and Backtrader and even though they provide other capabilities that you don't, you rock on making this framework simple to use.

I'm developing a strategy using Artificial Neural Networks trained using Genetic Algorithms. In here, I need to run multiple back tests in order to calculate the fitness of each individual. For this, I'm using Blankly backtest where I need to initialize an Exchange. I was using Alpaca but during initialization and execution, it performs some HTTPS calls that slows down the training (even if they are just a few). I switched to KeyLess exchange but I noticed that it mocks the OHCLV data (reference).

Would be nice to have this Exchange (or maybe another exchange) to provide all the OHCLV data. Maybe one exchange that requires a directory with multiple CSV files, one per Symbol (maybe similar to the ones generated by the backtests on the price_cache folder).

Why is it important?

Maybe other people needs this feature to perform offline trainings and backtests using the full OHCLV data. Just as a silly example, I want to code while I'm flying without internet connectivity and be able to tests my strategy which uses volume indicators.

How would you use the feature?

I would like to provide a folder that will contains all the CSV files that can be consumed by this Exchange. For instance:

if __name__ == "__main__":
    exchange = blankly.CSVExchange(folder='price_caches', initial_account_values={'USD': 100000})
    strategy = blankly.Strategy(exchange)
    strategy.add_bar_event(self.bar_event, 'AAPL', resolution='1d', init=self.init)

    strategy.backtest(start_date="20/10/2021", end_date="01/03/2022")

In here, the folder might have the following files:

How would you implement this? As mentioned, I'll probably do something quite similar to the KeylessExchange but instead of providing a file, I'll read each file on a folder looking for CSV files that follows the cached prices format and in their header have the high,low,volume,close,open,time. Then I'll somehow do a lazy load of those files (depending the symbols and date range that is needed)

Would you like to Contribute?

Additional Information

EmersonDove commented 2 years ago

Hey thanks for using blankly!

The mocked data is definitely a hole in our accuracy so fixing that would be awesome.

Right now that patch exists because we were going to make a DataReader object that could handle a variety of formats, like the csvs we provide or the WRDS database.

If you want to deal with the caching, we have a generalized time series caching algorithm implemented here: https://github.com/blankly-finance/blankly/blob/fd0a37f115a09fec44277507ad662ccab17e0689/blankly/exchanges/interfaces/paper_trade/backtest_controller.py#L50

Supporting a multi symbol, multi exchange, ohlcv format would be awesome for keyless exchange, our original conception would look something like this:

{
   "coinbase_pro": {
       "BTC-USD": {
          "open": [1,2,3,4,5],
          "high": [1,2,3,4,5],
          "low": [1,2,3,4,5],
          "close": [1,2,3,4,5],
          "volume": [1,2,3,4,5],
          "time": [160000000,160000000],
       }
   }
}

These are just some thoughts but we're very open to the idea or anything you come up with.

bfan1256 commented 2 years ago

Hey @lomagno2003 , what are your thoughts on this approach? Would also love to get some feedback from the overall community too?