Code: Select all
//+------------------------------------------------------------------+
//| HCPLINES_3.mq4 |
//| Copyright 2019, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property description " "
#property description "Copyright "
#property description "Version 1.03"
#property description "Updated on 20/07/2019"
#property version "1.00"
#property indicator_chart_window
// name of the indicator
input string ID = "HCPLines"; //High+Close+Pivot Lines
//--- these will be the inputs for choosing where to plot the Sup/Res and their targets if broken
input int Res = 20; //Price Tends To Reverse @ 20pips Above High
input int Sup = 20; //Price Tends To Reverse @ 20pips Below Low
input int ResTarget = 50; //Best To Aim For >2:1 Risk:Rewards
input int SupTarget = 50; //Best To Aim For >2:1 Risk:Rewards
//---
int dig, tf;
double pnts;
static datetime DayTime;
double HP, //Yesterdays High Price
LP, //Yesterdays Low Price
CP, //Yesterdays Close Price
PP, //Todays Pivot Price
RP, //Todays Possible Resistance Level
SP, //Todays Possible Support Level
HTP, //If Res Level is Broken This Is A Possible Target
LTP, //If Sup Level is Broken This Is A Possible Target
TSS, //Yesterday Session Start Time
YSS1,
YSS;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//----
Del_obj(ID);
//----
}
//+------------------------------------------------------------------+
//| 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[])
{
//---
//---- initialize variables
dig=SymbolInfoInteger(Symbol(),SYMBOL_DIGITS);
pnts=SymbolInfoDouble(Symbol(),SYMBOL_POINT);
if (dig==3 || dig==5) pnts*=10;
//----
tf=1440;
//DayTime=TimeCurrent();
//----
//+------------------------------------------------------------------+
//| Count How Many Bars Are Available On The Chart |
//+------------------------------------------------------------------+
int limit,
counted_bars=IndicatorCounted();
// check for possible errors
if(counted_bars<0) return(-1);
// last counted bars will be checked
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//--- Main Loop
for(int i=0; i<limit; i++)
// counting from current bar to the previous bar
// i=0 is the current bar, bar 1 = previous bar
// so current bar < all the bars available - all of the counted bars
// i++ includes the next available bar
// alternative method would be to start at bar 1 and look right to the current bar
// i=limit-1; i>0; i--;
{
//+------------------------------------------------------------------+
//| How To Calculate The Variables |
//+------------------------------------------------------------------+
HP = iHigh(Symbol(),PERIOD_D1,iHighest(Symbol(),PERIOD_D1,MODE_HIGH,1,1));
LP = iLow(Symbol(),PERIOD_D1,iLowest(Symbol(),PERIOD_D1,MODE_LOW,1,1));
CP = iClose(Symbol(),PERIOD_D1,1);
PP = (HP+LP+CP)/3;
RP = HP+(Res*pnts);
SP = LP-(Sup*pnts);
//----
HTP = RP+(ResTarget*pnts);
LTP = SP-(SupTarget*pnts);
//---- working with the time1,time2 variables
YSS = iTime(Symbol(),tf,0); //time1[0] trendline starts here
YSS1 = iTime(Symbol(),tf,1); //time1[1]
TSS = Time[0]; //time2[0] Time[0]+Period()*60
//+------------------------------------------------------------------+
//| How To Draw The Objects |
//+------------------------------------------------------------------+
//---- sequence from top to bottom: YHighPrice,YLowPrice,Pivot,PossibleResistance,PossibleSupport,DailyHighTarget,DailyLowTarget,StartOfDay
Del_obj(ID);
TrendCreate(0,ID+"HP",0,YSS1,TSS,HP,clrDarkRed,STYLE_DOT);
TrendCreate(0,ID+"LP",0,YSS1,TSS,LP,clrDarkGreen,STYLE_DOT);
TrendCreate(0,ID+"PP",0,YSS,TSS,PP,clrBlue,STYLE_SOLID);
//TrendCreate(0,ID+"PP",0,PP,clrBlue,STYLE_SOLID);
TrendCreate(0,ID+"RP",0,YSS,TSS,RP,clrLightPink,STYLE_DASH);
TrendCreate(0,ID+"SP",0,YSS,TSS,SP,clrLimeGreen,STYLE_DASH);
TrendCreate(0,ID+"HTP",0,YSS,TSS,HTP,clrBlack,STYLE_DASH);
TrendCreate(0,ID+"LTP",0,YSS,TSS,LTP,clrBlack,STYLE_DASH);
//----
VLineCreate(0,ID+"YVL",0,YSS,clrDarkGray,STYLE_DOT);
VLineCreate(0,ID+"TVL",0,YSS1,clrDarkGray,STYLE_DOT);
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| FUNCTION: Create a trend line by the given coordinates |
//+------------------------------------------------------------------+
//TrendCreate(0,ID+"ChartTrend",0,Time[3],lo,Time[1],lo,clrRed,STYLE_SOLID,3)
bool TrendCreate(const long chart_ID=0, // chart's ID
const string name="TrendLine", // line name
const int sub_window=0, // subwindow index
//datetime time=0, // time
double time1=0,
double time2=0,
double price1=0,
const color clr=clrRed, // line color
const ENUM_LINE_STYLE style=STYLE_SOLID, // line style
const int width=1, // line width
const bool back=true, // in the background
const bool selection=false, // highlight to move
const bool ray_right=true, // line's continuation to the right
const bool hidden=true, // hidden in the object list
const long z_order=0) // priority for mouse click
{
//--- create a trend line by the given coordinates
//if(!ObjectCreate(chart_ID,name,OBJ_TREND,sub_window,iTime(Symbol(),tf,0),price1,Time[0]+Period()*60,price1))
if(!ObjectCreate(chart_ID,name,OBJ_TREND,sub_window,time1,price1,time2,price1))
{
Print(__FUNCTION__,
": failed to create a trend line! Error code = ",GetLastError());
return(false);
}
//---
ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
ObjectSetInteger(chart_ID,name,OBJPROP_RAY_RIGHT,ray_right);
ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
return(true);
}
//+------------------------------------------------------------------+
//| Create the vertical line |
//+------------------------------------------------------------------+
bool VLineCreate(const long chart_ID=0, // chart's ID
const string name="VLine", // line name
const int sub_window=0, // subwindow index
double time1=0, // line time
const color clr=clrRed, // line color
const ENUM_LINE_STYLE style=STYLE_SOLID, // line style
const int width=1, // line width
const bool back=true, // 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
{
//--- reset the error value
ResetLastError();
//--- create a vertical line
if(!ObjectCreate(chart_ID,name,OBJ_VLINE,sub_window,time1,0))
{
Print(__FUNCTION__,
": failed to create a vertical line! Error code = ",GetLastError());
return(false);
}
ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
return(true);
}
//+------------------------------------------------------------------+
//| FUNCTION: Delete objects |
//+------------------------------------------------------------------+
void Del_obj(string str)
{
//----
int k=0;
while (k<ObjectsTotal())
{
string objname=ObjectName(k);
if (StringSubstr(objname,0,StringLen(str))==str) ObjectDelete(objname);
else k++;
}
}
//+------------------------------------------------------------------+
//| Create the horizontal line |
//+------------------------------------------------------------------+
/*bool HLineCreate(const long chart_ID=0, // chart's ID
const string name="HLine", // line name
const int sub_window=0, // subwindow index
double price=0, // line price
const color clr=clrRed, // line color
const ENUM_LINE_STYLE style=STYLE_SOLID, // line style
const int width=1, // line width
const bool back=true, // 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
{
//--- reset the error value
ResetLastError();
//--- create a horizontal line
if(!ObjectCreate(chart_ID,name,OBJ_HLINE,sub_window,0,price))
{
Print(__FUNCTION__,
": failed to create a horizontal line! Error code = ",GetLastError());
return(false);
}
ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
return(true);
}
*/