//+------------------------------------------------------------------+
//| PivotLine.mq4 |
//| Copyright 2023, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Crimson
#property indicator_color2 Yellow
#property indicator_color3 RoyalBlue
#property indicator_color4 White
#property indicator_color5 RoyalBlue
#property indicator_color6 Yellow
#property indicator_color7 Crimson
double R3[];
double R2[];
double R1[];
double P0[];
double S1[];
double S2[];
double S3[];
#define PREFIX "Grid_"
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
SetIndexBuffer(0,R3);
SetIndexBuffer(1,R2);
SetIndexBuffer(2,R1);
SetIndexBuffer(3,P0);
SetIndexBuffer(4,S1);
SetIndexBuffer(5,S2);
SetIndexBuffer(6,S3);
SetIndexStyle(0, DRAW_LINE, STYLE_DOT, 1);
SetIndexStyle(1, DRAW_LINE, STYLE_DOT, 1);
SetIndexStyle(2, DRAW_LINE, STYLE_DOT, 1);
SetIndexStyle(3, DRAW_LINE, STYLE_SOLID,1);
SetIndexStyle(4, DRAW_LINE, STYLE_DOT, 1);
SetIndexStyle(5, DRAW_LINE, STYLE_DOT, 1);
SetIndexStyle(6, DRAW_LINE, STYLE_DOT, 1);
SetIndexLabel(0,"R3");
SetIndexLabel(1,"R2");
SetIndexLabel(2,"R1");
SetIndexLabel(3,"PIVOT");
SetIndexLabel(4,"S1");
SetIndexLabel(5,"S2");
SetIndexLabel(6,"S3");
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0, PREFIX);
}
//+------------------------------------------------------------------+
//| 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[])
{
int limit = Bars - prev_calculated - 2;
int min = PeriodSeconds(PERIOD_D1) / PeriodSeconds();
if (limit < min) limit = min;
bool uptrend=true;
for (int i = limit; i >= 0; i--)
{
double h = iHigh(NULL, PERIOD_D1,iBarShift(NULL, PERIOD_D1, Time[i])+1);
double c = iClose(NULL, PERIOD_D1,iBarShift(NULL, PERIOD_D1, Time[i])+1);
double l = iLow(NULL, PERIOD_D1,iBarShift(NULL, PERIOD_D1, Time[i])+1);
double p =(h+l+c)/3;
P0[i] = p;
R1[i] = (2*p)-l;
R2[i] = p+h-l;
R3[i] = (2*p)-(2*l)+h;
S1[i] = (2*p)-h;
S2[i] = p-h+l;
S3[i] = (2*p)-(2*h)+l;
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
if (id == CHARTEVENT_CHART_CHANGE) {
ObjectsDeleteAll(0, PREFIX);
int barF = (int)ChartGetInteger(0, CHART_FIRST_VISIBLE_BAR);
if (barF > Bars - 2) barF = Bars - 2;
int barR = barF - (int)ChartGetInteger(0, CHART_WIDTH_IN_BARS);
if (barR < 0) barR = 0;
for (int i = barF; i >= barR; i--) {
bool drawV = false;
switch (_Period) {
case PERIOD_M1 :
drawV = Time[i] % PeriodSeconds(PERIOD_D1) == 0;
break;
case PERIOD_M5 :
drawV = Time[i] % PeriodSeconds(PERIOD_D1) == 0;
break;
case PERIOD_M15 :
case PERIOD_M30 :
drawV = Time[i] % PeriodSeconds(PERIOD_D1) == 0;
break;
case PERIOD_H1 :
drawV = Time[i] % PeriodSeconds(PERIOD_D1) == 0;
break;
case PERIOD_H4 :
drawV = Time[i] % PeriodSeconds(PERIOD_D1) == 0;// && TimeDayOfWeek(Time[i]) == MONDAY;
break;
case PERIOD_D1 :
drawV = TimeMonth(Time[i]) != TimeMonth(Time[i + 1]);
break;
case PERIOD_W1 :
drawV = TimeMonth(Time[i]) != TimeMonth(Time[i + 1]) && TimeMonth(Time[i]) % 3 == 1;
break;
case PERIOD_MN1 :
drawV = TimeYear(Time[i]) != TimeYear(Time[i + 1]);
break;
}
if (drawV) {
LineCreate(0, PREFIX + "VL" + IntegerToString(i), OBJ_VLINE, 0, Time[i]);
}
}
}
}
//+------------------------------------------------------------------+
//| Create the vertical line |
//+------------------------------------------------------------------+
bool LineCreate(const long chart_ID = 0, // chart's ID
const string name = "VLine", // line name
const int type = OBJ_VLINE, // type
const int sub_window = 0, // subwindow index
datetime time = 0, // line time
double price = 0, // line price
const color clr = clrGray, // line color
const ENUM_LINE_STYLE style = STYLE_DOT, // line style
const int width = 1, // line width
const bool back = false, // in the background
const bool selection = false, // highlight to move
const bool hidden = true, // hidden in the object list
const long z_order = 0) // priority for mouse click
{
//--- create a vertical line
if(!ObjectCreate(chart_ID, name, type, sub_window, time, price)) {
return(false);
}
//--- set line color
ObjectSetInteger(chart_ID, name, OBJPROP_COLOR, clr);
//--- set line display style
ObjectSetInteger(chart_ID, name, OBJPROP_STYLE, style);
//--- set line width
ObjectSetInteger(chart_ID, name, OBJPROP_WIDTH, width);
//--- display in the foreground (false) or background (true)
ObjectSetInteger(chart_ID, name, OBJPROP_BACK, back);
//--- enable (true) or disable (false) the mode of moving the line by mouse
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
ObjectSetInteger(chart_ID, name, OBJPROP_SELECTABLE, selection);
ObjectSetInteger(chart_ID, name, OBJPROP_SELECTED, selection);
//--- hide (true) or display (false) graphical object name in the object list
ObjectSetInteger(chart_ID, name, OBJPROP_HIDDEN, hidden);
//--- set the priority for receiving the event of a mouse click in the chart
ObjectSetInteger(chart_ID, name, OBJPROP_ZORDER, z_order);
//--- successful execution
return(true);
}
MQL4 Pivotサンプルコード
当ページのリンクには広告が含まれています。