AI4Finance-Foundation / FinRL-Meta

FinRL­-Meta: Dynamic datasets and market environments for FinRL.
https://ai4finance.org
MIT License
1.2k stars 560 forks source link

FinRL_PaperTrading_Demo.ipynb - errors on trading operation #226

Open marcipops opened 1 year ago

marcipops commented 1 year ago

Environment: Win10 Chrome Colab File at https://colab.research.google.com/github/AI4Finance-Foundation/FinRL-Tutorials/blob/master/3-Practical/FinRL_PaperTrading_Demo.ipynb

No change to this file other than Alpaca credentials required.

Function call: paper_trading_erl = AlpacaPaperTrading(ticker_list = DOW_30_TICKER, time_interval = '1Min', drl_lib = 'elegantrl', agent = 'ppo', cwd = './papertrading_erl_retrain', net_dim = ERL_PARAMS['net_dimension'], state_dim = state_dim, action_dim= action_dim, API_KEY = API_KEY, API_SECRET = API_SECRET, API_BASE_URL = API_BASE_URL, tech_indicator_list = INDICATORS, turbulence_thresh=30, max_stock=1e2) paper_trading_erl.run()

Error Output: load actor from: ./papertrading_erl_retrain/actor.pth Waiting for market to open... 0 minutes til market open. Market opened. Exception in thread Thread-12: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 182, in trade state = self.get_state() File "", line 247, in get_state tech_indicator_list=self.tech_indicator_list) File "/usr/local/lib/python3.7/dist-packages/finrl/meta/data_processors/processor_alpaca.py", line 355, in fetch_latest_data raise ValueError ValueError

Exception in thread Thread-13: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 182, in trade state = self.get_state() File "", line 247, in get_state tech_indicator_list=self.tech_indicator_list) File "/usr/local/lib/python3.7/dist-packages/finrl/meta/data_processors/processor_alpaca.py", line 355, in fetch_latest_data raise ValueError ValueError

Exception in thread Thread-14: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 182, in trade state = self.get_state() File "", line 247, in get_state tech_indicator_list=self.tech_indicator_list) File "/usr/local/lib/python3.7/dist-packages/finrl/meta/data_processors/processor_alpaca.py", line 355, in fetch_latest_data raise ValueError ValueError

Exception in thread Thread-15: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 182, in trade state = self.get_state() File "", line 247, in get_state tech_indicator_list=self.tech_indicator_list) File "/usr/local/lib/python3.7/dist-packages/finrl/meta/data_processors/processor_alpaca.py", line 355, in fetch_latest_data raise ValueError ValueError

Exception in thread Thread-16: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 182, in trade state = self.get_state() File "", line 247, in get_state tech_indicator_list=self.tech_indicator_list) File "/usr/local/lib/python3.7/dist-packages/finrl/meta/data_processors/processor_alpaca.py", line 355, in fetch_latest_data raise ValueError ValueError

Exception in thread Thread-17: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 182, in trade state = self.get_state() File "", line 247, in get_state tech_indicator_list=self.tech_indicator_list) File "/usr/local/lib/python3.7/dist-packages/finrl/meta/data_processors/processor_alpaca.py", line 355, in fetch_latest_data raise ValueError ValueError

Exception in thread Thread-18: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 182, in trade state = self.get_state() File "", line 247, in get_state tech_indicator_list=self.tech_indicator_list) File "/usr/local/lib/python3.7/dist-packages/finrl/meta/data_processors/processor_alpaca.py", line 355, in fetch_latest_data raise ValueError ValueError

Exception in thread Thread-19: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 182, in trade state = self.get_state() File "", line 247, in get_state tech_indicator_list=self.tech_indicator_list) File "/usr/local/lib/python3.7/dist-packages/finrl/meta/data_processors/processor_alpaca.py", line 355, in fetch_latest_data raise ValueError ValueError

Exception in thread Thread-20: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 182, in trade state = self.get_state() File "", line 247, in get_state tech_indicator_list=self.tech_indicator_list) File "/usr/local/lib/python3.7/dist-packages/finrl/meta/data_processors/processor_alpaca.py", line 355, in fetch_latest_data raise ValueError ValueError

