Reply

TrailingStop Function – questionable

5 replies

Mike H.

Subscriber, bbp_participant, community, 76 replies.

Visit profile

6 years ago #112526

When I viewed the TrailingStop Function in the Editor, I notices that the code was identical

for both the BUY OrderType and the SELL OrderType.

 

I was wondering why my TrailingStop Rules for my SHORT TRADES were not activating…

 

Below shows how the EA Wizard writes the Function: (I underlined the segments in question) (notice the different MagicNumbers)

 

double getOrderTrailingStop(int orderMagicNumber, int orderType, double price) {
   double value = 0;
   if(orderMagicNumber == 2456) {
      value = ( 11 * getPointCoef(orderMagicNumber));
      if(value > 0) {
         if(orderType == OP_BUY || orderType == OP_BUYSTOP || orderType == OP_BUYLIMIT) {        
            value = Bid – value;
         } else {
            value = Ask + value;
         }
      }

 

The same with OrderBreakEven:
   }
   if(orderMagicNumber == 3798) {
      value = ( 11 * getPointCoef(orderMagicNumber));
      if(value > 0) {
         if(orderType == OP_BUY || orderType == OP_BUYSTOP || orderType == OP_BUYLIMIT) {        
            value = Bid – value;
         } else {
            value = Ask + value;
         }
      }
   }
   return(NormalizeDouble(value, Digits));
}
 

The Same with OrderBreakEven:

 

double getOrderBreakEven(int orderMagicNumber, int orderType, double price) {
   double value = 0;
   if(orderMagicNumber == 2456) {
      value = ( 7 * getPointCoef(orderMagicNumber));
      if(value > 0) {
         if(orderType == OP_BUY || orderType == OP_BUYSTOP || orderType == OP_BUYLIMIT) {
            value = Bid – value;
         } else {
            value = Ask + value;
         }
      }
   }
   if(orderMagicNumber == 3798) {
      value = ( 7 * getPointCoef(orderMagicNumber));
      if(value > 0) {
         if(orderType == OP_BUY || orderType == OP_BUYSTOP || orderType == OP_BUYLIMIT) {
            value = Bid – value;
         } else {
            value = Ask + value;
         }
      }
   }
   return(NormalizeDouble(value, Digits));
}
 

Dear Mark, Rather than having to wait for an EA Wizard Update, will you please show my how to make the changes in the Editor ?

 

??? Change BUY to SELL and value = ???

0

Mike H.

Subscriber, bbp_participant, community, 76 replies.

Visit profile

6 years ago #126086

Dear Mark, I couldn’t wait, so I already made the necessary changes…

 

   if(orderMagicNumber == 3978) {
      value = ( 11 * getPointCoef(orderMagicNumber));
      if(value > 0) {
         if(orderType == OP_SELL || orderType == OP_SELLSTOP || orderType == OP_SELLLIMIT) {
            value = Ask + value;
         } else {
            value = Bid – value;
 

   if(orderMagicNumber == 3798) {
      value = ( 0 * getPointCoef(orderMagicNumber));
      if(value > 0) {
         if(orderType == OP_SELL || orderType == OP_SELLSTOP || orderType == OP_SELLLIMIT) {
            value = Ask + value;
         } else {
            value = Bid – value;
 

 

The TrailingStop & BreakEven Functions work good now…

 

Later, Mike H.

0

Mark Fric

Administrator, sq-ultimate, 3 replies.

Visit profile

6 years ago #126096

Hello Mike,

 

I’m sorry, but if I’m not mistaken these codes are functionally equivalent.

 

if(orderType == OP_SELL || orderType == OP_SELLSTOP || orderType == OP_SELLLIMIT) {
   value = Ask + value;
} else {
   value = Bid – value;

}

 

is functionally as same as:

 

if(orderType == OP_BUY || orderType == OP_BUYSTOP || orderType == OP_BUYLIMIT) {

   value = Bid – value;
} else {
   value = Ask + value;
}

 

The If condition recognizes if the order is buy o sell order, and it uses the appropriate value. What you did is that you only changed the check, but it should do exactly the same.

 

So if it suddenly works maybe it was caused by something else than this.

Mark
StrategyQuant architect

0

Mike H.

Subscriber, bbp_participant, community, 76 replies.

Visit profile

6 years ago #126108

Hello Mark, The first post in this topic shows how the EA Wizard is outputting the code…

The EA Wizard outputs the code as follows:

 

double getOrderTrailingStop(int orderMagicNumber, int orderType, double price) {
   double value = 0;
   if(orderMagicNumber == 2456) {
      value = ( 11 * getPointCoef(orderMagicNumber));
      if(value > 0) {
         if(orderType == OP_BUY || orderType == OP_BUYSTOP || orderType == OP_BUYLIMIT) {        
            value = Bid – value;
         } else {
            value = Ask + value;
         }
      }

 

The same with OrderBreakEven:
   }
   if(orderMagicNumber == 3798) {
      value = ( 11 * getPointCoef(orderMagicNumber));
      if(value > 0) {
         if(orderType == OP_BUY || orderType == OP_BUYSTOP || orderType == OP_BUYLIMIT) {        
            value = Bid – value;
         } else {
            value = Ask + value;
         }
      }
   }
   return(NormalizeDouble(value, Digits));
}

 

What I see is that there is no Condition for SELL… The 2 IFs are identical…

Above is how the EA Wizard is writing the code >>> identical underlined IFs for different Magic Numbers…

0

Mark Fric

Administrator, sq-ultimate, 3 replies.

Visit profile

6 years ago #126147

yes, but the logic is correct. If the order is buy order it computes the price in one way, else (the order must be sell order then) the price is computed in another way.

 

 

 if(orderType == OP_BUY || orderType == OP_BUYSTOP || orderType == OP_BUYLIMIT) {  

  this means it is a buy order

  …

} else {

  this means it is a sell order

  …

}

 

There really doesn’t seem to be any error in the EA, it was tested on both sides.

Mark
StrategyQuant architect

0

Mike H.

Subscriber, bbp_participant, community, 76 replies.

Visit profile

6 years ago #126157

I see & understand it now… Thank You Mark !!!

0

Viewing 6 posts - 1 through 6 (of 6 total)