Leverage Discussion



  • Thank you for the quick update with leverage. It is very convenient now to use leverage, no additional code is required. I have only one comment on it - looks like broker data is calculated slightly incorrectly while the position is open.

    Here is an output from my buy-on-Monday-sell-on-Thursday script with broker margin of 2.0, first trade:

    Starting Portfolio Value: 100000.00
    2016-06-01  Open Pos, 0; Price 210.2700; Lev Val 100000.0000 Val 100000.00; Cash 100000.00, 
    2016-06-02  Open Pos, 0; Price 210.9100; Lev Val 100000.0000 Val 100000.00; Cash 100000.00, 
    2016-06-03  Open Pos, 0; Price 210.2800; Lev Val 100000.0000 Val 100000.00; Cash 100000.00, 
    2016-06-06  Open Pos, 0; Price 211.3500; Lev Val 100000.0000 Val 100000.00; Cash 100000.00, 
    2016-06-06 ORDER CREATED, Price 211.35; Bought, 946
    2016-06-07  Open Pos, 946; Price 211.6800; Lev Val 200280.7205 Val 100156.08; Cash 31.45, 
    2016-06-08  Open Pos, 946; Price 212.3700; Lev Val 200933.4624 Val 100482.45; Cash 31.45, 
    2016-06-09  Open Pos, 946; Price 212.0800; Lev Val 200659.1291 Val 100345.29; Cash 31.45, 
    2016-06-09 ORDER CREATED, Price 212.08; Sold, 946
    2016-06-10 OPERATION PROFIT, GROSS 690.58, NET 690.58
    

    First trade: bought on 2016-06-06 946 shares, price 211.35, sold on 2016-06-09 946 shares, price 212.08. End of day operations.

    What I've noticed:

    1. Even we took money from broker, it is still cash available. If we consider cash as an own trader cash only than I think it should be 0, otherwise if cash is all amount available (with the broker loan) then it should be 62.9: 2 x 100,000 - 946 x 211.35 = 62.9

    2. This comes from item 1: leveraged value on 2016-06-07 = 946 x 211.68 + 62.9 = 200,312.18

    3. Un-leveraged value on 2016-06-07 = 100,000 + (211.68 - 211.35) x 946 = 100,312.18 or
      211.68 x 946 - 99,937.1 (took from broker when bought stocks) = 100,312.18


  • administrators

    The implementation idea is that you can buy x leverage times with the same amount of cash.

    In your example you would still be able to use the remaining 31.46 monetary units for an extra acquisition (also applying the 2x factor)

    With a rationale in mind: you may be trading simultaneously products that have leverage and products which don't. If the leveraged products deplete your cash reserves, without even entering in a leveraged status, there is no chance to trade the other products.



  • But what about items 2 and 3?

    Leveraged value is calculated little bit inconsistently: full securities position size plus only half cash size. And unleveraged value for open position doesn't account for increased due to leverage P&L.

    For example,
    I have 100,000. Bought 2,000 of security XXX by 100/share (2x leverage). Price went down to 50 and my un-leveraged value went to 0.

    Looks like bt calculates it as follows (based on numbersfrom script):
    Un-lev value = (Pos Size x Price) / Leverage + Cash
    And un-leveraged value will be 50,000 in case of the open position.


  • administrators

    Before considering a new release

    1. Even we took money from broker, it is still cash available. If we consider cash as an own trader cash only than I think it should be 0, otherwise if cash is all amount available (with the broker loan) then it should be 62.9: 2 x 100,000 - 946 x 211.35 = 62.9

    No. The money is not leveraged per se. As stated above the money could be used to buy a non-leveraged asset whilst still holding the leveraged asset. If such a thing were bought, then the total leveraged value would be: value leveraged asset + value unleveraged asset. And the 2nd part of that sum is still (barring any price variation) the value of the cash which has been used to buy the non-leveraged asset.

    1. This comes from item 1: leveraged value on 2016-06-07 = 946 x 211.68 + 62.9 = 200,312.18

    As explained for 1., the cash is not leveraged but real and that's why the calculation is:

    • 946 x 211.68 + 31.45

    You actually have 946 units of the asset and also 31.45 monetary units. Of course, the acquisition of the 946 units has only been possible because the broker has allowed you to buy with 2x power.

    This would be the actual real value of your account, but as you rightly point out, this destroys regular calculations because of the constant jumps between leveraged and unleveraged states.

    1. Un-leveraged value on 2016-06-07 = 100,000 + (211.68 - 211.35) x 946 = 100,312.18 or
      211.68 x 946 - 99,937.1 (took from broker when bought stocks) = 100,312.18

    The unleveraged value accounts for the existing cash plus the current value of the position unleveraged (in a 2x case it will be halved)

    You actually didn't take 99,937. You bought with 2x power. The total acquisition cost was 199,937 and that means you actually got a free lunch for 99968 monetary units.

    That's what's explained in 1.. The money is not leveraged, the money has a value. And you double the value when you acquire an asset which allows leverage.

    In any case this calculation could possibly be improved to avoid unleveraging a profit/loss, because that will actually be given/taken to/from you regardless of the leverage level.

    For example,
    I have 100,000. Bought 2,000 of security XXX by 100/share (2x leverage). Price went down to 50 and my un-leveraged value went to 0.

    0 is for sure not reported by the broker, so let's assume that's a sample scenario. It actually summarizes what you have depicted above: you are not buying with 2x power (leverage), you are expecting to get a loan. And when the position goes against you, the losses are detracted from your cash and not the loan.

    Although inside backtrader the actual value is reported by a CommissionInfo instance, in the case of stocks your formula summarize the final calculation:

    Un-lev value = (Pos Size x Price) / Leverage + Cash

    As stated above this cannot yield 0. You have 2000 units of the asset and the price is 50. No cash is left. The returned value is 50,000

    Let's put another scenario in play:

    Scenario A

    • Cash: 100,000
    • Acquisition of a non-leveraged asset for 50,000. The price remains constant every day and with it
    • 50,000 monetary units remain available
    • This means that a leveraged asset (with 2x leverage) can be bought for a total of 100,000 whilst still retaining the non-leveraged asset in the portfolio.

    This is the same as:

    Scenario B

    • Cash: 100,000
    • Acquisition of a leveraged (2x) asset for 100,000. Because the leverage is 2x only 50,000 units are taken from the cash.
    • 50,000 monetary units remain available
    • A non-leveraged asset can still be bought for 50,000 units.

    Both A and B are equivalent because the act of acquiring a leveraged asset is considered as having the ability to buy with 2x power.



  • @backtrader Thank you for the update! I've run test using new bt release and get 0 (remaining cash value to beexact) in case of 50% price drop and 2x leverage (see below). I agree with you on the cash discussion, it was my glitch.

    50% price drop and 2x leverage:

    Prices:

    Date,Open,High,Low,Close,Volume,Adj Close
    2016-12-09,225,226,225,226,80848700,226
    2016-12-08,224,225,224,225,90856300,225
    2016-12-07,110.5,112,110.5,112,104873600,112
    2016-12-06,110.5,110.5,110,110.5,57526300,110.5
    2016-12-05,220,221,220,221,65431900,221
    2016-12-02,219,220,219,219,70863400,219
    2016-12-01,220,220,219,219,77230500,219
    

    Broker values:

    Starting Portfolio Value: 100000.00
    2016-12-01  Open Pos, 0; Price 219.0000; Lev Val 100000.0000 Val 100000.00; Cash 100000.00, 
    2016-12-05  Open Pos, 0; Price 221.0000; Lev Val 100000.0000 Val 100000.00; Cash 100000.00, 
    2016-12-05 ORDER CREATED, Price 221.00; Bought, 904
    2016-12-06  Open Pos, 904; Price 110.5000; Lev Val 100000.0000 Val 108.00; Cash 108.00, 
    2016-12-07  Open Pos, 904; Price 112.0000; Lev Val 101356.0000 Val 1464.00; Cash 108.00, 
    2016-12-08  Open Pos, 904; Price 225.0000; Lev Val 203508.0000 Val 103616.00; Cash 108.00, 
    2016-12-09  Open Pos, 904; Price 226.0000; Lev Val 204412.0000 Val 104520.00; Cash 108.00, 
    2016-12-09 ORDER CREATED, Price 226.00; Sold, 904
    Final Portfolio Value: 104520.00
    it took 0.000999927520752 second !
    

    You can see that on 2016-12-06 price went down on half (110.5 vs 221 buy price) and we have cash only on our account. That is exactly how leverage works - multiplying your price change (positive or negative).

    We are little bit different in terms about leverage - you talking about buying with Nx power, and I am talking about real brokerage where you take loan from broker to buy something and you can have negative cash on your account for certain period.


  • administrators

    It doesn't give you 0, it gives you remaining cash. See the logic with the extreme use case in which the cash is 0

    • Starting cash: 100,000 and leverage `2x``
    • Buy 2000 units at price 100 for a total of: 200,000. Because of the 2x leverage only 100,000 are needed
    • Remaining cash: 0
    • Price drops to 50, which means an unrealized loss of: -50 x 2000 = -100,000
    • This loss is detracted from the calculated value which is 50 x 2000 = 100,000

    In that case you are actually losing your entire money base. It's the right unleveraged value. You can even lose more (classic disclaimer in Forex and CFD brokers), but to lose your entire money you only need the price to drop. In fact you can lose an additional total of 100,000.

    The leverage concept implemented also gives you loaned funds, but right from the beginning, rather than when the cash reserves are depleted. The rationale behind is be aligned with the market rules. See for example:

    Reading that should clearly show that even if you get a loan from your broker to buy with a (for example) 2x leverage, you need to keep cash in your account. The reason is obvious as seen in the example above: you can lose more than the actual amount invested.

    That's why the leverage scheme implemented doesn't kick in when the cash is gone: it kicks in from the very first moment to simulate keeping cash reserves in the account.



  • @backtrader said in Release 1.9.21.105:

    Scenario B

    Cash: 100,000
    Acquisition of a leveraged (2x) asset for 100,000. Because the leverage is 2x only 50,000 units are taken from the cash.
    50,000 monetary units remain available
    A non-leveraged asset can still be bought for 50,000 units.

    Lets agree on terms. What do you mean under leveraged (2x) asset in this scenario?


  • administrators

    The same as in Scenario A. Something that can be bought with a 2x leverage.

    The difference between Scenario A and Scenario B are not the assets. They are the same. But one of the assets can be bought using leverage, whilst the other is just a regular asset (aka leverage == 1.0)

    This is typical in the account which allow the acquisition of assets with leverage. For example, an international broker with access to several exchanges.

    • NYSE: Stocks can be bought with a 2x leverage
    • XETRA: being in other country (and continent) the broker only allows a 1x (ie: NO) leverage

    You should be able to by the same amounts of the NYSE stock and the XETRA stock regardless of the order.

    But you cannot buy the XETRA sotck in *Scenario B with depicted loan scheme, because the cash reserves are fully depleted before the loan kicks in (in the particular example there wouldn't even be a loan, because we buy 100,000 and that's exactly the cash level and the loan doesn't kick in)



  • Now I got it. I've never seen such separation on leveraged and non-leveraged stocks before in the backtesting software and at the broker level. What I've learned from my experience that it is an account condition: you are allowed to borrow money (reg T margin), or not allowed (cash only), independently from the stocks you are buying. At least for vast majority of common stocks and ETFs.

    I still have one concern about your implementation, but I need to run couple more tests. :)


  • administrators

    With "Reg T margin" it seems like if you had an Interactive Brokers account in mind.

    There you cannot deplete the cash reserves before the loan kicks in.

    Reg T Margin accounts in IB support futures and even if you can buy stocks with Xx leverage, the policies are completely different for futures (there you need to have the cash in the account to cover the continuous cash adjustment)

    This separation of assets is commonplace. Directly from IB, what's actually supported for Margin (or Porftfolio Margin accounts) See for example:

    US Stocks, Canadian Stocks, Hong Kong Stocks, Other Stocks and Special Margin Stocks have different leverage/margin rules. Notice that European (with 30+ countries), Central/South America, Australia, New Zealand and Asia are not mentioned. You could assume they all fall under the Other Stocks heading, but it is a matter of trial and error finding out if leverage is supported for a specific exchange (and it isn't for all)



  • Yes, that is IB. But I believe regulation T is common for all brokers in States. Also it is around the same in Russia. If you have no cash on account, take the loan based on your open position value. Is it different in Europe?


  • administrators

    @ab_trader The answer above was updated. Each broker/trading house will offer different rules based on how much their hands are tied by regulation and little things like if the risk can be covered (collateralized or passed on) by someone else.

    But the most common policy is to increase your buying power, because that's what allow having leverage levels over 2x which make sense (in Forex 40x can be found and 100x was common)



  • I was talking specifically about stocks/ETFs. For futures you have a commission scheme which close enough to reality, I don't think that leverage is necessary in this case.

    But in case of stocks and ETFs it is really weird to get leveraged value higher than un-leveraged value if you bought only on 50% of your cash and 50% of cash is still on your account. Any way it is your call, you think that it is how it should be, than it will be this way. Non-leveraged value accounts for increased price changes correctly and this is enough for me.

    By the way, how do you define the max value which can be used to buy smthng in case of leverage? (I believe it is equal to cash in case of leverage=1.0) I've ran couple tests, but didn't get clear understanding. I was able to buy more than remaining cash value but less then non-leveraged value.

    I can post an example, if it will be easier.


  • administrators

    You miss the point. It's not about leveraging futures. It is that you can buy futures at the same time. It's one of the assets for which you don't have extra Xx buying power (in spite of the calculated notional value which simply reflects the nature of the multiplier applied to the profit and loss) And Scenario A and B must work by having one product being a stock and the other future.

    The leveraged value isn't higher. It is the actual real value your account, because the broker has allowed you to buy with extra power (the loaned money).

    The unleveraged value simply tells you how much cash will be restored to your account once the operation is over. That's why the last version refines the calculation by removing unrealized profit and loss from the equation before un-leveraging, because the profit and loss will be added/deducted directly from your cash.



  • @backtrader Could you please clarify what is the max permissible value of the open position in case of Nx leverage? In case of 1x leverage it is cash.


  • administrators

    The leverage acts when you open a position and plays no role when the position is already open. For an open position one would have to consider what is the required cash margin the broker requires in the account for the given asset and issue a margin call if the cash reserves go below that.

    You are allowed to open a position for N times the amount of cash you have. This is the case in the btfd sample and blog post. This, being just a fact checking exercise, uses order_target_percent with a target=1.0 and no commissions to be 100% invested each and every time.

    That is of course unrealistic as shown in the continuation post which applies commissions.



  • checksubmit (default: True) check margin/cash before accepting an order into the system

    I set cerebro.broker.set_checksubmit(False). It didn't show any difference with the default behavior, Nx cash still limited size of the position bt going to open. My understanding was that with False I will be able to open unlimited position. Is it not true? What does checksubmit check?


  • administrators

    checksubmit controls if a submitted order can be accepted by the broker by simulating execution and with it checking if the current cash reserves are enough. It actually pseudo-executes all pending orders, because an order could actually be 1st opening a short and increasing the pseudo-cash reserves.

    But this is only pseudo-execution. When the actual execution takes place, the actual cash level is checked again and execution will be rejected if not possible.


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.