How to Subtract the current price from the previous price (of 2 hours) any hints.

qjol
3201
qjol  
pls explain, the current price or the price from 2 hrs ago ?
William Roeder
25849
William Roeder  
tafadzwa:  Can anyone show me how to subtract the current price from the previous price (of 2 hours)
  1. If the chart period is small enough:
    #define HR0200  7200    // 2 * 3600
    double priceM2Hr = Close[HR0200/Period()];
    

  2. If within one minute is close enough:
    #define HR0200  7200    // 2 * 3600
    datetime now  = TimeCurrent(),
             m2Hr = now - HR0200;                       // 2 hours ago
    int iM1 = iBarShift(NULL, PERIOD_M1, m2Hr);         // M1 bar 2 hours ago.
    double priceM2Hr = iClose(NULL, PERIOD_M1, iM1);

  3. You insist on getting the price of a specific time you must keep every tick (price and time) in a dynamic array.
    Not compiled, not tested
    extern int TickHistoryMax.Seconds = 7200;
    int start(){
        SaveTick();
        datetime now = Timecurrent();
        double   twoHrAgo = GetTick(now - 7200);
        :
    }
    #define TICK_DT   0
    #define TICK_BID  1
        #define TICK_SIZE 2
    double ticks[][TICK_SIZE];
    void SaveTick(){
        int nBars = ArrayRange(ticks, 0);   ResizeBuffer(ticks, nBars+1);
        datetime now = TimeCurrent();
        ticks[0][TICK_DT] = now;        ticks[0][TICK_BID] = Bid;
    
        if (ticks[nBars][TICK_DT] - now > TickHistoryMax.Seconds*1.1){
            nBars = iGetTick(now + TickHistoryMax.Seconds);
            ArrayResize(ticks, nBars);
        }
    }
    double GetTick(datetime when){  return( ticks[iGetTick(when)][TICK_BID] );  }
    int iGetTick(datetime when){
        int iOld = ArrayRange(ticks, 0)-1;
        int iNew = 0;
        while(iOld < iNew){
            int iTm = (iOld+iNew)/2;
            if (tick[iTm] <= when)   iOld = iTm; else iNew = iTm - 1;
        }
        return(iTm);
    }
    bool    ResizeBuffer(double& buffer[], int size){
        if (ArrayRange(buffer,0) != size){      // ArrayRange allows 1D or 2D arrays
            ArraySetAsSeries(buffer, false);    // Shift values B[2]=B[1]; B[1]=B[0]
            if (ArrayResize(buffer, size) <= 0){ DisableTrading(
                "ArrayResize [2] failed: " + GetLastError() );  return(false);  }
            ArraySetAsSeries(buffer, true);
        }
        return(true);
    }
    
    Not compiled, not tested
tafadzwa
117
tafadzwa  
WHRoeder:
  1. If the chart period is small enough:
  2. If within one minute is close enough:
  3. You insist on getting the price of a specific time you must keep every tick (price and time) in a dynamic array.
    Not compiled, not testedNot compiled, not tested

Thanks so much. Compiled an error shows up. Again thanks.