166 lines
5.1 KiB
MQL5
166 lines
5.1 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| PivotPoint.mq5 |
|
|
//| Copyright 2018, Benjamin Pillet |
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright 2018, Benjamin Pillet"
|
|
#property link ""
|
|
#property version "1.00"
|
|
#property indicator_chart_window
|
|
#property indicator_buffers 7
|
|
#property indicator_plots 7
|
|
//--- plot pivot
|
|
#property indicator_label1 "pivot"
|
|
#property indicator_type1 DRAW_LINE
|
|
#property indicator_color1 clrGold
|
|
#property indicator_style1 STYLE_SOLID
|
|
#property indicator_width1 1
|
|
//--- plot R1
|
|
#property indicator_label2 "R1"
|
|
#property indicator_type2 DRAW_LINE
|
|
#property indicator_color2 clrRed
|
|
#property indicator_style2 STYLE_SOLID
|
|
#property indicator_width2 1
|
|
//--- plot R2
|
|
#property indicator_label3 "R2"
|
|
#property indicator_type3 DRAW_LINE
|
|
#property indicator_color3 clrRed
|
|
#property indicator_style3 STYLE_SOLID
|
|
#property indicator_width3 1
|
|
//--- plot R3
|
|
#property indicator_label4 "R3"
|
|
#property indicator_type4 DRAW_LINE
|
|
#property indicator_color4 clrRed
|
|
#property indicator_style4 STYLE_SOLID
|
|
#property indicator_width4 1
|
|
//--- plot S1
|
|
#property indicator_label5 "S1"
|
|
#property indicator_type5 DRAW_LINE
|
|
#property indicator_color5 clrBlue
|
|
#property indicator_style5 STYLE_SOLID
|
|
#property indicator_width5 1
|
|
//--- plot S2
|
|
#property indicator_label6 "S2"
|
|
#property indicator_type6 DRAW_LINE
|
|
#property indicator_color6 clrBlue
|
|
#property indicator_style6 STYLE_SOLID
|
|
#property indicator_width6 1
|
|
//--- plot S3
|
|
#property indicator_label7 "S3"
|
|
#property indicator_type7 DRAW_LINE
|
|
#property indicator_color7 clrBlue
|
|
#property indicator_style7 STYLE_SOLID
|
|
#property indicator_width7 1
|
|
//--- indicator buffers
|
|
double pivotBuffer[];
|
|
double R1Buffer[];
|
|
double R2Buffer[];
|
|
double R3Buffer[];
|
|
double S1Buffer[];
|
|
double S2Buffer[];
|
|
double S3Buffer[];
|
|
|
|
//--- input parameters and variables
|
|
//input ENUM_TIMEFRAMES TFpivot = PERIOD_D1;
|
|
input bool weekend; //merge Saturday and Sunday with Friday
|
|
double P;
|
|
double R1;
|
|
double R2;
|
|
double R3;
|
|
double S1;
|
|
double S2;
|
|
double S3;
|
|
int prevPeriod;
|
|
double H=0;
|
|
double L=0;
|
|
double O=0;
|
|
double newO=0;
|
|
double C=0;
|
|
MqlDateTime current;
|
|
int pos;
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Custom indicator initialization function |
|
|
//+------------------------------------------------------------------+
|
|
int OnInit()
|
|
{
|
|
//--- indicator buffers mapping
|
|
SetIndexBuffer(0,pivotBuffer,INDICATOR_DATA);
|
|
SetIndexBuffer(1,R1Buffer,INDICATOR_DATA);
|
|
SetIndexBuffer(2,R2Buffer,INDICATOR_DATA);
|
|
SetIndexBuffer(3,R3Buffer,INDICATOR_DATA);
|
|
SetIndexBuffer(4,S1Buffer,INDICATOR_DATA);
|
|
SetIndexBuffer(5,S2Buffer,INDICATOR_DATA);
|
|
SetIndexBuffer(6,S3Buffer,INDICATOR_DATA);
|
|
|
|
//---
|
|
return(INIT_SUCCEEDED);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Custom indicator iteration function |
|
|
//+------------------------------------------------------------------+
|
|
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[])
|
|
{
|
|
//---
|
|
pos = prev_calculated -1;
|
|
if(pos < 1){
|
|
pos = 1;
|
|
}
|
|
for(int i=pos; i<rates_total; i++){
|
|
TimeToStruct(time[i],current);
|
|
//if same day, check if new high or low
|
|
if(prevPeriod == current.day_of_year || ((current.day_of_week == 7 || current.day_of_week == 0) && weekend)){
|
|
if(high[i]>H){
|
|
H = high[i];
|
|
}
|
|
if(low[i]<L){
|
|
L = low[i];
|
|
}
|
|
}
|
|
//if new day
|
|
else{
|
|
//set open and close
|
|
O = newO;
|
|
newO = open[i];
|
|
C = close[i-1];
|
|
|
|
//calculate pivot, supports and resistances for the day with data from previous day
|
|
P = (H+L+C)/3;
|
|
R1 = 2*P-L;
|
|
S1 = 2*P-H;
|
|
R2 = P+(H-L);
|
|
S2 = P-(H-L);
|
|
R3 = R1+(H-L);
|
|
S3 = S1-(H-L);
|
|
|
|
//reset high and low
|
|
H = high[i];
|
|
L = low[i];
|
|
}
|
|
//pass the values to the indicator buffers
|
|
pivotBuffer[i] = P;
|
|
R1Buffer[i] = R1;
|
|
S1Buffer[i] = S1;
|
|
R2Buffer[i] = R2;
|
|
S2Buffer[i] = S2;
|
|
R3Buffer[i] = R3;
|
|
S3Buffer[i] = S3;
|
|
|
|
//reset period
|
|
prevPeriod = current.day_of_year;
|
|
}
|
|
|
|
|
|
//--- return value of prev_calculated for next call
|
|
return(rates_total);
|
|
}
|
|
//+------------------------------------------------------------------+
|