AutoTrader icon indicating copy to clipboard operation
AutoTrader copied to clipboard

AutoData docs

Open rseverinop opened this issue 1 year ago • 2 comments

**Is your feature request related to a problem? Imagine user wants to use data from other sources for AutoData() for example a data frame from MetaTrader5, how that user could integrate their strategies with auto trader?

rseverinop avatar Dec 01 '22 04:12 rseverinop

Hi @rseverinop,

There are two ways you could go about this. The first (and the simplest) way would be to use the add_data method to read from local data files. An example run script is provided below, where a file name "EUdata.csv" contains data for the "EUR_USD" instrument in the strategy config file. Of course, you would need to figure out a way to write data from MetaTrader5 to a CSV file.

from autotrader import AutoTrader

# Create AutoTrader instance, configure it, and run backtest
at = AutoTrader()
at.configure(verbosity=1, show_plot=False, mode="periodic")
at.add_strategy('macd')
at.backtest(start='1/1/2021', end='1/5/2021')
at.virtual_account_config(initial_balance=1000, leverage = 30)
at.add_data(data_dict={"EUR_USD": "EUdata.csv"})
at.run()

Note that AutoTrader expects the data to be kept in a price_data/ directory, as per the recommended directory structure, but you can specify a different directory using the data_directory path.


The second option is to use a custom data pipeline. Unfortunately, I haven't had the chance to document any examples of this approach, but you can use the source code for guidance. As a short summary to get you started:

  • Each trading bot instantiates its own DataStream object, responsible for providing processed data to the bot.
  • You can create your own custom DataStream class by inheriting it first and adding your own logic, then provide it to AutoTrader using the add_data method, via the stream_object argument.
  • The first method of DataStream to overwrite is the refresh method, called by the trading bots. This is where you place any logic to load the data. Here, you could connect to MetaTrader to retrieve the data, before formatting it into a DataFrame.
  • The second method which you may need to overwrite is get_trading_bars. If your data is in the standard Pandas DataFrame format, you may skip this step.

If this option is something you are interested in, let me know and I will try to find a minimum working example.

Please let me know if you have any questions about the options above.

kieran-mackle avatar Dec 06 '22 04:12 kieran-mackle

Hi, thank you so much for sharing this great framework and keeping it well maintained.

I am actually facing a similar issue. I have the OHLC data saved as a local database file. During analysis, I usually use make a SQL query and put them in a DataFrame. Just wonder how to make it work without re-exporting to CSVs. Is there some more info on using custom data source?

I looked a bit into the DataStream class, Am I correct that in addition to OHLC price data, quote_data must also be provided? And what AutoData class/instance should I use in such case? (e.g. do I need to subclass it or config it in some ways?)

x-EricH-x avatar Nov 18 '23 00:11 x-EricH-x