Shift stoploss EA problem

 
//----Buy entry------     
 
    if((LastFastMovingAverage <LastSlowMovingAverage)&&(FastMovingAverage > SlowMovingAverage)&&(Trend1D>0.0))
      {
      signal="Buy";
      }
     
    if (signal=="Buy" && OrdersTotal()==0)
    OrderSend (_Symbol,OP_BUY,1.00,Ask,3,Ask-600*Point,Ask+2050*Point,NULL,0,0,Green);
    
    if(OrdersTotal()>0)
     {
       for(int i=0; i<OrdersTotal();i++)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
         if(OrderType()==OP_BUY)
           {
             if(Bid>= OrderOpenPrice()+Take_Profit_in_Points*Point_Value)  
               {
                if(OrderStopLoss()<Ask-(600*Point))
                 {
                   OrderModify(OrderTicket(),OrderOpenPrice(),Ask-(300*Point),OrderTakeProfit(),0,clrNONE);
                 }
               }
           }          
         }
      }

Hi everyone, I planned to move my stoploss from 60 pips to 30 pips if the price has 100pips profit. I already did the code but when i use the strategy tester it doesn't work. Can anyone help me to solve the problem?

 
Tan Zhi:

Hi everyone, I planned to move my stoploss from 60 pips to 30 pips if the price has 100pips profit. I already did the code but when i use the strategy tester it doesn't work. Can anyone help me to solve the problem?

ypu dont specify any variable for OrderModify output

Check the output...

 
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      double Open_Price_Of_Buy_Order    =0;
      double Point_Value =0;
      string signal="";
    
      double SlowMovingAverage = iMA(NULL,0,100,0,MODE_SMMA,PRICE_CLOSE,0);
      double LastSlowMovingAverage =iMA(NULL,0,100,0,MODE_SMMA,PRICE_CLOSE,1);
      double FastMovingAverage=iMA(NULL,0,20,0,MODE_SMMA,PRICE_CLOSE,0);
      double LastFastMovingAverage =iMA(NULL,0,20,0,MODE_SMMA,PRICE_CLOSE,1);

      
      double MA60m=iMA(_Symbol,PERIOD_M1,60,0,MODE_SMA,PRICE_CLOSE,0); //moving average for 60min
      double OldMA60m=iMA(_Symbol,PERIOD_M1,60,0,MODE_SMA,PRICE_CLOSE,5); //moving average for 60min in previous 5 candles
      double Trend1D=MA60m-OldMA60m;
      
    
      
//----Buy entry------     
 
    if((LastFastMovingAverage <LastSlowMovingAverage)&&(FastMovingAverage > SlowMovingAverage)&&(Trend1D>0.0))
      {
      signal="Buy";
      }
     
    if (signal=="Buy" && OrdersTotal()==0)
    OrderSend (_Symbol,OP_BUY,1.00,Ask,3,Ask-600*Point,Ask+2050*Point,NULL,0,0,Green);
    
    if(OrdersTotal()>0)
     {
       for(int i=0; i<OrdersTotal();i++)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
         if(OrderType()==OP_BUY)
           {
             if(Bid>= OrderOpenPrice()+Take_Profit_in_Points*Point_Value)  
               {
                if(OrderStopLoss()<Ask-(600*Point))
                 {
                   OrderModify(OrderTicket(),OrderOpenPrice(),Ask-(300*Point),OrderTakeProfit(),0,clrNONE);
                 }
               }
           }          
         }
      }
        
    

//----Sell entry------  
    
    if((LastFastMovingAverage >LastSlowMovingAverage)&&(FastMovingAverage < SlowMovingAverage)&&(Trend1D<0.0))
     {
      signal="Sell";
     }  
    if (signal=="Sell" && OrdersTotal()==0)
    OrderSend (_Symbol,OP_SELL,1.00,Bid,3,Bid+600*Point,Bid-2050*Point,NULL,0,0,Red);
   
  }
//+------------------------------------------------------------------+

This is my full code. How do I specify variable? as in how to code it? because im still new in coding not really know what suppose to do.
 
Tan Zhi: This is my full code. How do I specify variable? as in how to code it? because im still new in coding not really know what suppose to do.
  1. double SlowMovingAverage = iMA(NULL,0,100,0,MODE_SMMA,PRICE_CLOSE,0);

    Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2.     OrderSend (_Symbol,OP_BUY,1.00,Ask,3,Ask-600*Point,Ask+2050*Point,NULL,0,0,Green);
        ⋮
        OrderSend (_Symbol,OP_SELL,1.00,Bid,3,Bid+600*Point,Bid-2050*Point,NULL,0,0,Red);

    Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum 2012.05.20
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 25 March 2014

  3. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    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.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

 

1. Im sorry that i posted at the wrong place. Will post in the correct place next time.

2. There is no any error for the order modify but i doesn't function when i tested, so i wonder how should i code would be correct. If you could show me the example or help me to amend it then would be great. I could learn from that.

3. Thanks for the info, i totally get what you meant. Will change for it.

William Roeder:
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum 2012.05.20
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 25 March 2014

  3. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    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.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).