Back

Forex and leverage

This has been brought up previously, however I'm still struggling to set the leverage for Forex.

I'm using 

SetHoldings(pair1, 1);

to enter and exit trades.

When setting the leverage in AddForex:

AddForex(pair1, Resolution.Hour, Market.FXCM, leverage:10.0M);

or with:

Securities[pair1].SetLeverage(10.0M);

The results seem to be the same.

However, if I use a higher portfolio percentage in SetHoldings my position size is scaled up, but I'm not sure what the interaction is between this and the set leverage.

 

So my two questions are:

  1. Do Forex securities still default to a leverage of 50x ? (This was mentioned in a previous discussion), and if so, in which file in the LEAN code is this set ? (To check if this changes in the future)
  2. How to properly set the leverage for Forex?

Any help would be appreciated :)

(I'm attaching a minimal working example where I'm playing around with this)

 

Update Backtest








The value you enter in SetHoldings doesn't take leverage into account. In order to use the full 10x, you'd want to do this instead:

SetHoldings(pair1, 10);

You may also be able to pull the leverage for that pair automatically, but I haven't tried it. I'd guess it would look like this?

SetHoldings(pair1,(double)Securities[pair1].Leverage);

Another option might be to skip SetHoldings, and calculate the order size based on Portfolio.MarginRemaining:

int Quantity = (int)(Portfolio.MarginRemaining / Securities[pair1].Price);
MarketOrder(pair1, Quantity);

 

2

Thanks, the link cleared it up. I'm actually glad that leverage isn't used implicitily, I'm guessing that setting the leverage is mostly used for the margin calculations.

0

Let me answer the two questions:
Yes, the default leverage is set to 50 at the Forex class. Instead of monitoring code changes, we can rely on the GetLeverage method:

// In Initialize
var forex = AddForex("EURUSD");
var leverage = forex.MarginModel.GetLeverage(forex);

We have three alternative methods to set the leverage of subscribed assets:
1. Modify the target leverage before we subscribe to them: SetSecurityInitializer 

2 .Set it when we subscribing to them: AddSecurity, Add Forex, AddEquity, etc...

3. Modify it after we subscribing to them: ISecurity.SetLeverage

Here is a simple example on how to use SetSecurityInilializer:

// In Initialize
SetSecurityInitializer(new CustomSecurityInitializer());
var forex = AddForex("EURUSD");

// Create CustomSecurityInitializer:
public class CustomSecurityInitializer : ISecurityInitializer
{
public void Initialize(Security security)
{
// Set leverage to 10
security.SetLeverage(10m);
}
}
1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


I was also asking about the code just to familiarize myself with the API as well. I take it the functions mentioned in (2) SetLeverage of the security, so the result is equivalent?

@Alexandre, rather than opening another discussion, I have another question rather the time of forex securities. Damien mentions here that the timezone is EST, and Jared mentions here that the timezone is GMT. I'm assuming that the timezone is determined by the data? Also, is there a function or property to get the timezone of the data?

Thanks!

0

Yes, the results from the 3 methods I have enumerated are equivalent.

The timezone is defined by the exchange the asset is traded at.
In Forex' case, the timezone currently is EST, because we are referring to the US session to be consistent with our supported brokerages and/or data providers.

1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed