//+------------------------------------------------------------------+ //| Probability.mq4 | //| Copyright 2020, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict #property indicator_separate_window #property indicator_buffers 4 enum AverageType{DAILY,WEEKLY}; input string inp1 = "<<<<< INDICATOR SETTINGS >>>>>"; input int CheckDays = 5; // Average Day input double TresholdCounter = 0.75; // Max Treshold input double TresholdMin = 0.50; // Min Treshold input AverageType ChooseType = 0; int NormalizingPeriod = 5; double UpBar[]; double DnBar[]; double Neutral[]; bool IsFirstLoad; double Avg; double Pip; int PipsToPoints; int TresholdCounterVal; int TresholdMinVal; int OppositeHit; ENUM_TIMEFRAMES MyTime = 0; int DayCounter; datetime DateToStop = D'2040.06.15 00:00:00'; // SET THE DATE FOR EXPIRY //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping if (Digits % 2 == 1) Pip = Point * 10; else Pip = Point; PipsToPoints = int(Pip / _Point); if(ChooseType == 1) MyTime = PERIOD_W1; else MyTime = PERIOD_D1; SetIndexBuffer(0,Neutral); SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,4,clrNONE); SetIndexBuffer(1,UpBar); SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,4,clrLimeGreen); SetIndexBuffer(2,DnBar); SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,4,clrCrimson); //--- //* if(_Period >= PERIOD_) /* { Alert("USE LOWER THAN D1 TIMERAME"); return 0; }*/ return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| 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 limit = 0; limit=rates_total-prev_calculated; /* if(isLocked()) return(0);*/ if((rates_total-prev_calculated)>1) { limit = Bars-100; IsFirstLoad = true; ArrayInitialize(UpBar,EMPTY_VALUE); ArrayInitialize(DnBar,EMPTY_VALUE); DayCounter = 0; ArrayInitialize(Neutral,EMPTY_VALUE); TresholdCounterVal = 0; TresholdMinVal = 0; OppositeHit = 0; } for( int i = limit; i >= 0; --i) { int y = iBarShift(_Symbol,MyTime,Time[i],true); double SumAvgHiLow = 0; for(int k = NormalizingPeriod; k >= 0; --k) { SumAvgHiLow += (iHigh(_Symbol,MyTime,y+k) - iLow(_Symbol,MyTime,y+k))/Pip; Avg = SumAvgHiLow/NormalizingPeriod; } double CurrentDifference = MathAbs((iOpen(_Symbol,MyTime,y) - iClose(_Symbol,_Period,i))/Pip); double Ratio = CurrentDifference/Avg; if(iOpen(_Symbol,MyTime,y) < iClose(_Symbol,_Period,i)) UpBar[i] = Ratio; else if(iOpen(_Symbol,MyTime,y) > iClose(_Symbol,_Period,i)) DnBar[i] = Ratio; else Neutral[i] = Ratio; static bool IsMinTresholdHit = false; static bool IsMaxTresholdHit = false; static bool IsOppositeHit = false; static int TresholdDirection = 3; static datetime LastDay; if(LastDay != iTime(_Symbol,PERIOD_D1,y)) { ++DayCounter; LastDay = iTime(_Symbol,PERIOD_D1,y); IsMaxTresholdHit = false; IsMinTresholdHit = false; IsOppositeHit = false; } if(Ratio >= TresholdMin && Ratio < TresholdCounter && !IsMinTresholdHit) { IsMinTresholdHit = true; if(iOpen(_Symbol,MyTime,y) < iClose(_Symbol,_Period,i)/2) { TresholdDirection = 1; } else if (iOpen(_Symbol,MyTime,y) > iClose(_Symbol,_Period,i)/2) { TresholdDirection = 2; } ++TresholdMinVal; } if(IsMinTresholdHit && Ratio < TresholdMin && !IsMaxTresholdHit && !IsOppositeHit) { ++OppositeHit; IsOppositeHit = true; } if(Ratio >= TresholdCounter && !IsMaxTresholdHit && IsMinTresholdHit) { IsMaxTresholdHit = true; if(TresholdDirection == 1 && iOpen(_Symbol,MyTime,y) < iClose(_Symbol,_Period,i)/2) ++TresholdCounterVal; if(TresholdDirection == 2 && iOpen(_Symbol,MyTime,y) > iClose(_Symbol,_Period,i)/2) ++TresholdCounterVal; } if(TresholdCounterVal != 0 && TresholdMinVal != 0) { double result = (double)TresholdCounterVal/TresholdMinVal; double resultOpp = (double)OppositeHit/TresholdMinVal; string space = "\n"; string msg = "Days Checked : " +" " + IntegerToString(DayCounter)+space+ "MaxTresholdHit : "+ IntegerToString(TresholdCounterVal)+space+ "MinTresholdHit : "+ IntegerToString(TresholdMinVal)+space+ "OppositeHit : " + IntegerToString(OppositeHit)+space+ "Ratio Of Continuation : "+ DoubleToString(result,2)+space+ "Ratio Of Reversal : "+ DoubleToString(resultOpp,2); Comment(msg); } } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ bool IsNewDay(int i) { static datetime LastDateTime; if(LastDateTime == iTime(_Symbol,PERIOD_D1,i)) return false; else { LastDateTime = iTime(_Symbol,PERIOD_D1,i); return true; } return false; } bool isLocked() { return (TimeCurrent() > DateToStop) ? true : false; }