//+------------------------------------------------------------------+ //| 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 // Modified by ALX Feb 2010 //#property indicator_separate_window #property indicator_chart_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=false;//true; // Bar handling used to determine when a bar has moved datetime bartime=0, previousBarTime = 0; // Objects int uniq=0; string Object_ID = "atrpeter"; // buffers double AtrBuffer[]; double TempBuffer[]; int arraySize = 0; double myPoint; int myDigits, subPipDivider; //-------------------------------------- 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); */ arraySize = Bars-AtrPeriod-10; ArrayResize(AtrBuffer,arraySize); ArrayInitialize(AtrBuffer, EMPTY_VALUE); ArraySetAsSeries(AtrBuffer, true); ArrayResize(TempBuffer,arraySize); ArrayInitialize(TempBuffer, EMPTY_VALUE); ArraySetAsSeries(TempBuffer, true); myPoint = Point; myDigits = Digits; subPipDivider = 1; // usefull for converting 5 digits to 4 digits notation. if (myDigits == 3 || myDigits == 5) { myPoint *= 10; myDigits --; subPipDivider = 10; } } //-------------------------------------- int deinit() { myObjectsDeleteAll(); } //-------------------------------------- int start() { // First thins first - Save CPU! bartime = Time[0]; if(bartime <=previousBarTime) return(0); previousBarTime = bartime; int t; int i; double high; double low; double prevclose; myObjectsDeleteAll(); // recalc entire array minus safety buffer int limit=Bars-AtrPeriod-10; if (limit > arraySize) { arraySize = limit; increaseArray (AtrBuffer, arraySize); increaseArray (TempBuffer, arraySize); } i = limit; while(i>=0) { high=High[i]; low =Low[i]; prevclose=Close[i+1]; TempBuffer[i]=MathMax(high,prevclose)-MathMin(low,prevclose); i--; } 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); } } } //-------------------------------------- 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); ObjectSet(lbl, OBJPROP_BACK, true); } //-------------------------------------- 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); } //-------------------------------------- bool increaseArray(double& array[], int targetSize) { int currentSize = ArraySize(array); if (targetSize <= currentSize) return (false); int result = ArrayResize(array, targetSize); if (result == targetSize) return (true); else return (false); }