Trailingstop EA won't work

 

Hi Everyone.  I cannot get my EA to work properly.  I am running it on 10 different pairs/charts simultaneously.  There seem to be two problems:

1.  It does not trigger the Trailingstop most of the time.  Sometimes it works, though.  I think the problem lies with my OrderSelect, but cannot resolve it.

2.  It sometimes gives me Error 130, but I cannot see how my SL/TP can be invalid.  All the values that I am printing are above the stoplevel for all pairs.  And even though I get error 130, it actually sometimes does modify the order as if nothing is wrong.

Here is the whole EA.  Will you please have a look at it and let me know what the problem might be?

#property strict;
extern string Label_TrailingStart="Pip threshold to activate TrailingStop";
extern int TrailingStart=11;
extern string Label_TrailingStop="Pips trailing behind";
extern int TrailingStop=5;
double stoplevel=(MarketInfo(Symbol(),MODE_STOPLEVEL))/10;
int TS=TrailingStart-TrailingStop-1;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init(){
   if(TS<stoplevel){
      MessageBox("Please note: Your inputs for TrailingStart and/or TrailingStop cannot"+
                 "\nbe less than the minimum levels required by your broker and the"+
                 "\nTrailingStart has been increased automatically to "+StringConcatenate(stoplevel+TrailingStop+1)+" Pips");
     }
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit(){
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start(){
   double Pip=Point*10;
   if(TS<stoplevel) TrailingStart=(int)stoplevel+TrailingStop+1;
   
   Print("stoplevel = ",stoplevel);
   Print("TrailingStart = ",TrailingStart);
   Print("TrailingStop = ",TrailingStop);
   Print("TS = ",TS);
   
   for(int i=OrdersTotal()-1; i>=0; i--)
      if(OrderSelect(i,SELECT_BY_POS)){
         if(OrderSymbol()!=Symbol()) continue;{
            if(OrderType()==OP_BUY){
               if(OrderSelect(OrderTicket(),SELECT_BY_TICKET)){
                  if(Bid-OrderOpenPrice()>TrailingStart*Pip){
                     if(OrderStopLoss()<Bid-TrailingStop*Pip){
                        RefreshRates();
                        if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*Pip,OrderTakeProfit(),Blue))
                        Print("Buy = ",GetLastError());
                        Print("Bid = ",Bid);
                        Print("Bid-TrailingStop*Pip = ",Bid-TrailingStop*Pip);
                        Print("TrailingStart*Pip = ",TrailingStart*Pip);
                        }     
                    }  
                }     
            }  
            if(OrderType()==OP_SELL){
               if(OrderSelect(OrderTicket(),SELECT_BY_TICKET)){
                  if(OrderOpenPrice()-Ask>TrailingStart*Pip){
                     if(OrderStopLoss()>Ask+TrailingStop*Pip){
                        RefreshRates();
                        if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStop*Pip,OrderTakeProfit(),Red))
                        Print("Sell = ",GetLastError());
                        Print("Ask = ",Ask);
                        Print("Ask+TrailingStop*Pip = ",Ask+TrailingStop*Pip);
                        Print("TrailingStart*Pip = ",TrailingStart*Pip);
                        }
                    }
                }  
            }
         }
      }   
  return(0);
}
//+------------------------------------------------------------------+
 
  1. You have several misaligned code blocks due to parenthesis that are missing. Although balanced, they are missing, such as at the beginning of the "for" and a few of the "if" blocks
  2. The line "if(OrderSymbol()!=Symbol()) continue;{" is very suspect and makes it difficult to see how the code flow should be. Just make it "if(OrderSymbol()==_Symbol) {". Instead of "Symbol()", you can also use "_Symbol"
  3. Use "OrderClosePrice()" and not "Ask" or "Bid".
  4. Add parenthesis when doing comparisons to make it easier to read as well as guarantee the order of things. For example instead of "if(Bid-OrderOpenPrice()>TrailingStart*Pip)" use "if( ( OrderClosePrice() - OrderOpenPrice() ) > ( TrailingStart * Pip ) )"
  5. Try to merge both the Buy and Sell code blocks into a single code block that handles both. That will make it less code to debug and easier to read and modify in the future.

