TickVolumeEma

Author: Copyright 2016, Daisuke
0 Views
0 Downloads
0 Favorites
TickVolumeEma
ÿþ//+------------------------------------------------------------------+

//|                                                TickVolumeEma.mq4 |

//|                                          Copyright 2016,  Daisuke|

//|                                   http://mt4program.blogspot.jp/ |

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

#property copyright "Copyright 2016,  Daisuke"

#property link      "http://mt4program.blogspot.jp/"

#property version   "1.00"

#property strict

#property indicator_chart_window



//Ð0Ã0Õ0¡0ü0’0cš[Y0‹00

#property indicator_buffers 1



//×0í0Ã0È0pe’0cš[Y0‹00

#property indicator_plots   1



#property indicator_label1  "TVEMA"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrIndianRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1



input int SmoothPeriod=4;   // Ü0ê0å0ü0à0$Ps^ÑnSg“•

input int AveragePeriod=100; // s^GWg“•



input int MaPeriod = 21;// ûyÕRs^GWg“•

input int   MaShift = 0;// hˆ:yûyÕR

input ENUM_APPLIED_PRICE MaPrice=PRICE_CLOSE;//i(u¡O<h

input bool IsAcceralationOnly=false;// R¹eTn00n04XTTrue



// hˆ:yÐ0Ã0Õ0¡0

double ma[];



// s^ÑnScpe

double alfa;



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

//| RgS                                                            |

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

int OnInit()

  {

   SetIndexBuffer(0,ma);



   alfa=2.0/(MaPeriod+1.0);



   SetIndexShift(0,MaShift);



   return INIT_SUCCEEDED;

  }



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

//| Š—{¤0Ù0ó0È0                                                        |

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

int OnCalculate(const int rates_total,          //Tì0ü0È0‰ }pe

                const int prev_calculated,      //Š—{n0‰ }pe

                const datetime &time[],         //‰ }T0h0n0Bf“•M‘R

                const double &open[],           //ª0ü0×0ó0¡O<hM‘R

                const double &high[],           //ؚ$PM‘R

                const double &low[],            //‰[$PM‘R

                const double &close[],          //¯0í0ü0º0¡O<hM‘R

                const long &tick_volume[],      //Æ0£0Ã0¯0peÿ‰ }n0ôf°eÞVpe	ÿ

                const long &volume[],           //Ÿ[Ü0ê0å0ü0à0ÿÿ	ÿ

                const int &spread[])            //¹0×0ì0Ã0È0

  {

   for(int i=rates_total-prev_calculated-1; i>=0; i--)

     {

      // Tick$Pn0Ú}b_ R͑ûyÕRs^GW’0Bl0‹00

      double smoothVolume=0;

      int count = 0;

      for(int j = 0; j < SmoothPeriod &&(i + j) < rates_total; j++ )

        {

         int weight=SmoothPeriod-j;

         count+=weight;

         smoothVolume+=(double)tick_volume[i+j] *weight;

        }

      if(count>0)

        {

         smoothVolume=smoothVolume/count;

        }



      // Tick$Pn0XS}s^GW’0Bl0‹00

      double average=0;

      count=0;



      for(int j=0; j<AveragePeriod && (i+j)<rates_total; j++)

        {

         count++;

         average+=(double)tick_volume[i+j];

        }

      if(count>0)

        {

         average=average/count;

        }



      //EMA’0Š—{Y0‹00

      double price=GetPrice(open[i],close[i],high[i],low[i],MaPrice);

      if(average>0 && (i+1)<rates_total)

        {

         double currentAlfa=alfa*smoothVolume/average;

         //         double currentAlfa = smoothVolume > average ? (alfa * 2) : (alfa / 2);



         if(currentAlfa>=1) currentAlfa=1;

         if(IsAcceralationOnly && currentAlfa<alfa) currentAlfa=alfa;



         // cpes^ÑnSûyÕRs^GW

         ma[i]=price*currentAlfa+(1-currentAlfa)*ma[i+1];

        }

      else

        {

         ma[i]=price;

        }

     }



   return rates_total - 1;

  }

  

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

//| ¡O<h’0Š—{Y0‹0                                                     |

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

double GetPrice(

                double open,   // ª0ü0×0ó0$P

                double close,  // ¯0í0ü0º0$P

                double high,   // ؚ$P

                double low,    // ‰[$P

                ENUM_APPLIED_PRICE maPrice    //ÖS—_¡O<h

                )

  {

   double price=0;



   switch(maPrice)

     {

      case PRICE_CLOSE:

         price=close;

         break;

      case PRICE_OPEN:

         price=open;

         break;

      case PRICE_HIGH:

         price=high;

         break;

      case PRICE_LOW:

         price=low;

         break;

      case PRICE_MEDIAN:

         price=(high+low)/2;

         break;

      case PRICE_TYPICAL:

         price=(high+low+close)/3;

         break;

      case PRICE_WEIGHTED:

         price=(high+low+close+close)/4;

         break;

     }

   return price;

  }

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

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