Skip to content

Requesting an article explaining the math of StockTradingEnv #3

@fightthepower

Description

@fightthepower

First of all thank you for writing those articles. I was searching for a stock simulating system similar to gym. In your article you have given a good explanation of programming a gym enviornment but many of us don't know finance, so can you please add an article explaining the math behind it.

 if action_type < 1:
    # Buy amount % of balance in shares
    total_possible = self.balance / current_price
    shares_bought = total_possible * amount
    prev_cost = self.cost_basis * self.shares_held
    additional_cost = shares_bought * current_price
    self.balance -= additional_cost
    self.cost_basis = (prev_cost + additional_cost) / 
                            (self.shares_held + shares_bought)
    self.shares_held += shares_bought
  elif actionType < 2:
    # Sell amount % of shares held
    shares_sold = self.shares_held * amount . 
    self.balance += shares_sold * current_price
    self.shares_held -= shares_sold
    self.total_shares_sold += shares_sold
    self.total_sales_value += shares_sold * current_price

Look at this code I really don't know what is happening inside those conditional statements. It seems that it only buy and sell and don't hold it. I for one wants to build a simulator which trade x number of shares(not percentage of balance) for a amount of say 100 dollars. Without understanding the math I really can't build a new stocksimenv.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions