Script to Open New Pending Order not always working

 

Hi All,


I have put together the script below to open a new pending order with the same comment and magic number of an existing one.

The script seems to be working fine most of the times but occasionally it will not open any order and there are no error messages are indicated in the Journal.

Any tips?


Thanks,
MG



input int TicketOrd=20000000;     // Ticket Number
input double StepPerc=0.45;     
input double NewPrice=0.0; //New Pending Price
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
OrderSelect(TicketOrd,SELECT_BY_TICKET,MODE_TRADES);

int symboldigits=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_DIGITS),0);
double BuyTP=NormalizeDouble(NewPrice+NewPrice*StepPerc/100,symboldigits);
double SellTP=NormalizeDouble(NewPrice-NewPrice*StepPerc/100,symboldigits);


if ((OrderType()==2||OrderType()==4)&&Ask>=NewPrice) 
      {
      Print ("Order to be opened is a BUY LIMIT with MN ",OrderMagicNumber());
      OrderSend(OrderSymbol(), ORDER_TYPE_BUY_LIMIT, OrderLots(), NewPrice, 2,0, BuyTP, OrderComment(),OrderMagicNumber());
      }
      else if ((OrderType()==2||OrderType()==4)&&Ask<NewPrice)
      {
      Print ("Order to be opened is a BUY STOP with MN ",OrderMagicNumber());
      OrderSend(OrderSymbol(), ORDER_TYPE_BUY_STOP, OrderLots(), NewPrice, 2,0, BuyTP, OrderComment(),OrderMagicNumber());
      }       
      else if ((OrderType()==3||OrderType()==5)&&Ask<=NewPrice) 
      {
      Print ("Order to be opened is a SELL LIMIT with MN ",OrderMagicNumber());
      OrderSend(OrderSymbol(), ORDER_TYPE_SELL_LIMIT, OrderLots(), NewPrice, 2,0, SellTP, OrderComment(),OrderMagicNumber());
      }
      else if((OrderType()==3||OrderType()==5)&&Ask>NewPrice)
      {
      Print ("Order to be opened is a SELL STOPwith MN ",OrderMagicNumber());
      OrderSend(OrderSymbol(), ORDER_TYPE_SELL_STOP, OrderLots(), NewPrice, 2,0, SellTP, OrderComment(),OrderMagicNumber());
      }
      
  
   
  }
 
MacGiamma:

Hi All,


I have put together the script below to open a new pending order with the same comment and magic number of an existing one.

The script seems to be working fine most of the times but occasionally it will not open any order and there are no error messages are indicated in the Journal.

Any tips?


Thanks,
MG



Hello,

script run OnStart() function once.

Try to replace it with OnTick() function.

Reason: