Mql5 code idea of indicators for beginners? - page 7

 
Gerard Willia G J B M Dinh Sy #: And one more quote. I'm very proud of it, on the developer channel

I humbly ask you to please stop with the self-promotion every time your CodeBase publications are discussed on MetaQuotes developer channels.

Doing so once or twice is understandable, but doing it every time is not. You and your publications are not the only ones mentioned and you don’t see anyone else promoting themselves about it in the forum.

Self-promotion or advertising is not allowed on the forum. Consider instead, posting such content on your profile wall or in your blog — https://www.mql5.com/en/blogs

Trading blogs and financial markets analysis
Trading blogs and financial markets analysis
  • www.mql5.com
Read blogs to find the latest news on various topics from all over the world — rumors about companies, country and industry reports, market analysis, latest developments in speculative trading and more. Start your own blog to share new ideas and trading achievements with the members of MQL5.community. Post interesting images and videos, enjoy unlimited possibilities!
 
Fernando Carreiro # :
I humbly ask you to please stop with the self-promotion every time your CodeBase publications are discussed on MetaQuotes developer channels.

Hello Fernando.
No worries, I won't do it again.
Very nice day

 

Hello!

I would like to expand this tick chart builder with stochastic/RSI/BB... indicators: https://www.mql5.com/en/code/16482

The final result should be an input mask to define the path of the additional indicator(s) and its values that will be used on the tickchart itself. (So one could say its an indicator with the posibilty to draw other indicators inside)

What do you think? Maybe there is a better solution? Thanks.

Candles, arbitrary seconds
Candles, arbitrary seconds
  • www.mql5.com
This is an indicator that generates simulated data for any period - but in seconds
 
Good morning
I'm not sure I understand.
it seems that part of the answer is in icustom...
It is with this command that you can look for another indicator or call this one from another.

Happy holidays
 

To clarify: I need to have BB inside a 3 seconds tick chart. The tickchart is " Candles, arbitrary seconds ". The question is: How to add Bollinger Bands?

There are OHLC values coming from Candles, arbitrary seconds I can work with, but I have no idea how to make them plot BBs.


Happy holidays
 
Good morning
Ok I understand better.

Here is what I think you need to do to get your bollinger bands.

You must modify the udpate() function and add 3 buffers to display them + 1 for the standard deviation

Here are the codes to make these bands, since the ibands function cannot work this way

Here is the code https://www.mql5.com/en/code/14 in codebase
Here are the parts to remember for the formulas to take into account

   for(int i=pos; i<rates_total && !IsStopped(); i++)
     {
      //--- middle line
      ExtMLBuffer[i]=SimpleMA(i,ExtBandsPeriod,price);
      //--- calculate and write down StdDev
      ExtStdDevBuffer[i]=StdDev_Func(i,price,ExtMLBuffer,ExtBandsPeriod);
      //--- upper line
      ExtTLBuffer[i]=ExtMLBuffer[i]+ExtBandsDeviations*ExtStdDevBuffer[i];
      //--- lower line
      ExtBLBuffer[i]=ExtMLBuffer[i]-ExtBandsDeviations*ExtStdDevBuffer[i];
     }

This loop calls a function to calculate the standard deviation

double StdDev_Func(const int position,const double &price[],const double &ma_price[],const int period)
  {
   double std_dev=0.0;
//--- calcualte StdDev
   if(position>=period)
     {
      for(int i=0; i<period; i++)
         std_dev+=MathPow(price[position-i]-ma_price[position],2.0);
      std_dev=MathSqrt(std_dev/period);
     }
//--- return calculated value
   return(std_dev);
  }

Use the same buffers in the first part of the code to save a little time

double        ExtMLBuffer[];
double        ExtTLBuffer[];
double        ExtBLBuffer[];
double        ExtStdDevBuffer[];

Good development

Bollinger Bands ®
Bollinger Bands ®
  • www.mql5.com
The Bollinger Bands ® Indicator (BB) is similar to Envelopes. The only difference is that the bands of Envelopes are plotted a fixed distance (%) away from the moving average, while the Bollinger Bands are plotted a certain number of standard deviations away from it.
 

I added the buffers and the stdev loop and it compiles. (2nd and 3rd block) Easy.

Adding the first block of the code can not be done on global scope so I made a void (BBCal) for it. This causes many errors in the compiler: missing variables and so on. I tried to declare some... but this is where things get tricky.

I changed:

#property indicator_buffers from 5 to 9.

We have 4 more buffers now.


I added:

input int ExtBandsPeriod = 20
input double ExtBandsDeviations = 2.0

This should be adjustable on the UI.

And this is how far I get. Code looks like this now:


//------------------------------------------------------------------
#property copyright   "mladen"
#property link        "www.forex-tsd.com"
#property version     "1.00"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 9
#property indicator_plots   1

#property indicator_label1  "open;high;low;close"
#property indicator_type1   DRAW_COLOR_CANDLES
#property indicator_color1  clrGray,clrLimeGreen,clrSandyBrown

//
//
//
//
//

input int Seconds = 3;  // Seconds for candles interval

input int ExtBandsPeriod = 20;
input double ExtBandsDeviations = 2.0;

double canc[],cano[],canh[],canl[],colors[],seconds[][4];
#define sopen  0
#define sclose 1
#define shigh  2
#define slow   3



//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int OnInit()
{
   SetIndexBuffer(0,cano  ,INDICATOR_DATA);
   SetIndexBuffer(1,canh  ,INDICATOR_DATA);
   SetIndexBuffer(2,canl  ,INDICATOR_DATA);
   SetIndexBuffer(3,canc  ,INDICATOR_DATA);
   SetIndexBuffer(4,colors,INDICATOR_COLOR_INDEX);
      EventSetTimer(Seconds);
      IndicatorSetString(INDICATOR_SHORTNAME,(string)Seconds+" seconds chart");
   return(0);
}
void OnDeinit(const int reason)
{
   EventKillTimer();
}
void OnTimer()
{
   double close[]; CopyClose(_Symbol,_Period,0,1,close);
   int size = ArrayRange(seconds,0);
             ArrayResize(seconds,size+1);
                         seconds[size][sopen]  = close[0];
                         seconds[size][sclose] = close[0];
                         seconds[size][shigh]  = close[0];
                         seconds[size][slow]   = close[0];
   updateData();                         
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

void updateData()
{
   int rates_total = ArraySize(canh);
   int size = ArrayRange(seconds,0); 
      if (size<=0) 
      {
         for (int i=rates_total-1; i>=0; i--)
         {
            canh[i] = EMPTY_VALUE;
            canl[i] = EMPTY_VALUE;
            cano[i] = EMPTY_VALUE;
            canc[i] = EMPTY_VALUE;
         }
         return;
      }         
   double close[]; CopyClose(_Symbol,_Period,0,1,close);
      seconds[size-1][shigh]  = MathMax(seconds[size-1][shigh] ,close[0]);
      seconds[size-1][slow]   = MathMin(seconds[size-1][slow]  ,close[0]);
      seconds[size-1][sclose] =                                 close[0];
   for (int i=(int)MathMin(rates_total-1,size-1); i>=0 && !IsStopped(); i--)
   {
      int y = rates_total-i-1;
         canh[y] = seconds[size-i-1][shigh ];
         canl[y] = seconds[size-i-1][slow  ];
         cano[y] = seconds[size-i-1][sopen ];
         canc[y] = seconds[size-i-1][sclose];
         colors[y] = cano[y]>canc[y] ? 2 : cano[y]<canc[y] ? 1 : 0; 
   }
}

//
//
//
//
//

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 bars = Bars(_Symbol,_Period); if (bars<rates_total) return(-1); updateData();
   return(rates_total);
}

double        ExtMLBuffer[];
double        ExtTLBuffer[];
double        ExtBLBuffer[];
double        ExtStdDevBuffer[];

double StdDev_Func(const int position,const double &price[],const double &ma_price[],const int period)
  {
   double std_dev=0.0;
//--- calcualte StdDev
   if(position>=period)
     {
      for(int i=0; i<period; i++)
         std_dev+=MathPow(price[position-i]-ma_price[position],2.0);
      std_dev=MathSqrt(std_dev/period);
     }
//--- return calculated value
   return(std_dev);
  }
  
void BBCal()

{
for(int i=pos; i<rates_total && !IsStopped(); i++)
     {
      //--- middle line
      ExtMLBuffer[i]=SimpleMA(i,ExtBandsPeriod,price);
      //--- calculate and write down StdDev
      ExtStdDevBuffer[i]=StdDev_Func(i,price,ExtMLBuffer,ExtBandsPeriod);
      //--- upper line
      ExtTLBuffer[i]=ExtMLBuffer[i]+ExtBandsDeviations*ExtStdDevBuffer[i];
      //--- lower line
      ExtBLBuffer[i]=ExtMLBuffer[i]-ExtBandsDeviations*ExtStdDevBuffer[i];
     }  
}
MQL5 forum
MQL5 forum
  • www.mql5.com
MQL5: Forum on automated trading systems and strategy testing
 

Good morning

There are several steps to having good code.

You need the idea to guide you in your codes.
Some even create UMLs to see all the necessary codes before even trying to code them.

Afterwards, you have to write the code, and the F7 compiler on metaeditor is there to tell you what errors are in your code

The code you give contains 10 errors.
Some will be easy to correct, others will be more complicated.

But until these errors are corrected, you will not be able to go further.

Afterwards, there is still the test to pass and then the result on the terminal.

Based on your errors, I suggest you read the documentation on these topics
Variables - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

And

Event Handling - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

I put some code in codebase. It doesn't do what you ask, but it's pretty close all the same.
It plots an RSI, but does not use the native iRsi function.

So all the variables are in their place
A function is called, so you can see how it communicates with the main code.

The only thing missing is the include because I didn't need it and which is present in the code for the bollinger bands

ExtMLBuffer[i]=SimpleMA(i,ExtBandsPeriod,price);

SimpleSMA is a function.

Here is my code https://www.mql5.com/en/code/46520

Come back if you have any questions

Documentation on MQL5: Language Basics / Variables
Documentation on MQL5: Language Basics / Variables
  • www.mql5.com
Variables - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Good morning.

And sorry for wasting your time.

"There are several steps to having good code."

"You need the idea to guide you in your codes."

"Afterwards, you have to write the code, and the F7 compiler on metaeditor is there to tell you what errors are in your code "

Really? I never imagined that things like good code, an idea or a compiler would have such a great meaning!

I found a better solution for my needs.

That's all. Byebye.

Market product reference redacted by moderator. Discussing specific products or signals is not allowed on the forum.
 
Janusz Wiechnik # :
Really? I never imagined that things like good code, an idea or a compiler would have such a great meaning!

:-(
I really wanted to help you without making fun of you.
Good year