TP/SL(EA): Can SL/TP be hardcoded into EA without parameters?

 

Hi,

As my thread title implies I would like to know if TP/SL can be hard-coded into an existing EA.

Currently, a trade is opened and then it is modified.

Thanks.

David.

 
davefx:

Hi,

As my thread title implies I would like to know if TP/SL can be hard-coded into an existing EA.

Currently, a trade is opened and then it is modified.

Thanks.

David.


Not that it is desirable to hard code anything, but yes. The Sky is the limit.

 
Enrique Dangeroux:

Not that it is desirable to hard code anything, but yes. The Sky is the limit.

I ask only because of pic related.
Files:
Untitled.png  130 kb
 

Send me mq4 file so I can check it out for you.

 
Anton Nel:

Send me mq4 file so I can check it out for you.

Files:
SA_EA_600.mq4  20 kb
 

These parameters need hardcoding:

extern int    ActionPips     = 1;
extern int    TargetPips     = 1;
extern int    StopPips        = 150;

 
davefx: These parameters need hardcoding:
extern int    ActionPips     = 1;
extern int    TargetPips     = 1;
extern int    StopPips        = 150;
  1. When you post code please use the SRC button! Please edit your post.
               General rules and best pratices of the Forum. - General - MQL5 programming forum
  2. So do it; remove the extern keyword. What's your problem?
 

Sometimes it will close out the trade very quickly without modify TP and SL. Other time it does modify with two options ~ MathMin/MathMax (TP or STOPLEVEL). '

Copy and paste from the original code.

               if((CurrentPips>=TargetPips || CurrentPips<=-StopPips) && IsTradeContextBusy()==false)
                 {
                  if(OrderType()==OP_BUY)
                     OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(dBid,Digits), Slippage*MyPoint/Point, Blue);
                  else
                     OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(dAsk,Digits), Slippage*MyPoint/Point, Red);
                 }
               else  
               if(VirtualStops==false && OrderStopLoss()<Point && IsTradeContextBusy()==false)
                 {
                  double MinDistance=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point+MyPoint;
                  if(OrderType()==OP_BUY)
                     OrderModify(OrderTicket(),OrderOpenPrice()
                                ,MathMin(OrderOpenPrice()-StopPips*MyPoint,dBid-MinDistance)
                                ,MathMax(OrderOpenPrice()+TargetPips*MyPoint,dAsk+MinDistance),0); 
                  else  
                     OrderModify(OrderTicket(),OrderOpenPrice()
                                ,MathMax(OrderOpenPrice()+StopPips*MyPoint,dAsk+MinDistance)
                                ,MathMin(OrderOpenPrice()-TargetPips*MyPoint,dBid-MinDistance),0); 
                 }
 
Anton Nel: Sometimes it will close out the trade very quickly without modify TP and SL. Other time it does modify with two options ~ MathMin/MathMax (TP or STOPLEVEL).
  1. Use the debugger or print out your variables, including _LastError and find out why.
  2.                      OrderClose(OrderTicket(), OrderLots(), ...);
                      else
                         OrderClose(OrderTicket(), OrderLots(), ...);
                     }
                   else if(...){
                      double MinDistance=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point+MyPoint;
                      if(OrderType()==OP_BUY)
                         OrderModify(OrderTicket(),OrderOpenPrice() ...); 
                      else  
                         OrderModify(OrderTicket(),OrderOpenPrice() ...); 
    Check your return codes for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. if(OrderType()==OP_BUY)
                         OrderModify(OrderTicket(),OrderOpenPrice()
                                    ,MathMin(OrderOpenPrice()-StopPips*MyPoint,dBid-MinDistance)
                                    ,MathMax(OrderOpenPrice()+TargetPips*MyPoint,dAsk+MinDistance),0); 
                      else  
                         OrderModify(OrderTicket(),OrderOpenPrice()
                                    ,MathMax(OrderOpenPrice()+StopPips*MyPoint,dAsk+MinDistance)
                                    ,MathMin(OrderOpenPrice()-TargetPips*MyPoint,dBid-MinDistance),0); 
    You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)
 
Anton Nel:

Sometimes it will close out the trade very quickly without modify TP and SL. Other time it does modify with two options ~ MathMin/MathMax (TP or STOPLEVEL). 

This is the exact problem I am having. Trades are opened and closed too damn quickly without modifying TP and SL.

Question: Can it just open a trade at market price with hardcoded SL("StopPips = 5"), TP("TargetPips = 1") and our trigger: ("ActonPips = 1")  ???

 
davefx:

This is the exact problem I am having. Trades are opened and closed too damn quickly without modifying TP and SL.

Question: Can it just open a trade at market price with hardcoded SL("StopPips = 5"), TP("TargetPips = 1") and our trigger: ("ActonPips = 1")  ???


You need to ask one of freelances to help you. It can be fixed.

Reason: