Move stop to previous bar

 

Guys I am trying to move a stop to the previous high of te bar if a Sell and Low of a Bar if a Buy. I have written the following code. It compiles beautifully but does not work!!

Can anyone show me what I am doing wrong? Can anyone correct it?

   {
      if (OrderSelect(tradeTicket, SELECT_BY_TICKET) == true)
      {
         double newStopValue;

         tradeType   = OrderType();             // Type of selected order
         tradePrice  = OrderOpenPrice();        // Price of selected order
         tradeSL     = OrderStopLoss();         // SL of selected order
         tradeTP     = OrderTakeProfit();       // TP of selected order
         tradeLot    = OrderLots();             // Amount of lots
         lastTradeTime = OrderOpenTime();       // Order Open Time

         if (tradeType == OP_BUY)
         {
            newStopValue = iLow(0,0,1);

            if (tradeSL < newStopValue)
            {
               // Modify Order to tighten up the Stop Loss and lock in some Pips
               if (OrderModify(OrderTicket(), OrderOpenPrice(), newStopValue, tradeTP, 0) == FALSE)
               {
                  err=GetLastError();
                  Print(myMagicHex, ": Error adjusting StopLoss value of trade ", tradeTicket," to ", DoubleToStr(newStopValue, Digits-1)," (",err,")");
               }
               else
               {
                  tradeSL     = OrderStopLoss();         // SL of selected order
                  Print(myMagicHex, ": Modifed Buy order ",tradeTicket," Stop Loss: ", DoubleToStr(tradeSL, Digits)," Take Profit: ", DoubleToStr(tradeTP, Digits));
               }
            }
         }
         else if (tradeType == OP_SELL)
         {
            newStopValue = iHigh(0,0,1 + Ask - Bid);

            if (tradeSL > newStopValue)
            {
               // Modify Order to tighten up the Stop Loss and lock in some Pips
               if (OrderModify(OrderTicket(), OrderOpenPrice(), newStopValue, tradeTP, 0) == FALSE)
               {
                  err=GetLastError();
                  Print(myMagicHex, ": Error adjusting StopLoss value of trade ", tradeTicket," to ", DoubleToStr(newStopValue, Digits-1)," (",err,")");
               }
               else
               {
                  tradeSL     = OrderStopLoss();         // SL of selected order
                  Print(myMagicHex, ": Modifed Sell order ",tradeTicket," Stop Loss: ", DoubleToStr(tradeSL, Digits)," Take Profit: ", DoubleToStr(tradeTP, Digits));
               }
            }
         }
      }
      else
      {
         err=GetLastError();
         Print(myMagicHex, ": Error failed to select Trade ", tradeTicket," (",err,")");
      }
   }
}
 
Willforth :

Guys I am trying to move a stop to the previous high of te bar if a Sell and Low of a Bar if a Buy. I have written the following code. It compiles beautifully but does not work!!

Can anyone show me what I am doing wrong? Can anyone correct it?

Get into the habit of checking the documentation for the functions you are using . . . for example . . .

newStopValue = iLow(0,0,1);

newStopValue = iHigh(0,0,1 + Ask - Bid);

iLow() and iHigh()

The first parameter you need to pass is string symbol . . . not 0 and your iHigh() call, what bar is bar number (1 + Ask - Bid) ?

 

ooops. "Numpty" is a term that comes to mind or is it iNumpty (NULL,0,1)?

On a serious note, I am assuming I can use "NULL" in place of the string symbol if I want it to work on any currency pair so:-

iLow(NULL,0,1);

iHigh(NULL,0,1); +Ask-Bid;

 
iLow(NULL,0,1);  // Low[1]  Why not use the simpler
iHigh(NULL,0,1); // High[1] and more efficient predefineds
 
