I wrote simple expert advisor which doesnt open position. What i missed ?

cuneytates
243

Hi friends;

I wrote simple EA which i want to open buy position when;

1- price bigger then 15 period moving average and 2- Price above highest value of last 12 candle (Like passing demand zone - like passing resistance zone to up) 

Yeah that was my dream but doesnt work.  Anyone tell me where did i went wrong ?


//+------------------------------------------------------------------+
//|                                         Testtttttttttttttttt.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>

CTrade trade;

void OnTick()
  {

MqlRates enYuksekMumlar[];  //Highest candle of last 12 candle (Supply line)
  MqlRates enDusukMumlar[];   //Lowest candle of last 12 candle (Demand line)
  
  ArraySetAsSeries(enYuksekMumlar,true);
  ArraySetAsSeries(enDusukMumlar,true);
  int yuksekData = CopyRates(_Symbol,_Period,0,12,enYuksekMumlar);
  int dusukData  = CopyRates(_Symbol,_Period,0,12,enDusukMumlar);
  
  int HighestCandle;   //create variable for highest candle
  int LowestCandle;
  double High [];      //create an array for price data
  double Low[];
  
  ArraySetAsSeries(High,true); //sort the array from current candle downwards
  ArraySetAsSeries(Low,true);
  CopyHigh(_Symbol,_Period,0,120,High); //fill the array with high prices
  CopyLow(_Symbol,_Period,0,120,Low);
  HighestCandle = ArrayMaximum(High,0,120);
  LowestCandle = ArrayMinimum(Low,0,120);
  
  // draw highest candle line for visualisation
  ObjectCreate(_Symbol,"En yüksek", OBJ_HLINE,0,0,enYuksekMumlar[HighestCandle].high); //set object properties for line
  ObjectSetInteger(0,"En yüksek",OBJPROP_COLOR,clrMagenta);    //color
  ObjectSetInteger(0,"En yüksek",OBJPROP_WIDTH,2);           //width
  ObjectMove(_Symbol,"En yüksek",0,0,enYuksekMumlar[HighestCandle].high);  //move the object
  // draw lowest line candle line for visualisation
  ObjectCreate(_Symbol,"En düşük", OBJ_HLINE,0,0,enDusukMumlar[LowestCandle].low); //set object properties for line
  ObjectSetInteger(0,"En düşük",OBJPROP_COLOR,clrPink);    //color
  ObjectSetInteger(0,"En düşük",OBJPROP_WIDTH,2);           //width
  ObjectMove(_Symbol,"En düşük",0,0,enDusukMumlar[LowestCandle].low);  //move the object
       
        
 double  Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); 
 double  Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); 
         
 string signal = "";  
      
         
      
         double timeH1_m15[];  // 5 minutes iMA15  expo close
         int timeH1_m15Defination = iMA(_Symbol,_Period,15,0,MODE_EMA,PRICE_CLOSE); 
         ArraySetAsSeries(timeH1_m15,true); 
         CopyBuffer(timeH1_m15Defination,0,0,3,timeH1_m15); 
         
      
         double m5kapanis = iClose(_Symbol,_Period,0);   //close bar of m5



//////////////// BUY AL - KAPA KOŞULLARI ////////////////////////////         
         if 
 (    
         Bid > High[1] 
         &&
         Bid > timeH1_m15[1]           
 )
        
         { signal = "buy";}
  
  
         if 
         (signal == "buy" && PositionsTotal() == 0 && PositionsTotal() < 2 )
               {
               trade.Buy(0.10,NULL,Ask,NULL,NULL,NULL);
               }
   
         
         //close buy - işlem kapama
         if ( Bid < timeH1_m15[1]) 
         {
         closeBuy ();
         }
           
  
////// SELL SAT - KAPA KOŞULLARI /////////////     
         
         if 
( 
         Bid <Low[1]
         &&
         Bid < timeH1_m15[0] 
        
)
            { signal = "sell"; }

         if 
         (signal == "sell" && PositionsTotal() == 0 && PositionsTotal() < 2 )
         trade.Sell(0.10,NULL,Bid,NULL,NULL,NULL);
         
         
         //sell işlem kapama
         if (Bid > timeH1_m15[1]) //&& PositionsTotal()>0 
         {
         closeSell ();
         }
        

   
  }  //Bottom of OnTick function


void closeBuy()   //buy close kapama
   {
   for (int i=PositionsTotal() -1; i>=0; i--)
      {
      ulong ticket = PositionGetTicket(i);            
      string CurrencyPair = PositionGetSymbol(i);  
      ulong PositionDirection = PositionGetInteger(POSITION_TYPE);  
      
      if (PositionDirection == POSITION_TYPE_BUY) 
      if (_Symbol == CurrencyPair)
      
         {
               trade.PositionClose(ticket);

         }
      }
   }
   
void closeSell()  // sell close
   {
   for (int i=PositionsTotal() -1; i>=0; i--)
      {
      ulong ticket = PositionGetTicket(i);   
      string CurrencyPair = PositionGetSymbol(i);  
      ulong PositionDirection = PositionGetInteger(POSITION_TYPE);  
      
      if (PositionDirection == POSITION_TYPE_SELL) 
      if (_Symbol == CurrencyPair)
         {
               trade.PositionClose(ticket);

         }
      }
   }



William Roeder
26081
William Roeder  
         int timeH1_m15Defination = iMA(_Symbol,_Period,15,0,MODE_EMA,PRICE_CLOSE); 

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020.03.08)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020.07.05)
          How to call indicators in MQL5 - MQL5 Articles (2010)

cuneytates
243
cuneytates  
William Roeder #:

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020.03.08)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020.07.05)
          How to call indicators in MQL5 - MQL5 Articles (2010)

Thank you for your reply Mr Roeder,

i can handle iMA later. I think my main problem with high and low value for open position.

I erased iMA, what about your suggestion now without RTFM and STFW ?

//+------------------------------------------------------------------+
//|                                         Testtttttttttttttttt.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>

CTrade trade;

void OnTick()
  {

MqlRates enYuksekMumlar[];  //Highest candle of last 12 candle (Supply line)
  MqlRates enDusukMumlar[];   //Lowest candle of last 12 candle (Demand line)
  
  ArraySetAsSeries(enYuksekMumlar,true);
  ArraySetAsSeries(enDusukMumlar,true);
  int yuksekData = CopyRates(_Symbol,_Period,0,12,enYuksekMumlar);
  int dusukData  = CopyRates(_Symbol,_Period,0,12,enDusukMumlar);
  
  int HighestCandle;   //create variable for highest candle
  int LowestCandle;
  double High [];      //create an array for price data
  double Low[];
  
  ArraySetAsSeries(High,true); //sort the array from current candle downwards
  ArraySetAsSeries(Low,true);
  CopyHigh(_Symbol,_Period,0,120,High); //fill the array with high prices
  CopyLow(_Symbol,_Period,0,120,Low);
  HighestCandle = ArrayMaximum(High,0,120);
  LowestCandle = ArrayMinimum(Low,0,120);
  
  // draw highest candle line for visualisation
  ObjectCreate(_Symbol,"En yüksek", OBJ_HLINE,0,0,enYuksekMumlar[HighestCandle].high); //set object properties for line
  ObjectSetInteger(0,"En yüksek",OBJPROP_COLOR,clrMagenta);    //color
  ObjectSetInteger(0,"En yüksek",OBJPROP_WIDTH,2);           //width
  ObjectMove(_Symbol,"En yüksek",0,0,enYuksekMumlar[HighestCandle].high);  //move the object
  // draw line candle line for visualisation
  ObjectCreate(_Symbol,"En düşük", OBJ_HLINE,0,0,enDusukMumlar[LowestCandle].low); //set object properties for line
  ObjectSetInteger(0,"En düşük",OBJPROP_COLOR,clrPink);    //color
  ObjectSetInteger(0,"En düşük",OBJPROP_WIDTH,2);           //width
  ObjectMove(_Symbol,"En düşük",0,0,enDusukMumlar[LowestCandle].low);  //move the object
       
        
 double  Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); 
 double  Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); 
         
 string signal = "";  
      
         
    
         
      
         double m5kapanis = iClose(_Symbol,_Period,0);   //close bar of m5



//////////////// BUY AL - KAPA KOŞULLARI ////////////////////////////         
         if 
 (    
         Bid > High[1] 
                
 )
        
         { signal = "buy";}
  
  
         if 
         (signal == "buy" && PositionsTotal() == 0 && PositionsTotal() < 2 )
               {
               trade.Buy(0.10,NULL,Ask,NULL,NULL,NULL);
               }
   
         
         //close buy - işlem kapama
         if ( Bid < Low[1]) 
         {
         closeBuy ();
         }
           
  
////// SELL SAT - KAPA KOŞULLARI /////////////     
         
         if 
( 
         Bid <Low[1]
        
        
)
            { signal = "sell"; }

         if 
         (signal == "sell" && PositionsTotal() == 0 && PositionsTotal() < 2 )
         trade.Sell(0.10,NULL,Bid,NULL,NULL,NULL);
         
         
         //sell işlem kapama
         if (Bid > High[1]) //&& PositionsTotal()>0 
         {
         closeSell ();
         }
        

   
  }  //Bottom of OnTick function


void closeBuy()   //buy close kapama
   {
   for (int i=PositionsTotal() -1; i>=0; i--)
      {
      ulong ticket = PositionGetTicket(i);            
      string CurrencyPair = PositionGetSymbol(i);  
      ulong PositionDirection = PositionGetInteger(POSITION_TYPE);  
      
      if (PositionDirection == POSITION_TYPE_BUY) 
      if (_Symbol == CurrencyPair)
      
         {
               trade.PositionClose(ticket);

         }
      }
   }
   
void closeSell()  // sell close
   {
   for (int i=PositionsTotal() -1; i>=0; i--)
      {
      ulong ticket = PositionGetTicket(i);   
      string CurrencyPair = PositionGetSymbol(i);  
      ulong PositionDirection = PositionGetInteger(POSITION_TYPE);  
      
      if (PositionDirection == POSITION_TYPE_SELL) 
      if (_Symbol == CurrencyPair)
         {
               trade.PositionClose(ticket);

         }
      }
   }

Carl Schreiber
Moderator
12963
Carl Schreiber  

If one's EA compiles without any error but doesn't behave as expected use the debugger to control the relevant variables it is made for exactly this!

https://www.metatrader5.com/en/metaeditor/help/development/debug
https://www.mql5.com/en/articles/2041 // Error Handling and Logging in MQL5
https://www.mql5.com/en/articles/272
https://www.mql5.com/en/articles/35 // scrol down to: "Launching and Debuggin"

Code debugging - Developing programs - MetaEditor Help
Code debugging - Developing programs - MetaEditor Help
  • www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...