def _buy_stock(self, index, action):

    def _do_buy():
        available_amount = (self.state[0] // self.state[(index + 1)])
        buy_num_shares = min(available_amount, action)
        buy_amount = ((self.state[(index + 1)] * buy_num_shares) * (1 + self.buy_cost_pct))
        self.state[0] -= buy_amount
        self.state[((index + self.stock_dim) + 1)] += min(available_amount, action)
        self.cost += ((self.state[(index + 1)] * min(available_amount, action)) * self.buy_cost_pct)
        self.trades += 1
        return buy_num_shares
    if (self.turbulence_threshold is None):
        buy_num_shares = _do_buy()
    elif (self.turbulence < self.turbulence_threshold):
        buy_num_shares = _do_buy()
    else:
        pass
    return buy_num_shares
def _buy_stock(self, index, action):

    def _do_buy():
        available_amount = (self.state[0] // self.state[(index + 1)])
        buy_num_shares = min(available_amount, action)
        buy_amount = ((self.state[(index + 1)] * buy_num_shares) * (1 + self.buy_cost_pct))
        self.state[0] -= buy_amount
        self.state[((index + self.stock_dim) + 1)] += min(available_amount, action)
        self.cost += ((self.state[(index + 1)] * min(available_amount, action)) * self.buy_cost_pct)
        self.trades += 1
        return buy_num_shares
    if (self.turbulence_threshold is None):
        buy_num_shares = _do_buy()
    elif (self.turbulence < self.turbulence_threshold):
        buy_num_shares = _do_buy()
    else:
        pass
    return buy_num_shares