Financial leverage could be easily described as the tool that allows traders to multiply returns at the cost of multiplying also the risk involved in every trade. So, for instance, if a stock today costs 10\$ and tomorrow it appreciates up to 11\$, the trader would experience a return of: $$ R = (\frac{11}{10} -1)*100 = 10\% $$

While, by using a leverage of - let's say 2x - the return would still be: $$ R = (\frac{22}{20} -1)*100 = 10\% $$

But of course in absolute terms, after having given back the borrowed 10\$ from the broker, the trader would experience twice the return. 2\$ instead of just 1\$.

Now, my question is: how this kind of phenomena could be described programmatically, let's say in Python? I found out that even if I could theoretically describe how leverage works, I fail to algorithmically think of it.

Let's say that I have a black box that produces trading signals: $$ \boldsymbol{\tau} = \{1, 2, 3, 4\} \\ $$

where: $$ 1:= Sell \ with \ 10x \ leverage \\ 2:=Sell \ with \ no \ leverage \\ 3 := Buy \ with \ no \ leverage \\ 4 := Buy \ with \ 10x \ leverage$$

I started implementing Python code to depict such an algorithm. How could it be completed implementing leverage?

    if signal == 1:
        #Sell with 10x leverage
        if stock_holdings > 0:
            # Code here
    elif signal == 2:
        #Sell with no leverage
        if stock_holdings > 0:
            USD_holdings = stock_holdings * stock_price
            stock_holdings = 0
    elif signal == 3:
        #Buy with no leverage
        if USD_holdings > 0:
            stock_holdings = USD_holdings / stock_price
            USD_holdings = 0
    elif signal == 4:
        #Buy with 10x leverage
        if USD_holdings > 0:
            # Code here

1 Answer 1


Leverage can be represented by allowing Cash (which you call USD_holdings) to go negative when you buy stock. When it is negative it represents a margin loan. When you sell stock cash will increase, which could make it positive again. And when you buy stock cash decreases. It also decreases once a month when you are charged interest on your average outstanding margin balance for the month.

In any case at all times your total_equity (or net worth) is equal to cash plus the market value of stock holdings (and it is total equity that you care about in determining your profit, percentage return, etc). That's an identity: total_equity $\equiv$ cash + MVS.

Furthermore there will be a lower limit to cash at all times, e.g. cash > -0.50* total_equity, to limit leverage. This should be checked once a day and also at the time you buy additional stock (you don't allow buying additional stock if this would not be satisfied after the purchase). The fact that the lower limit on Cash is not zero, but a negative number, is what distinguishes an account which is allowed to use leverage from an account that is not allowed.

  • $\begingroup$ The thing is: how to implement it in Python? That's my actual problem. I tried, but things start messing up when I liquidate my stock holdings. $\endgroup$ Jun 11, 2019 at 10:20
  • $\begingroup$ @AndreyE.Vedishchev, it depends on how you're tracking return. in PnL terms, you gain/lose based on the levered equity of your position (eg, \$20 rather than \$10 in your example above; drop to \$18, your equity moves to \$8 from \$10). In percentage terms, your return would be wrt your investment (eg, \$20 to \$18, represents a \$2 loss against your initial $10 investment, hence -20%). IME, PnL are a little bit easier to work with. $\endgroup$
    – Chris
    Jun 11, 2019 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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