//+------------------------------------------------------------------+ //| ATR.mq4 | //| Copyright © 2005, MetaQuotes Software Corp. | //| http://www.metaquotes.net/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2005, MetaQuotes Software Corp." #property link "http://www.metaquotes.net/" // Modifications by Ron Thompson June 2009 #property indicator_separate_window #property indicator_buffers 1 #property indicator_color1 DodgerBlue // input parameters extern int AtrPeriod=2; extern string ar ="AtrRange example - 0.4 for 40%"; extern double AtrRange = 0.4; extern bool WantPrice=false; extern bool WantLines=true; extern bool WantRisk=true; extern bool WantAlert=true; // Bar handling used to determine when a bar has moved datetime bartime=0; // Objects int uniq=0; string Object_ID = "atrpeter"; // broker point size double myPoint; // buffers double AtrBuffer[]; double TempBuffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { myObjectsDeleteAll(); string short_name; short_name="ATR_Peter("+AtrPeriod+")"; IndicatorShortName(short_name); IndicatorBuffers(2); // indicator line SetIndexLabel(0,short_name); SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,AtrBuffer); SetIndexDrawBegin(0,AtrPeriod); // 1 additional buffer used for counting. SetIndexBuffer(1,TempBuffer); } int deinit() { myObjectsDeleteAll(); } //+------------------------------------------------------------------+ //| Average True Range | //+------------------------------------------------------------------+ int start() { int t; int i; double high; double low; double prevclose; myPoint=SetPoint(); // once per new bar if(bartime==iTime(Symbol(), 0, 0) ) return(0); // reset which bar is new bartime=iTime(Symbol(), 0, 0) ; myObjectsDeleteAll(); // recalc entire array minus safety buffer i=Bars-AtrPeriod-10; while(i>=0) { high=High[i]; low =Low[i]; prevclose=Close[i+1]; TempBuffer[i]=MathMax(high,prevclose)-MathMin(low,prevclose); i--; } int limit=Bars-AtrPeriod-10; for(i=0; iAtrBuffer[i+2] && AtrBuffer[i+2] 0) { for (i = obj; i >= 0; i--) { objName = ObjectName(i); if (StringFind(objName,Object_ID, 0) >= 0) ObjectDelete(objName); } } } //provided by Robert Hill double SetPoint() { double mPoint; if (Digits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } void DrawLine(string lbl, datetime x1, datetime x2, double y1, double y2, color lineColor, double style) { ObjectDelete(lbl); ObjectCreate(lbl, OBJ_TREND, 0, x1, y1, x2, y2, 0, 0); ObjectSet(lbl, OBJPROP_RAY, 0); ObjectSet(lbl, OBJPROP_COLOR, lineColor); ObjectSet(lbl, OBJPROP_STYLE, style); ObjectSet(lbl, OBJPROP_WIDTH, 3); } void DrawRisk( string lbl, datetime x1, double y1, double risk ) { ObjectDelete(lbl); ObjectCreate(lbl, OBJ_TEXT, 0, x1, y1 ); ObjectSetText(lbl, DoubleToStr(risk, 0),16,"Arial",Aqua); }