//+------------------------------------------------------------------+ //| ocn - nma.mq5 | //+------------------------------------------------------------------+ #property copyright "mladen" #property link "mladenfx@gmail.com" #property version "1.00" #property indicator_chart_window #property indicator_buffers 5 #property indicator_plots 3 // // // // // #property indicator_label1 "Ocean nma" #property indicator_type1 DRAW_LINE #property indicator_color1 DimGray #property indicator_style1 STYLE_SOLID #property indicator_width1 1 #property indicator_label2 "Ocean nma up band" #property indicator_type2 DRAW_LINE #property indicator_color2 Lime #property indicator_style2 STYLE_DOT #property indicator_width2 1 #property indicator_label3 "Ocean nma down band" #property indicator_type3 DRAW_LINE #property indicator_color3 Red #property indicator_style3 STYLE_DOT #property indicator_width3 1 // // // // // 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 bool inpUseLog = true; // Use log for NMA calcualtion input string _2 = ""; // Bands settings input int inpSdPeriod = 30; // Standard deviation period input double inpSdUp = 2.0; // Standard deviations up band input double inpSdDown = 2.0; // Standard deviations down band // // // // // double nmaBuffer[]; double prcBuffer[]; double dupBuffer[]; double ddoBuffer[]; double devBuffer[]; double tBuffer[][5]; double alpha; // // // // // int iNmaPeriod; int iTemaPeriod; int iSdPeriod; int priceHandle; int devHandle; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // int OnInit() { SetIndexBuffer(0,nmaBuffer,INDICATOR_DATA); ArraySetAsSeries(nmaBuffer,true); SetIndexBuffer(1,dupBuffer,INDICATOR_DATA); ArraySetAsSeries(dupBuffer,true); SetIndexBuffer(2,ddoBuffer,INDICATOR_DATA); ArraySetAsSeries(ddoBuffer,true); SetIndexBuffer(3,prcBuffer,INDICATOR_CALCULATIONS); ArraySetAsSeries(prcBuffer,true); SetIndexBuffer(4,devBuffer,INDICATOR_CALCULATIONS); ArraySetAsSeries(devBuffer,true); // // // // // iNmaPeriod = (inpNmaPeriod>1) ? inpNmaPeriod : 1; iTemaPeriod = (inpTemaPeriod>1) ? inpTemaPeriod : 1; iSdPeriod = (inpSdPeriod>1) ? inpSdPeriod : 1; alpha = 2.0 /(1.0 + iTemaPeriod); // // // // // 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); devHandle = iStdDev(NULL,0,iSdPeriod,0,MODE_SMA,priceHandle); return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // #define iPrc 3 #define iMom 4 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 (!checkCalculated(devHandle,totalBars,"deviations")) return(totalCalculated); if (!doCopy(priceHandle,prcBuffer,0,limit ,"prices")) return(totalCalculated); if (!doCopy(devHandle,devBuffer,0,limit ,"deviations")) 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); 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; for (int k = 0; k0) dupBuffer[i] = nmaBuffer[i]+devBuffer[i]*inpSdUp; if (inpSdDown>0) ddoBuffer[i] = nmaBuffer[i]-devBuffer[i]*inpSdDown; } // // // // // return(totalBars); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // double iTema(double price,int i) { if (i < 1) { tBuffer[i][0] = price; tBuffer[i][1] = price; tBuffer[i][2] = price; } else { tBuffer[i][0] = tBuffer[i-1][0]+alpha*(price -tBuffer[i-1][0]); tBuffer[i][1] = tBuffer[i-1][1]+alpha*(tBuffer[i][0]-tBuffer[i-1][1]); tBuffer[i][2] = tBuffer[i-1][2]+alpha*(tBuffer[i][1]-tBuffer[i-1][2]); } return(3.0*tBuffer[i][0] - 3.0*tBuffer[i][1] + tBuffer[i][2]); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // bool checkCalculated(int bufferHandle, int total, string checkDescription) { int calculated=BarsCalculated(bufferHandle); if (calculated