PS! NB! Also make use of a Magic Number for the Orders!

PS! Also remember to account for slippage that can cause the 130 errors. A minimum of StopLevel alone is not enough. Make it slightly larger to account for possible slippage such as adding a factor of the current spread. There may also be a FreezeLevel in place by the Broker!

 
FMIC:
  1. You have several misaligned code blocks due to parenthesis that are missing. Although balanced, they are missing, such as at the beginning of the "for" and a few of the "if" blocks
  2. The line "if(OrderSymbol()!=Symbol()) continue;{" is very suspect and makes it difficult to see how the code flow should be.
    Just make it "if(OrderSymbol()==_Symbol) {". Instead of "Symbol()", you can also use "_Symbol"
  3. Use "OrderClosePrice()" and not "Ask" or "Bid".
  4. Add parenthesis when doing comparisons to make it easier to read as well as guarantee the order of things:
    For example instead of "if(Bid-OrderOpenPrice()>TrailingStart*Pip)" use "if( ( OrderClosePrice() - OrderOpenPrice() ) > ( TrailingStart * Pip ) )"
  5. Try to merge both the Buy and Sell code blocks into a single code block that handles both. That will make it less code to debug and easier to read and modify in the future.

PS! NB! Also make use of a Magic Number for the Orders!

PS! Also remember to account for slippage that can cause the 130 errors. A minimum of StopLevel alone is not enough. Make it slightly larger to account for possible slippage such as adding a factor of the current spread. There may also be a FreezeLevel in place by the Broker!

Thank you so much for your reply and for trying to help me.  I have a few questions/comments:

1.  In #1 you suggested that I add parenthesis (), which I did as per your suggestion in #4.  But I do not see how I can add parenthesis () at the beginning of the "for".  Will you please explain this or did you mean curly brackets {}?

2.  I have changed it to _Symbol, but to improve my knowledge I was wondering if you can please explain what the benefit/advantage is to using _Symbol over Symbol()?

3.  I have changed all the Asks and Bids to OrderClosePrice(), however, will you please explain what the benefit/advantage of this is?

4.  I did this

5.  I will try and see how I can simplify this more.

6.  Since I enter my trades manually, it's my understanding that I cannot use MagicNumbers.  Is this not true?

7.  I do not see how or where I can account for slippage.  Will you please tell me where or how to add this.  For OrderModify it only allows for the following parameters:

bool  OrderModify(
   int        ticket,      // ticket
   double     price,       // price
   double     stoploss,    // stop loss
   double     takeprofit,  // take profit
   datetime   expiration,  // expiration
   color      arrow_color  // color
   );

 8.  In my code I already added 1 pip (10 points) to the minimum stoplevel, which should account for spread in most cases.

9.  I have printed the Freezelevel for all the pairs and they are all 0.0, so does not seem to be an issue.

10.  Even though I am still getting error 130 and error one, when the EA does work, it still modifies and closes the order.  So as per these results, I'm getting the error as the order is being modified at the same time.  As if its a partime problem:

 2016.04.15 13:11:06.183 Trailing_v18 USDCAD,M15: modify #50202284 buy 0.01 USDCAD at 1.28477 sl: 1.28598 tp: 0.00000 ok

2016.04.15 13:11:05.860 Trailing_v18 USDCAD,M15: Buy = 130

2016.04.15 13:10:58.939 Trailing_v18 USDCAD,M15: modify #50202284 buy 0.01 USDCAD at 1.28477 sl: 1.28597 tp: 0.00000 ok

2016.04.15 13:10:57.835 Trailing_v18 USDCAD,M15: modify #50202284 buy 0.01 USDCAD at 1.28477 sl: 1.28596 tp: 0.00000 ok

2016.04.15 13:10:56.974 Trailing_v18 USDCAD,M15: Buy = 130

2016.04.15 13:10:56.531 Trailing_v18 USDCAD,M15: Buy = 130

2016.04.15 13:10:56.263 Trailing_v18 USDCAD,M15: modify #50202284 buy 0.01 USDCAD at 1.28477 sl: 1.28590 tp: 0.00000 ok

2016.04.15 13:10:54.318 Trailing_v18 USDCAD,M15: modify #50202284 buy 0.01 USDCAD at 1.28477 sl: 1.28582 tp: 0.00000 ok

