From the diary of the worlds worst programmer part 2

Back to topics list To post a new topic, please log in or register
avatar
30
forexbob1970 2016.07.27 16:30
 

This is following on from a recent post

I built this little indicator with some valued help to measure in my eyes, a pairs "strength". Use it much like an RSI or Stochs. But that's another topic.

 

///+------------------------------------------------------------------+
//|                                                         boom.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_level1  5
#property indicator_levelcolor clrBlack
#property indicator_levelstyle STYLE_SOLID
//--- plot Strength
#property indicator_label1  "Strength"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//--- plot Smoothed
#property indicator_label2  "Smoothed"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2
//--- input parameters
input int L_Period=55;
input int Smoothed=5;
//--- indicator buffers
double    StrengthBuffer[];
double    SmoothedBuffer[];
//--- internal arrays
double    LongCandle[],AbsCandle[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(4);
   SetIndexBuffer(0,StrengthBuffer);
   SetIndexBuffer(1,SmoothedBuffer);
   SetIndexBuffer(2,LongCandle);
   SetIndexBuffer(3,AbsCandle);

//---
   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[])
  {
//---
   for(int i=rates_total-1-MathMax(L_Period,prev_calculated); i>=0; --i)
     {
        {
         double Body     = MathAbs(Open[i]-Close[i]);
         double BodySize = Body/Point;

         if(BodySize==0.0) BodySize=0.01;

         if(Open[i]<Close[i])LongCandle[i]=BodySize;
         else LongCandle[i]=0.01;

         AbsCandle[i]=BodySize;
        }

      double deltalong=0.0,deltaabs=0.0;

      for(int cnt=L_Period+i; cnt>i; cnt--)
        {
         deltalong=deltalong+LongCandle[cnt];
         deltaabs=deltaabs+AbsCandle[cnt];
        }

      StrengthBuffer[i]=(deltalong/deltaabs)*10;

      SmoothedBuffer[i]=(StrengthBuffer[i]+StrengthBuffer[i+1]+StrengthBuffer[i+2])/3;
     }
//--- return value of prev_calculated for next call
   return(rates_total-1);
  }
//+------------------------------------------------------------------+

 

 Now, as I'm happy to admit, I'm not too clever at this programming gig. My goal here is to build a currency strength meter. So from this above code, I built this indicator to measure a single currency strength using the iCustom function

 

//+------------------------------------------------------------------+
//|                                               Strength Meter.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 7
#property indicator_plots   7
//--- plot USDEUR
#property indicator_label1  "USDEUR"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot USDGPB
#property indicator_label2  "USDGPB"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot USDJPY
#property indicator_label3  "USDJPY"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrGreen
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- plot USDAUD
#property indicator_label4  "USDAUD"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrYellow
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1
//--- plot USDCAD
#property indicator_label5  "USDCAD"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrFuchsia
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1
//--- plot USDNZD
#property indicator_label6  "USDNZD"
#property indicator_type6   DRAW_LINE
#property indicator_color6  clrLime
#property indicator_style6  STYLE_SOLID
#property indicator_width6  1
//--- plot USD STRENGTH
#property indicator_label7  "USD STRENGTH"
#property indicator_type7   DRAW_LINE
#property indicator_color7  clrBlack
#property indicator_style7  STYLE_SOLID
#property indicator_width7  3
//--- input parameters
input int      Look_Back=96;
input int      Smoothed=5;
input int      TimeFrame=5;
//--- indicator buffers
double         USDEURBuffer[];
double         USDGPBBuffer[];
double         USDJPYBuffer[];
double         USDAUDBuffer[];
double         USDCADBuffer[];
double         USDNZDBuffer[];
double         USDBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,USDEURBuffer);
   SetIndexBuffer(1,USDGPBBuffer);
   SetIndexBuffer(2,USDJPYBuffer);
   SetIndexBuffer(3,USDAUDBuffer);
   SetIndexBuffer(4,USDCADBuffer);
   SetIndexBuffer(5,USDNZDBuffer);
   SetIndexBuffer(6,USDBuffer);
   
//---
   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[])
  {
//---
   for(int i=rates_total-1-MathMax(Look_Back,prev_calculated); i>=0; --i)
    {
     USDEURBuffer[i] = 10-iCustom("EURUSD",TimeFrame,"boom v1.7",Look_Back,Smoothed,1,0);
     USDGPBBuffer[i] = 10-iCustom("GBPUSD",TimeFrame,"boom v1.7",Look_Back,Smoothed,1,0);
     USDJPYBuffer[i] = iCustom("USDJPY",TimeFrame,"boom v1.7",Look_Back,Smoothed,1,0);
     USDAUDBuffer[i] = 10-iCustom("AUDUSD",TimeFrame,"boom v1.7",Look_Back,Smoothed,1,0);
     USDCADBuffer[i] = iCustom("USDCAD",TimeFrame,"boom v1.7",Look_Back,Smoothed,1,0);
     USDNZDBuffer[i] = 10-iCustom("NZDUSD",TimeFrame,"boom v1.7",Look_Back,Smoothed,1,0);
     
     USDBuffer[i] = (USDEURBuffer[i]+USDGPBBuffer[i]+USDJPYBuffer[i]+USDAUDBuffer[i]+USDCADBuffer[i]+USDNZDBuffer[i])/6;
    }
//--- return value of prev_calculated for next call
   return(rates_total-1);
  }