Exception in thread Thread-21: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 182, in trade state = self.get_state() File "", line 247, in get_state tech_indicator_list=self.tech_indicator_list) File "/usr/local/lib/python3.7/dist-packages/finrl/meta/data_processors/processor_alpaca.py", line 355, in fetch_latest_data raise ValueError ValueError

Exception in thread Thread-22: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 182, in trade state = self.get_state() File "", line 247, in get_state tech_indicator_list=self.tech_indicator_list) File "/usr/local/lib/python3.7/dist-packages/finrl/meta/data_processors/processor_alpaca.py", line 355, in fetch_latest_data raise ValueError ValueError

Exception in thread Thread-23: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 182, in trade state = self.get_state() File "", line 247, in get_state tech_indicator_list=self.tech_indicator_list) File "/usr/local/lib/python3.7/dist-packages/finrl/meta/data_processors/processor_alpaca.py", line 355, in fetch_latest_data raise ValueError ValueError

Exception in thread Thread-24: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 182, in trade state = self.get_state() File "", line 247, in get_state tech_indicator_list=self.tech_indicator_list) File "/usr/local/lib/python3.7/dist-packages/finrl/meta/data_processors/processor_alpaca.py", line 355, in fetch_latest_data raise ValueError ValueError

Exception in thread Thread-25: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 182, in trade state = self.get_state() File "", line 247, in get_state tech_indicator_list=self.tech_indicator_list) File "/usr/local/lib/python3.7/dist-packages/finrl/meta/data_processors/processor_alpaca.py", line 355, in fetch_latest_data raise ValueError ValueError

Succesfully add technical indicators Successfully transformed into array 30 Exception in thread Thread-26: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 186, in trade s_tensor = torch.as_tensor((state,), device=self.device) AttributeError: 'AlpacaPaperTrading' object has no attribute 'device'

Succesfully add technical indicators Successfully transformed into array 30 Exception in thread Thread-27: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 186, in trade s_tensor = torch.as_tensor((state,), device=self.device) AttributeError: 'AlpacaPaperTrading' object has no attribute 'device'

Succesfully add technical indicators Successfully transformed into array 30 Exception in thread Thread-28: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 186, in trade s_tensor = torch.as_tensor((state,), device=self.device) AttributeError: 'AlpacaPaperTrading' object has no attribute 'device'

Succesfully add technical indicators Successfully transformed into array 30 Exception in thread Thread-29: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 186, in trade s_tensor = torch.as_tensor((state,), device=self.device) AttributeError: 'AlpacaPaperTrading' object has no attribute 'device'

Succesfully add technical indicators Successfully transformed into array 30 Exception in thread Thread-30: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "", line 186, in trade s_tensor = torch.as_tensor((state,), device=self.device) AttributeError: 'AlpacaPaperTrading' object has no attribute 'device'

marcipops commented 1 year ago

Confirmed that I also get the same error in a local environment (Win10/WSL/Ubuntu20.04/Python 3.8.10) running in Vscode 1.70.2. Hope that helps.

zhumingpassional commented 1 year ago

Thanks for reporting it.

We are revising the code.

marcipops commented 1 year ago

Thanks Ming!

Get Outlook for Androidhttps://aka.ms/AAb9ysg