2016.04.15 13:10:53.175 Trailing_v18 USDCAD,M15: modify #50202284 buy 0.01 USDCAD at 1.28477 sl: 1.28581 tp: 0.00000 ok

2016.04.15 13:10:52.879 Trailing_v18 USDCAD,M15: modify #50202284 buy 0.01 USDCAD at 1.28477 sl: 1.28577 tp: 0.00000 ok

2016.04.15 13:10:51.810 Trailing_v18 USDCAD,M15: Buy = 130

2016.04.15 13:10:51.085 Trailing_v18 USDCAD,M15: modify #50202284 buy 0.01 USDCAD at 1.28477 sl: 1.28575 tp: 0.00000 ok

2016.04.15 13:10:50.744 Trailing_v18 USDCAD,M15: Buy = 130

2016.04.15 13:10:50.020 Trailing_v18 USDCAD,M15: Buy = 1

2016.04.15 13:10:50.020 Trailing_v18 USDCAD,M15: modify #50202284 buy 0.01 USDCAD at 1.28477 sl: 1.28571 tp: 0.00000 ok

Here is another example of where the Trailingstop is clearly above the stoplevel (by 10 pips/100 points) and yet it still produces error 130:

2016.04.15 14:15:03.432 Trailing_v18 EURAUD,M15: Buy error = 130

2016.04.15 14:15:03.432 Trailing_v18 EURAUD,M15: TS = 20

2016.04.15 14:15:03.432 Trailing_v18 EURAUD,M15: TrailingStop = 5

2016.04.15 14:15:03.432 Trailing_v18 EURAUD,M15: TrailingStart = 25

2016.04.15 14:15:03.432 Trailing_v18 EURAUD,M15: stoplevel = 10.0



 

Please supply most recent code with the updates you have made. Otherwise I cannot comment on recent changes made by you to see if they are correct.

  1. Sorry, I meant curly brackets. You have to fix that, or else it will continue to fail. This is the most important of all the points.
  2. a) This point is not primarily about the "_Symbol", but instead about the "!=" and the "continue".
    b) "_Symbol" is executed faster because it is a variable, while "Symbol()" would be execute as a function call which is slower.
  3.  OrderClosePrice() is the order's current closing price. If still open, it would automatically report the Bid or Ask depending on the order type. Please read the docs for this function.
  4. OK! Lets see the code changes you made.
  5. OK! Lets see the code changes you made.
  6. Yes, for manual trades, the magic number is 0. However, in your code define the Magic Number as an "extern" or "input", assigning it a "0" as the default, and then use that variable in your code. That way you can easily use the EA for other situations or reuse your code for other EAs.
  7. I did explain how to account for slippage. Make sure that the "TrailStop > ( StopLevel + ( CurrentSpread * Factor ) )" where Factor is at least "1.0" (ideally "1.5" or "2.0"; greater if you see that you still experience Error 130 due to excessive slippage).
  8. There is no guarantee that 10 points would account for spread, because spread is different depending on Symbol and is always changing, especially during news events and when liquidity is low such as during off hours.
  9. OK! If "FreezeLevel" is not used that is OK, but I suggest that you still cater for it in your code, so that in case you use it with a different broker (that might have it). the EA will still work.
  10. Supply new code with changes so that it can be analysed.
 
FMIC:

Please supply most recent code with the updates you have made. Otherwise I cannot comment on recent changes made by you to see if they are correct.

  1. Sorry, I meant curly brackets. You have to fix that, or else it will continue to fail. This is the most important of all the points.
  2. a) This point is not primarily about the "_Symbol", but instead about the "!=" and the "continue".
    b) "_Symbol" is executed faster because it is a variable, while "Symbol()" would be execute as a function call which is slower.
  3.  OrderClosePrice() is the order's current closing price. If still open, it would automatically report the Bid or Ask depending on the order type. Please read the docs for this function.
  4. OK! Lets see the code changes you made.
  5. OK! Lets see the code changes you made.
  6. Yes, for manual trades, the magic number is 0. However, in your code define the Magic Number as an "extern" or "input", assigning it a "0" as the default, and then use that variable in your code. That way you can easily use the EA for other situations or reuse your code for other EAs.
  7. I did explain how to account for slippage. Make sure that the "TrailStop > ( StopLevel + ( CurrentSpread * Factor ) )" where Factor is at least "1.0" (ideally "1.5" or "2.0"; greater if you see that you still experience Error 130 due to excessive slippage).
  8. There is no guarantee that 10 points would account for spread, because spread is different depending on Symbol and is always changing, especially during news events and when liquidity is low such as during off hours.
  9. OK! If "FreezeLevel" is not used that is OK, but I suggest that you still cater for it in your code, so that in case you use it with a different broker (that might have it). the EA will still work.
  10. Supply new code with changes so that it can be analysed.


Thank you very much.  I will look into all the above advice and suggestions.  In the meantime here is the latest code.  Also, I think I found a mistake in this line:

#property strict;
extern string Label_TrailingStart="Pip threshold to activate TrailingStop";
extern int TrailingStart=12;
extern int TrailingStop=5;
double stoplevel=(MarketInfo(Symbol(),MODE_STOPLEVEL))/10;
int TS=TrailingStart-TrailingStop;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init(){
   if(TS<stoplevel){
      MessageBox("Please note: Your inputs for TrailingStart and/or TrailingStop cannot"+
                 "\nbe less than the minimum levels required by your broker and the"+
                 "\nTrailingStart has been increased automatically to "+StringConcatenate(stoplevel+TrailingStop+2)+" Pips");
     } 
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit(){
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start(){
   double Pip=Point*10;
   int ticket=0;
   if(TS<stoplevel) TrailingStart=(int)stoplevel+TrailingStop+2;
   
   /*Print("stoplevel = ",stoplevel);
   Print("TrailingStart = ",TrailingStart);
   Print("TrailingStop = ",TrailingStop);
   Print("TS = ",TS);
   Print("Buy error = ",GetLastError());
                        Print("OrderOpenPrice = ",OrderOpenPrice());
                        Print("OrderClosePrice()-OrderOpenPrice()>TrailingStart*Pip = ",
                        OrderClosePrice()-OrderOpenPrice()>TrailingStart*Pip);
                        Print("OrderClosePrice-TrailingStop*Pip = ",OrderClosePrice()-TrailingStop*Pip);
                        Print("TrailingStart*Pip = ",TrailingStart*Pip);
                        Print("TrailingStop*Pip = ",TrailingStop*Pip);
                        Print("OrderClosePrice = ",OrderClosePrice());
                        Print("OrderStopLoss = ",OrderStopLoss());
                        Print("OrderSymbol = ",OrderSymbol());
                        Print("OrdersTotal = ",OrdersTotal());
                        Print("OrderSelect = ",OrderSelect(OrderTicket(),SELECT_BY_TICKET));
                        Print("ticket = ",ticket);
                        Print("OrderTicket = ",OrderTicket());
                        Print("Selectbyticket = ",SELECT_BY_TICKET);
                        Print("Selectbypos = ",SELECT_BY_POS);
                        Print("Ask = ",Ask);
                        Print("OrderModify = ",OrderModify(OrderTicket(),OrderOpenPrice(),OrderClosePrice()-(TrailingStop*Pip),
                        OrderTakeProfit(),Blue));*/
   
   for(int i=OrdersTotal()-1; i>=0; i--)
      if(OrderSelect(i,SELECT_BY_POS)){
      ticket++;
         if(OrderSymbol()==_Symbol){
            if(OrderType()==OP_BUY){
               if(OrderSelect(OrderTicket(),SELECT_BY_TICKET)){
                  if((OrderClosePrice()-OrderOpenPrice())>(TrailingStart*Pip)){
                     if(TrailingStop<OrderClosePrice()-(TrailingStop*Pip)){
                        RefreshRates();
                        if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderClosePrice()-(TrailingStop*Pip),OrderTakeProfit(),Blue))
                        Print("Buy error = ",GetLastError());
                        }     
                    }  
                }     
            }  
            if(OrderType()==OP_SELL){
               if(OrderSelect(OrderTicket(),SELECT_BY_TICKET)){
                  if((OrderOpenPrice()-OrderClosePrice())>(TrailingStart*Pip)){
                     if(TrailingStop>OrderClosePrice()+(TrailingStop*Pip)){
                        RefreshRates();
                        if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderClosePrice()+(TrailingStop*Pip),OrderTakeProfit(),Red))
                        Print("Sell error = ",GetLastError());
                        /*Print("Ask = ",Ask);
                        Print("Ask+TrailingStop*Pip = ",Ask+TrailingStop*Pip);
                        Print("TrailingStart*Pip = ",TrailingStart*Pip);*/
                        }
                    }
                }  
            }
         }
      }   
  return(0);
}
//+------------------------------------------------------------------+

 A rather major change that I made was I changed the first line below to the second, because I think there must be a value higher than zero for the stoploss.  Is that true?

 if(OrderStopLoss()<OrderClosePrice()-(TrailingStop*Pip)){
if(TrailingStop<OrderClosePrice()-(TrailingStop*Pip)){
 

The following code example serves as a possible solution/alternative to your code. Please note however, that the code was not tested or debugged (only compiled), so use use it as "pseudo" code only:

#property strict

extern double
   dblTrailStartPips = 11.0,  // Trailing Start (Pips)
   dblTrailStopPips  = 5.0,   // Trailing Stop (Pips)
   dblPipMultiplier  = 10.0,  // Pips to Points Multiplier
   dblSpreadFactor   = 2.0;   // Spread Factor for Slippage Compensation
   
extern int
   intMagicNumber    = 0;     // Magic Number (0 for Manual Orders)
   
double
   dblTickSizeDelta,          // Size of a Tick (Delta)
   dblStopLevelDelta,         // Market Stop Level (Delta)
   dblTrailStartDelta,        // Trailing Start (Delta)
   dblTrailStopDelta;         // Trailing Stop (Delta)

// Initialisation
int OnInit()
{
   dblTickSizeDelta   = MarketInfo( _Symbol, MODE_TICKSIZE );
   dblStopLevelDelta  = MarketInfo( _Symbol, MODE_STOPLEVEL ) * _Point;
   dblTrailStartDelta = dblTrailStartPips * dblPipMultiplier  * _Point;
   dblTrailStopDelta  = dblTrailStopPips  * dblPipMultiplier  * _Point;
   
   return( INIT_SUCCEEDED );
}

// Process Tick Event
void OnTick()
{
   double
      dblSpreadDelta  = Ask - Bid,
      dblMinStopDelta = dblStopLevelDelta + ( dblSpreadDelta * dblSpreadFactor ),
      dblTrailDelta   = ( dblTrailStopDelta > dblMinStopDelta ) ? dblTrailStopDelta : dblMinStopDelta;
   
   for( int i = OrdersTotal() - 1; i >= 0; i-- )
   {
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) )
      {
         if( ( OrderSymbol() == _Symbol ) && ( OrderMagicNumber() == intMagicNumber ) )
         {
            int intDirection = 0;
            switch( OrderType() )
            {
               case OP_BUY:  intDirection = +1; break;
               case OP_SELL: intDirection = -1; break;
               default:                         continue;
            }
            
            double
               dblOpenPrice  = OrderOpenPrice(),
               dblCloseDelta = ( OrderClosePrice() - dblOpenPrice ) * intDirection;
               
            if( dblCloseDelta > dblTrailStartDelta )
            {
               double
                  dblStopLossPrice = OrderStopLoss(),
                  dblStopLossDelta = ( dblStopLossPrice  - dblOpenPrice ) * intDirection,
                  dblTrailingDelta = dblCloseDelta - dblTrailDelta;
               
               if( ( dblTrailingDelta > dblStopLossDelta ) || ( dblStopLossPrice == 0 ) )
               {
                  double dblStopLoss = round( ( dblOpenPrice + ( dblTrailingDelta * intDirection ) ) / dblTickSizeDelta ) * dblTickSizeDelta;
                  if( dblStopLoss != dblStopLossPrice )
                  {
                     if( !OrderModify( OrderTicket(), dblOpenPrice, dblStopLoss, OrderTakeProfit(), OrderExpiration() ) )
                     {
                        Print( "Order Modification Failed with Error: ", GetLastError() );
                     }
                  }
               }
            }
         }
      }
   }
}
 

