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

To add comments, please log in or register
Automated-Trading
148229
Automated-Trading  

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

Alain Verleyen
38390
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 %.
MQL4 Trading Automation
33814
Luca Spinello  
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) 

William Roeder
18933
William Roeder  
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
MQL4 Trading Automation
33814
Luca Spinello  
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 :)
Alain Verleyen
38390
Alain Verleyen  
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.
Semnomic
11
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 :)
MQL4 Trading Automation
33814
Luca Spinello  
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 

To add comments, please log in or register