//+------------------------------------------------------------------+ //| ocn - nma.mq5 | //+------------------------------------------------------------------+ #property copyright "mladen" #property link "mladenfx@gmail.com" #property version "1.00" #property indicator_chart_window #property indicator_buffers 3 #property indicator_plots 1 // // // // // #property indicator_label1 "Ocean nma" #property indicator_type1 DRAW_LINE #property indicator_color1 SteelBlue #property indicator_style1 STYLE_SOLID #property indicator_width1 2 // // // // // input string _1 = ""; // NMA settings input int inpNmaPeriod = 40; // NMA calculation period input ENUM_APPLIED_PRICE inpNmaPrice = PRICE_CLOSE; // NMA price input int inpTemaPeriod = 1; // triple EMA smoothing period input int inpSmoothPeriod = 8; // NMA two pole smoothing period input bool inpUseLog = true; // Use log for NMA calcualtion // // // // // double nmsBuffer[]; double nmaBuffer[]; double prcBuffer[]; double tBuffer[][9]; // // // // // int iNmaPeriod; int iTemaPeriod; int iSmoothPeriod; int priceHandle; int oneFifth; double alpha; double coef1; double coef2; double coef3; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // #define Pi 3.141592653589793238462643 int OnInit() { SetIndexBuffer(0,nmsBuffer,INDICATOR_DATA); ArraySetAsSeries(nmsBuffer,true); SetIndexBuffer(1,nmaBuffer,INDICATOR_CALCULATIONS); ArraySetAsSeries(nmaBuffer,true); SetIndexBuffer(2,prcBuffer,INDICATOR_CALCULATIONS); ArraySetAsSeries(prcBuffer,true); // // // // // iNmaPeriod = (inpNmaPeriod>1) ? inpNmaPeriod : 1; iTemaPeriod = (inpTemaPeriod>1) ? inpTemaPeriod : 1; iSmoothPeriod = (inpSmoothPeriod>1) ? inpSmoothPeriod : 1; oneFifth = (int)MathMax(MathRound(iNmaPeriod/5),1); alpha = 2.0 /(1.0 + iTemaPeriod); double a = MathExp(-1.414*Pi/iSmoothPeriod); double b = 2*a*MathCos(1.414*Pi/iSmoothPeriod); coef2 = b; coef3 = -a*a; coef1 = 1-coef2-coef3; // // // // // string PriceType; switch(inpNmaPrice) { case PRICE_CLOSE: PriceType = "Close"; break; // 0 case PRICE_OPEN: PriceType = "Open"; break; // 1 case PRICE_HIGH: PriceType = "High"; break; // 2 case PRICE_LOW: PriceType = "Low"; break; // 3 case PRICE_MEDIAN: PriceType = "Median"; break; // 4 case PRICE_TYPICAL: PriceType = "Typical"; break; // 5 case PRICE_WEIGHTED: PriceType = "Weighted"; break; // 6 } IndicatorSetString(INDICATOR_SHORTNAME,"Ocean nma ("+(string)iNmaPeriod+","+(string)iTemaPeriod+","+PriceType+")"); priceHandle = iMA(NULL,0,1,0,MODE_SMA,inpNmaPrice); return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // #define iPrc 6 #define iPrp 7 #define iMom 8 int OnCalculate(const int totalBars, const int totalCalculated, 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 = totalBars-totalCalculated; if (totalCalculated > 0) limit++; if (totalCalculated ==0) limit -= (iNmaPeriod>1) ? iNmaPeriod : 2; if (!checkCalculated(priceHandle,totalBars,"prices")) return(totalCalculated); if (!doCopy(priceHandle,prcBuffer,0,limit ,"prices")) return(totalCalculated); if (ArrayRange(tBuffer,0) != totalBars) ArrayResize(tBuffer,totalBars); // // // // // for(int i=limit, r=totalBars-i-1; i >= 0; i--,r++) { double rawPrice = prcBuffer[i]; double calPrice = iTema(rawPrice,r,0); tBuffer[r][iPrc] = calPrice; if (inpUseLog && (calPrice>0)) tBuffer[r][iPrc] = MathLog(calPrice); tBuffer[r][iMom] = tBuffer[r][iPrc]-tBuffer[r-1][iPrc]; // // // // // double momRatio = 0.00; double sumMomen = 0.00; double ratio = 0.00; int tperiod = iNmaPeriodLen(1,oneFifth,i,r); for (int k = 0; k=0; i++) { double tMomentum = MathAbs((currPrice-tBuffer[r-i][iPrp])/MathSqrt(i)); if (tMomentum > maxmomen) { maxmomen = tMomentum; maxlookb = i; } i += (startPeriod-1); } return((int)MathMax(maxlookb,minPeriod)); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // double iTema(double price,int i, int start) { if (i < 1) { tBuffer[i][start+0] = price; tBuffer[i][start+1] = price; tBuffer[i][start+2] = price; } else { tBuffer[i][start+0] = tBuffer[i-1][start+0]+alpha*(price -tBuffer[i-1][start+0]); tBuffer[i][start+1] = tBuffer[i-1][start+1]+alpha*(tBuffer[i][start+0]-tBuffer[i-1][start+1]); tBuffer[i][start+2] = tBuffer[i-1][start+2]+alpha*(tBuffer[i][start+1]-tBuffer[i-1][start+2]); } return(3.0*tBuffer[i][start+0] - 3.0*tBuffer[i][start+1] + tBuffer[i][start+2]); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // bool checkCalculated(int bufferHandle, int total, string checkDescription) { int calculated=BarsCalculated(bufferHandle); if (calculated