'While Loop' in Script not updating...

 
Hi,

Consider this trailing stop script for a long position (it actually opens an order then tracks it, setting stoplosses). Furthermore consider that Lows are ascending (making new highs so to speak). I would think that the script would have no problem updating the stopLoss according to the previous bars low. It doesnt. I use a 'while loop' to scan for price change to trigger the order modification, but it does nothing. I added a 'Print' statement to check the values being returned and I noticed that once in the 'while loop' the value of 'Low[1]' would remain the same as the value it returned when the script was initiated, regardless of how many new bars were being drawn.

I am trying to write a script that will open an order and place an initial stoploss. A stoploss which is to be modified automatically until the stop is hit, closing the order and canceling out the script.

Any thoughts, suggestions and or help would be most appreciated,
lucidlamp

// trailing stop for long positions...

double dStop;
int x;
int iTicket;

int start()
 {
  dStop=Low[1];
  iTicket=OrderSend(Symbol(),OP_BUY,0.01,Ask,3,dStop,0,NULL,0,0,CLR_NONE);
  while(iTicket==iOrderTicket())
   {
    Print(Low[1]);
    if(Low[1]>dStop) // Why does Low[1] return the same value it returned when the script was initiated, even when new bars are drawn?
     {
      dStop=Low[1];
      vOrderModify(dStop);
     }
   }
  return(0);
 }

int iOrderTicket()
 {
  for(x=OrdersTotal()-1;x>=0;x--)
   {
    OrderSelect(x,SELECT_BY_POS);
    if(OrderSymbol()==Symbol())
     {
      return(OrderTicket());
     }
   }
  return(0);
 }

void vOrderModify(double dStopLoss)
 {
  for(x=OrdersTotal()-1;x>=0;x--)
   {
    OrderSelect(x,SELECT_BY_POS);
    if(OrderSymbol()==Symbol())
     {
      OrderModify(OrderTicket(),OrderOpenPrice(),dStopLoss,0,0,CLR_NONE);
     }
   }
  return(0);
 } 
 
Hi,

I've figured it out. adding RefreshRates() withing the 'while loop' solced the issue.

Regards,
lucidlamp

  while(iTicket==iOrderTicket())
   {
    Print(Low[1]);
    if(Low[1]>dStop) // Why does Low[1] return the same value it returned when the script was initiated, even when new bars are drawn?
     {
      dStop=Low[1];
      vOrderModify(dStop);
     }
    RefreshRates();
   }