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

 

OpenOrder (With Risk Management, TP and SL):

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.



OpenOrder (With Risk Management, TP and SL)

Author: MQL4AutoTrading

 
  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 %.
 
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) 

 
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
 
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 :)
 
MQL4AutoTrading:

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.
 
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 :)
 
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 

 
Hi, I don't know how to code. But can you teach me how to code it so that it base it's SL at x1.5 ATR and TP at x1 ATR?
 
Marvz:
Hi, I don't know how to code. But can you teach me how to code it so that it base it's SL at x1.5 ATR and TP at x1 ATR?

Hi Marvz,

It is not easy to just teach that if you don't have coding basics, I would suggest to start from the basics first