Trend direction and force - DSEMA smoothed

Author: © mladen, 2018
0 Views
0 Downloads
0 Favorites
Trend direction and force - DSEMA smoothed
ÿþ//+------------------------------------------------------------------

#property copyright   "© mladen, 2018"

#property link        "mladenfx@gmail.com"

#property version     "1.00"

#property description "Trend direction and force"

//+------------------------------------------------------------------

#property strict

#property indicator_separate_window



#property indicator_buffers 3

#property indicator_plots   3



#property indicator_label1  "Trend direction and force"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrDeepSkyBlue

#property indicator_width1  2



#property indicator_label2  "Trend direction and force"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrDarkGray

#property indicator_width2  2



#property indicator_label3  "Trend direction and force"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrSandyBrown

#property indicator_width3  2



//

//--- input parameters

//



input int    inpTrendPeriod  = 20;      // Trend period

input double inpSmooth       = 3;       // Smoothing period

input double inpTriggerUp    =  0.05;   // Trigger up level

input double inpTriggerDown  = -0.05;   // Trigger down level

input int    inpMaxBarCount  = 10000;   // Draw Bars



//

//--- buffers and global variables declarations

//



double ««val[],«val»[],val»»[],val[],valc[],mma[],smma[],tdf[],tdfa[],val1[],ª_alpha®,ª_alpha2®;

int ª_maxPeriod©;



//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

int OnInit()

  {

   IndicatorBuffers(9);

   SetIndexBuffer(0,««val);

   SetIndexEmptyValue(0,0.0f);

   SetIndexBuffer(1,«val»);

   SetIndexEmptyValue(1,0.0f);

   SetIndexBuffer(2,val»»);

   SetIndexEmptyValue(2,0.0f);

   SetIndexBuffer(3,mma);

   SetIndexBuffer(4,smma);

   SetIndexBuffer(5,tdf);

   SetIndexEmptyValue(5,0.0f);

   SetIndexBuffer(6,tdfa);

   SetIndexEmptyValue(6,0.0f);

   SetIndexBuffer(7,val1);

   SetIndexBuffer(8,val);



   ª_alpha®     = 2.0/(1.0+inpTrendPeriod);

   ª_alpha2®    = 2.0/(1.0+MathSqrt(inpSmooth>1?inpSmooth:1));

   ª_maxPeriod© = 3*inpTrendPeriod;



   IndicatorSetInteger(INDICATOR_LEVELS,2);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,inpTriggerUp);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,inpTriggerDown);

   IndicatorSetString(INDICATOR_SHORTNAME,"Trend direction and force ("+(string)inpTrendPeriod+","+(string)(inpSmooth>1?inpSmooth:1)+")");

   return (INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

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 i = 0;

   if(!(prev_calculated>0))

     {

      i= inpMaxBarCount < Bars-ª_maxPeriod©-1?inpMaxBarCount:Bars-ª_maxPeriod©-1;

      if(i < 0)

         return 0;

      ArrayFill(tdf,0,Bars-1,0);

      ArrayFill(tdfa,0,Bars-1,0);

      smma[i] = mma[i] = close[i];

      val1[i] = val[i] = 0;

     }



#define  Inline(Sto, Term) if(Term){ Sto[i] = val[i]; Sto[i+1] = val[i+1]; continue; }



   for(; i>=0 && ! _StopFlag; i--)

     {

      smma[i] = smma[i+1] + ª_alpha® * ((mma[i]  = mma [i+1] + ª_alpha® * (close[i] - mma [i+1])) - smma[i+1]);

      tdfa[i] = MathAbs(tdf[i]  = 0.125f * MathAbs(mma[i]-smma[i]) * pow(mma[i] + smma[i] - mma[i+1] - smma[i+1], 3.0f));



      double _absValue = tdfa[ArrayMaximum(tdfa,ª_maxPeriod©,i)];

      val1[i] = val1[i+1] + ª_alpha2® * ((_absValue > 0 ? tdf[i] / _absValue : 0)  - val1[i+1]);

      val[i]  = val[i+1]  + ª_alpha2® * (val1[i] - val[i+1]);



      Inline(««val,val[i] > inpTriggerUp);

      Inline(val»»,val[i] < inpTriggerDown);

      Inline(«val»,true);

     }

   return rates_total;

  }

//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---