Trouble moving stop to pivot points?

Back to topics list To post a new topic, please log in or register
avatar
52
renzbub 2015.06.16 14:59
 

What Im trying to get my EA to do is move the stop to each previous pivot as price moves into profit but without being slowly trailed. 

// Input variables                
input int MagicNumber = 101;                          
input int Slippage = 10;

input double LotSize = 0.1; 
input int StopLoss = 0; 
input int TakeProfit = 0;

input int MaPeriod = 200;
input ENUM_MA_METHOD MaMethod = MODE_EMA;
input ENUM_APPLIED_PRICE MaPrice = PRICE_CLOSE;

// Global variables
int gBuyTicket, gSellTicket;
datetime time = iTime(NULL,0,0);  
double yclose = iClose(_Symbol,PERIOD_D1,1); 
double yhigh = iHigh(_Symbol,PERIOD_D1,1); 
double ylow = iLow(_Symbol,PERIOD_D1,1); 

double pivot = MathAbs((yhigh + ylow + yclose)/3);  
double S1 = MathAbs(2 * pivot - yhigh);
double S2 = MathAbs(pivot - (yhigh - ylow));
                        

// OnInit() event handler     runs once at start of program when its initialised
int OnInit() 
{
   bool r1line = ObjectCreate("r1line",OBJ_HLINE,0,time,R1); 
   bool pivotline = ObjectCreate("pivotline",OBJ_HLINE,0,time,pivot); 
   bool s1line = ObjectCreate("s1line",OBJ_HLINE,0,time,S1);  
   bool s2line = ObjectCreate("s2line",OBJ_HLINE,0,time,S2);   
   return(INIT_SUCCEEDED);
}

// OnTick() event handler    runs and evaluates code on every price tick
void OnTick()
{
   
   // Current order counts     
   int buyCount = 0, sellCount = 0;
   
   for(int order = 0; order <= OrdersTotal() - 1; order++)
   {
      bool select = OrderSelect(order,SELECT_BY_POS);
      
      if(OrderMagicNumber() == MagicNumber && select == true)
      {
         if(OrderType() == OP_BUY) buyCount++;
         else if(OrderType() == OP_SELL) sellCount++;
      }   
   }
   
   // Moving average and close price from last bar                Section 3
   double ma = iMA(_Symbol,_Period,MaPeriod,0,MaMethod,MaPrice,1);
   double close = Close[1];

   // Sell order condition
   if(close < ma && sellCount == 0 && buyCount == 0 && gSellTicket == 0)    
   {
      // Close buy order
      for(order = 0; order <= OrdersTotal() - 1; order++)
      {
         select = OrderSelect(order,SELECT_BY_POS);
         
         if(OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber && select == true)
         {
            // Close order
            bool closed = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrRed);
            
            if(closed == true)
            {
               gBuyTicket = 0;
               order--;
            }
         }
      }
      
      // Open sell order
      gSellTicket = OrderSend(_Symbol,OP_SELL,LotSize,Bid,Slippage,0,0,"Sell order",MagicNumber,0,clrRed);
      gBuyTicket = 0;  
   }

   // BreakEven Stop Management all within the OnTick() event handler       
   for(order = 0; order <= OrdersTotal() - 1; order++)
   {
      select = OrderSelect(order,SELECT_BY_POS);  
      
      if(OrderMagicNumber() == MagicNumber && select == true)
      {          
         RefreshRates();

         // Check sell order for stop placement
         if(OrderType() == OP_SELL)
         {        
            if(Ask <= S1)  
            {
               bool pivotstop = OrderModify(OrderTicket(), OrderOpenPrice(), pivot, OrderTakeProfit(),0);
            }
            
             if(Ask <= S2)  
            {
               bool s1stop = OrderModify(OrderTicket(), OrderOpenPrice(), S1, OrderTakeProfit(),0); 
            }                      
         }
      }
   }            
}


I can get EA to move stop to pivot when Ask <= S1 with one basic if statement but problem is I cant get it to move on to the next if statement when Ask <= S2. It doesn't read the next OrderModify () function.

I guess its because they are both clashing as both Ask <= S1 and Ask <= S2 are true.

 

avatar
13012
WHRoeder 2015.06.16 18:31 #
 
  1. datetime time = iTime(NULL,0,0);  
    double yclose = iClose(_Symbol,PERIOD_D1,1); 
    double yhigh = iHigh(_Symbol,PERIOD_D1,1); 
    double ylow = iLow(_Symbol,PERIOD_D1,1); 
    
    double pivot = MathAbs((yhigh + ylow + yclose)/3);  
    double S1 = MathAbs(2 * pivot - yhigh);
    double S2 = MathAbs(pivot - (yhigh - ylow));
    None of those variables are changing. assign the variables in OnStart.
  2.      for(order = 0; order <= OrdersTotal() - 1; order++)
          {
      ...          bool closed = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrRed);
      
    
    In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
  3.                bool pivotstop = OrderModify(OrderTicket(), OrderOpenPrice(), pivot, OrderTakeProfit(),0);
    
    Check your return codes (OrderModify and OrderSelect) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Back to topics list  

To add comments, please log in or register