Try this Trader3000 

Compare this code with what you already have... it's very similar and I have been using it for years and it works like a bomb...

//+------------------------------+
//| TRAILING STOP Function       |
//+------------------------------+
void Trailing_Stop_Function()
{
bool Ticket_TS_Long;
   for(int iTS_Long = OrdersTotal() - 1; iTS_Long >= 0 ; iTS_Long --)  
   {
      
      if(OrderSelect(iTS_Long,SELECT_BY_POS,MODE_TRADES) == true
      && OrderSymbol() == Symbol()
      && OrderType() == OP_BUY
      && OrderMagicNumber() == MagicNumberLong
      && Trailing_Stop_In_Pips > 0
      && Bid - OrderOpenPrice() > Trail_After_Pips_Profit*PipMultiplier
      && OrderStopLoss() < Bid - (Trailing_Stop_In_Pips*PipMultiplier))
      {
      Ticket_TS_Long = (OrderModify(OrderTicket(),OrderOpenPrice(),Bid - (Trailing_Stop_In_Pips*PipMultiplier),NULL,0,Green));
         if (Ticket_TS_Long != true)
         {
         Print("TS-Order Modify Error ",GetLastError());
         }
      }
    } 
bool Ticket_TS_Short;
   for(int iTS_Short = OrdersTotal() - 1; iTS_Short >= 0 ; iTS_Short --)  
   {
      
   if(OrderSelect(iTS_Short,SELECT_BY_POS,MODE_TRADES) == true
      && OrderSymbol() == Symbol()
      && OrderType() == OP_SELL
      && OrderMagicNumber() == MagicNumberShort
      && Trailing_Stop_In_Pips > 0
      && OrderOpenPrice() - Ask > Trail_After_Pips_Profit*PipMultiplier
      && OrderStopLoss() > Ask + (Trailing_Stop_In_Pips*PipMultiplier))
      {
      Ticket_TS_Short = (OrderModify(OrderTicket(),OrderOpenPrice(),Ask + (Trailing_Stop_In_Pips*PipMultiplier),NULL,0,Green));
         if (Ticket_TS_Short != true)
         {
         Print("TS-Order Modify Error ",GetLastError());
         }
      }
   }
}

 

