A guid for calling indicator #CRS

1 November 2019, 19:44
Ziheng Zhuang
1
1 020


Indicator#CRS 

MT4 Version:  https://www.mql5.com/en/market/product/24310

MT5 Version:  https://www.mql5.com/en/market/product/121008

The code for your reference.

//+------------------------------------------------------------------+
//|                                                     CRS-Demo.mq4 |
//|                                           Copyright 2019,fxMeter |
//|                            https://www.mql5.com/en/users/fxmeter |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019,fxMeter"
#property link      "https://www.mql5.com/en/users/fxmeter"
#property version   "1.00"
#property strict

input string CRS_Setting="------------------------";
input ENUM_TIMEFRAMES CRS_TimeFrame=PERIOD_D1;
input int BarsToCalculate=90;
input int MaPeriodsToSmoothLines=0;
input string Suffix="";


string Currency[8]= {"USD","EUR","GBP","AUD","NZD","CAD","CHF","JPY"};

//basic definition: CURRENCY_BASE and CURRENCY_MARGIN from SymbolInfoString()
//EURUSD:  EUR is currency base, USD is currency margin
//USDJPY:  USD is currency base, JPY is currency margin
string currencyBase,currencyMargin; //the current symbol of chart: EUR,USD
double currencyBasePower[5]= {0},currencyMarginPower[5]= {0};
double powerDiff[5]= {0};// power difference = base power - margin power

//-- index1: the index of currency base in array Currency[8]
//-- index2: the index of currency margin in array Currency[8]
int index1,index2;
double overBoughtLevel = 80,overSoldLevel=20;

//---struct
struct StructCurrencyPower
  {
   double            value;
   string            currency;
  };
StructCurrencyPower AllCurPower[8]; //all 8 currency power
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//--- get the currency base / currency margin from the current symbol
   currencyBase=StringSubstr(Symbol(),0,3);
   currencyMargin=StringSubstr(Symbol(),3,3);

//--- index1,index2
   for(int i=0; i<8; i++)
     {
      if(currencyBase==Currency[i])
        {
         index1=i;
        }
      if(currencyMargin==Currency[i])
        {
         index2=i;
        }
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

//---
   GetCurrentChartPower();

//--1. buy signal -- currency base cross up currency margin
   bool buySignal  =   powerDiff[1]>0 && powerDiff[2]<0;

//--2. sell signal -- currency base cross down currency margin
   bool sellSignal =   powerDiff[1]<0 && powerDiff[2]>0;

//--3. over bought -- currency base over bought
   bool baseOverBought = currencyBasePower[1]<overBoughtLevel && currencyBasePower[2]>=overBoughtLevel;

//--4. over sold -- currency base over sold
   bool baseOverSold = currencyBasePower[1]>overSoldLevel && currencyBasePower[2]<=overSoldLevel;

//--5. over bought -- currency margin over bought
   bool marginOverBought = currencyMarginPower[1]<overBoughtLevel && currencyMarginPower[2]>=overBoughtLevel;

//--6. over sold -- currency margin over sold
   bool marginOverSold = currencyMarginPower[1]>overSoldLevel && currencyMarginPower[2]<=overSoldLevel;

//--7. get all 8 currency power,and sort
    GetAllPower(0);  //0: the current bar 
    SortAllPower();
    //--try to comment on chart
    string text="";
    for(int i=7;i>=0;i--)
      {
        text += AllCurPower[i].currency +","+DoubleToString(AllCurPower[i].value,2)+"\n";
      }
    Comment(text);

//---
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void GetCurrentChartPower()
  {
   double v=0;
   for(int i=0; i<5; i++)
     {
      v=iCustom(NULL,CRS_TimeFrame,"\\Market\\Currency Relative Strength",
                false,BarsToCalculate,MaPeriodsToSmoothLines,Suffix,
                index1,i);
      currencyBasePower[i]=NormalizeDouble(v,2);
     }
   for(int i=0; i<5; i++)
     {
      v=iCustom(NULL,CRS_TimeFrame,"\\Market\\Currency Relative Strength",
                false,BarsToCalculate,MaPeriodsToSmoothLines,Suffix,
                index2,i);
      currencyMarginPower[i]=NormalizeDouble(v,2);
     }

   for(int i=0; i<5; i++)
     {
      powerDiff[i]=currencyBasePower[i]-currencyMarginPower[i];
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetUsdPower(int shift)
  {
   double  v=iCustom(NULL,CRS_TimeFrame,"\\Market\\Currency Relative Strength",
                     false,BarsToCalculate,MaPeriodsToSmoothLines,Suffix,0,shift);
   v = NormalizeDouble(v,2);
   return(v);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetEurPower(int shift)
  {
   double  v=iCustom(NULL,CRS_TimeFrame,"\\Market\\Currency Relative Strength",
                     false,BarsToCalculate,MaPeriodsToSmoothLines,Suffix,1,shift);
   v = NormalizeDouble(v,2);
   return(v);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetGbpPower(int shift)
  {
   double  v=iCustom(NULL,CRS_TimeFrame,"\\Market\\Currency Relative Strength",
                     false,BarsToCalculate,MaPeriodsToSmoothLines,Suffix,2,shift);
   v = NormalizeDouble(v,2);
   return(v);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetAudPower(int shift)
  {
   double  v=iCustom(NULL,CRS_TimeFrame,"\\Market\\Currency Relative Strength",
                     false,BarsToCalculate,MaPeriodsToSmoothLines,Suffix,3,shift);
   v = NormalizeDouble(v,2);
   return(v);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetNzdPower(int shift)
  {
   double  v=iCustom(NULL,CRS_TimeFrame,"\\Market\\Currency Relative Strength",
                     false,BarsToCalculate,MaPeriodsToSmoothLines,Suffix,4,shift);
   v = NormalizeDouble(v,2);
   return(v);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetCadPower(int shift)
  {
   double  v=iCustom(NULL,CRS_TimeFrame,"\\Market\\Currency Relative Strength",
                     false,BarsToCalculate,MaPeriodsToSmoothLines,Suffix,5,shift);
   v = NormalizeDouble(v,2);
   return(v);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetChfPower(int shift)
  {
   double  v=iCustom(NULL,CRS_TimeFrame,"\\Market\\Currency Relative Strength",
                     false,BarsToCalculate,MaPeriodsToSmoothLines,Suffix,6,shift);
   v = NormalizeDouble(v,2);
   return(v);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetJpyPower(int shift)
  {
   double  v=iCustom(NULL,CRS_TimeFrame,"\\Market\\Currency Relative Strength",
                     false,BarsToCalculate,MaPeriodsToSmoothLines,Suffix,7,shift);
   v = NormalizeDouble(v,2);
   return(v);
  }
//+------------------------------------------------------------------+
//---Get all 8 currency power
void GetAllPower(int shift)
  {
   for(int i=0; i<8; i++)
     {
      AllCurPower[i].currency = Currency[i];
     }
   for(int i=0; i<8; i++)
     {
      double v=iCustom(NULL,CRS_TimeFrame,"\\Market\\Currency Relative Strength",
                       false,BarsToCalculate,MaPeriodsToSmoothLines,Suffix,i,shift);
      AllCurPower[i].value = NormalizeDouble(v,2);
     }
  }
//--- sort AllCurPower
void SortAllPower()
  {
   int total = ArraySize(AllCurPower);
   bool sorted=false;
   StructCurrencyPower tmp;
   while(!sorted)
     {
      sorted=true;
      for(int i=1; i<total; i++)
        {
         if(AllCurPower[i-1].value>AllCurPower[i].value)
           {
            tmp=AllCurPower[i-1];
            AllCurPower[i-1]=AllCurPower[i];
            AllCurPower[i]=tmp;
            sorted=false;
           }
        }
      total--;
     }
  }
//+------------------------------------------------------------------+


MT5 Version:

CRS-MT5-DEMO


The code for your reference:

//+------------------------------------------------------------------+
//|                                                     CRS-Demo.mq5 |
//|                                           Copyright 2019,fxMeter |
//|                            https://www.mql5.com/en/users/fxmeter |
//+------------------------------------------------------------------+
// Currency Relative Strength MT5 
// https://www.mql5.com/zh/market/product/121008 
// https://www.mql5.com/en/blogs/post/730961
#property copyright "Copyright 2019,fxMeter"
#property link      "https://www.mql5.com/en/users/fxmeter"
#property version   "1.00"
#property strict

#define  CRS_NAME    "Market\\Currency Relative Strength MT5"

input string CRS_Setting="------------------------"; //CRS Setting 
input ENUM_TIMEFRAMES CRS_TimeFrame =  PERIOD_D1;   //CRS Timeframe
input int         BarsToShow = 90;                 //Bars To Show
input int         MaPeriodsToSmoothLines = 0;      //Ma Periods to Smooth Curves

string Currency[8]= {"USD","EUR","GBP","AUD","NZD","CAD","CHF","JPY"};

//basic definition: CURRENCY_BASE and CURRENCY_MARGIN from SymbolInfoString()
//EURUSD:  EUR is currency base, USD is currency margin
//USDJPY:  USD is currency base, JPY is currency margin
string currencyBase,currencyMargin; //the current symbol of chart: EUR,USD
double currencyBasePower[5]= {0},currencyMarginPower[5]= {0};
double powerDiff[5]= {0};// power difference = base power - margin power

//-- index1: the index of currency base in array Currency[8]
//-- index2: the index of currency margin in array Currency[8]
int index1,index2;
double overBoughtLevel = 80,overSoldLevel=20;

//---struct
struct StructCurrencyPower
  {
   double            value;
   string            currency;
  };
StructCurrencyPower AllCurPower[8]; //all 8 currency power

int handle = -1;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- get the currency base / currency margin from the current symbol
   currencyBase=StringSubstr(Symbol(),0,3);
   currencyMargin=StringSubstr(Symbol(),3,3);

//--- index1,index2
   for(int i=0; i<8; i++)
     {
      if(currencyBase==Currency[i])
        {
         index1=i;
        }
      if(currencyMargin==Currency[i])
        {
         index2=i;
        }
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  if(handle==-1)
  {
    ResetLastError();
    handle = iCustom(Symbol(),CRS_TimeFrame,CRS_NAME,false,BarsToShow,MaPeriodsToSmoothLines,0);
    if(handle==-1)
    {
      printf("Failed to create handle for CRS, error code: %d",GetLastError());
      return;
    }
  }
  
  if(handle!=-1)
  {
    if(BarsCalculated(handle)==-1)
    {
      printf("CRS data is not calculated yet");
      return;
    }
  }


//---
   GetCurrentChartPower();

//--1. buy signal -- currency base cross up currency margin
   bool buySignal  =   powerDiff[1]>0 && powerDiff[2]<0;

//--2. sell signal -- currency base cross down currency margin
   bool sellSignal =   powerDiff[1]<0 && powerDiff[2]>0;

//--3. over bought -- currency base over bought
   bool baseOverBought = currencyBasePower[1]<overBoughtLevel && currencyBasePower[2]>=overBoughtLevel;

//--4. over sold -- currency base over sold
   bool baseOverSold = currencyBasePower[1]>overSoldLevel && currencyBasePower[2]<=overSoldLevel;

//--5. over bought -- currency margin over bought
   bool marginOverBought = currencyMarginPower[1]<overBoughtLevel && currencyMarginPower[2]>=overBoughtLevel;

//--6. over sold -- currency margin over sold
   bool marginOverSold = currencyMarginPower[1]>overSoldLevel && currencyMarginPower[2]<=overSoldLevel;

//--7. get all 8 currency power,and sort
    GetAllPower(0);  //0: the current bar 
    SortAllPower();
    //--try to comment on chart
    string text="";
    for(int i=7;i>=0;i--)
      {
        text += AllCurPower[i].currency +","+DoubleToString(AllCurPower[i].value,2)+"\n";
      }
    Comment(text);

//---
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void GetCurrentChartPower()
  {
   if(handle==-1)return;
   double v1[],v2[];
   ArraySetAsSeries(v1,true);
   ArraySetAsSeries(v2,true);
   if(CopyBuffer(handle,index1,0,5,v1)!=5)return;
   if(CopyBuffer(handle,index2,0,5,v2)!=5)return;
   
   for(int i=0;i<5;i++)
     {
       currencyBasePower[i]=NormalizeDouble(v1[i],2);
       currencyMarginPower[i]=NormalizeDouble(v2[i],2);
       powerDiff[i]=currencyBasePower[i]-currencyMarginPower[i];
     } 
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetUsdPower(int shift)
  {
   int buff = 0;
   double  v[1];
   if(CopyBuffer(handle,buff,shift,1,v)!=1)return(0.0);  
   return(NormalizeDouble(v[0],2));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetEurPower(int shift)
  {
   int buff = 1;
   double  v[1];
   if(CopyBuffer(handle,buff,shift,1,v)!=1)return(0.0);  
   return(NormalizeDouble(v[0],2));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetGbpPower(int shift)
  {
   int buff = 2;
   double  v[1];
   if(CopyBuffer(handle,buff,shift,1,v)!=1)return(0.0);  
   return(NormalizeDouble(v[0],2));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetAudPower(int shift)
  {
   int buff = 3;
   double  v[1];
   if(CopyBuffer(handle,buff,shift,1,v)!=1)return(0.0);  
   return(NormalizeDouble(v[0],2));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetNzdPower(int shift)
  {
   int buff = 4;
   double  v[1];
   if(CopyBuffer(handle,buff,shift,1,v)!=1)return(0.0);  
   return(NormalizeDouble(v[0],2));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetCadPower(int shift)
  {
   int buff = 5;
   double  v[1];
   if(CopyBuffer(handle,buff,shift,1,v)!=1)return(0.0);  
   return(NormalizeDouble(v[0],2));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetChfPower(int shift)
  {
   int buff = 6;
   double  v[1];
   if(CopyBuffer(handle,buff,shift,1,v)!=1)return(0.0);  
   return(NormalizeDouble(v[0],2));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetJpyPower(int shift)
  {
   int buff = 7;
   double  v[1];
   if(CopyBuffer(handle,buff,shift,1,v)!=1)return(0.0);  
   return(NormalizeDouble(v[0],2));
  }
//+------------------------------------------------------------------+
//---Get all 8 currency power
void GetAllPower(int shift)
  {
   for(int i=0; i<8; i++)
     {
      AllCurPower[i].currency = Currency[i];
     }
  
    AllCurPower[0].value = GetUsdPower(shift);
    AllCurPower[1].value = GetEurPower(shift);
    AllCurPower[2].value = GetGbpPower(shift);
    AllCurPower[3].value = GetAudPower(shift);  
    AllCurPower[4].value = GetNzdPower(shift);   
    AllCurPower[5].value = GetCadPower(shift);  
    AllCurPower[6].value = GetChfPower(shift);   
    AllCurPower[7].value = GetJpyPower(shift);            
     
  }
//--- sort AllCurPower
void SortAllPower()
  {
   int total = ArraySize(AllCurPower);
   bool sorted=false;
   StructCurrencyPower tmp;
   while(!sorted)
     {
      sorted=true;
      for(int i=1; i<total; i++)
        {
         if(AllCurPower[i-1].value>AllCurPower[i].value)
           {
            tmp=AllCurPower[i-1];
            AllCurPower[i-1]=AllCurPower[i];
            AllCurPower[i]=tmp;
            sorted=false;
           }
        }
      total--;
     }
  }
//+------------------------------------------------------------------+



Files:
CRS-Demo.mq4  20 kb
CRS-Demo.mq5  20 kb