//+------------------------------------------------------------------+
//| DayWeekHL_v1.mq4 |
//| Copyright 2020, Fabio Cavalloni |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Fabio Cavalloni"
#property link "https://www.mql5.com/en/users/kava93"
#property version "1.00"
#property strict
#property indicator_chart_window
input color iDailyColors = clrMagenta;
input color iWeeklyColors = clrBlue;
datetime lastDay = 0, lastWeek = 0, lastBar = 0;
double YDH, YDL, LWH, LWL;
int OnInit() {
HistorySync(_Symbol,PERIOD_D1);
HistorySync(_Symbol,PERIOD_W1);
return(INIT_SUCCEEDED);
}
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[]) {
//---------- Refresh history of D1 and W1 timeframe--------------
RefreshRates();
if( !HistorySync(_Symbol,PERIOD_D1) || !HistorySync(_Symbol,PERIOD_W1) ) return(prev_calculated);
//---------- Main calculation (one for bar)----------------------
bool IsNewBar = iTime(NULL,0,0) > lastBar;
if( IsNewBar ) {
bool IsNewDay = iTime(NULL,PERIOD_D1,0) > lastDay;
bool IsNewWeek = iTime(NULL,PERIOD_W1,0) > lastWeek;
lastBar = iTime(NULL,0,0);
//---------- Daily-----------------------------------------------
if( IsNewDay ) {
lastDay = iTime(NULL,PERIOD_D1,0);
YDH = iHigh(NULL,PERIOD_D1,1);
YDL = iLow(NULL,PERIOD_D1,1);
TrendCreate(0,"DayWeekHL_YDH",0,lastDay,YDH,lastDay+PERIOD_D1*60,YDH,iDailyColors,STYLE_DOT,1);
TextCreate(0,"DayWeekHL_YDH_Text",0,lastDay,YDH,"YDH","Arial",10,iDailyColors,0,ANCHOR_LEFT_LOWER);
TrendCreate(0,"DayWeekHL_YDL",0,lastDay,YDL,lastDay+PERIOD_D1*60,YDL,iDailyColors,STYLE_DOT,1);
TextCreate(0,"DayWeekHL_YDL_Text",0,lastDay,YDL,"YDL","Arial",10,iDailyColors,0,ANCHOR_LEFT_UPPER);
}
//----------Weekly-----------------------------------------------
if( IsNewWeek ) {
lastWeek = iTime(NULL,PERIOD_W1,0);
LWH = iHigh(NULL,PERIOD_W1,1);
LWL = iLow(NULL,PERIOD_W1,1);
TrendCreate(0,"DayWeekHL_LWH",0,lastWeek,LWH,lastWeek+PERIOD_D1*6*60,LWH,iWeeklyColors,STYLE_DOT,1);
TextCreate(0,"DayWeekHL_LWH_Text",0,lastWeek,LWH,"LWH","Arial",10,iWeeklyColors,0,ANCHOR_LEFT_LOWER);
TrendCreate(0,"DayWeekHL_LWL",0,lastWeek,LWL,lastWeek+PERIOD_D1*6*60,LWL,iWeeklyColors,STYLE_DOT,1);
TextCreate(0,"DayWeekHL_LWL_Text",0,lastWeek,LWL,"LWL","Arial",10,iWeeklyColors,0,ANCHOR_LEFT_LOWER);
}
}
return(rates_total);
}
void OnDeinit(const int reason) {
ObjectsDeleteAll(0,"DayWeekHL_");
}
//----------Synchronize history-----------------------------------------------
bool HistorySync(string symbol, ENUM_TIMEFRAMES period=PERIOD_CURRENT){
if(period == PERIOD_CURRENT) period = (ENUM_TIMEFRAMES)_Period;
ResetLastError();
datetime time = iTime(symbol, period, 0);
if(_LastError == 0 && time != 0) return true;
if(_LastError != ERR_HISTORY_WILL_UPDATED
&& _LastError != ERR_NO_HISTORY_DATA
) PrintFormat("iTime(%s,%i) Failed: %i", symbol, period, _LastError);
return false;
}
//+------------------------------------------------------------------+
//| Create a trend line by the given coordinates |
//+------------------------------------------------------------------+
bool TrendCreate(const long chart_ID=0, // chart's ID
const string name="TrendLine", // line name
const int sub_window=0, // subwindow index
datetime time1=0, // first point time
double price1=0, // first point price
datetime time2=0, // second point time
double price2=0, // second point 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 ray_right=false, // line's continuation to the right
const bool hidden=true, // hidden in the object list
const long z_order=0) // priority for mouse click
{
//--- set anchor points' coordinates if they are not set
ChangeTrendEmptyPoints(time1,price1,time2,price2);
//--- reset the error value
ResetLastError();
if( ObjectFind(chart_ID,name)>-1 ) TrendDelete(chart_ID,name);
//--- create a trend line by the given coordinates
if(!ObjectCreate(chart_ID,name,OBJ_TREND,sub_window,time1,price1,time2,price2))
{
Print(__FUNCTION__,
": failed to create a trend line! Error code = ",GetLastError());
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);
//--- enable (true) or disable (false) the mode of continuation of the line's display to the right
ObjectSetInteger(chart_ID,name,OBJPROP_RAY_RIGHT,ray_right);
//--- 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);
}
//+------------------------------------------------------------------+
//| Move trend line anchor point |
//+------------------------------------------------------------------+
bool TrendPointChange(const long chart_ID=0, // chart's ID
const string name="TrendLine", // line name
const int point_index=0, // anchor point index
datetime time=0, // anchor point time coordinate
double price=0) // anchor point price coordinate
{
//--- if point position is not set, move it to the current bar having Bid price
if(!time)
time=TimeCurrent();
if(!price)
price=SymbolInfoDouble(Symbol(),SYMBOL_BID);
//--- reset the error value
ResetLastError();
//--- move trend line's anchor point
if(!ObjectMove(chart_ID,name,point_index,time,price))
{
Print(__FUNCTION__,
": failed to move the anchor point! Error code = ",GetLastError());
return(false);
}
//--- successful execution
return(true);
}
//+------------------------------------------------------------------+
//| The function deletes the trend line from the chart. |
//+------------------------------------------------------------------+
bool TrendDelete(const long chart_ID=0, // chart's ID
const string name="TrendLine") // line name
{
//--- reset the error value
ResetLastError();
//--- delete a trend line
if(!ObjectDelete(chart_ID,name))
{
Print(__FUNCTION__,
": failed to delete a trend line! Error code = ",GetLastError());
return(false);
}
//--- successful execution
return(true);
}
//+------------------------------------------------------------------+
//| Check the values of trend line's anchor points and set default |
//| values for empty ones |
//+------------------------------------------------------------------+
void ChangeTrendEmptyPoints(datetime &time1,double &price1,
datetime &time2,double &price2)
{
//--- if the first point's time is not set, it will be on the current bar
if(!time1)
time1=TimeCurrent();
//--- if the first point's price is not set, it will have Bid value
if(!price1)
price1=SymbolInfoDouble(Symbol(),SYMBOL_BID);
//--- if the second point's time is not set, it is located 9 bars left from the second one
if(!time2)
{
//--- array for receiving the open time of the last 10 bars
datetime temp[10];
CopyTime(Symbol(),Period(),time1,10,temp);
//--- set the second point 9 bars left from the first one
time2=temp[0];
}
//--- if the second point's price is not set, it is equal to the first point's one
if(!price2)
price2=price1;
}
//+------------------------------------------------------------------+
//| Creating Text object |
//+------------------------------------------------------------------+
bool TextCreate(const long chart_ID=0, // chart's ID
const string name="Text", // object name
const int sub_window=0, // subwindow index
datetime time=0, // anchor point time
double price=0, // anchor point price
const string text="Text", // the text itself
const string font="Arial", // font
const int font_size=10, // font size
const color clr=clrRed, // color
const double angle=0.0, // text slope
const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type
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
{
//--- set anchor point coordinates if they are not set
ChangeTextEmptyPoint(time,price);
//--- reset the error value
ResetLastError();
if( ObjectFind(chart_ID,name)>-1 ) TextDelete(chart_ID,name);
//--- create Text object
if(!ObjectCreate(chart_ID,name,OBJ_TEXT,sub_window,time,price))
{
Print(__FUNCTION__,
": failed to create \"Text\" object! Error code = ",GetLastError());
return(false);
}
//--- set the text
ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
//--- set text font
ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
//--- set font size
ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
//--- set the slope angle of the text
ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);
//--- set anchor type
ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);
//--- set color
ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- 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 object by mouse
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);
}
//+------------------------------------------------------------------+
//| Move the anchor point |
//+------------------------------------------------------------------+
bool TextMove(const long chart_ID=0, // chart's ID
const string name="Text", // object name
datetime time=0, // anchor point time coordinate
double price=0) // anchor point price coordinate
{
//--- if point position is not set, move it to the current bar having Bid price
if(!time)
time=TimeCurrent();
if(!price)
price=SymbolInfoDouble(Symbol(),SYMBOL_BID);
//--- reset the error value
ResetLastError();
//--- move the anchor point
if(!ObjectMove(chart_ID,name,0,time,price))
{
Print(__FUNCTION__,
": failed to move the anchor point! Error code = ",GetLastError());
return(false);
}
//--- successful execution
return(true);
}
//+------------------------------------------------------------------+
//| Change the object text |
//+------------------------------------------------------------------+
bool TextChange(const long chart_ID=0, // chart's ID
const string name="Text", // object name
const string text="Text") // text
{
//--- reset the error value
ResetLastError();
//--- change object text
if(!ObjectSetString(chart_ID,name,OBJPROP_TEXT,text))
{
Print(__FUNCTION__,
": failed to change the text! Error code = ",GetLastError());
return(false);
}
//--- successful execution
return(true);
}
//+------------------------------------------------------------------+
//| Delete Text object |
//+------------------------------------------------------------------+
bool TextDelete(const long chart_ID=0, // chart's ID
const string name="Text") // object name
{
//--- reset the error value
ResetLastError();
//--- delete the object
if(!ObjectDelete(chart_ID,name))
{
Print(__FUNCTION__,
": failed to delete \"Text\" object! Error code = ",GetLastError());
return(false);
}
//--- successful execution
return(true);
}
//+------------------------------------------------------------------+
//| Check anchor point values and set default values |
//| for empty ones |
//+------------------------------------------------------------------+
void ChangeTextEmptyPoint(datetime &time,double &price)
{
//--- if the point's time is not set, it will be on the current bar
if(!time)
time=TimeCurrent();
//--- if the point's price is not set, it will have Bid value
if(!price)
price=SymbolInfoDouble(Symbol(),SYMBOL_BID);
}
//+------------
Comments