I know my entry price and my Stop Loss price. I want to when stop loss will be hit then i always loose for example 10USD.
I followed discussion here (link bellow) but it didnt worked for me. Orders sizes always too small
https://www.quantconnect.com/forum/discussion/7442/calculating-lot-size-for-a-forex-order-based-on-portfolio-value-and-leverage/p1
I managed to make code to work almost like i want but results are just approximate. Also i am sure i overcomplicated things.
How calculate order size based on SL price and wanted amount to loose?
private decimal GetNewMinOrderQuantityForex(int desiredAmountToLose, decimal entryPrice, decimal stopOrTpPrice, decimal minPriceVariation, decimal margin, decimal leverage)
{
if (entryPrice == 0) return 0;
var maxOrderSize = margin * leverage / entryPrice;
if (minPriceVariation == 0 || entryPrice == 0 || minPriceVariation == 0) return 0;
const int multiplier = 10;
var pipsDiffSl = (entryPrice - stopOrTpPrice) / minPriceVariation / multiplier;
var pipPrice = minPriceVariation / entryPrice * maxOrderSize * multiplier;
var slValueInMoney = pipsDiffSl * pipPrice;
//const int desiredAmoutnToLose = 10;
var newPipValue = desiredAmountToLose * pipPrice / slValueInMoney;
var newMinimalOrderSize = (newPipValue * entryPrice) / (minPriceVariation * multiplier);
newMinimalOrderSize = Math.Round(newMinimalOrderSize, 0);
return newMinimalOrderSize;
}
Shile Wen
Hi Valentas,
The formula for orders based on absolute dollar risk is:
num orders = Dollar Risk / (Conversion To USD Rate * abs(Entry Price - Stop Loss Price)).
This may mean we would need to add extra pairs if neither is in USD, e.g. EURJPY. For EURJPY, dividing the value of EURJPY by USDJPY would give us the conversion rate to USD.
Best,
Shile Wen
Valentas
Works great.
I should read more book probably :).
Valentas
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!