An array is out of range - page 2

GergelyB
44
GergelyB  

I dont? is there a function that "shows" the "wick-length"? and I can check that on the last bar when new bar is detected or something?

(Thank you very much for the arrayresize quick lesson :) ) 

Alain Verleyen
41263
Alain Verleyen  
GergelyB:

I think sharing with you my goal would help this a lot.

 I would like to identify the bars where ther last price is the highest or lowest in the bar (basicly that means in a japancandelstick graph that there is no little "wick"? or how its called on the top or bottom of the candle, coming to the conclusion that the market is on a up or down trend respectively (at least for the next few ticks) and I try to use zeromemory if i find newbars.

Opening the position and close it shortly after (with relativly low take profit) 

Example for upper wick :

//--- just need to be checked on each new bar
   double high[],close[];
   ArraySetAsSeries(high,true);ArraySetAsSeries(close,true);

   int copied1=CopyHigh(Symbol(),Period(),1,1000,high);
   int copied2=CopyClose(Symbol(),Period(),1,1000,close);
//---
   if(copied1!=-1 && copied2!=-1)
     {
      int copied=MathMin(copied1,copied2);
      for(int i=0;i<copied;i++)
        {
         if(high[i]==close[i])
           {
            // japanese candelstick that there is no little "wick"
            printf("Candle %i doesn't have an UPPER wick",i);
           }
        }
     }
   else
     {
      // error processing
     }
GergelyB
44
GergelyB  
Alain Verleyen:

Example for upper wick :

Thank you, I think I follow all but one aspect of the example code you posted.

 for(int i=0;i<copied;i++)   
        {}

 whats the reson for the For cycle? shouldn't I just use if(high[1]=close[1]) to determine if there is no "upper wick" and the same with CopyLow I assume? (since I want to see if the last closed bar had a wick at the end and try to open a position according to it on the next bar)

 

edit: or I misunderstand the meaning of high_ and low_ arrays , and also shouldn't i use MathMax if i search for the highest price that I want to check if its = to the close price?

Alain Verleyen
41263
Alain Verleyen  
GergelyB:

Thank you, I think I follow all but one aspect of the example code you posted.

 whats the reson for the For cycle? shouldn't I just use if(high[1]=close[1]) to determine if there is no "upper wick" and the same with CopyLow I assume? (since I want to see if the last closed bar had a wick at the end and try to open a position according to it on the next bar)

 

edit: or I misunderstand the meaning of high_ and low_ arrays , and also shouldn't i use MathMax if i search for the highest price that I want to check if its = to the close price?

It was only an example, checking the last 1000 candles. Just adapt it to your needs.
GergelyB
44
GergelyB  
ayay commandante
GergelyB
44
GergelyB  

Hopefully I succesfully handled the arrays, but I have one and a half problems.

The half problem is that the EA tries to open a position instantly when its started succesfully (I assume both arrayvalue is zero therefore they are equal, I should add if xy = 0 then return something)

but my real problem is that I get error 4756.

I found this articel about it: https://www.mql5.com/en/forum/5431

I had the ZeroMemory where the thread suggest it and changed int STP to double.

I can only suspect that I have some problem with my account settings (im on a demo account) but I have no idea what can cause it or even where should I look in to it)

Here is my code: (I even tried stoploss=0 from tha thread I linked)

input int      StopLoss=0;
input int      TakeProfit=15;
input int      EA_MagicBuy=12345;
input int      EA_MagicSell=54321;
input double   Lot=1;                           //Lots to trade
input string Symbol="NZDJPY";
double STP, TKP;
int i = 0; //for array resizing
double p_last[];
double p_high[];
double p_low[];

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
  // So we can change SL or TP
   STP = StopLoss;
   TKP = TakeProfit;
    if(_Digits==5 || _Digits==3)
     {
      STP = STP*10;
      TKP = TKP*10;
     }

 and my OnTick:

void OnTick()
  {

  ArrayResize(p_high,ArraySize(p_high)+1,100);
  ArrayResize(p_low,ArraySize(p_low)+1,100);
  ArrayResize(p_last,ArraySize(p_last)+1,100);
  ArraySetAsSeries(p_high,true);
  ArraySetAsSeries(p_low,true);
  ArraySetAsSeries(p_last,true);
  
  int copiedhigh = CopyHigh(Symbol(),Period(),0,1,p_high);
  int copiedlow = CopyLow(Symbol(),Period(),0,1,p_low);
  int copiedlast = CopyClose(Symbol(),Period(),0,1,p_last);

  //trades open?-----------------------------------------
   bool Buy_opened=false;  
   bool Sell_opened=false;
   
    if(PositionSelect(Symbol)==true) // we have an opened position
     {
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
        {
         Buy_opened=true;  //It is a Buy
        }
      else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
        {
         Sell_opened=true; // It is a Sell
        }
     }
   
  //-----------------------------------------------------

  //watching for new bars--------------------------------
  static datetime Old_Time = TimeCurrent();
  datetime New_Time[1];
  bool IsNewBar=false;

      
  int CopiedTime=CopyTime(Symbol,_Period,0,1,New_Time);
   if(CopiedTime>0) // ok, the data has been copied successfully
     {
      if(Old_Time!=New_Time[0]) // if old time isn't equal to new bar time. 1970? Nope 
        {
         IsNewBar=true;   // if it isn't a first call, the new bar has appeared
         int copiedhigh = CopyHigh(Symbol(),Period(),1,1,p_high);
         int copiedlow = CopyLow(Symbol(),Period(),1,1,p_low);
         int copiedlast = CopyClose(Symbol(),Period(),1,1,p_last);
         if(MQL5InfoInteger(MQL5_DEBUGGING)) Print("We have new bar here ",New_Time[0]," old time was ",Old_Time," higest price: ",p_high[0]," lowest price: ",p_low[0]," Close price: ",p_last[0]);
         Old_Time=New_Time[0];            // saving bar time
        }
     }
   else
     {
      Alert("Error in copying historical times data, error =",GetLastError());
      ResetLastError();
      return;
     }
     
  ArrayResize(p_high,ArraySize(p_high)+1,100);
  ArrayResize(p_low,ArraySize(p_low)+1,100);
  ArrayResize(p_last,ArraySize(p_last)+1,100);
     
 //---- Ez nem lehetne feljebb? a is newbar kivételével? -------------      
   MqlTradeRequest mrequest;  
   MqlTradeResult mresult;    
   MqlRates mrate[];          // To be used to store the prices, volumes and spread of each bar
   //ZeroMemory(mrequest);      // wiping of mrequest structure, done in the position opening
   //ZeroMemory(mresult);
   
   ArraySetAsSeries(mrate,true);
   ArrayResize(mrate,ArraySize(mrate)+1,1000);
//---------------------------
//---------------------------
if (IsNewBar)
   if(copiedhigh != -1 && copiedlow != -1 && copiedlast != -1)
     {
      //int copied=MathMin(copiedhigh,copiedlow);//Ez csak akkor kéne, ha több Bar-t nézzünk?
      //p_last[0] = p_close;
      //Print("Adat copizás sikeres!");
        /*
      for(int i=0;i<copied;i++)
        {
         if(high[i]==close[i])
           {
            // japanese candelstick that there is no little "wick"
            printf("Candle %i doesn't have an UPPER wick",i);
           }
        }
        */        
     }
   else
     {
            Alert("The Copy order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;         
     }
//----------
   
   bool BuyCondition = (p_high[1] = p_last[1]);
   bool SellCondition = (p_low[1] = p_last[1]);
   
   if(BuyCondition && IsNewBar && !Buy_opened) //ORDER TIMEEEE!!!
   {     
         ZeroMemory(mrequest);
         ZeroMemory(mresult);
         mrequest.action = TRADE_ACTION_DEAL;                                  // immediate order execution
         mrequest.price = NormalizeDouble(p_high[1],_Digits);           // latest ask price
         mrequest.sl = NormalizeDouble(p_high[1] - STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(p_high[1] + TKP*_Point,_Digits); // Take Profit
         mrequest.symbol = Symbol;                                            // currency pair
         mrequest.volume = Lot;                                                 // number of lots to trade
         mrequest.magic = EA_MagicBuy;                                             // Order Magic Number
         mrequest.type = ORDER_TYPE_BUY;                                        // Buy Order
         mrequest.type_filling = ORDER_FILLING_FOK;                             // Order execution type
         mrequest.deviation=100;                                                // Deviation from current price
         //--- send order
         OrderSend(mrequest,mresult);
         // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed according to a lovely post
           {
            Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Alert("The Buy order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;
           }
       
    }
 
   if(SellCondition && IsNewBar && !Sell_opened) //ORDER TIMEEEE!!!
   {
         ZeroMemory(mrequest);
         ZeroMemory(mresult);
         mrequest.action = TRADE_ACTION_DEAL;                                  // immediate order execution
         mrequest.price = NormalizeDouble(p_low[1],_Digits);           // latest ask price
         mrequest.sl = NormalizeDouble(p_low[1] - STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(p_low[1] + TKP*_Point,_Digits); // Take Profit
         mrequest.symbol = Symbol;                                            // currency pair
         mrequest.volume = Lot;                                                 // number of lots to trade
         mrequest.magic = EA_MagicSell;                                             // Order Magic Number
         mrequest.type = ORDER_TYPE_SELL;                                        // Sell Order
         mrequest.type_filling = ORDER_FILLING_FOK;                             // Order execution type
         mrequest.deviation=100;                                                // Deviation from current price
         //--- send order
         OrderSend(mrequest,mresult);
         // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
            Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Alert("The Buy order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;
           }
    }
  
 if(IsNewBar)
   {
   ZeroMemory(mrate); // do i need this here?
   ZeroMemory(p_last);
   ZeroMemory(p_high);
   ZeroMemory(p_low);
   } 
  }

 PS.: I'm sorry if I should have opened a new thread for this, if a moderator comes along and tells me so I move it in to a new one, but I'm actually not sure if these are linked or not to the original problem, probably not.

PS2.: Also I know I should do the copied or not check somewhere after I actually copied....