//+------------------------------------------------------------------+

 

Do this across the majors  and I can now build my meter which looks like this

 

//+------------------------------------------------------------------+
//|                                 Bobs Currency Strength meter.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 7
#property indicator_plots   7
//--- plot USD
#property indicator_label1  "USD"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot EUR
#property indicator_label2  "EUR"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot GBP
#property indicator_label3  "GBP"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrGreen
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- plot JPY
#property indicator_label4  "JPY"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrYellow
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1
//--- plot AUS
#property indicator_label5  "AUS"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrFuchsia
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1
//--- plot CAD
#property indicator_label6  "CAD"
#property indicator_type6   DRAW_LINE
#property indicator_color6  clrLawnGreen
#property indicator_style6  STYLE_SOLID
#property indicator_width6  1
//--- plot NZD
#property indicator_label7  "NZD"
#property indicator_type7   DRAW_LINE
#property indicator_color7  clrWhite
#property indicator_style7  STYLE_SOLID
#property indicator_width7  1
//--- input parameters
input int      Look_Back=96;
input int      Smoothed=3;
input int      TimeFrame=5;
//--- indicator buffers
double         USDBuffer[];
double         EURBuffer[];
double         GBPBuffer[];
double         JPYBuffer[];
double         AUSBuffer[];
double         CADBuffer[];
double         NZDBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,USDBuffer);
   SetIndexBuffer(1,EURBuffer);
   SetIndexBuffer(2,GBPBuffer);
   SetIndexBuffer(3,JPYBuffer);
   SetIndexBuffer(4,AUSBuffer);
   SetIndexBuffer(5,CADBuffer);
   SetIndexBuffer(6,NZDBuffer);
   
//---
   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[])
  {
//---
   for(int i=rates_total-1-MathMax(Look_Back,prev_calculated); i>=0; --i)
    {
     USDBuffer[i] = iCustom(Symbol(),TimeFrame,"Bobs USD Strength Meter",Look_Back,Smoothed,6,0);
     EURBuffer[i] = iCustom(Symbol(),TimeFrame,"Bobs EURO Strength Meter",Look_Back,Smoothed,6,0);
     GBPBuffer[i] = iCustom(Symbol(),TimeFrame,"Bobs GBP Strength Meter",Look_Back,Smoothed,6,0);
     JPYBuffer[i] = iCustom(Symbol(),TimeFrame,"Bobs JPY Strength Meter",Look_Back,Smoothed,6,0);
     AUSBuffer[i] = iCustom(Symbol(),TimeFrame,"Bobs AUS Strength Meter",Look_Back,Smoothed,6,0);
     CADBuffer[i] = iCustom(Symbol(),TimeFrame,"Bobs CAD Strength Meter",Look_Back,Smoothed,6,0);
     NZDBuffer[i] = iCustom(Symbol(),TimeFrame,"Bobs NZD Strength Meter",Look_Back,Smoothed,6,0);
    }   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

 

Now whether what I've done works for the right reasons or wrong.  Whether the maths is flawed in the logic. Etc... that's up for critisim and I'm happy to hear veiws. 

But for myself, it returns a value I can use. And the goal of the 2nd and 3rd codes was to prove that using the iCustom function I can call upon these values generated by the 1st code to reference in an EA. Cause remember, I'm just not skilled enough to build these calculations into a single indicator or bot.

I'm also not clever enough to know how to rank the values from high to low. And that's my question and hoping someone can help out. Say I'm using the second code, I get a series of values like 3.4, 5.8, 4.6, 6.4, 5.3 & 6.1 returned. What's the best way to organize them so I can rank them from high to low ie, 6.4, 6.1, 5.8, 5.3, 4.6 & 3.4

Many thanks in advance  

avatar
20
Thaddeus_39 2016.08.03 23:02 #
 

Hello, forexbob1970. Thanks for sharing.

From one layman to the other ;), as to your question of ranking, why don't you save the values into an array and then apply a ranking algorithm?

There must be plenty of ranking algos on the internet.

Please let me know if such an idea works.

Cheers 

 

avatar
20
Thaddeus_39 2016.08.03 23:18 #
 

forexbob1970, I have a better idea: try the ArraySort() function.

https://docs.mql4.com/array/arraysort 

Let me know how it goes. 

Cheers 

Back to topics list  

To add comments, please log in or register