// More information about this indicator can be found at: // http://fxcodebase.com/code/viewtopic.php?f=38&t=65696 //+------------------------------------------------------------------+ //| Copyright © 2018, Gehtsoft USA LLC | //| http://fxcodebase.com | //+------------------------------------------------------------------+ //| Developed by : Mario Jemic | //| mario.jemic@gmail.com | //+------------------------------------------------------------------+ //| Support our efforts by donating | //| Paypal: https://goo.gl/9Rj74e | //| BitCoin : 15VCJTLaz12Amr7adHSBtL9v8XomURo9RF | //| BitCoin Cash: 1BEtS465S3Su438Kc58h2sqvVvHK9Mijtg | //| Ethereum : 0x8C110cD61538fb6d7A2B47858F0c0AaBd663068D | //| LiteCoin : LLU8PSY2vsq7B9kRELLZQcKf5nJQrdeqwD | //+------------------------------------------------------------------+ #property copyright "Copyright © 2018, Gehtsoft USA LLC" #property link "http://fxcodebase.com" #property version "1.0" #property strict #property indicator_chart_window #property indicator_buffers 6 extern double Step = 0.02; extern double Max = 0.2; extern double Level1 = 1; extern double Level2 = 1.5; extern double Level3 = 2; extern color StartLineColor = clrBlue; extern int width1 = 1; extern color LabelColor = clrGray; extern int FontSize = 10; extern color StopLineColor = clrRed; extern int width2 = 1; extern color TargetLineColor1 = clrGreen; extern int width3 = 1; extern color TargetLineColor2 = clrGreen; extern int width4 = 1; extern color TargetLineColor3 = clrGreen; extern int width5 = 1; extern bool Sound_Alert = true; double Pp; double tradeHigh[]; double tradeLow[]; double parOp[]; double position[]; double af[]; double SAR[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { if (Point() == 0.0001 || Point() == 0.00001) Pp = 0.0001; if (Point() == 0.01 || Point() == 0.001) Pp = 0.01; SetIndexStyle(0,DRAW_NONE); SetIndexBuffer(0,tradeHigh); SetIndexStyle(1,DRAW_NONE); SetIndexBuffer(1,tradeLow); SetIndexStyle(2,DRAW_NONE); SetIndexBuffer(2,parOp); SetIndexStyle(3,DRAW_NONE); SetIndexBuffer(3,position); SetIndexStyle(4,DRAW_NONE); SetIndexBuffer(4,af); SetIndexStyle(5,DRAW_NONE); SetIndexBuffer(5,SAR); return(INIT_SUCCEEDED); } int FindLast(const int period) { for(int i = period; i >= 1; --i) { if (position[i] != position[i-1]) { return i; } } return 0; } void DrawLabel(const double rate, const datetime date, const string text, const int id, const color clr, const int width) { ObjectCreate(ChartID(), "Line" + IntegerToString(id), OBJ_TREND, 0, date, rate, Time[0], rate); ObjectSetInteger(ChartID(), "Line" + IntegerToString(id), OBJPROP_COLOR, clr); ObjectSetInteger(ChartID(), "Line" + IntegerToString(id), OBJPROP_WIDTH, width); ObjectCreate(ChartID(), "Label" + IntegerToString(id), OBJ_TEXT, 0, date, rate); ObjectSetText("Label" + IntegerToString(id), text, FontSize, "Arial", LabelColor); } //+------------------------------------------------------------------+ //| 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[]) { int precision = 5; for (int i = 1; i < Bars - 1; i++) { double prevHigh = high[i - 1]; double prevLow = low[i - 1]; if (i == 1) { tradeHigh[i] = prevHigh; tradeLow[i] = prevLow; position[i] = -1; parOp[i] = prevHigh; af[i] = 0; } else { parOp[i] = parOp[i - 1]; position[i] = position[i - 1]; tradeHigh[i] = tradeHigh[i - 1]; tradeLow[i] = tradeLow[i - 1]; af[i] = af[i - 1]; } double lastHighest = tradeHigh[i]; double lastLowest = tradeLow[i]; if (high[i] > lastHighest) { tradeHigh[i] = high[i]; } if (low[i] < lastLowest) { tradeLow[i] = low[i]; } if (position[i] == 1) { if (low[i] < parOp[i]) { position[i] = -1; SAR[i] = lastHighest; tradeHigh[i] = high[i]; tradeLow[i] = low[i]; af[i] = Step; parOp[i] = SAR[i] + af[i] * (tradeLow[i] - SAR[i]); if (parOp[i] < high[i]) { parOp[i] = high[i]; } if (parOp[i] < prevHigh) { parOp[i] = prevHigh; } } else { SAR[i] = parOp[i]; if (tradeHigh[i] > tradeHigh[i - 1] && af[i] < Max) { af[i] = af[i] + Step; if (af[i] > Max) { af[i] = Max; } } parOp[i] = SAR[i] + af[i] * (tradeHigh[i] - SAR[i]); if (parOp[i] > low[i]) { parOp[i] = low[i]; } if (parOp[i] > prevLow) { parOp[i] = prevLow; } } } else { if (high[i] > parOp[i]) { position[i] = 1; SAR[i] = lastLowest; tradeHigh[i] = high[i]; tradeLow[i] = low[i]; af[i] = Step; parOp[i] = SAR[i] + af[i] * (tradeHigh[i] - SAR[i]); if (parOp[i] > low[i]) { parOp[i] = low[i]; } if (parOp[i] > prevLow) { parOp[i] = prevLow; } } else { SAR[i] = parOp[i]; if (tradeLow[i] < tradeLow[i - 1] && af[i] < Max) { af[i] = af[i] + Step; if (af[i] > Max) { af[i] = Max; } } parOp[i] = SAR[i] + af[i] * (tradeLow[i] - SAR[i]); if (parOp[i] < high[i]) { parOp[i] = high[i]; } if (parOp[i] < prevHigh) { parOp[i] = prevHigh; } } } if (position[i] == 1 && position[i-1] != 1) { ArrowBuyCreate("B" + TimeToString(time[i], TIME_DATE|TIME_MINUTES), time[i], SAR[i]); } else if (position[i]== -1 && position[i-1] != -1) { ArrowSellCreate("S" + TimeToString(time[i], TIME_DATE|TIME_MINUTES), time[i], SAR[i]); } int p = FindLast(i); double Delta; if (p!=0) { if (position[p] == 1) { Delta=MathAbs(SAR[p] - high[p]); DrawLabel(high[p], time[i] , "Entry :" + DoubleToString(high[p], precision) , 1, StartLineColor, width1); DrawLabel(SAR[p], time[i] , "Stop : " + DoubleToString(SAR[p], precision) + " (" + DoubleToString(MathAbs(SAR[p] - low[p]) / Pp, 1) + ")" , 2, StopLineColor, width2); DrawLabel(high[p]+Delta*Level1, time[i] , "1. Target : " + DoubleToString(high[p]+Delta*Level1, precision) + " (" + DoubleToString(( Delta * Level1) / Pp, 1) + ")" , 3, TargetLineColor1, width3); DrawLabel(high[p]+Delta*Level2, time[i] , "2. Target : " + DoubleToString(high[p]+Delta*Level2, precision) + " (" + DoubleToString(( Delta * Level2 ) / Pp, 1) + ")" , 4, TargetLineColor2, width4); DrawLabel(high[p]+Delta*Level3, time[i] , "3. Target : " + DoubleToString(high[p]+Delta*Level3, precision) + " (" + DoubleToString(( Delta * Level3) / Pp, 1) + ")" , 5, TargetLineColor3, width5); } else { Delta=MathAbs(SAR[p]- low[p]); DrawLabel(low[p], time[i] , "Entry : " + DoubleToString(low[p], precision) , 1, StartLineColor, width1); DrawLabel(SAR[p], time[i] , "Stop : " + DoubleToString(SAR[p], precision) + " (" + DoubleToString(MathAbs(SAR[p] - low[p]) / Pp, 1) + ")" , 2, StopLineColor, width2); DrawLabel(low[p]-Delta*Level1, time[i] , "1. Target : " + DoubleToString(low[p]-Delta*Level1, precision) + " (" + DoubleToString(( Delta * Level1) / Pp, 1) + ")" , 3, TargetLineColor1, width3); DrawLabel(low[p]-Delta*Level2, time[i] , "2. Target : " + DoubleToString(low[p]-Delta*Level2, precision) + " (" + DoubleToString(( Delta * Level2 ) / Pp, 1) + ")" , 4, TargetLineColor2, width4); DrawLabel(low[p]-Delta*Level3, time[i] , "3. Target : " + DoubleToString(low[p]-Delta*Level3, precision) + " (" + DoubleToString(( Delta * Level3) / Pp, 1) + ")" , 5, TargetLineColor3, width5); } } if (position[i] == 1 && position[i-1] != 1) { doAlert("Open Long"); } else if (position[i] == -1 && position[i-1] != -1) { doAlert("Open Short"); } } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { ObjectsDeleteAll(0, -1, OBJ_ARROW); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void ArrowSellCreate(string name, datetime time, double price) { ResetLastError(); if(ObjectCreate(0,name,OBJ_ARROW_SELL,0,time,price)) { ObjectSetInteger(0,name,OBJPROP_COLOR,clrTomato); ObjectSetInteger(0,name,OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,name,OBJPROP_WIDTH,1); ObjectSetInteger(0,name,OBJPROP_BACK,true); ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false); ObjectSetInteger(0,name,OBJPROP_HIDDEN,true); ObjectSetInteger(0,name,OBJPROP_ZORDER,0); } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void ArrowBuyCreate(string name, datetime time, double price) { ResetLastError(); if(ObjectCreate(0,name,OBJ_ARROW_BUY,0,time,price)) { ObjectSetInteger(0,name,OBJPROP_COLOR,clrDodgerBlue); ObjectSetInteger(0,name,OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,name,OBJPROP_WIDTH,1); ObjectSetInteger(0,name,OBJPROP_BACK,true); ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false); ObjectSetInteger(0,name,OBJPROP_HIDDEN,true); ObjectSetInteger(0,name,OBJPROP_ZORDER,0); } } //+-----------------------------------------------------------------+ void doAlert(string message) { if (!Sound_Alert) return; static datetime previousTime; if (previousTime != Time[0]) { previousTime = Time[0]; Alert(message); PlaySound("alert2.wav"); } }