From: Ming Zhu @.> Sent: Saturday, September 3, 2022 6:08:43 AM To: AI4Finance-Foundation/FinRL-Meta @.> Cc: marcipops @.>; Author @.> Subject: Re: [AI4Finance-Foundation/FinRL-Meta] FinRL_PaperTrading_Demo.ipynb - errors on trading operation (Issue #226)

Thanks for reporting it.

We are revising the code.

— Reply to this email directly, view it on GitHubhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FAI4Finance-Foundation%2FFinRL-Meta%2Fissues%2F226%23issuecomment-1236050333&data=05%7C01%7C%7C8d61068f7af345ddb1b008da8d6a6029%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637977785259874643%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=sAiOEjrHTVnoSszDAh3nIyj4R2FrtY38nt5KO1r9El0%3D&reserved=0, or unsubscribehttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAG7HSZNZBCT2L3OUCXDU2RTV4LMNXANCNFSM6AAAAAAQDJS5E4&data=05%7C01%7C%7C8d61068f7af345ddb1b008da8d6a6029%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637977785259874643%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=%2FuvJrdsNyljAx%2BHcIFvA%2F1N09eE%2B9DoQLPM9oXoCS38%3D&reserved=0. You are receiving this because you authored the thread.Message ID: @.***>

marcipops commented 1 year ago

Any update on this please Ming?

zhumingpassional commented 1 year ago

@marcipops we will submit PRs. sorry for delayed update.

zhumingpassional commented 1 year ago

updated. pls test it.

marcipops commented 1 year ago

Tested on Colab. Still fails with the same error.

ZiyiXia commented 1 year ago

A new version of paper trading demo was just uploaded, please check it out.

marcipops commented 1 year ago

@ZiyiXia much appreciated - yes, it seems to work on Colab.

A minor point, but was discussed some weeks ago. There are lots of orders sent every run() cycle that are invalid i.e. quantity should not be 0. Example: Quantity is 0, order of | 0 CAT sell | not completed.

I appreciate that this is due to the agent wanting to do "nothing", but it would be better to only issue orders with a valid quantity. Also, fixing this would make it easier to see the valid orders from the majority of invalid ones :)

I also have the demo also running locally from yesterday in my test environment of Win10 WSL/Ubuntu 20.04, as Ubuntu 20.04 is my target production environment. Jingyang Rui had a similar environment. t does have some warnings and exceptions but hasn't crashed (yet).


The warnings are:

data_df = data_df.append(barset) /home/marc/venv/lib/python3.8/site-packages/finrl/meta/data_processors/processor_alpaca.py:301: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead. ... /home/marc/venv/lib/python3.8/site-packages/finrl/meta/data_processors/processor_alpaca.py:178: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead. indicator_df = indicator_df.append(temp_indicator, ignore_index=True)


The exceptions are:

Exception in thread Thread-8814: Traceback (most recent call last): File "/home/marc/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 234, in _one_request resp.raise_for_status() File "/home/marc/venv/lib/python3.8/site-packages/requests/models.py", line 1021, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 422 Client Error: Unprocessable Entity for url: https://data.alpaca.markets/v2/stocks/bars?timeframe=1Min&adjustment=raw&limit=100&symbols=AXP

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner self.run() File "/usr/lib/python3.8/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "/tmp/ipykernel_506/4288445362.py", line 182, in trade File "/tmp/ipykernel_506/4288445362.py", line 245, in get_state File "/home/marc/venv/lib/python3.8/site-packages/finrl/meta/data_processors/processor_alpaca.py", line 298, in fetch_latest_data barset = self.api.get_bars([tic], time_interval, limit=limit).df # [tic] File "/home/marc/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 714, in get_bars bars = list(self.get_bars_iter(symbol, File "/home/marc/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 698, in get_bars_iter for bar in bars: File "/home/marc/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 585, in _data_get resp = self.data_get(path, data=data, feed=feed, File "/home/marc/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 270, in data_get return self._request( File "/home/marc/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 213, in _request return self._one_request(method, url, opts, retry) File "/home/marc/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 242, in _one_request raise APIError(error, http_error) alpaca_trade_api.rest.APIError: your subscription does not permit querying data from the past 15 minutes Exception in thread Thread-8815: Traceback (most recent call last): File "/home/marc/venv/lib/python3.8/site-packages/alpaca_trade_api/rest.py", line 234, in _one_request resp.raise_for_status() File "/home/marc/venv/lib/python3.8/site-packages/requests/models.py", line 1021, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 422 Client Error: Unprocessable Entity for url: https://data.alpaca.markets/v2/stocks/bars?timeframe=1Min&adjustment=raw&limit=100&symbols=AXP