Need little help please

 

I trie to prog an easy EA.

It should buy & sell at the same time (only one trade per hour or day) with trailing stops.

But I get the error message "ordermodify error 1"

What can i do? THX FOR HELP


//+------------------------------------------------------------------+
//| Casino                                                           |
//|                                                                  |
//| - Works best                                                     |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010 - FrancisLeeMartin"
#define IDENT  "Casino 1.0"

extern double  lots           = 0.1;
extern double  stop_loss      = 100;   // (10 pips) 
extern double  take_profit    = 1000;  // (100 pips) 
extern double    TrailingStop = 100;   // (10 pips)

int       MagicNumber = 101090;



int start(){


   int cnt,total,ticket;
   int h=TimeHour(CurTime());
   int m=TimeMinute(CurTime());
   
   int stop_level;
   stop_level = MarketInfo(Symbol(), MODE_STOPLEVEL) + MarketInfo(Symbol(), MODE_SPREAD);
   if (stop_loss < stop_level) stop_loss = stop_level;
   if (take_profit < stop_level) take_profit = stop_level;
   
   
   
   if (h==9)
   {
      if (m==0)
      {
         ticket=OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, Ask - stop_loss * Point, Bid + take_profit * Point, IDENT, MagicNumber, 0, Blue);
         ticket=OrderSend(Symbol(), OP_SELL, lots ,Bid, 3, Bid + stop_loss * Point, Ask - take_profit * Point, IDENT, MagicNumber, 0, Red);
      }
   }
   
  
  
   total  = OrdersTotal();
   
   for(cnt=0;cnt<total;cnt++) 
      {
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

         if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
         {
            if(OrderType()==OP_BUY)   //<-- Long position is opened
            {
               TrailOrder(OrderType()); return(0); //<-- Trailling the order
            }
            if(OrderType()==OP_SELL) //<-- Go to short position
            {
               TrailOrder(OrderType()); return(0); //<-- Trailling the order
            }
         }
      }
   
   
   return(0);
}


void TrailOrder(int type)
{
   if(TrailingStop>0)
   {
      if(OrderMagicNumber() == MagicNumber)
      {
         if(type==OP_BUY)
         {
            if(Bid-OrderOpenPrice()>Point*TrailingStop)
            {
               if(OrderStopLoss()<Bid-Point*TrailingStop)
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
               }
            }
         }
         if(type==OP_SELL)
         {
            if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
            {
               if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
               }
            }
         }
      }
   }
}
 

need an OrderSelect() loop in your TrailOrder() function.

 

You need to check whether your order needs modification.

https://www.mql5.com/en/forum/127818

v

 

thx so far.

The OrderSelect() sounds good, but I don´t understand how it is posible to select by ticket or pos???

 
Doomsday26:

thx so far.

The OrderSelect() sounds good, but I don´t understand how it is posible to select by ticket or pos???


don't think the TrailOrder() function will 'know' OrderMagicNumber() unless you pass it so rather try:

total  = OrdersTotal();
   
for(cnt=0;cnt<total;cnt++) 
   {
    OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

    if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
      {
       TrailOrder(OrderType()); return(0); //<-- Trailling the order
      }
   return(0);
   }


void TrailOrder(int type)
  {
   if(TrailingStop>0)
     {
      int o;
      for(o=0;o<total;o++) 
         {
          OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
          if(OrderMagicNumber() == MagicNumber)
            {
             if(type==OP_BUY)
               {
                if(Bid-OrderOpenPrice()>Point*TrailingStop)
                  {
                   if(OrderStopLoss()<Bid-Point*TrailingStop)
                     {
                      OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                     }
                  }
               }
             if(type==OP_SELL)
               {
                if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                  {
                   if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                     {
                      OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                     }
                  }
               }
            }
         }
 
19730719:


don't think the TrailOrder() function will 'know' OrderMagicNumber() unless you pass it so rather try:

I think you may run into trouble by repeating the order select in the function. The order you select in start() may not match the order selected in the function as you start the loop again. imho the order select loop needs to be in one place only... either in start or in the function. Just pass the ticket number and current SL into the TrailOrder() function and test in there whether it requires modification.

V

 
Doomsday26:

thx so far.

The OrderSelect() sounds good, but I don´t understand how it is posible to select by ticket or pos???

Select by position(order pool is just like any array), then retrieve the ticket number for that position and action on the ticket number

V

Reason: