How to use Currency Strength Meter Pro to write your own Expert Advisor for MT5

How to use Currency Strength Meter Pro to write your own Expert Advisor for MT5

27 July 2020, 12:34
ALEKSANDR SHUKALOVICH
2
2 630

At this article I'll demonstrate how to use Currency Strength Meter Pro indicator to write your own Expert Advisor for MT5 platform. Also I have added examples for indicator and EA for MT4/MT5 (see attachments below).


Introduction

First of all  Currency Strength Meter (CSM) indicator is the most easiest way to identify strong and weak currency. There are many trading strategies which are using CSM indicator. And many traders want to automate this strategies. 

If you are not still very familiar with CSM indicator you can  watch these helpful videos to get more knowledge how to use it in our trading: 


    Required Instruments

    To build your own Expert Advisor with CSM we need to have this list of instruments:


    Building EA

    To connect any indicator to Expert Advisor MQL5 language contains special function with name iCustom.

    int  iCustom(
       string           symbol,     // symbol name
       ENUM_TIMEFRAMES  period,     // period
       string           name        // folder/custom_indicator_name
       ...                          // list of indicator input parameters
       );


    Last parameter of this function is list of our indicator input parameters. Here we should input parameters from Currency Strength Meter Pro for EA MT5 indicator. It contains 12 input parameters.

    CSM Input parameters


    Lets describe each input parameter one by one:

    Parameter name Type Description
    Latency
    int Refresh CSM data delay in seconds. Higher values reduce processor hi-load. Lower values increase updating of CSM data. Value 0 means every tick.
    Log level
    int Log level (Trace = 0, Info = 1, Warning = 2, Error = 3, Critical = 4).
    Candles for calculation
    int How many currency strength values will be calculated to the past candles. If your EA is operating only with the latest data this parameter should be set to 1.
    Candles shift from the beginning
    int It represents last currency strength meter value as shifted value from the beginning.
    Use Moving Average smoothing
    bool Smoothing currency strength meter values with MA algorithms.
    Moving Average Period for smoothing int How many historical data of CSM will be used to calculate smoothing.
    Moving Average Method for smoothing int Smoothing algorithm (0 - Simple averaging, 1 - Exponential averaging, 2 - Smoothed averaging, 3 - Linear-weighted averaging).
    Auto detect symbols suffix bool If true - automatically detects  symbols suffix, otherwise auto detection of  symbols suffix will be ignored. If parameter " Use suffix" is set to true this parameter will be ignored.
    Use suffix bool If true -  symbols suffix will be used from parameter " Suffix"
    Suffix string Symbols suffix which will be added to each currency pair. Parameter " Use suffix" should be set to true, otherwise this parameter will be ignored.
    CSM algorithm type int Algorithm type which will be used for calculation of currency strength (RSI = 0, CCI = 1, RVI = 2, MFI = 3, Stochastic = 4, DeMarket = 5, Momentum = 6).
    CSM algorithm params string CSM algorithm params (Empty string means default params)


    CSM algorithm types and their parameters:

    CSM algorithm type
    Description CSM algorithm params   CSM algorithm params description CSM algorithm params example 
    RSI
    Relative Strength Index Param1,Param2,Param3

    Param1 - RSI Period;
    Param2 - Applied Price (0 - CLOSE, 1 - OPEN, 2 - HIGH, 3 - LOW, 4 - MEDIAN, 5 - TYPICAL, 6 - WEIGHTED);
    Param3 - Scale;

    3,1,1.1
    CCI
    Commodity Channel Index Param1,Param2 Param1 - CCI Period;
    Param2 - Applied Price (0 - CLOSE, 1 - OPEN, 2 - HIGH, 3 - LOW, 4 - MEDIAN, 5 - TYPICAL, 6 - WEIGHTED);
    14,5
    RVI
    Relative Vigor Index Param1,Param2 Param1 - RVI Period;
    Param2 - Mode (0 - MODE_MAIN, 1 - MODE_SIGNAL);
    10,0
    MFI
    Money Flow Index Param1 Param1 - MFI Period; 14
    Stochastic
    Stochastic
    Param1,Param2,Param3,Param4,Param5,Param6 Param1 - K Period;
    Param2 - D Period;
    Param3 - Slowing;
    Param4 - Method (0 - Simple averaging, 1 - Exponential averaging, 2 - Smoothed averaging, 3 - Linear-weighted averaging);
    Param5 - Price Field (0 - Low/High, 1 - Close/Close);
    Param6 - Mode (0 - MODE_MAIN, 1 - MODE_SIGNAL);
    5,3,3,0,0,0
    DeMarket
    DeMarket Param1 Param1 - DeMarket Period; 14
    Momentum Momentum Param1,Param2 Param1 - Momentum Period;
    Param2 - Applied Price (0 - CLOSE, 1 - OPEN, 2 - HIGH, 3 - LOW, 4 - MEDIAN, 5 - TYPICAL, 6 - WEIGHTED);
    14,0
    MACD MACD Param1,Param2,Param3,Param4,Param5
    Param1 - Fast EMA;
    Param2 - Slow EMA;
    Param3 - Signal EMA;
    Param4 - Applied Price (0 - CLOSE, 1 - OPEN, 2 - HIGH, 3 - LOW, 4 - MEDIAN, 5 - TYPICAL, 6 - WEIGHTED);
    Param5 - Mode (0 - MODE_MAIN, 1 - MODE_SIGNAL);
     12,26,9,0,0


    Let's apply this information to iCustom function with default input parameters.

       g_handle = iCustom(
                     NULL,       // Symbol for CSM
                     PERIOD_H4,  // Period for CSM
                     "Market\\Currency Strength Meter Pro for EA MT5",     // Indicator path
                     0,          // Latency (Refresh delay in seconds) 0 means every tick
                     3,          // Log level (Trace = 0, Info = 1, Warning = 2, Error = 3, Critical = 4)
                     1,          // Candles for calculation
                     0,          // Candles shift from the beginning
                     false,      // Use Moving Average smoothing
                     0,          // Moving Average Period for smoothing
                     0,          // Moving Average Method for smoothing (MODE_SMA = 0 (Simple averaging), MODE_EMA = 1 (Exponential averaging), MODE_SMMA = 2 (Smoothed averaging), MODE_LWMA = 3 (Linear-weighted averaging))
                     true,       // Auto detect symbols suffix ("Use suffix" should be disabled)
                     false,      // Use suffix
                     "",         // Symbols Suffix
                     0,          // CSM algorithm type (RSI = 0, CCI = 1, RVI = 2, MFI = 3, Stochastic = 4, DeMarket = 5, Momentum = 6)
                     ""          // CSM algorithm params (Empty string means default params)
                  );


    Also make an attention to first two parameters. The first parameter can be set to NULL because CSM does not depend from chart symbol. The seconds parameter is more important. This parameter present timeframe from which CSW will calculate currencies strength. If EA require currency strength from several timeframes it's needed to call iCustom function for each timeframe separately. 


    iCustom returns the handle of the CSM indicator. We should store this handle in global variable to get access to CSM data.

    // CSM indicator handle
    int g_handle = INVALID_HANDLE;


    Currency Strength meter uses 8 buffers for storing strength of 8 major currency pairs AUD, CAD, CHF, EUR, GBP, JPY, NZD, USD. For the easiest access to buffers we should define corresponding indexes.

    #define AUD_BUFFER_INDEX 0
    #define CAD_BUFFER_INDEX 1
    #define CHF_BUFFER_INDEX 2
    #define EUR_BUFFER_INDEX 3
    #define GBP_BUFFER_INDEX 4
    #define JPY_BUFFER_INDEX 5
    #define NZD_BUFFER_INDEX 6
    #define USD_BUFFER_INDEX 7
    
    #define CURRENCIES_COUNT 8
    
    // Map symbols name to their indexes
    string g_symbols[CURRENCIES_COUNT] = {"AUD", "CAD", "CHF", "EUR", "GBP", "JPY", "NZD", "USD"};


    To get access to CSM indicator buffers data we should use MQL function with name CopyBuffer.

    bool fetchCurrenciesStrength(double& currenciesStrength[])
      {
       for(int index = 0; index < CURRENCIES_COUNT; ++index)
         {
          double strength[1] = { EMPTY_VALUE };
          // Copying currency strength data from CSM indicator to strength buffer
          if(CopyBuffer(g_handle, index, 0, 1, strength) != 1)
            {
             Print("CopyBuffer from CSM failed, no data");
             return false;
            }
    
          if(MathAbs(strength[0] - EMPTY_VALUE) < 0.0001)
            {
             Print("Currency strength is not ready yet (downloading historical data..)");
             return false;
            }
    
          currenciesStrength[index] = strength[0];
         }
    
       return true;
      }


    Now we have array currenciesStrength with latest data of currency strength for all 8 currency pairs. To find strong and weak currency from this array we can use this 2 function.

    int findFirstStrongCurrency(double& currenciesStrength[])
      {
       for(int index = 0; index < ArraySize(currenciesStrength); ++index)
         {
          if(currenciesStrength[index] >= 60)
            {
             return index;
            }
         }
    
       return -1;
      }
    
    //+------------------------------------------------------------------+
    //|                                                                  |
    //+------------------------------------------------------------------+
    int findFirstWeakCurrency(double& currenciesStrength[])
      {
    
       for(int index = 0; index < ArraySize(currenciesStrength); ++index)
         {
          if(currenciesStrength[index] <= 40)
            {
             return index;
            }
         }
    
       return -1;
      }


    This to functions return indexes with strong and weak currencies. To get names for these currencies it's needed to use g_symbols array which was define above.

    const string strongCurrencyName = g_symbols[strongCurrencyIndex];
    const string weakCurrencyName = g_symbols[weakCurrencyIndex];


    Now we should build symbol name with strong and weak currencies to trade them. It can be done like this.

    bool buildSymbolName(string firstInstrumentName, string secondInstrumentName, string suffix, string& symbolName)
      {
       string outTmp;
       symbolName = firstInstrumentName + secondInstrumentName + suffix;
       if(!SymbolInfoString(symbolName, SYMBOL_CURRENCY_BASE, outTmp))
         {
          symbolName = secondInstrumentName + firstInstrumentName + suffix;
          if(!SymbolInfoString(symbolName, SYMBOL_CURRENCY_BASE, outTmp))
            {
             return false;
            }
         }
    
       return true;
      }
       // Building symbol for trading
       string symbol;
       if(!buildSymbolName(strongCurrencyName, weakCurrencyName, "", symbol))
         {
          Print("Failed to build symbol name)");
          return;
         }

    Do not forget to specify correct symbol suffix at third parameter.


    Also we should identify direction of our trade (long or short). We can do it with this code.

    // Determining direction of the trade long or short
       const bool isLongTrade = (StringSubstr(symbol, 0, StringLen(strongCurrencyName)) == strongCurrencyName);


    Finally we can combine all together to get template of EA which will be using CSM indicator.

    //+------------------------------------------------------------------+
    //|                                             ExpertsWithCSM.mq5 |
    //|                           Copyright 2019, Alexander Shukalovich. |
    //|                                             https://www.mql5.com |
    //+------------------------------------------------------------------+
    #property copyright "Copyright 2020, Alexander Shukalovich."
    #property link      "https://www.mql5.com/en/users/nomadmain"
    #property version   "1.00"
    
    #define AUD_BUFFER_INDEX 0
    #define CAD_BUFFER_INDEX 1
    #define CHF_BUFFER_INDEX 2
    #define EUR_BUFFER_INDEX 3
    #define GBP_BUFFER_INDEX 4
    #define JPY_BUFFER_INDEX 5
    #define NZD_BUFFER_INDEX 6
    #define USD_BUFFER_INDEX 7
    
    #define CURRENCIES_COUNT 8
    
    // Map symbols name to their indexes
    string g_symbols[CURRENCIES_COUNT] = {"AUD", "CAD", "CHF", "EUR", "GBP", "JPY", "NZD", "USD"};
    
    // CSM indicator handle
    int g_handle = INVALID_HANDLE;
    
    //+------------------------------------------------------------------+
    //|                                                                  |
    //+------------------------------------------------------------------+
    int OnInit(void)
      {
    // Initializing of CSM indicator
       g_handle = iCustom(
                     NULL,       // Symbol for CSM
                     PERIOD_H4,  // Period for CSM
                     "Market\\Currency Strength Meter Pro for EA MT5",     // Indicator path
                     0,          // Latency (Refresh delay in seconds) 0 means every tick
                     3,          // Log level (Trace = 0, Info = 1, Warning = 2, Error = 3, Critical = 4)
                     1,          // Candles for calculation
                     0,          // Candles shift from the beginning
                     false,      // Use Moving Average smoothing
                     0,          // Moving Average Period for smoothing
                     0,          // Moving Average Method for smoothing (MODE_SMA = 0 (Simple averaging), MODE_EMA = 1 (Exponential averaging), MODE_SMMA = 2 (Smoothed averaging), MODE_LWMA = 3 (Linear-weighted averaging))
                     true,       // Auto detect symbols suffix ("Use suffix" should be disabled)
                     false,      // Use suffix
                     "",         // Symbols Suffix
                     0,          // CSM algorithm type (RSI = 0, CCI = 1, RVI = 2, MFI = 3, Stochastic = 4, DeMarket = 5, Momentum = 6)
                     ""          // CSM algorithm params (Empty string means default params)
                  );
    
    // Checking that CSM indicator was initialised
       if(g_handle == INVALID_HANDLE)
         {
          printf("Error creating CSM indicator");
          return INIT_FAILED;
         }
    
    //--- ok
       return INIT_SUCCEEDED;
      }
    
    //+------------------------------------------------------------------+
    //|                                                                  |
    //+------------------------------------------------------------------+
    void OnTick(void)
      {
       double currenciesStrength[CURRENCIES_COUNT];
       ArrayInitialize(currenciesStrength, 0);
    
    // Getting currencies strength for all 8 currencies to the currenciesStrength buffer
       if(!fetchCurrenciesStrength(currenciesStrength))
         {
          return;
         }
    
    // Searching for strong and weak currencies
       const int strongCurrencyIndex = findFirstStrongCurrency(currenciesStrength);
       const int weakCurrencyIndex = findFirstWeakCurrency(currenciesStrength);
    
       if(strongCurrencyIndex < 0 || weakCurrencyIndex < 0)
         {
          // There is no strong or weak currency (do nothing)
          return;
         }
    
    // Building symbol for trading
       string symbol;
       const string strongCurrencyName = g_symbols[strongCurrencyIndex];
       const string weakCurrencyName = g_symbols[weakCurrencyIndex];
       if(!buildSymbolName(strongCurrencyName, weakCurrencyName, "", symbol))
         {
          Print("Failed to build symbol name)");
          return;
         }
    
    // Determining direction of the trade long or short
       const bool isLongTrade = (StringSubstr(symbol, 0, StringLen(strongCurrencyName)) == strongCurrencyName);
    
       if(isLongTrade)
         {
          Print(StringFormat(
             "Placing long order for symbol = %s and strength = %.2f/%.2f", 
             symbol, 
             currenciesStrength[strongCurrencyIndex], 
             currenciesStrength[weakCurrencyIndex]));
         }
       else
         {
          Print(StringFormat(
             "Placing short order for symbol = %s and strength = %.2f/%.2f", 
             symbol, 
             currenciesStrength[weakCurrencyIndex], 
             currenciesStrength[strongCurrencyIndex]));
         }
      }
    
    //+------------------------------------------------------------------+
    //|                                                                  |
    //+------------------------------------------------------------------+
    bool fetchCurrenciesStrength(double& currenciesStrength[])
      {
       for(int index = 0; index < CURRENCIES_COUNT; ++index)
         {
          double strength[1] = { EMPTY_VALUE };
          // Copying currency strength data from CSM indicator to strength buffer
          if(CopyBuffer(g_handle, index, 0, 1, strength) != 1)
            {
             Print("CopyBuffer from CSM failed, no data");
             return false;
            }
    
          if(MathAbs(strength[0] - EMPTY_VALUE) < 0.0001)
            {
             Print("Currency strength is not ready yet (downloading historical data..)");
             return false;
            }
    
          currenciesStrength[index] = strength[0];
         }
    
       return true;
      }
    
    //+------------------------------------------------------------------+
    //|                                                                  |
    //+------------------------------------------------------------------+
    bool buildSymbolName(string firstInstrumentName, string secondInstrumentName, string suffix, string& symbolName)
      {
       string outTmp;
       symbolName = firstInstrumentName + secondInstrumentName + suffix;
       if(!SymbolInfoString(symbolName, SYMBOL_CURRENCY_BASE, outTmp))
         {
          symbolName = secondInstrumentName + firstInstrumentName + suffix;
          if(!SymbolInfoString(symbolName, SYMBOL_CURRENCY_BASE, outTmp))
            {
             return false;
            }
         }
    
       return true;
      }
    
    //+------------------------------------------------------------------+
    //|                                                                  |
    //+------------------------------------------------------------------+
    int findFirstStrongCurrency(double& currenciesStrength[])
      {
       for(int index = 0; index < ArraySize(currenciesStrength); ++index)
         {
          if(currenciesStrength[index] >= 60)
            {
             return index;
            }
         }
    
       return -1;
      }
    
    //+------------------------------------------------------------------+
    //|                                                                  |
    //+------------------------------------------------------------------+
    int findFirstWeakCurrency(double& currenciesStrength[])
      {
    
       for(int index = 0; index < ArraySize(currenciesStrength); ++index)
         {
          if(currenciesStrength[index] <= 40)
            {
             return index;
            }
         }
    
       return -1;
      }
    
    //+------------------------------------------------------------------+
    //|                                                                  |
    //+------------------------------------------------------------------+
    void OnDeinit(const int reason)
      {
      }
    //+------------------------------------------------------------------+
    


    Conclusion

    It's possible to write your own indicator which will be based on  Currency Strength Meter Pro for EA MT5. It's even more easier to do this than to write EA. To demonstrate it I have created indicator template which can be downloaded from attachment of this article.

    Custom indicator which based on CSM


    There are several powerful CSM indicators based on this CSM:


    Hope this article helps to improve your trading and your win rate will be increased.


    And as always   Take profit!




    Share it with friends: