Problems checking for open trade - page 7

 
dazamate:
Yeah just reading over your comment again and what I have done is wrong,
will not give me the bar opening time would I have to use Time[0]?


Yes, or . . . .

tradeopened = TimeHour(TimeCurrent());
 
OrderDelete(OrderTicket( ) );  

That fixed it Raptor.


---------> http://clip2net.com/s/14aYs

 
dazamate:

That fixed it Raptor.


---------> http://clip2net.com/s/14aYs

LOL, glad to be of some help . . . :-)
 

Mate you have been a lot of help I finally have the ea in a state now where I can start tuning up the strategy. Thankyou very much but I doubt you have heard the last of me sorry to say haha


Also big thanks to WHRoeder too
 

Sooo.. you thought it was over lol


I added some code that will trail the stoploss from a moving average value. The code works well on buy trades but it doesn't work on sell trades. I can't spot anything maybe you guys can.


// Trailing Stop Code 

   if(Tradeopen()==true && emastoptrail==TRUE)                                  // Trades are open and Trailing stop feature is turned on
    {
      for(int tnumber2 = OrdersTotal()-1; tnumber2 >= 0 ; tnumber2--)                   //scan through open orders
       {
        if (OrderSelect(tnumber2, SELECT_BY_POS) && 
            OrderMagicNumber()==Mnumber3)                                                // The orders magic number is the same as the magic number used the profit trade
            {                                          
          
            if(OrderType()==OP_BUY && (iMA(Symbol(), 60, ematrailperiod, 0, 1, 0, 1) > OrderStopLoss() ))       // The stop loss is less than the moving average value
          
               {
                OrderModify(OrderTicket(),OrderOpenPrice(),iMA(Symbol(), 60, ematrailperiod, 0, 1, 0, 1),OrderTakeProfit(),0);  // Modify Stop to MA value
               }
            
            if(OrderType()==OP_SELL && (iMA(Symbol(), 60, ematrailperiod, 0, 1, 0, 1) < OrderStopLoss() ))       // The stop loss is more than the moving average value
          
               {
                OrderModify(OrderTicket(),OrderOpenPrice(),iMA(Symbol(), 60, ematrailperiod, 0, 1, 0, 1),OrderTakeProfit(),0);   // Modify Stop to MA value
               }
            }
       }
     }
 

Well the trailing stop code may not be the problem. There is a problem with the code that deletes pending orders after a certain time...


// DELETE PENDING ORDERS THAT HAVE NOT BEEN TRIGGERED WITHIN ' int = pendinglimit'

   if((iBarShift(Symbol(),60 ,tradeopened)>pendinglimit) && Tradeopen()==true)              // Check to see if pending orders have expired
     {
        for(int tnumber = OrdersTotal()-1; tnumber >= 0 ; tnumber--)                  //scan through open orders
        {
          if (OrderSelect(tnumber, SELECT_BY_POS) &&   
             ((OrderType()==OP_BUYSTOP)|| (OrderType()== OP_SELLSTOP)) &&               // The order selected is either a pending buy on stop order or a buy on sell order
             ((OrderMagicNumber()== Mnumber1) || (OrderMagicNumber()==Mnumber3)))       // The orders magic number is the same as the magic number used in this ea
          
               {
                OrderDelete(OrderTicket( ) );                                            // Delete it
               }
        }
       
     }

After each trade is opened I also run this...

tradeopened = TimeHour(TimeCurrent());

I Comment ...

(iBarShift(Symbol(),60 ,tradeopened)

and it starts off from 1000 and just keeps counting on each new bar and does not get reset when I run ...

tradeopened = TimeHour(TimeCurrent());


This makes the check to delete pending orders always true and if the trade is not triggered within the next bar the pending order is deleted straight away -__-

Ahhh HELP lol

 

I think I may have fixed it ....

I changed it so that one more check to delete the trade was to shift itime back the max allowed time a pending order could be opened and see if it matched up with the time the trade was opened :)

  for(int tnumber = OrdersTotal()-1; tnumber >= 0 ; tnumber--)                  //scan through open orders
        {
          if (OrderSelect(tnumber, SELECT_BY_POS) &&   
             ((OrderType()==OP_BUYSTOP)|| (OrderType()== OP_SELLSTOP)) &&               // The order selected is either a pending buy on stop order or a buy on sell order
             ((OrderMagicNumber()== Mnumber1) || (OrderMagicNumber()==Mnumber3)) &&       // The orders magic number is the same as the magic number used in this ea
              (iTime(   Symbol(), 60, pendinglimit) == tradeopened))
               {
                OrderDelete( OrderTicket() );                                            // Delete it
               }
        }
 
dazamate:

I think I may have fixed it ....

I changed it so that one more check to delete the trade was to shift itime back the max allowed time a pending order could be opened and see if it matched up with the time the trade was opened :)

You almost have something like what I was envisioning . . . .

There is a problem using your tradeopened variable . . . if there are open trades and the EA gets shut down you lose the info in that variable . . . why not replace it with OrderOpenTime( ) and do something like this . . .

for(int tnumber = OrdersTotal()-1; tnumber >= 0 ; tnumber--)                  //scan through open orders
        {
          if (OrderSelect(tnumber, SELECT_BY_POS) &&   
             ((OrderType()==OP_BUYSTOP)|| (OrderType()== OP_SELLSTOP)) &&               // The order selected is either a pending buy on stop order or a buy on sell order
             ((OrderMagicNumber()== Mnumber1) || (OrderMagicNumber()==Mnumber3)) &&       // The orders magic number is the same as the magic number used in this ea

              (iBarShift(OrderSymbol(),PERIOD_H1 ,OrderOpenTime()) - pendinglimit) <= 0));    //  <----------- this . . .

               {
                OrderDelete( OrderTicket() );                                            // Delete it
               }
        }
 
The only problem is I was using the ibarshift fucntion before and for some reason it was outputting 1000+
 

Also raptor I need to pick your brain.... again :)


Is there a way to turn a moving average value into degrees? I am just googling now to see if I can find any maths forumlas. That way it would be easy for sideways market detection and clear up and down trends

Reason: