Download MetaTrader 5
To add comments, please log in or register
Store your source codes in MQL5 Storage. It is secure!
xavilin
48
xavilin 2014.07.29 14:09 

I am having problems with storing data in arrays in an EA. i think i've done everything OK, but it is obvious i haven't and i can't see where the problem is. I hope anyone sees what i am doing wrong:

//+------------------------------------------------------------------+
//|                                                      prueba1.mq4 |
//|                                             Xavier Pardo Lavella |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Xavier Pardo Lavella"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

//--PARAMETERS
input int MagicNumber = 1234;
input int slippage = 3;
//--INTERNAL VARIABLES
static double Size;
static double lots = 0.1;
int BarsOnChart = 0;
double ATR[];
                              
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
  double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
  if(ticksize == 0.00001 || ticksize == 0.001) 
  pips = ticksize*10;
  else pips = ticksize;
//-----------arrays used as buffers resizing

   ArraySetAsSeries(ATR,false);
   ArrayResize(ATR,MK_Period+1);
   ArraySetAsSeries(ATR,true);


//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
if(IsNewCandle())OrderSize();   
  }
//+------------------------------------------------------------------+
//---GLOBAL FUNCTIONS                                                |
//-------------------------------------------------------------------+

//----Function defining order lots size
double OrderSize(){  
   double TR = iHigh(NULL,0,1)-iLow(NULL,0,1);
   int Counted_bars=iBars(NULL,0); // Number of counted bars
   int i=Bars-Counted_bars-1;           // Index of the first uncounted
   while(i>=0 && i<=MK_Period+1)                      // Loop for uncounted bars
   {ATR[i] = iATR(NULL,0,MK_Period,i);    //array that contains ATR data to calculate its StDev.
   i--;}
   double ATR_STDV = iStdDevOnArray(ATR,0,MK_Period,0,0,1);    // Standard Deviation of ATR on previous bar
   if (TR >= ATR[1] + 1.64*ATR_STDV){                                     //Three situations, three outcomes in Size
   Size = lots*3;}                                                       
   if (TR <= ATR[1] - 1.64*ATR_STDV){
   Size = lots;}
   if (ATR[1] + 1.64*ATR_STDV >= TR && TR >= ATR[1] - 1.64*ATR_STDV){
   Size = lots*2;}
   Print (ATR_STDV,"  ATR StDv");
   Print (Size,"  Size");
   Print (ATR[1],"  ATR1  ",ATR[2],"  ATR2  ");
   return(Size);
   
}     


//----Function checking if it is a new candle
bool IsNewCandle()
{
   if(Bars == BarsOnChart) return(False);
   BarsOnChart = Bars;
   return(True);
}

This is just the part of code that's not working. Tester prints are:

2014.07.29 15:47:38.860    2014.07.29 16:47  prueba1_b_test EURUSD,M1: 0.0  ATR1  0.0  ATR2 
2014.07.29 15:47:38.860    2014.07.29 16:47  prueba1_b_test EURUSD,M1: 0.3  Size
2014.07.29 15:47:38.860    2014.07.29 16:47  prueba1_b_test EURUSD,M1: 0.0  ATR StDv
2014.07.29 15:47:38.860    2014.07.29 16:47  prueba1_b_test EURUSD,M1: 0.0  ATR1  0.0  ATR2 
2014.07.29 15:47:38.860    2014.07.29 16:47  prueba1_b_test EURUSD,M1: 0.3  Size
2014.07.29 15:47:38.860    2014.07.29 16:47  prueba1_b_test EURUSD,M1: 0.0  ATR StDv
2014.07.29 15:47:38.860    2014.07.29 16:47  prueba1_b_test EURUSD,M1: 0.0  ATR1  0.0  ATR2 
2014.07.29 15:47:38.860    2014.07.29 16:47  prueba1_b_test EURUSD,M1: 0.3  Size
2014.07.29 15:47:38.860    2014.07.29 16:47  prueba1_b_test EURUSD,M1: 0.0  ATR StDv
2014.07.29 15:47:38.860    2014.07.29 16:47  prueba1_b_test EURUSD,M1: 0.0  ATR1  0.0  ATR2 
2014.07.29 15:47:38.860    2014.07.29 16:47  prueba1_b_test EURUSD,M1: 0.3  Size
2014.07.29 15:47:38.860    2014.07.29 16:47  prueba1_b_test EURUSD,M1: 0.0  ATR StDv


So the problem is my ATR[] is not storing data, and i dunno why. I dunno if i have to ArrayInitialize or if the problem is something else. Any help appreciated.

Ian Venner
2583
Ian Venner 2014.07.29 17:47  
   int Counted_bars=iBars(NULL,0); // Number of counted bars
   int i=Bars-Counted_bars-1;      // Index of the first uncounted

The result of this is the same as i = Bars - Bars -1 which == -1

Also your method of simulating an indicator buffer is not going to work. When a new bar happens you need to add a new empty index to the front end of the array. Reverse the array using the arraysetasseries function so the value in your zero index is now at the other end, then resize the array to make it one index bigger, the new empty index will then be after that last value then reverse the array back again so it is now at the front. You will need to do that in the main function every new bar not just one time in OnInit. I hope that makes sense.

xavilin
48
xavilin 2014.07.29 19:27  
SDC:

The result of this is the same as i = Bars - Bars -1 which == -1

Also your method of simulating an indicator buffer is not going to work. When a new bar happens you need to add a new empty index to the front end of the array. Reverse the array using the arraysetasseries function so the value in your zero index is now at the other end, then resize the array to make it one index bigger, the new empty index will then be after that last value then reverse the array back again so it is now at the front. You will need to do that in the main function every new bar not just one time in OnInit. I hope that makes sense.


Thanks, i solved it changing that you're saying, changing the array size to limit+1, not just limit (i didn't count the 0) and also by changing the "while" loop for a "for" loop  counting i++:
double OrderSize(){  
   double TR = iHigh(NULL,0,1)-iLow(NULL,0,1);
   
   int Counted_bars=iBars(NULL,0); // Number of counted bars
   int i=Bars-Counted_bars-1;           // Index of the first uncounted
   int limit=MK_Period*2;                      // Loop for uncounted bars
   
   for(i = 0; i<=limit; i++)        // Loop for uncounted bars
   ATR[i] = iATR(NULL,0,MK_Period,i);    //array that contains ATR data to calculate its StDev.
   
   double ATR_STDV = iStdDevOnArray(ATR,0,MK_Period,0,0,1);    // Standard Deviation of ATR on previous bar
   
   if (TR >= ATR[1] + 1.64*ATR_STDV){                                     //Three situations, three outcomes in Size
   Size = lots*3;}                                                       
   if (TR <= ATR[1] - 1.64*ATR_STDV){
   Size = lots;}
   if (ATR[1] + 1.64*ATR_STDV >= TR && TR >= ATR[1] - 1.64*ATR_STDV){
   Size = lots*2;}
   return(Size);
   
}     

int OnInit()
  {
//---
  double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
  if(ticksize == 0.00001 || ticksize == 0.001) 
  pips = ticksize*10;
  else pips = ticksize;
//-----------arrays used as buffers resizing

   ArrayInitialize(ATR,0);
   ArraySetAsSeries(ATR,false);
   ArrayResize(ATR,MK_Period*2+1);
   ArraySetAsSeries(ATR,true);


//---
   return(INIT_SUCCEEDED);
  }
By the way, anybody feel free to use the code. It is kind of a money management (the rest of the code, the one fixing "lots" size i haven't written yet, but will be more-less the standard function based on free margin and risk). This piece of code can be used for breakaways EAs, as it gives more volume to the orders sent in a more volatile scenario.
/
To add comments, please log in or register