Author: yupeng shaw
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
A
ÿþ//+------------------------------------------------------------------+

//|                                                            V.mq4 |

//|                                                Author:yupeng shaw|

//|                                           E-mail:shaw_yp@126.com |

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

#property copyright   "yupeng shaw"

#property link        "shaw_yp@126.com"

#property description "velocity and accelerated speed"

//--- indicator settings

#property indicator_separate_window

#property indicator_buffers 1

#property indicator_type1   DRAW_LINE

#property indicator_color1  Red

#property indicator_label1  "A"

//--- input parameters

input int InpVPeriod=21;   // velocity period

input int InpAPeriod=13;   // accelerated speed period

//--- indicator buffers

double    ExtVBuffer[];

double    ExtABuffer[];

double    ExtSBuffer[];

//--- global variable

int       ExtVPeriod;

int       ExtAPeriod;

int       ExtAStart;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- check for input value

   if(InpVPeriod<=0 || InpAPeriod<=0)

     {

      ExtVPeriod=22;

      ExtAPeriod=14;

      printf("Incorrect input parameter!");

     }

   else

     {

      ExtVPeriod=InpVPeriod;

      ExtAPeriod=InpAPeriod;

     } 

   ExtAStart=ExtVPeriod+ExtAPeriod-1;

//--- 1 additional buffer used for counting.

   IndicatorBuffers(3);

   IndicatorDigits(Digits);

//--- indicator line

   SetIndexStyle(0,DRAW_LINE);

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtABuffer);

   SetIndexBuffer(1,ExtVBuffer);

   SetIndexBuffer(2,ExtSBuffer);

//--- name for DataWindow and indicator subwindow label

   string short_name="A("+string(ExtVPeriod)+" "+string(ExtAPeriod)+")";

   IndicatorShortName(short_name);

   SetIndexLabel(0,short_name);

//--- sets first bar from what index will be drawn

   SetIndexDrawBegin(0,ExtAStart);

//--- initialization done

   return(INIT_SUCCEEDED);

  }

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

//| velocity and accelerated speed                                              |

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

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,limit_V,limit_A;

//--- check for bars count

   if(rates_total<=ExtVPeriod)

      return(0); // not enough bars for calculation

//--- counting from 0 to rates_total

   ArraySetAsSeries(ExtABuffer,false);

   ArraySetAsSeries(ExtVBuffer,false);

   ArraySetAsSeries(ExtSBuffer,false);

   ArraySetAsSeries(close,false);

//--- preliminary calculations

   if(prev_calculated==0)

     {

      limit_V=ExtVPeriod-1;

      limit_A=ExtAStart-1;

      //--- filling out the array of True Range values for each period

      for(i=0;i<limit_V && !IsStopped();i++)

         {

          ExtVBuffer[i]=0.0;

          ExtSBuffer[i]=0.0;

         }

      for(i=limit_V;i<=limit_A && !IsStopped();i++)

         {

          ExtSBuffer[i]=close[i]-close[i-ExtVPeriod+1];

          ExtVBuffer[i]=ExtSBuffer[i]/ExtVPeriod;

         } 

      //--- 

      for(i=0;i<limit_A && !IsStopped();i++)

         {

          ExtABuffer[i]=0.0;

         }

      ExtABuffer[limit_A]=(ExtVBuffer[limit_A]-ExtVBuffer[limit_A-ExtAPeriod+1])/ExtAPeriod;

      limit_V=ExtAStart;

      limit_A=ExtAStart;

     }

   else

     { 

      limit_A=prev_calculated-1;

      limit_V=prev_calculated-1;

     }

//--- the main loop of calculations

   for(i=limit_A;i<rates_total && !IsStopped();i++)

     {

      ExtSBuffer[i]=close[i]-close[i-ExtVPeriod+1];

      ExtVBuffer[i]=ExtSBuffer[i]/ExtVPeriod;

      ExtABuffer[i]=(ExtVBuffer[i]-ExtVBuffer[i-ExtAPeriod+1])/ExtAPeriod;

     }

//--- return value of prev_calculated for next call

   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 ---