//+------------------------------------------------------------------+ //| ATRx & TradeDisplay_3.1.mq4 | //| Concept: Cazz / Code: ChuangKe | //| Copyright 2018, chuangke | //| https://www.mql5.com/zh/users/chuangke | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, chuangke" #property link "https://www.mql5.com/zh/users/chuangke" #property version "3.1" #property strict #property indicator_chart_window input ENUM_TIMEFRAMES TF = PERIOD_D1; //Time Frame input int period = 14; //ATR Period enum TimeSetEnum{UseCandle = 0,UseDate = 1}; input TimeSetEnum TimeSelect = 0; input int candle = 1; //ATR-X reading at closed candle input string candletime = "2020.01.09"; //ATR candle time input string DisplaySetting = "===========Display Setting==========="; input ENUM_BASE_CORNER LabelCorner = CORNER_RIGHT_UPPER; //Corner input int X = 300; // X coordinate input int Y = 30; // Y coordinate input int Font_Size = 9; input color Font_Color = clrYellow; input int distance = 30; string prefix = ""; string LableName = "LableName"; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping prefix = "ATR_1.0"; DeleteAllObject(); ShowATR(); //--- return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { //--- DeleteAllObject(); } //+------------------------------------------------------------------+ //| 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[]) { //--- DeleteAllObject(); ShowATR(); //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ void ShowATR() { int tempX = X; int tempY = Y; string text = ""; double atr = 0; if(TimeSelect == 0) { atr = GetATRValue(Symbol(),TF,candle); } else { for(int i = 0;i < Bars;i++) { if(iTime(Symbol(),TF,i) == StringToTime(candletime)) { atr = iATR(Symbol(),TF,period,i); break; } } if(atr == 0) { Print("can not find the time,please set right time."); return; } } if(TimeSelect == 0) { text = GetPeriod(TF) + " | ATRx" + "(" + IntegerToString(period) + ")" + "-" + TimeToString(iTime(Symbol(),TF,candle),TIME_DATE) + "["+ DoubleToString(atr,SymbolInfoInteger(Symbol(),SYMBOL_DIGITS)) + "]"; } else { text = GetPeriod(TF) + " |ATRx" + "(" + IntegerToString(period) + ")" + "-" + candletime + "["+ DoubleToString(atr,SymbolInfoInteger(Symbol(),SYMBOL_DIGITS)) + "]"; } text = text + "-(" + DoubleToString(iATR(Symbol(),PERIOD_CURRENT,period,0),SymbolInfoInteger(Symbol(),SYMBOL_DIGITS)) + ")"; if(ObjectIsChange(prefix + LableName,text)) { ObjectDelete(0,prefix + LableName); LabelCreate(prefix + LableName,LabelCorner,X,Y,text,Font_Size,Font_Color); } long ticket = 0; string symbol = ""; double profitpoint = 0; double profitmoney = 0; double ratio = 0; for(int i = 0;i < OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol() == Symbol() && OrderType() <= OP_SELL) { ticket = OrderTicket(); symbol = OrderSymbol(); profitmoney = OrderProfit(); if(OrderType() == OP_BUY) { profitpoint = (SymbolInfoDouble(symbol,SYMBOL_BID) - OrderOpenPrice()) * GetPointMultiple(symbol); } else { profitpoint = (OrderOpenPrice() - SymbolInfoDouble(symbol,SYMBOL_ASK)) * GetPointMultiple(symbol); } atr = GetATRValue(symbol,TF,candle); ratio = profitpoint / (atr * GetPointMultiple(symbol)); text = "#" + IntegerToString(ticket) + " | " + DoubleToString(profitpoint,0) + " (" + "$" + DoubleToString(profitmoney,2) + ") " + " | rATR" + " (" + DoubleToString(ratio,2) +") "; tempY = tempY + distance; if(ObjectIsChange(prefix + IntegerToString(ticket),text)) { ObjectDelete(0,prefix + IntegerToString(ticket)); LabelCreate(prefix + IntegerToString(ticket),LabelCorner,tempX,tempY,text,Font_Size,Font_Color); } } } } double GetPointMultiple(string inSymbol) { double value = 1; for(int i = 0;i < SymbolInfoInteger(inSymbol,SYMBOL_DIGITS);i++) { value = value * 10; } return value; } double GetATRValue(string inSymbol,int inTimeFrame,int inShift) { double value = 0; if(TimeSelect == 0) { value = iATR(inSymbol,inTimeFrame,period,inShift); } else { for(int i = 0;i < Bars;i++) { if(iTime(inSymbol,inTimeFrame,i) == StringToTime(candletime)) { value = iATR(inSymbol,inTimeFrame,period,i); break; } } if(value == 0) { Print("can not find the time,please set right time."); return 0; } } return value; } string GetPeriod(int inTimeFrame) { string timeframe = ""; if(inTimeFrame == PERIOD_CURRENT) { inTimeFrame = Period(); } if(inTimeFrame == PERIOD_M1) { timeframe = "M1"; } else if(inTimeFrame == PERIOD_M5) { timeframe = "M5"; } else if(inTimeFrame == PERIOD_M15) { timeframe = "M15"; } else if(inTimeFrame == PERIOD_M30) { timeframe = "M30"; } else if(inTimeFrame == PERIOD_H1) { timeframe = "H1"; } else if(inTimeFrame == PERIOD_H4) { timeframe = "H4"; } else if(inTimeFrame == PERIOD_D1) { timeframe = "D1"; } else if(inTimeFrame == PERIOD_W1) { timeframe = "W1"; } else if(inTimeFrame == PERIOD_MN1) { timeframe = "MN1"; } return timeframe; } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ int CountNumber(int inType) { int number = 0; for(int i = 0;i < OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol() == Symbol() && OrderType() == inType) { number++; } } return number; } //内容是否改变 bool ObjectIsChange(string inObject, string inStrValue) { if(ObjectFind(0,inObject) <= -1) //对象不存在,需要绘制 { return true; } else //对象存在 { if(ObjectGetString(0,inObject,OBJPROP_TEXT,0) == inStrValue) { return false; } else { return true; } } return true; } //+------------------------------------------------------------------+ //| Create a text label | //+------------------------------------------------------------------+ void LabelCreate(string name="Label", // label name ENUM_BASE_CORNER corner=CORNER_RIGHT_UPPER, // chart corner for anchoring int x=0, // X coordinate int y=0, // Y coordinate string text="Label", // text int font_size = 10, color clr= clrDarkOrchid ) { long inchart_ID=0; // chart's ID int sub_window=0; // subwindow index string font="Arial"; // font bool back=false; // in the background bool selection=false; // highlight to move bool hidden=true; // hidden in the object list long z_order=0; // priority for mouse click double angle=0.0; // text slope ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER; // anchor type //--- create a text label ObjectCreate(inchart_ID,name,OBJ_LABEL,sub_window,0,0); //--- set label coordinates ObjectSetInteger(inchart_ID,name,OBJPROP_XDISTANCE,x); ObjectSetInteger(inchart_ID,name,OBJPROP_YDISTANCE,y); //--- set the chart's corner, relative to which point coordinates are defined ObjectSetInteger(inchart_ID,name,OBJPROP_CORNER,corner); //--- set the text ObjectSetString(inchart_ID,name,OBJPROP_TEXT,text); //--- set text font ObjectSetString(inchart_ID,name,OBJPROP_FONT,font); //--- set font size ObjectSetInteger(inchart_ID,name,OBJPROP_FONTSIZE,font_size); //--- set the slope angle of the text ObjectSetDouble(inchart_ID,name,OBJPROP_ANGLE,angle); //--- set anchor type ObjectSetInteger(inchart_ID,name,OBJPROP_ANCHOR,anchor); //--- set color ObjectSetInteger(inchart_ID,name,OBJPROP_COLOR,clr); //--- display in the foreground (false) or background (true) ObjectSetInteger(inchart_ID,name,OBJPROP_BACK,back); //--- enable (true) or disable (false) the mode of moving the label by mouse ObjectSetInteger(inchart_ID,name,OBJPROP_SELECTABLE,selection); ObjectSetInteger(inchart_ID,name,OBJPROP_SELECTED,selection); //--- hide (true) or display (false) graphical object name in the object list ObjectSetInteger(inchart_ID,name,OBJPROP_HIDDEN,hidden); //--- set the priority for receiving the event of a mouse click in the chart ObjectSetInteger(inchart_ID,name,OBJPROP_ZORDER,z_order); //--- successful execution } void DeleteAllObject() { for(int tries = 0; tries < 10; tries++) { int obj = ObjectsTotal(); for(int o = 0; o < obj;o++) { string name = ObjectName(o); int index = StringFind(name,prefix,0); if(index > -1) { ObjectDelete(name); } } } }