Back

Calculating lot size for a Forex order based on portfolio value and leverage

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? 

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.


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.

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.


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? 

0

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

0

Still struggling with some of the constraints of the Forum software, keeping me from deleting or editing previous posts.  Lets try again:

Assuming that

  • I don't want to risk more than 1% of my portfolio value per trade 
  • my strategy only enters one position at any time
  • my strategy sets a stop loss of 30 pips below and a profit target of 20 pips above the current market price per trade
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? 

0

Hi Filib,

You can create a ratio to determine what portion of your margin you should use to limit your max loss to 1% for an entry at that price.

Max Loss for trade = ((pip * 30)/price) * orderSize
Max Loss for Portfolio = 0.01 * margin

# We can set those equal to restrict our Max Loss for a trade to be at most 1% of our portfolio margin.
((pip * 30)/price) * orderSize = 0.01 * margin

# This implies that
orderSize = (0.01 * margin * price) / (30 * pip)

 

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.


Thanks a lot! (pun intended....) 

1

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