manage trade bug

 

This only seems to manage the trade when the EA is running by itself. If I have the same EA running in another chart, it doesn't work.

Have I done something wrong in the loop or is it something to do with the spread meaning it doesn't actually work after 20pips profit?


      ////////////////////////////////////////////////
      //Move trade to break even after x pips
      ////////////////////////////////////////////////
      if (MovetoBreakEven>0)
      {
         int totaltoBE = OrdersTotal();
         for(int itoBE=totaltoBE-1;itoBE>=0;itoBE--)
         {
            if( OrderSelect(itoBE,SELECT_BY_POS) && OrderCloseTime()==0 )
            {
               if( ( OrderType()==OP_BUY ) && 
                  (OrderSymbol() == Symbol()) &&
                  ((Bid-OrderOpenPrice()) > Point*MovetoBreakEven) &&
                  (OrderStopLoss()<OrderOpenPrice())
               )
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0);
               }
               else if( ( OrderType()==OP_SELL ) && 
                        (OrderSymbol() == Symbol()) &&
                        ((OrderOpenPrice()-Ask) > Point*MovetoBreakEven) &&
                        (OrderStopLoss()>OrderOpenPrice())
                     )
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0);
               } 
               
            }
      
         }
      }  
 
SanMiguel:

This only seems to manage the trade when the EA is running by itself. If I have the same EA running in another chart, it doesn't work.

Have I done something wrong in the loop or is it something to do with the spread meaning it doesn't actually work after 20pips profit?


Your order select loop processes ALL orders. Each EA instance should have a different magic number and the loop should process only orders with its own number.

 
WHRoeder:

Your order select loop processes ALL orders. Each EA instance should have a different magic number and the loop should process only orders with its own number.

Shouldn't make a difference. The are manual trades that are then managed by the EA. It picks up the correct one by looking at the Symbol().

It works correctly in the 2nd part of my code, which is a sort of gtrailing stop but this part of the code (the move to breakeven) won't work:


#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#include <WinUser32.mqh>
#include <stderror.mqh>
#include <stdlib.mqh>

/*
1.0.0    Created

To Do
1.1.0    Added ability to move the stop according to the change in senkou as long as it is closer to the entry price 
1.1.1    Move to break even according to the ATR.
         We decided that moving to breakeven after 20 on the 50 pairs was ok. 50 was about 30% of ADR, so we move to break even at
         2/5ths of the 30% ADR.    
*/

int start()
{

   double KS_val = iIchimoku(NULL, 0, 9, 26, 52, MODE_KIJUNSEN, 0);
   double MovetoBreakEven;
   double Buffer;
   
   //20pips for short term trades
   if (Period() != PERIOD_D1 || Period() != PERIOD_H4)
   {
      MovetoBreakEven = 20;
   }
   //40pips for longer term trades
   if (Period() == PERIOD_D1 || Period() != PERIOD_H4)
   {
      MovetoBreakEven = 40;
   }
   //40pips for longer term trades
   
   
   //5 decimal systems like Alpari
   if (Point==0.00001)
   {
      MovetoBreakEven=MovetoBreakEven*10;
      Buffer=0.0010;
   }
   if (Point==0.001)
   {
      MovetoBreakEven=MovetoBreakEven*10;
      Buffer=0.10;
   }
   
      //////////////////////////
      //Rules are:
      //1. The stop loss should follow the kumo cloud either span a or B, whichever is further away. Add 10pips plus the spread
      //2. Trade should be moved to break even at 20 for short term, 40 for long term. Maybe use ADR here
      //3. Trail by the kijun sen on the longer term timeframes
      //////////////////////////
      
      ////////////////////////////////////////////////
      //Before trade has broken even manage the trade by moving stops with the kumo
      //buffer 10pips plus spread
      ////////////////////////////////////////////////
      
      
      
   
      ////////////////////////////////////////////////
      //Move trade to break even after x pips
      ////////////////////////////////////////////////
      if (MovetoBreakEven>0)
      {
         int totaltoBE = OrdersTotal();
         for(int itoBE=totaltoBE-1;itoBE>=0;itoBE--)
         {
            if( OrderSelect(itoBE,SELECT_BY_POS) && OrderCloseTime()==0 )
            {
               if( ( OrderType()==OP_BUY ) && 
                  (OrderSymbol() == Symbol()) &&
                  ((Bid-OrderOpenPrice()) > Point*MovetoBreakEven) &&
                  (OrderStopLoss()<OrderOpenPrice())
               )
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0);
               }
               else if( ( OrderType()==OP_SELL ) && 
                        (OrderSymbol() == Symbol()) &&
                        ((OrderOpenPrice()-Ask) > Point*MovetoBreakEven) &&
                        (OrderStopLoss()>OrderOpenPrice())
                     )
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0);
               } 
               
            }
      
         }
      }  

if (Period() == PERIOD_D1 || Period() == PERIOD_H4 || Period() == PERIOD_H1)
{
         int totaltrail = OrdersTotal();
         for(int itrail=totaltrail-1;itrail>=0;itrail--)
         {
            if( OrderSelect(itrail,SELECT_BY_POS) && OrderCloseTime()==0 )
            {
               if( OrderType()==OP_BUY && 
                   OrderStopLoss()!=KS_val &&
                   KS_val-Buffer > OrderStopLoss() &&
                   ((Bid-OrderOpenPrice()) > Point*MovetoBreakEven) &&
                   OrderSymbol() == Symbol()
               )
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),KS_val-Buffer,OrderTakeProfit(),0);
               }
               else if( OrderType()==OP_SELL && 
                        OrderStopLoss()!=KS_val &&
                        KS_val+Buffer < OrderStopLoss() &&
                        ((OrderOpenPrice()-Bid) > Point*MovetoBreakEven) &&
                        OrderSymbol() == Symbol()
                     )
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),KS_val+Buffer,OrderTakeProfit(),0);
               } 
               
            }
      
         }
}
}


 
helooooooooooooooooooooo :)
 

SanMiguel. Don't be surprised by the lack of response when you haven't seen the need to post:

- What symbols and periods you have applied the EA to.

- Whether these are implemented in different instances of MT4, different accounts/brokers.

- What is actually going wrong.

- What errors are thrown.


Meanwhile - is there any possibility you'd try using the Print() statement to check your logic flow and the contents of key variables during execution? That may tell you enough.


CB

 
cloudbreaker:

SanMiguel. Don't be surprised by the lack of response when you haven't seen the need to post:

- What symbols and periods you have applied the EA to.

- Whether these are implemented in different instances of MT4, different accounts/brokers.

- What is actually going wrong.

- What errors are thrown.


Meanwhile - is there any possibility you'd try using the Print() statement to check your logic flow and the contents of key variables during execution? That may tell you enough.


CB

Sorry - I thought I had explaine dit in post 1 but looking back it's not clear.

What happens is that, after 20 pips, the trade is supposed to move to breakeven.

It does move it after 20 pips but sometimes 24, 25, depending on the pair.

I'm sure it's the spread but the spread is not supposed to have to be figured out for MT4 calculations, I thought it was automatic?

So, something in the breakeven section of the code, is picking up the spread incorrectly.

 

Are you running this in the strategy tester, on a demo account or on a live account?


CB

 
SanMiguel:

Sorry - I thought I had explaine dit in post 1 but looking back it's not clear.

What happens is that, after 20 pips, the trade is supposed to move to breakeven.

It does move it after 20 pips but sometimes 24, 25, depending on the pair.

I'm sure it's the spread but the spread is not supposed to have to be figured out for MT4 calculations, I thought it was automatic?

So, something in the breakeven section of the code, is picking up the spread incorrectly.


((Bid-OrderOpenPrice()) > Point*MovetoBreakEven)

((OrderOpenPrice()-Ask) > Point*MovetoBreakEven)

Try using >= otherwise conditions will be true always beyond your MovetoBreakEven...

 
robofx.org:

Try using >= otherwise conditions will be true always beyond your MovetoBreakEven...


Live account.


Yes, but even so, it should still run the code at 20pips profit not a random number like 24 or 25.

Got to be the spread but I can't see how...

 
SanMiguel wrote >>

Live account.

...

Got to be the spread but I can't see how...

Spread varies on many live feeds, check with your broker or get EA to monitor spread

-BB-

 
BarrowBoy:

Spread varies on many live feeds, check with your broker or get EA to monitor spread

-BB-

Yes, but the spread is supposed to be included in target profits and stops automatically on MT4.

So, when MT4 says I am 20 pips in profit, the code above should move the stop to breakeven shouldn't it?

Reason: