pythonlessons / FinRock

Reinforcement Learning package for Finance
Apache License 2.0
82 stars 20 forks source link

calculating balance #8

Open daliel opened 4 months ago

daliel commented 4 months ago

I don't understand your math. my vision for calculating the balance is this:

if action == 2: # buy
    buy_order_size = order_size
    next_state.allocation_percentage = last_state.allocation_percentage + (1 - last_state.allocation_percentage) * buy_order_size
    asset = last_state.balance * buy_order_size / last_state.close
    next_state.assets = last_state.assets + asset
    balance = last_state.balance * buy_order_size
    next_state.balance = last_state.balance - balance - (balance * self.fee_ratio)

elif action == 1: # sell
    sell_order_size = order_size
    next_state.allocation_percentage = last_state.allocation_percentage - last_state.allocation_percentage * sell_order_size
    balance = last_state.assets * sell_order_size * last_state.close
    next_state.balance = last_state.balance + balance - (balance * self.fee_ratio)
    next_state.assets = last_state.assets - last_state.assets * sell_order_size

maybe this is valid only for BTCUSDT on binance

after all, the exchange receives a percentage of all transactions, and in your code only a percentage of transactions is taken away, and not the purchase/sale cost it is logical to assume that the transaction will be profitable if the sell_price / buy_price - 1 > fee_ratio

pythonlessons commented 4 months ago

Thanks for this investigation: sell_price / buy_price - 1 > fee_ratio I'll check this later