AI4Finance-Foundation / FinRL-Trading

For trading. Please star.
https://ai4finance.org
MIT License
2.01k stars 729 forks source link

Does the cost is updating with new value of shares or old ones ? #33

Open MariamDundua opened 3 years ago

MariamDundua commented 3 years ago

When the selling of stock happens, and the cost is updated, I think this update with new amount of shares. I mean, first time self.state[index+STOCK_DIM+1] is updated and this updated version is participated to compute costs

self.cost +=self.state[index+1]*min(abs(action),self.state[index+STOCK_DIM+1]) * \
             TRANSACTION_FEE_PERCENT ```
`Where I am wrong? The amount of shares  (self. state[index+STOCK_DIM+1]) in cost is a new one or an old one?
`
def _sell_stock(self, index, action):
    # perform sell action based on the sign of the action
    if self.state[index+STOCK_DIM+1] > 0:
        #update balance
        self.state[0] += \
        self.state[index+1]*min(abs(action),self.state[index+STOCK_DIM+1]) * \
         (1- TRANSACTION_FEE_PERCENT)

        self.state[index+STOCK_DIM+1] -= min(abs(action), self.state[index+STOCK_DIM+1])
        self.cost +=self.state[index+1]*min(abs(action),self.state[index+STOCK_DIM+1]) * \
         TRANSACTION_FEE_PERCENT
        self.trades+=1
    else:
        pass


`Here is the full version of codes from the training environment`
emjay032 commented 3 years ago

self.cost is a is a variable to track the cost of the agents strategy and updated after each sell/buy action += it will change the account balance because the cost for trading a stock is directly substracted from the money the agent has. It will not update the new amount of shares: What this function does:

  1. check if the stock is currently held if yes the sell the amount the agent decided as an action
  2. update the balance by incresasing the balance money you get from the selling of that stock (minus transaction costs so (1-TRANSACTION_FFE_PERCENT)) (not min(abs(action,self.state[index+STOCK_DIM+1]) ensures that you can just sell maximum the amount of shares currently held of that stock because if the rescaled action says "sell 10 stocks but you just hold 5 stocks" you can just sell 5 stocks because no short selling is allowed.
  3. update the number of that particular stock currently held if you hold 10 stocks and sell 5 then you just hold 5 stocks
  4. update the cost variable to track the money spend by follwing the agents strategy
  5. update te count of trades so after each sell/buy action it is increased by 1 to see in the end how much trades the agent made

so yes you are right every sell action deductes the transaction cost directly from the balance

i created a similar environment but seperated the balance, stock_prices, num_stocks in the portfolio to increase readability just think about it as this

def _sell_stock(self, index, action):

self.holdings[index] > 0: (step 1)

update balance, holding, cost, trade_count

                amount = round(min(abs(action),self.holdings[index]))  #amount to trade (step 2)
                stock_sell_activity = [self.day,index,amount] (aditionally Track sell activity)
                self.balance += self.stock_price[index]*amount*(1-TRANSACTION_FEE_PERCENT-BPS) (step 2) update balance note BPS is a spread considere because bid and aski prices differ 
                self.holdings[index] -= amount (step 3) update the hodling of the particular stock by substracting the amount sold
                self.cost += self.stock_price[index]*amount*(TRANSACTION_FEE_PERCENT+BPS) (step 4)
                self.daily_cost += self.stock_price[index]*amount*(TRANSACTION_FEE_PERCENT+BPS) (aditionally track daily cost of the strategy)
                self.daily_sell_amount += round(self.stock_price[index]*amount*(1-TRANSACTION_FEE_PERCENT-BPS)) (additonally to tracl daily amount sold )
                self.trades+=1 (step 5)
else:
    pass