Such a sample already exists. It is ATR-Based
(Of course, it is included in the sources)
Such a sample already exists. It is ATR-Based
(Of course, it is included in the sources)
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.
<error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:usfarm> notifications are OK. IB chose to notify that the status of the different data server through errors
21xx. The text in
errMsg is what's actually makes the diffference, but in this case it says
There shouldn't be any need to use
adddata, as you have already seen in the sample.
From the trace, the error can be tracked down to:
dt0 = min((d for i, d in enumerate(dts) ValueError: min() arg is an empty sequence
The entire code fragment which is in play:
# Get index to minimum datetime if onlyresample or noresample: dt0 = min((d for d in dts if d is not None)) else: dt0 = min((d for i, d in enumerate(dts) if d is not None and i not in rsonly))
Your code fails in the 2nd part which is for cases in which no resampling takes place or a mix of
replay takes place.
In any case, one can only be in that code fragment if one of the existing data feeds has reported to have something to deliver.
This is only a guess, because there is no description in your post as to what you are actually doing (why you don't get LIVE is part of the unknown configuration)
@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)
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)
The same as in Scenario A. Something that can be bought with a
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.
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)
It doesn't give you
0, it gives you remaining cash. See the logic with the extreme use case in which the cash is
100,000and leverage `2x``
2000units at price
100for a total of:
200,000. Because of the
50, which means an unrealized loss of:
-50 x 2000 = -100,000
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
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.
Before considering a new release
- 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.
- 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
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.
- 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.
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
Let's put another scenario in play:
2xleverage) can be bought for a total of 100,000 whilst still retaining the non-leveraged asset in the portfolio.
This is the same as:
2x) asset for 100,000. Because the leverage is
50,000units are taken from the cash.
Both A and B are equivalent because the act of acquiring a leveraged asset is considered as having the ability to buy with
There isn't at the moment