//+------------------------------------------------------------------+ //| ocn - nst.mq5 | //+------------------------------------------------------------------+ #property copyright "mladen" #property link "mladenfx@gmail.com" #property version "1.00" #property indicator_separate_window #property indicator_buffers 4 #property indicator_plots 3 // // // // // #property indicator_label1 "Ocean nma" #property indicator_type1 DRAW_LINE #property indicator_color1 Green #property indicator_style1 STYLE_SOLID #property indicator_label2 "Ocean fast nma" #property indicator_type2 DRAW_LINE #property indicator_color2 LimeGreen #property indicator_style2 STYLE_DOT #property indicator_label3 "Ocean nmm" #property indicator_type3 DRAW_LINE #property indicator_color3 DeepSkyBlue #property indicator_style3 STYLE_SOLID // // // // // input int inpNmmPeriod = 40; // Natural market mirror calculation period input ENUM_APPLIED_PRICE inpNmmPrice = PRICE_CLOSE; // Apply to price input int inpTemaPeriod = 10; // triple EMA smoothing period // // // // // double nmmBuffer[]; double nmaBuffer[]; double nmfBuffer[]; double prcBuffer[]; double tBuffer[][9]; double alpha; double oneFifth; // // // // // int iNmmPeriod; int iTemaPeriod; int priceHandle; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // int OnInit() { SetIndexBuffer(0,nmaBuffer,INDICATOR_DATA); ArraySetAsSeries(nmaBuffer,true); SetIndexBuffer(1,nmfBuffer,INDICATOR_DATA); ArraySetAsSeries(nmfBuffer,true); SetIndexBuffer(2,nmmBuffer,INDICATOR_DATA); ArraySetAsSeries(nmmBuffer,true); SetIndexBuffer(3,prcBuffer,INDICATOR_CALCULATIONS); ArraySetAsSeries(prcBuffer,true); iNmmPeriod = (inpNmmPeriod>1) ? inpNmmPeriod : 1; iTemaPeriod = (inpTemaPeriod>1) ? inpTemaPeriod : 1; oneFifth = MathMax(MathRound(iNmmPeriod/5),1); alpha = 2.0 /(1.0 + iTemaPeriod); // // // // // string PriceType; switch(inpNmmPrice) { 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 nmm & ocen mas ("+(string)iNmmPeriod+","+(string)iTemaPeriod+","+PriceType+")"); priceHandle = iMA(NULL,0,1,0,MODE_SMA,inpNmmPrice); 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 -= (iNmmPeriod>1) ? iNmmPeriod : 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 currPrice = iTema(prcBuffer[i],r,0); if (currPrice > 0) tBuffer[r][iPrc] = MathLog(currPrice); else tBuffer[r][iPrc] = 0.00; // // // // // nmmBuffer[i] = iNmmFunction(1,r); tBuffer[r][iMom] = nmmBuffer[i]-nmmBuffer[i+1]; nmaBuffer[i] = nmaBuffer[i+1]+iNmaRatio(r,iNmmPeriod) *(nmmBuffer[i]-nmaBuffer[i+1]); nmfBuffer[i] = nmfBuffer[i+1]+iNmaRatio(r,iNmfPeriodLen(1,oneFifth,r))*(nmmBuffer[i]-nmfBuffer[i+1]); } return(totalBars); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // // // double iNmmFunction(int startPeriod, int r) { int endPeriod = startPeriod*iNmmPeriod; double sum = 0.00; double currPrice = tBuffer[r][iPrc]; for (int i= startPeriod; (i<=r) && (i<=endPeriod); i += startPeriod) sum += (currPrice-tBuffer[r-i][iPrc])/MathSqrt(i); return(sum/(double)iNmmPeriod*1000.00); } // // // // // int iNmfPeriodLen(int startPeriod, int minPeriod,int r) { int endPeriod = startPeriod*iNmmPeriod; int maxlookb = 0; double maxmomen = 0.00; double currPrice = iTema(tBuffer[r][iPrc],r,3); tBuffer[r][iPrp] = currPrice; for (int i= startPeriod;(i<=r) && (i<=endPeriod); 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 iNmaRatio(int r,int period) { double momRatio = 0.00; double sumMomen = 0.00; double ratio = 0.00; // // // // // for (int k = 0; k