wassname / rl-portfolio-management

Attempting to replicate "A Deep Reinforcement Learning Framework for the Financial Portfolio Management Problem" https://arxiv.org/abs/1706.10059 (and an openai gym environment)
544 stars 179 forks source link

Calculation of transaction Cost #7

Closed cyferd974 closed 6 years ago

cyferd974 commented 6 years ago

Hi,

Congrats for this excellent job.

I have a question about the calculation of the transaction cost in your code.

In (eq 16) in the paper of Jiang et al., the transaction cost is calculated based on the changes of portfolio weights (sum of 1 to m, excluding the cash weight). In the code, the dw1 is of shape of m+1 (including cash weight) and the cost is calculated as:

mu1 = self.cost * (np.abs(dw1 - w1)).sum()

I wonder if this latter calculation would not have an impact by double counting costs for transactions between cash and asset? Should it not ne replaced by :

mu1 = self.cost * (np.abs(dw1[1:] - w1[1:])).sum() ?

wassname commented 6 years ago

Really nice catch! You're right about that. Your logic makes sense, and I we can test it with a unit test too:

env.reset()
env.step(np.array([1,0,0,0]))
state, reward, done, info = env.step(np.array([0,1,0,0])
print(info['cost'], env.sim.cost)
# 0.0050, 0.0025

Here I buy 100% cash, then sell 100% cash. For the selling step the cost for that single transaction was twice the transaction cost, so that's double counting.

I'll fix it and add a unit test, thanks again for spotting that.

cyferd974 commented 6 years ago

glad to be helpful