I am currently trying to understand the calculation of Forex lot sizes based on portfolio value and leverage.
In the following, highly simplified example
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
### <summary>
### lot size calculation
### </summary>>
class LotSizeCalculation(QCAlgorithm):
def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
self.SetStartDate(2015, 12, 1) #Set Start Date
self.SetEndDate(2015, 12, 1) #Set End Date
self.SetCash(5000) #Set Strategy Cash
self.AddForex("EURUSD", Resolution.Minute, Market.Oanda)
self.SetBrokerageModel(BrokerageName.OandaBrokerage);
def OnData(self,data):
portfolioValue = self.Portfolio.TotalHoldingsValue
orderSize = #?
self.MarketOrder("EURUSD",orderSize)
How would I calculate orderSize such that it is the maximum possible value based on the portfolio value and maximum leverage?
Rahul Chowdhury
Hey Filib,
First we need to know how much leverage we have at our disposal. One way to access the available leverage is through the Security object for the symbol we want to trade, leverage = self.Securities["EURUSD"].Leverage. The default leverage for Forex is 50.
Next, we want to know the margin we have available to use, this can be accessed through margin = self.Portfolio.MarginRemaining.The total buying power we have in USD is margin * leverage.
To find how many EUR we can purchase, let's divide by the current exchange rate.
leverage = self.Securities["EURUSD"].Leverage
margin = self.Portfolio.MarginRemaining
orderSize = margin * leverage / self.Securities["EURUSD"].Close
You can also use self.SetHoldings(symbol, 50) to enter the largest possible position,
or if you want to calculate the maximum quantity, you can use self.CalculateOrderQuantity(symbol, 50)
Note: Being fully levered makes you highly susceptible to margin calls.
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.
Filib Uster
Thanks a lot for your answer, Rahul Chowdhury !
Since you mentioned the risk of margin calls, let's add a risk factor and stopLoss/profitTargets to the calculation.
Assuming that I don't want to risk more than 1% per trade and my strategy sets a stop loss of 30 pips below and a profit target of 20 pips above the current market price:
price = self.Securities["EURUSD"].Close pip = self.Securities["EURUSD"].SymbolProperties.MinimumPriceVariation leverage = self.Securities["EURUSD"].Leverage margin = self.Portfolio.MarginRemaining risk = 0.01 stopLoss = price - pip * 30 profitTarget = price + pip * 20 orderSize = ?
How would I calculate the orderSize in this case?
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.
Filib Uster
Sorry, could not edit anymore, but obviously, in the previous post I should have said
Assuming that I don't want to risk more than 1% of my portfolio value per trade
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.
Filib Uster
Still struggling with some of the constraints of the Forum software, keeping me from deleting or editing previous posts. Lets try again:
Assuming that
price = self.Securities["EURUSD"].Close pip = self.Securities["EURUSD"].SymbolProperties.MinimumPriceVariation leverage = self.Securities["EURUSD"].Leverage margin = self.Portfolio.MarginRemaining risk = 0.01 stopLoss = price - pip * 30 profitTarget = price + pip * 20 orderSize = ? MarketOrder("EURUSD", orderSize); StopLimitOrder("EURUSD", -orderSize, profitTarget, stopLoss);
How would I calculate orderSize in this case?
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.
Filib Uster
Thanks a lot! (pun intended....)
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.
Shile Wen
Hi Zach,
The algorithm has many technical and stylistic issues, and since it seemed to be a popular algorithm, we've rewrote it to use the SymbolData pattern, replaced the History calls with RollingWindows, and fixed many stylistic issues. Please view the updated algorithm in the attached backtest.
Best,
Shile Wen
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.
Zach Oakes
Thanks ! It's very cool. It's like an MR take on Trend -- brilliant interpretation, and MUCH nicer than my translation.
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.
Mohamed Ajmal
How can we implement stoploss / reduce drawdown in this algorithm?
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.
Varad Kabade
Hi Mohamed Ajmal,
In the last backtest attached by Shile he has implemented the stop-loss which is triggered every day after 10 minutes of market open:
Refer to the following code snippet.
Best,
Varad Kabade
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.
Andres Arizpe
This appears to be a very interesting algo.
Is there any documentation I might use to get a basic understanding of it?
Cheers,
Andres
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.
Varad Kabade
Hi Andres Arizpe,
The above algorithm consists of components like the scheduled events, rolling window, and standard python libraries like numpy and pandas. We recommend going through the following docs[1, 2], and regarding the libraries, please look for their homepage/documentation. Please feel free to ask any specific doubts about the above algorithm.
Best,
Varad Kabade
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!