-1
$\begingroup$

In the book "Python for Algorithmic Trading" by Yves Hilpisch,

it calculates the logarithmic return by summing up all the log values.

When calculates the profit for long position: log(current_price/past_price)

When calculates the profit for short position: -1*log(current_price/past_price)

And summing up all these log values.

Is the above logic correct for calculating the profit for short position?


In Python code, it's something like this: (data is Pandas Dataframe. So, shift(1) means just the last price.)

# (I shortened the code a bit, but the logic is the same.)
if long:
    data['returns'] = np.log(data['price']/data['price].shift(1))

elif short: 
    data['returns'] = np.log(data['price']/data['price].shift(1)) * (-1)

# final return 
data['returns'].sum()
# plotting
data['returns'].dropna().cumsum().apply(np.exp).plot()

the logic for the long position is okay, but... that of the short position is not correct, I think.

(I also read this answer which seems correct, and still don't know why the above logic is correct.)

P.S. Let's say I shorted a stock with \$10 when it's \$100 and now it's \$20. Then my asset would be \$18 (80% profit). But, the above equation says my asset becomes \$50 ->This is the point I don't understand.

$\endgroup$
4
  • 2
    $\begingroup$ Yes it is correct. Keep in mind that $(-1) \times \ln \frac{p_t}{p_{t-1}} = \ln \frac{p_{t-1}}{p_t}$. So the two formulas are giving the same result. $\endgroup$
    – nbbo2
    Sep 27, 2021 at 10:41
  • $\begingroup$ @noob2 Thanks. But the two formulas are not supposed to be the same, aren't they? For example, let's say I short a stock when it's 100USD and the next day it becomes 20USD. Then I earn 80% profit, not 400%. If we follow the above equation, I should get 400% profit(my asset becomes 5 times larger), which makes no sense, doesn't it? $\endgroup$ Sep 27, 2021 at 10:49
  • $\begingroup$ 80% is the arithmetic return, not the logarithmic return. $\endgroup$
    – nbbo2
    Sep 27, 2021 at 11:36
  • $\begingroup$ @noob2 yes, but, whatever kind of return we use, our asset should be the same. We use logarithmic return because it has many advantages, but it doesn't change our final asset. So you're saying my asset becomes 5 times larger in the above example I commented? (that is not possible as far as I know) $\endgroup$ Sep 27, 2021 at 12:01

1 Answer 1

2
$\begingroup$

Did you try the differences?

You can check out logarithmic properties in this answer. Specifically,

Reason 2: The log difference is independent of the direction of change

If you code it, you see that the logarithmic return for $$ -1*log(p_1/p_0) = log(p_0/p_1)$$

p0 = 100
p1 = 101
retPos = p1/p0-1
retNeg = 1-p1/p0
retLogPos = log(p1/p0)
retLogNeg = log(p0/p1)
retNegSE = -1*retLogPos
println("Positive Return = $retPos")
println("Positive Log Return = $retLogPos")
println("Negative Return = $retNeg")
println("Negative Log Return = $retLogNeg")
println("Negative Log Return Alternative = $retNegSE")

enter image description here

Edit: Log return do NOT equal normal returns as this answer explains. The relationship between normal and log returns is $$(normal return) = exp(log return)-1$$

p0 = 100
p1 = 20
retPos = p1/p0-1
retNeg = 1-p1/p0
retLogPos = log(p1/p0)
retLogNeg = log(p0/p1)
retNegSE = -1*retLogPos
println("Positive Return = $retPos")
println("Positive Log Return = $retLogPos")
println("Negative Return = $retNeg")
println("Negative Log Return = $retLogNeg")
println("Negative Log Return Alternative = $retNegSE")
println("Order doesnt matter for log: $(retLogNeg == retNegSE)")
println("Normal return from Log = $(1- exp(-retLogNeg))")

enter image description here

If you get small differences, this is not a computational error but explained by floating point math:
Python Docs
Rounding in Python
Is floating point math broken

In my initial example I made a silly mistake. I used $p0/p1-1$ instead of $1-p1/p0$ which equals $(p0-p1)/p0$. It does not affect the log return computation but the arithmetic return itself. Apologies for the confusion. I need to look more properly before answering - thanks for teaching me a lesson!

The reason I used -retLogNeg is that A = P e^rt = $100 * e^-1.6094379124341003 = $20 (start value to get today's value). Technically, this assumes that the short sell is a liability that must be paid back at a future date. If it drops to zero, your liability vanishes and you get 100% return as you correctly stated.

The change in log is not a percentage per se, it approximates it for small changes (returns) as shown in my initial link.

$\endgroup$
7
  • $\begingroup$ Apologies, did not want to duplicate @noob's work. I did not see the post without refreshing the page. $\endgroup$
    – AKdemy
    Sep 27, 2021 at 10:45
  • $\begingroup$ Thanks a lot for your answer. Just let me describe my real question once again: Does the negative return 4.0 in your code mean that it's the return we get when we short a stock that was 100USD and now 20USD? (As far as I know, it should be 0.8, not 4.0) $\endgroup$ Sep 27, 2021 at 11:12
  • $\begingroup$ You sell at 100 and need to pay back only 20 (excluding fees). What is your return if you pay 20 and sell at 100? See also. $\endgroup$
    – AKdemy
    Sep 27, 2021 at 11:31
  • $\begingroup$ I think I get your point, but let's think about the real situation: Let's say my start asset is \$10. If I bought a stock at \$20 and sell at \$100, then of course my return would be 4.0(my asset becomes \$50). But, when we short, we sell first, so I can sell only a limited amount, isn't it? So, If I short a stock when its price was \$100 and now \$20, then my asset would be \$18. As far as I know, in short-selling, the return cannot be larger than 100% (if no leverage). Did I get something wrong? Thanks in advance. $\endgroup$ Sep 27, 2021 at 11:47
  • $\begingroup$ (The question I commented in the link you gave) If we just ignore fees, then the return becomes log(entryprice/exitprice). If exitprice = (1/100)*entryprice, then it's log(100). Does this possible in short-selling? $\endgroup$ Sep 27, 2021 at 12:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.