# Scripts: OpenOrder (With Risk Management, TP and SL)

148229

OpenOrder script allows you to open an order defining the % of balance to risk, the Stop Loss distance in pips and the Take Profit distance in pips and a magic number.

38390

if(UseRiskPercentage){
Lots=(AccountBalance()*RiskPercentage/100)/(StopLoss*nTickValue);
Lots=MathRound(Lots/MarketInfo(Symbol(),MODE_LOTSTEP))*MarketInfo(Symbol(),MODE_LOTSTEP);
}

This is not a correct way to calculate lot size from risk %.
33814

Alain Verleyen:
if(UseRiskPercentage){
Lots=(AccountBalance()*RiskPercentage/100)/(StopLoss*nTickValue);
Lots=MathRound(Lots/MarketInfo(Symbol(),MODE_LOTSTEP))*MarketInfo(Symbol(),MODE_LOTSTEP);
}

This is not a correct way to calculate lot size from risk %.

Hi Alain,

Thank you for the message, how would you calculate it?

That is the usual formula for the lot size calculation

Position size = ((account value x risk per trade) / pips risked)/ pip value per standard lot

I just used different brakets

Position size = (account value x risk per trade) / (pips risked * pip value per standard lot)

18933

double nTickValue=MarketInfo(Symbol(),MODE_TICKVALUE);
• Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around \$10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
• Do NOT use TickValue by itself - DeltaPerLot
• You must normalize lots properly and check against min and max.
• You must also check FreeMargin to avoid stop out
33814

whroeder1:
double nTickValue=MarketInfo(Symbol(),MODE_TICKVALUE);
• Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around \$10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
• Do NOT use TickValue by itself - DeltaPerLot
• You must normalize lots properly and check against min and max.
• You must also check FreeMargin to avoid stop out

Thanks whroeder1

• OSL is calculated considering the StopLoss value in pips so identical to (|OrderOpenPrice - OrderStopLoss|)
• Why not use TickValue? That is already considering the account currency and would make it easier to calculate
• Lots=MathRound(Lots/MarketInfo(Symbol(),MODE_LOTSTEP))*MarketInfo(Symbol(),MODE_LOTSTEP); that is the normalization. You're right I could check the min and max size and I would do that in a EA
• Yes free margin is a good idea, improvement for the next version :)
38390

Thanks whroeder1

• Why not use TickValue? That is already considering the account currency and would make it easier to calculate
Because, if you want something universal, you need to consider ticksize. Your code work only if 1 point = 1 tick. That's not always the case.
11

Hi nice script, im just learning and what i want to do seems simple (in my head) and your script is almost there...

Is there a way to make this trade exactly how you have it all set up but instead of setting a pip value for the STOP you set a % movement your willing to let the currency move by?

so say my intention is to risk 1% of my account on the EURUSD moving a max of 0.5% against me.

Hope that makes sense :)
33814

Semnomic:
Hi nice script, im just learning and what i want to do seems simple (in my head) and your script is almost there...

Is there a way to make this trade exactly how you have it all set up but instead of setting a pip value for the STOP you set a % movement your willing to let the currency move by?

so say my intention is to risk 1% of my account on the EURUSD moving a max of 0.5% against me.

Hope that makes sense :)

It makes sense but it doesn't :)

Do I understand well if I say, the open price is 100%, assume 1.06158, stop a buy when the price is at 99.5% (1.05627) or stop a sell if the price is 100.5% (1.06688)?

If that's the case it should be quite easy for you to achieve with some simple maths