OK code now reads as follows but I am testing to see if it moves the stop by using a 1 minute timeframe. Stop is not moving. Basically all that is changed is the use of Low[1] and
         if (tradeType == OP_BUY)
         {
            newStopValue = Low [1];

            if (tradeSL < newStopValue)
            {
               // Modify Order to tighten up the Stop Loss and lock in some Pips
               if (OrderModify(OrderTicket(), OrderOpenPrice(), newStopValue, tradeTP, 0) == FALSE)
               {
                  err=GetLastError();
                  Print(myMagicHex, ": Error adjusting StopLoss value of trade ", tradeTicket," to ", DoubleToStr(newStopValue, Digits-1)," (",err,")");
               }
               else
               {
                  tradeSL     = OrderStopLoss();         // SL of selected order
                  Print(myMagicHex, ": Modifed Buy order ",tradeTicket," Stop Loss: ", DoubleToStr(tradeSL, Digits)," Take Profit: ", DoubleToStr(tradeTP, Digits));
               }
            }
         }
         else if (tradeType == OP_SELL)
         {
            newStopValue = High [1] + Ask - Bid;

            if (tradeSL > newStopValue)
            {
               // Modify Order to tighten up the Stop Loss and lock in some Pips
               if (OrderModify(OrderTicket(), OrderOpenPrice(), newStopValue, tradeTP, 0) == FALSE)
               {
                  err=GetLastError();
                  Print(myMagicHex, ": Error adjusting StopLoss value of trade ", tradeTicket," to ", DoubleToStr(newStopValue, Digits-1)," (",err,")");
               }
               else
               {
                  tradeSL     = OrderStopLoss();         // SL of selected order
                  Print(myMagicHex, ": Modifed Sell order ",tradeTicket," Stop Loss: ", DoubleToStr(tradeSL, Digits)," Take Profit: ", DoubleToStr(tradeTP, Digits));
               }
            }
         }
      }
      else
      {
         err=GetLastError();
         Print(myMagicHex, ": Error failed to select Trade ", tradeTicket," (",err,")");
      }
   }
}
High[1]
 

I think that especially when using M1 bars, you will need to check that the SL will be outside of the freeze range.


Not sure if it makes a difference or not but shouldn't there be an extra parameter in OrderModify

//From 
if (OrderModify(OrderTicket(), OrderOpenPrice(), newStopValue, tradeTP, 0) == FALSE)   //To
if (OrderModify(OrderTicket(), OrderOpenPrice(), newStopValue, tradeTP,0, 0) == FALSE)




/*A little bit of code from my project, may be some help, but I am only a novice. This is for a sell trade
I don't know why, but  MarketInfo(Symbol(),MODE_FREEZELEVEL) doesn't seem to work, so I settled on 5 pips.*/

if(StopLoss-Ask >= 5*Pip)  //Pip has already been calculated earlier,eg for EURUSD its value is 0.0001     
      OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss,OrderTakeProfit(),0,CLR_NONE);

//If price is too close to the new stop level,EA will close trade if/when Ask is above calculated stop level

if(Ask >= StopLoss)
      OrderClose(OrderTicket(),OrderLots(),Ask,30,CLR_NONE);
 
GumRai :

I think that especially when using M1 bars, you will need to check that the SL will be outside of the freeze range.

if(StopLoss-Ask >= 5*Pip)
Freeze level AND stop level.
   //{A TP or SL can be not closer to the order Price (open, limit, or stop)
   // or closing Price (filled order) than this amount. A pending order Price
   // can be no closer to the current Price than this amount. On IBFX it was
   //}equal to 30 (3.0 pips.) before ECN.
   double   minGapStop     = MathMax(2.*pips2dbl,
                              MarketInfo(market.pair, MODE_STOPLEVEL)*Point),
   //{If the current Price is closer than this to the TP, SL, (or pending
   // Price,) then the existing order can not be modified, closed, or deleted.
   // On IBFX it's equal to zero (in tester,) but I still can't change it if
   //}market is closer than minGapStop either.
            minGapChange   = MathMax(minGapStop,
                              MarketInfo(market.pair, MODE_FREEZELEVEL)*Point);
 
WHRoeder:
Freeze level AND stop level.

Sorry, missed this reply earlier.


Thanks for the excellent explanation. I can also only get a zero value for FREEZELEVEL which is why I opted for a set 5 pips.

I had no idea what STOPLEVEL does because Help only describes it as "Stop level in points."

Strange that Help gives quite a good explanation of FREEZELEVEL but a pretty useless explanation of STOPLEVEL

 
freezelevel is usually nonzero near news events. BOTH are in points. I've gotten (tester) errors when market is too close to TP/SL of the MAX of either.
Reason: