First and Second Derivative of Custom Indicator Line

 

Hi,

I would like to ask, whether somebody could refer me to an exising / easy modifiable code. I would find it frequently useful for an custom indicator (and / or his line in the window) to see also the lines of first and second derivatives.

In particular, when having a custom indicator X (which draws a line in the windon and in one of the buffers are the values of the line), I would like to create a first derivaive custom indicator D1X by inserting X into an existing code. In a similar way, for the second derivative, a new indicator D2X, by inserting either X or D1X. Then I would want to call the all three indicators X, D1X and D2X by function iCustom from my EA.

Please, could somebody advise me how to do it or refer me to an existing code?

Thanks,

Sam

 
//+------------------------------------------------------------------+
//|                                                 MyPrettyLine.mq4 |
//|                      Copyright © 2009, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
double MyFirstLine[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,MyFirstLine);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   
   for(int i = WindowFirstVisibleBar()+1; i >= 0; i--){
   
      // Here is your indicator. Change the formula to suit your dreams
      MyFirstLine[i] = Close[i];
   
   }

   return(0);
  }
//+------------------------------------------------------------------+
 
phy wrote >>
// Here is your indicator. Change the formula to suit your dreams

I really like the comment. :-)

 
phy wrote >>

abstract_mind
wrote
>>

I really like the comment. :-)

Hi phy,

Thanks a lot for your prompt reaction and the code. However, I do not see how this code could create first derivaive of the line and it really does not work for me whatever I do.

I want to apply the first and second derivative on the indicator called Slope Direction Line, which I am sending attached. I mean it as follows. If I look at the Slope Direction Line indicator,Sdl, drawn in the main window, as a function of time t ... f(t), I want to create another indicator D1Sdl, which would represent mathematically df/dt. Then I want to create another indicator D2Sdl, which would be mathematically d2f/dt2. I want to drag all three indicators, Sdl, D1Sdl and D2Sdl to my window and have Sdl in the main window and D1Sdl and D2Sdl in separate windows.

Would it be possible for you to apply the attached Sdl into the code that you sent me and create for me D1Sdl and D2Sdl indicators? I would appreciate it very much.

Thanks,

Sam

Files:
sdl.mq4  5 kb
 

I think first derivative could be


FirstDerivative[i] =  (indicator[i] - indicator[i+1]) / ( iTime(Symbol(), NULL, i) - iTime(Symbol(), NULL, i+1) )

second derivative could be


SecondDerivative[i] =  (FirstDerivative[i] - FirstDerivative[i+1]) / ( iTime(Symbol(), NULL, i) - iTime(Symbol(), NULL, i+1) )

If this is not right, I am sorry. Just my idea.

 

I'm sorry, I misunderstood your request

 

Hey navodar,
Ur approach is correct but actually U dont need to divide by the time as the sampling time is constant and becomes then only a scale factor.
So the first derivative is just the difference between two consecutive values (speed at which the indicator raises/falls) and the second derivative is the difference of the differences (acceleration of raises/falls of the indicator.



Cheers,

Zyp

 
Zypkin wrote >>

Hey navodar,
Ur approach is correct but actually U dont need to divide by the time as the sampling time is constant and becomes then only a scale factor.
So the first derivative is just the difference between two consecutive values (speed at which the indicator raises/falls) and the second derivative is the difference of the differences (acceleration of raises/falls of the indicator.

Cheers,

Zyp

Hi navodar and Zyp,

thanks for your ideas. I agree that the difference of the of the two neighbouring values is the key, the rest is only scaling.

I shall try it and see how it will work in practice (whether it will numerically behave like formula-based functions).

I found the Slope Direction Line indicator very usefull and the first and second derivatives may be a good tool for monitoring it's behaviour.

Thanks .

Sam

 

Gentlemen,

let me conclude this thread. With your help I realized that the first derivative of the chart-line is actually the parameter referred to as divergence. The second derivative is "divergence on divergence parameter". First and second divergence can be applied on virtually and parameter.

Indeed, when testing in particular cases divergence on various parameters, they behave as as what we know from mathematics - ie first divergence is "speed", second divergence is "acceleration and/or force". Both are very useful.

Thanks a lot for helping me to clarify these useful tools.

Sam

 
SAMUEL007:

Gentlemen,

let me conclude this thread. With your help I realized that the first derivative of the chart-line is actually the parameter referred to as divergence. The second derivative is "divergence on divergence parameter". First and second divergence can be applied on virtually and parameter.

Indeed, when testing in particular cases divergence on various parameters, they behave as as what we know from mathematics - ie first divergence is "speed", second divergence is "acceleration and/or force". Both are very useful.

Thanks a lot for helping me to clarify these useful tools.

Sam

I realize this thread has been closed for a bit, but I thought I would clarify. Derivatives are not really divergence. To find formula for a good derivative look for Finite Difference methods. You can find a suitable formula that is either backwards difference or central difference. Forward difference doesn't really make sense for trading purposes.

 
Ungwegwebula #:
slacktrader, I guess, there is no need to divide by time on the first derivative because indicator values already consider equal time intervals(PERIOD_CURRENT). There the 1st derivative is actually the 2nd derivative.
Reason: