Miscellaneous
0
Views
0
Downloads
0
Favorites
Skewness_v1
//+------------------------------------------------------------------+
//| Skewness_v1.mq4 |
//| Copyright © 2009, Akif TOKUZ |
//| akifusenet@gmail.com |
//| 11.08.2009: v1=>Initial release |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Akif TOKUZ"
#property link "akifusenet@gmail.com"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red //Skew
#property indicator_width1 1
//---- input parameters
extern datetime StartDate=D'2009.08.06 00:00';
extern datetime EndDate=D'2009.08.07 00:00';
//---- buffers
double Skew[];
double PVP[];
double VWAP[];
double Hist[];
datetime OpenTime = 0; // To check if we have a new bar
int items; // numbers of items inside volume histogram
int Bars_Back = 0; // Shows the starting bar for given date
double SD; // standart deviation
// Finds the bar number for the given date
int FindStartIndex(datetime startDate)
{
for (int i=Bars-1; i>=0; i--)
{
if (Time[i]>=StartDate) return (i);
}
return( 0);
}
int init()
{
//---- indicators
IndicatorBuffers(3);
SetIndexStyle(0,DRAW_LINE);
SetIndexLabel(0,"Skew");
SetIndexBuffer(0,Skew);
SetIndexStyle(1,DRAW_NONE);
SetIndexLabel(1,"PVP");
SetIndexBuffer(1,PVP);
SetIndexStyle(2,DRAW_NONE);
SetIndexLabel(2,"VWAP");
SetIndexBuffer(2,VWAP);
string short_name="Skewness";
IndicatorShortName(short_name);
return(0);
}
int start()
{
double TotalVolume=0;
double TotalPV=0;
int n;
if (OpenTime != Open[0])
{
Bars_Back=FindStartIndex(StartDate);
OpenTime = Open[0];
double max = High[iHighest( NULL , 0, MODE_HIGH, Bars_Back, 0)];
double min = Low[ iLowest( NULL , 0, MODE_LOW, Bars_Back, 0)];
items = MathRound((max - min) / Point);
ArrayResize(Hist, items);
ArrayInitialize(Hist, 0);
TotalVolume=0;
TotalPV=0;
for (int i = Bars_Back; i >= 1; i--)
{
if (Time[i]<EndDate) // Only calculate if we didnt reach the end date
{
double t1 = Low[i], t2 = Open[i], t3 = Close[i], t4 = High[i];
if (t2 > t3) {t3 = Open[i]; t2 = Close[i];}
double totalRange = 2*(t4 - t1) - t3 + t2;
if (totalRange != 0.0)
{
for (double Price_i = t1; Price_i <= t4; Price_i += Point)
{
n = MathRound((Price_i - min) / Point);
if (t1 <= Price_i && Price_i < t2)
{
Hist[n] += MathRound(Volume[i]*2*(t2-t1)/totalRange);
}
if (t2 <= Price_i && Price_i <= t3)
{
Hist[n] += MathRound(Volume[i]*(t3-t2)/totalRange);
}
if (t3 < Price_i && Price_i <= t4)
{
Hist[n] += MathRound(Volume[i]*2*(t4-t3)/totalRange);
}
}//for
}else
{
// Check if all values are equal to each other
n = MathRound((t3 - min) / Point);
Hist[n] += Volume[i];
}//if
// use H+L+C/3 as average price
TotalPV+=Volume[i]*((Low[i]+High[i]+Close[i])/3);
TotalVolume+=Volume[i];
if (i==Bars_Back) PVP[i]=Close[i];
else PVP[i]=min+ArrayMaximum(Hist)*Point;
if (i==Bars_Back) VWAP[i]=Close[i];
else VWAP[i]=TotalPV/TotalVolume;
SD=0;
for (int k = Bars_Back; k >= i; k--)
{
double avg=(High[k]+Close[k]+Low[k])/3;
double diff=avg-VWAP[i];
SD+=(Volume[k]/TotalVolume)*(diff*diff);
}
SD=MathSqrt(SD);
Skew[i]=(VWAP[i]-PVP[i])/SD;
}//if for checking end date
}//for BARS BACK
}//MAIN IF BAR START
return(0);
}
int deinit()
{
return(0);
}
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---