Trail_After_Pips_Profit is an extern double variable that the user fills in...and

PipMultiplier is a simple function that calculates the currency decimal point value...

If you don't use MagicNumbers then delete that criteria.... 

 

This will...or rather "should" trail any order on any chart.....

I stand under correction but it works for me....

 

 

//+------------------------------------------+
//| Check for 5 Digits - Pipmultiplier       |
//+------------------------------------------+
   
   if(Digits==5||Digits==3) 
   {
   PipMultiplier = 10*Point;
   }
   else if(Digits==2) 
   {
   PipMultiplier = 100*Point;
   }
   
   else
   {
   PipMultiplier = Point;
   }
   

 

I'm impressed with your help FMIC.... Nice one

 

Mike.T:

I'm impressed with your help FMIC.... Nice one

Thank you!

Some advice; don't use "Ask" or "Bid" - use "OrderClosePrice()" instead. It is a real-time value irrespective of it being a Buy or Sell order.
 

@Mike.T: Something seems off with your PipMulytiplier code:

else if(Digits==2) 
{
   PipMultiplier = 100*Point;
}

That does not seem to be correct! Why multiply it by 100 for 2 digit symbols? It should not be multiplied at all! It should be the same condition as 4 digit symbols.

Here is an example. If the Price of USD/JPY goes from 108.65 to 108.77; that is a 12 pip increase, not 1200 pips!
 
FMIC:

Thank you!

Some advice; don't use "Ask" or "Bid" - use "OrderClosePrice()" instead. It is a real-time value irrespective of it being a Buy or Sell order.

Thanx... didn't know that.... so... (without hi-jacking this thread)... I should rather use OrderClosePrice() instead of Bid or Ask...?
 
Mike.T:
Thanx... didn't know that.... so... (without hi-jacking this thread)... I should rather use OrderClosePrice() instead of Bid or Ask...?
Yes! For the reasons mentioned (see docs) as well as allowing you to merge both the Buy and Sell logic into one code block as I have demonstrated in my example.
Reason: