Log in

Arrays in MQL4

its not a big job just where the array is starting from.... ive had a good go - cant get it working still...

Comments

  • jaguar1637 2836 days ago

    show the code source

  • francisfinley 2836 days ago

    Here are 2 examples of arrays - when i use either one my code they do not work - i get a start error - i am running 10 bars back of an icustom.

    int start()

    {

    //---- last counted bar will be recounted
    int counted_bars = IndicatorCounted();
    if(counted_bars>0) counted_bars--;
    int limit=Bars-counted_bars;

    //---- Current OSMA value
    for(int i = limit-1; i>=0; i--)// Count down for one pass
    OSMAbuffer[i]=iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,i)*10000;

    //---- Highest OSMA value in 20 periods
    for(i = limit-1; i>=0; i--) // Count down for one pass
    Hbuffer[i] = OSMAbuffer[ArrayMaximum(OSMAbuffer, lookbackBars, i)];

    //---- Lowest OSMA value in 20 periods
    for(i= limit-1; i>=0; i--)
    Lbuffer[i] = OSMAbuffer[ArrayMinimum(OSMAbuffer, lookbackBars, i)];

    //----

    return(0);

    }

    //+------------------------------------------------------------------+

    //--------------------------------------------------------------------
    // newbar.mq4
    // The code should be used for educational purpose only.
    //--------------------------------------------------------------------
    extern int Quant_Bars=15; // Amount of bars
    bool New_Bar=false; // Flag of a new bar
    //--------------------------------------------------------------------
    int start() // Special funct. start()
    {
    double Minimum, // Minimal price
    Maximum; // Maximal price
    //--------------------------------------------------------------------
    Fun_New_Bar(); // Function call
    if (New_Bar==false) // If bar is not new..
    return; // ..return
    //--------------------------------------------------------------------
    int Ind_max =ArrayMaximum(High,Quant_Bars,1);// Bar index of max. price
    int Ind_min =ArrayMinimum(Low, Quant_Bars,1);// Bar index of min. price
    Maximum=High[Ind_max]; // Desired max. price
    Minimum=Low[Ind_min]; // Desired min. price
    Alert("For the last ",Quant_Bars, // Show message
    " bars Min= ",Minimum," Max= ",Maximum);
    return; // Exit start()
    }
    //--------------------------------------------------------------------
    void Fun_New_Bar() // Funct. detecting ..
    { // .. a new bar
    static datetime New_Time=0; // Time of the current bar
    New_Bar=false; // No new bar
    if(New_Time!=Time[0]) // Compare time
    {
    New_Time=Time[0]; // Now time is so
    New_Bar=true; // A new bar detected
    }
    }
    //--------------------------------------------------------------------

  • fxez 2836 days ago

    Did you try this to size your arrays before use?

    ArrayResize(OSMAbuffer, limit);

    ArrayResize(Hbuffer, limit);

    ArrayResize(Hbuffer, limit);

    (after limit is set but before the loop where the arrays are accessed)

  • fxez 2836 days ago

    Third one should be:

    ArrayResize(Lbuffer, limit);

  • francisfinley 2836 days ago

    so something like 

    int start()

    {

    //---- last counted bar will be recounted
    int counted_bars = IndicatorCounted();
    if(counted_bars>0) counted_bars--;
    int limit=Bars-counted_bars;

    ArrayResize(OSMAbuffer, limit);

    ArrayResize(Hbuffer, limit);

    ArrayResize(Lbuffer, limit);

     

    //---- Current OSMA value
    for(int i = limit-1; i>=0; i--)// Count down for one pass
    OSMAbuffer[i]=iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,i)*10000;

    //---- Highest OSMA value in 20 periods
    for(i = limit-1; i>=0; i--) // Count down for one pass
    Hbuffer[i] = OSMAbuffer[ArrayMaximum(OSMAbuffer, lookbackBars, i)];

    //---- Lowest OSMA value in 20 periods
    for(i= limit-1; i>=0; i--)
    Lbuffer[i] = OSMAbuffer[ArrayMinimum(OSMAbuffer, lookbackBars, i)];

    //----

    return(0);

     

     

    also is for(int i = limit-1; i>=0; i--)

  • francisfinley 2836 days ago

    keeps cutting text off - also is for(int i = limit-1; i>=0; i--) correct? should it not be for(int i=0; i<20; i++)