Market_Statistics_v7_0

Author: Copyright � 2010, Akif TOKUZ
Market_Statistics_v7_0
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
Market_Statistics_v7_0
//+------------------------------------------------------------------+
//|                                       Market_Statistics_v7_0.mq4 |
//|                                     Copyright © 2010, Akif TOKUZ |
//|                                             akifusenet@gmail.com |
//| Some concepts are taken from Igorad's AllAverages_v2_2           |
//| inspired from Jperl's `Trading With Market Statistics` thread on |
//| Traders Laboratory.                                              |
//|                                                                  |
//| History:                                                         |
//| 23.05.2010: v7.0=>Realization of 6.2 is a reinvention of the     |
//|               => the wheel. This is much MORE faster.
//| 17.03.2010: v6.2=>Bug-fix of resetting at week start             |
//| 04.03.2010: v6.1=>Record MTF calculated stat values for every bar|
//| 17.01.2010: v5.0=>Sliding window for days back instead of fixed  |
//|               =>starting date(calculate starting from x day      |
//|               =>back, starting date is updated automatically in  |
//|               =>real-time)                                       |
//| 17.01.2010: v4.2=>Small Bugfix [zero division possibility ]      |
//|               =>When time period change call deinit and init     |
//| 09.09.2009: v4.1=>Small bugfix [delete startDate correct prefix] |
//| 09.09.2009: v4=>Instead of fixed start date now we have a        |
//|               =>relative startdate like 2 days back at 22:00     |
//| 02.09.2009: v3=>Calculation done only at the start of a new bar  |
//|               =>implementation is corrected                      |
//|               =>histogram defaulted 50 bar sd3show made true     |
//| 10.08.2009: v2=>End date added.SD2Show defaulted to true         |
//|               =>Show/disable options added for every line        |
//| 06.08.2009: v1=>Initial release                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Akif TOKUZ"
#property link      "akifusenet@gmail.com"

#property indicator_chart_window
#property indicator_buffers 8

#property indicator_color1 Aqua        //VWAP
#property indicator_width1 2

#property indicator_color2 Green       //SD1Pos
#property indicator_width2 1
#property indicator_style2 2

#property indicator_color3 Red         //SD1Neg
#property indicator_width3 1
#property indicator_style3 2

#property indicator_color4 DarkOliveGreen//SD2Pos
#property indicator_width4 1
#property indicator_style4 2

#property indicator_color5 Crimson     //SD2Neg
#property indicator_width5 1
#property indicator_style5 2

#property indicator_color6 DarkGreen   //SD3Pos
#property indicator_width6 1
#property indicator_style6 2

#property indicator_color7 FireBrick   //SD3Neg
#property indicator_width7 1
#property indicator_style7 2

//---- input parameters
extern int Periods=20;
extern int AppliedPrice=5;

extern double  SD1_Deviation  = 1.0;
extern double  SD2_Deviation  = 2.0;
extern double  SD3_Deviation  = 3.0;

extern bool    Show_VWMA      = true;
extern bool    Show_SD1       = true;
extern bool    Show_SD2       = true;
extern bool    Show_SD3       = true;

//---- buffers
double VWMA[];
double SD1Pos[];
double SD1Neg[];
double SD2Pos[];
double SD2Neg[];
double SD3Pos[];
double SD3Neg[];
double UsedPrice[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(8);
 
   if (Show_VWMA==true) SetIndexStyle(0,DRAW_LINE);
   else  SetIndexStyle(0,DRAW_NONE);
   SetIndexLabel(0,"VWMA");      
   SetIndexBuffer(0,VWMA);   
   
   if (Show_SD1==true) SetIndexStyle(1,DRAW_LINE);
   else  SetIndexStyle(1,DRAW_NONE);
   SetIndexLabel(1,"SD1Pos");      
   SetIndexBuffer(1,SD1Pos);
   
   if (Show_SD1==true) SetIndexStyle(2,DRAW_LINE);
   else  SetIndexStyle(2,DRAW_NONE);
   SetIndexLabel(2,"SD1Neg");      
   SetIndexBuffer(2,SD1Neg);
   
   if (Show_SD2==true) SetIndexStyle(3,DRAW_LINE);
   else  SetIndexStyle(3,DRAW_NONE);
   SetIndexLabel(3,"SD2Pos");      
   SetIndexBuffer(3,SD2Pos);
   
   if (Show_SD2==true) SetIndexStyle(4,DRAW_LINE);
   else  SetIndexStyle(4,DRAW_NONE);
   SetIndexLabel(4,"SD2Neg");      
   SetIndexBuffer(4,SD2Neg);
   
   if (Show_SD3==true) SetIndexStyle(5,DRAW_LINE);
   else  SetIndexStyle(5,DRAW_NONE);
   SetIndexLabel(5,"SD3Pos");      
   SetIndexBuffer(5,SD3Pos);
   
   if (Show_SD3==true) SetIndexStyle(6,DRAW_LINE);
   else  SetIndexStyle(6,DRAW_NONE);
   SetIndexLabel(6,"SD3Neg");      
   SetIndexBuffer(6,SD3Neg);
   
   SetIndexStyle(7,DRAW_NONE);
   SetIndexBuffer(7,UsedPrice);

   string short_name="VWMA_Bands";
   IndicatorShortName(short_name);     

//---- first values aren't drawn
   SetIndexDrawBegin(0,Periods);
//----
   return(0);
  }
  

// MA_Method=19: VWMA - Volume Weighted Moving Average 
double VWMA(int per,int bar)
{
   double Sum = 0;
   double Weight = 0;
   
      for(int i = 0;i < per;i++)
      { 
      Weight+= Volume[bar+i];
      Sum += UsedPrice[bar+i]*Volume[bar+i];
      }
   if(Weight>0) double vwma = Sum/Weight;
   else vwma = 0; 
   return(vwma);
} 
  
  
//+------------------------------------------------------------------+
//| VWMA Bands                                               |
//+------------------------------------------------------------------+
int start()
  {
   int    i,j,nLimit,nCountedBars;
   double dAPrice,dAmount,dMovingAverage,SD;
   double totalVolume;
     
//---- insufficient data
   if(Bars<=Periods) return(0);
//---- bars count that does not changed after last indicator launch.
   nCountedBars=IndicatorCounted();

   i=Bars-Periods-1;
   if(nCountedBars>Periods) 
      i=Bars-nCountedBars;  
  
   while(i>=0)
     {
      UsedPrice[i] = GetAppliedPrice(AppliedPrice,i);
      dAmount=0.0;
      //dMovingAverage=iCustom(NULL,0,"AllAverages_v2.2",0,AppliedPrice,Periods,0,19,0,0,i);
      dMovingAverage=VWMA(Periods,i);
      
      totalVolume=0;
      for(j=0; j<Periods; j++)
        {
         dAPrice=GetAppliedPrice(AppliedPrice,i+j);
         dAmount+=Volume[i+j]*(dAPrice-dMovingAverage)*(dAPrice-dMovingAverage);
         totalVolume+=Volume[i+j];
        }
      SD=MathSqrt(dAmount/totalVolume);
      VWMA[i]=dMovingAverage;
      SD1Pos[i]=dMovingAverage+SD*SD1_Deviation;
      SD1Neg[i]=dMovingAverage-SD*SD1_Deviation;
      SD2Pos[i]=dMovingAverage+SD*SD2_Deviation;
      SD2Neg[i]=dMovingAverage-SD*SD2_Deviation;
      SD3Pos[i]=dMovingAverage+SD*SD3_Deviation;
      SD3Neg[i]=dMovingAverage-SD*SD3_Deviation;         
      
      i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetAppliedPrice(int nAppliedPrice, int nIndex)
  {
   double dPrice;
//----
   switch(nAppliedPrice)
     {
      case 0:  dPrice=Close[nIndex];                                  break;
      case 1:  dPrice=Open[nIndex];                                   break;
      case 2:  dPrice=High[nIndex];                                   break;
      case 3:  dPrice=Low[nIndex];                                    break;
      case 4:  dPrice=(High[nIndex]+Low[nIndex])/2.0;                 break;
      case 5:  dPrice=(High[nIndex]+Low[nIndex]+Close[nIndex])/3.0;   break;
      case 6:  dPrice=(High[nIndex]+Low[nIndex]+2*Close[nIndex])/4.0; break;
      default: dPrice=0.0;
     }
//----
   return(dPrice);
  }
//+------------------------------------------------------------------+

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