How can I deal with CCI shift in MQL5

 

Hello everyone 

I have a mql4 code who paint an arrow  when (CCI shift = 2) and CCI(shift =1) > CCI (shift = 2) and I want to convert it into mql5.

In mql4it's possible to include the shift  but with mql5, iCCI can only use 4 arguments(no shift)

Can anyone help me on how can I do it

The mql4 code is like this

 //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
     //Indicator Buffer 1
      if(iCCI(NULL, PERIOD_CURRENT, 14, PRICE_TYPICAL, i) > iCCI(NULL, PERIOD_CURRENT, 14, PRICE_TYPICAL, 1+i)
      && iCCI(NULL, PERIOD_CURRENT, 14, PRICE_TYPICAL, i+1) < iCCI(NULL, PERIOD_CURRENT, 14,  PRICE_TYPICAL, 1+i+1) //Commodity Channel Index crosses above Commodity Channel Index
      && Close[1+i] > iMA(NULL, PERIOD_CURRENT, 55, 0, MODE_EMA, PRICE_CLOSE) //Candlestick Close > Moving Average
      && Close[2+i] > iMA(NULL, PERIOD_CURRENT, 55, 0, MODE_EMA, PRICE_CLOSE) //Candlestick Close > Moving Average
      && Close[3+i] > iMA(NULL, PERIOD_CURRENT, 55, 0, MODE_EMA, PRICE_CLOSE) //Candlestick Close > Moving Average
      && Close[4+i] > iMA(NULL, PERIOD_CURRENT, 55, 0, MODE_EMA, PRICE_CLOSE) //Candlestick Close > Moving Average
      && Close[5+i] > iMA(NULL, PERIOD_CURRENT, 55, 0, MODE_EMA, PRICE_CLOSE) //Candlestick Close > Moving Average
      && iCCI(NULL, PERIOD_CURRENT, 14, PRICE_TYPICAL, 1+i) < CCI_value_buy //Commodity Channel Index < 0
      )
        {
         Buffer1[i] = Low[i]; //Set indicator value at Candlestick Low
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Buy"); //Alert on next bar open
         time_alert = Time[1];
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
 

Please search and read the documentation before you post.

In MQL5, indicator functions return handles, not data. You must obtain the handle in OnInit() and then obtain the data with the CopyBuffer() function.

Documentation on MQL5: Technical Indicators
Documentation on MQL5: Technical Indicators
  • www.mql5.com
Technical Indicators - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Your topic has been moved to the section: Technical Indicators
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

Here's how you can apply a shift to the MQL5 iCCI


#property copyright   "Absolutely forgot to copyright, it's stealable code"
#property description "CCI with shift"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_width1  1
#property indicator_label1  "CCI"
#property indicator_level1  -100.0
#property indicator_level2   100.0

// Indicator input parameters
input int ma_period = 14; // Period
input ENUM_APPLIED_PRICE applied_price = PRICE_CLOSE; //Applied price
input int shift = 0; // Shift

// Indicator handle
int cci_handle;

double plotting_buffer[];
double data[];

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    // Create the CCI indicator
    cci_handle = iCCI(Symbol(), Period(), ma_period, applied_price);

    if (cci_handle == INVALID_HANDLE)
    {
        Print("Failed to create CCI indicator!");
        return(INIT_FAILED);
    }

    // Set the indicator label
    string indicator_name = "CCI(" + IntegerToString(ma_period) + ")";
    IndicatorSetString(INDICATOR_SHORTNAME, indicator_name);

    // Set up the indicator buffers
    SetIndexBuffer(0, plotting_buffer, INDICATOR_DATA);
    SetIndexBuffer(1, data, INDICATOR_CALCULATIONS);

    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    // Release the CCI indicator
    IndicatorRelease(cci_handle);
}

//+------------------------------------------------------------------+
//| Custom indicator calculation function                            |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
    // Check if we have enough bars to calculate
    if (rates_total <= ma_period + shift)
        return(0);
        

    // Calculate the CCI values and copy them to the buffer
    int copied = CopyBuffer(cci_handle, 0, 0, rates_total, data);
   
    if (copied <= 0){
        Print("Failed to copy CCI values!");
        return(0);
    }
    
    for(int i=prev_calculated; i<rates_total; i++){
    
         plotting_buffer[i] = data[i + shift];
    }

    return(rates_total);
}

I hope it helps


EDIT:  

To call this custom indicator then with the shift parameter, you could use iCustom

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

#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_width1  1
#property indicator_label1  "CCI"
#property indicator_level1  -100.0
#property indicator_level2   100.0
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

input int ma_period = 14;
input ENUM_APPLIED_PRICE applied_price = PRICE_CLOSE;
input int shift = 0;

int custom_cci_handle = INVALID_HANDLE;

double test_buf[];

int OnInit(){

   SetIndexBuffer(0, test_buf, INDICATOR_DATA);
   
   custom_cci_handle = iCustom(Symbol(), Period(), "Examples\\CCI_withShift.ex5", ma_period, applied_price, shift); //the modded custom CCI
      
   if (custom_cci_handle == INVALID_HANDLE){
      Print("Still shows as invalid handle. Failed to initialize custom CCI indicator!");
      return INIT_FAILED;
   }

   return(INIT_SUCCEEDED);
}
  
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[]){


    int copied = CopyBuffer(custom_cci_handle, 0, 0, rates_total, test_buf);
   
    if (copied <= 0){
        Print("Failed to copy CCI values!");
        return(0);
    }

   
   return(rates_total);
}
//+------------------------------------------------------------------+
Files: