AI4Finance-Foundation / FinRL

FinRL: Financial Reinforcement Learning. 🔥
https://ai4finance.org
MIT License
9.66k stars 2.34k forks source link

Runtime error: FinRL_PaperTrading_Demo.ipynb; AlpacaPaperTrading.trade(); buy_num_shares is NaN #758

Open marcipops opened 1 year ago

marcipops commented 1 year ago

File: https://github.com/AI4Finance-Foundation/FinRL/blob/master/tutorials/3-Practical/FinRL_PaperTrading_Demo.ipynb

Part 3: Deploy the agent Setup Alpaca Paper trading environment

class AlpacaPaperTrading()

def trade(self): state = self.get_state() . . .

        for index in np.where(action > min_action)[0]:  # buy_index:
            if self.cash < 0:
                tmp_cash = 0
            else:
                tmp_cash = self.cash
            buy_num_shares = min(tmp_cash // self.price[index], abs(int(action[index])))
            qty = abs(int(buy_num_shares))
    .
    .
    .

RuntimeWarning: invalid value encountered in double_scalars buy_num_shares = min(tmp_cash // self.price[index], abs(int(action[index])))

Snapshot of variables:

Error: buy_num_shares = NaN index: 24 tmp_cash: 0 self.price[index]: 0.0 abs(int(action[index]): 22

Athe-kunal commented 1 year ago

@marcipops Why the self. price[index] is 0.0? Does this happen in live paper trading? Which ticker symbol are you using for live paper trading?

marcipops commented 1 year ago

Hi @Athe-kunal, not sure why self. price[index] is 0.0. Yes, this is happening in live paper trading. Also sometimes the calc gives: RuntimeWarning: divide by zero encountered in double_scalars. Presumably, this is when tmp_cash is > 0, but self.price[index]=0

I can put some more debug statements in to help find the cause. What is the best way to access the relevant tic? Can this be obtained from the index, for example?

zhumingpassional commented 1 year ago

@marcipops have you revised any code? have set the alpaca-key, alpaca-secret?

marcipops commented 1 year ago

The code is unchanged apart from the alpaca credentials of course (suggest that you might put these config parameters in a separate config file, then the code could run out of the box).

Looks like the issue is self.price[index]: 0.0 I have put in a debug statement to track down the relevant stock, but I could not replicate the issue yesterday. I'll need to save the data somewhere to be able to inspect - as the price might be coming in as 0 in the raw data from download_data() or not being cleaned data from clean_data().

zhumingpassional commented 1 year ago

Got it. your method is good. I suggest to print the stocks with price 0, which requires to write several lines of code.

marcipops commented 1 year ago

@zhumingpassional have added print statements and managed to capture the issue as below:

RuntimeWarning: invalid value encountered in double_scalars buy_num_shares = min(tmp_cash // self.price[index], abs(int(action[index]))) Error: buy_num_shares = NaN index: 24 self.cash: -2074.35 tmp_cash: 0 self.price[index]: 0.0 self.price: [137.56 268.8 144.7 160.2 228. 139.85 44.94 183.6 100.3 49.05 361.1 284. 208.91 137.58 28.18 171.45 130. 59.61 275.5 125.03

  1. 221.85 96.89 134.98 0. 543.51 198.24 37.38 37.05 140.75] self.stockUniverse[index]: VZ abs(int(action[index]): 11

Is this sufficient for you to find the error? Will a price of 0 escape the cleaning process?

zhumingpassional commented 1 year ago

Hi. self.price[index] is 0, is self.price the output of download_data() or clean_data()? what is the stock name with the index 24? can you print the open high low close price of this stock in download_data() and clean_data()?

joshwigginton commented 1 year ago

Getting the same error when running the Alpaca Paper Trading from the FinRL-Tutorials

Market opened. 30 Quantity is 0, order of | 0 AXP sell | not completed. Quantity is 0, order of | 0 AAPL sell | not completed. Quantity is 0, order of | 0 INTC sell | not completed. Quantity is 0, order of | 0 JNJ sell | not completed. Quantity is 0, order of | 0 V sell | not completed. Quantity is 0, order of | 0 WBA sell | not completed. Market order of | 17 AMGN buy | completed. Market order of | 19 CSCO buy | completed. Market order of | 17 HD buy | completed. Market order of | 40 IBM buy | completed. ... Quantity is 0, order of | 0 JPM buy | not completed. Quantity is 0, order of | 0 PG buy | not completed. Quantity is 0, order of | 0 CRM buy | not completed. Quantity is 0, order of | 0 VZ buy | not completed. /tmp/ipykernel_6705/973891003.py:218: RuntimeWarning: invalid value encountered in double_scalars buy_num_shares = min(tmp_cash // self.price[index], abs(int(action[index]))) Exception in thread Thread-38: Traceback (most recent call last): File "/home/user/anaconda3/envs/FinRL/lib/python3.9/threading.py", line 980, in _bootstrap_inner self.run() File "/home/user/anaconda3/envs/FinRL/lib/python3.9/threading.py", line 917, in run self._target(*self._args, self._kwargs) File "/tmp/ipykernel_6705/973891003.py", line 223, in trade ValueError: cannot convert float NaN to integer**

roliver-88 commented 1 year ago

Did anyone find a fix to this issue?