Здравствуйте. Пишу советник состоящий из двух индикаторов TriangularMa centered envelope_mtf+alerts 2 nmc и TriangularMA centered abands alerts 2. Нужно чтобы открывалась и закрывалась сделка по пересечению.
А у меня получается совсем не так как надо. Помогите мне написать условия.
С данным индикатором и не получится
Можете рассказать почему? И если с этим не получится, то может посоветуете на замену другой индикатор?
Если выложите код индикатора, то смогу объяснить "почему" на примере
TriangularMA centered abands alerts 2
//+------------------------------------------------------------------+ //| TriangularMA centered asymmetric bands.mq4 | //| mladen | //| arrowse coded acording to idea presented by umesh | //+------------------------------------------------------------------+ #property copyright "mladen" #property link "mladenfx@gmail.com" #property indicator_chart_window #property indicator_buffers 5 #property indicator_color1 DimGray #property indicator_color2 Red #property indicator_color3 LimeGreen #property indicator_color4 Red #property indicator_color5 Blue #property indicator_style1 STYLE_DOT // // // // // extern string TimeFrame = "current time frame"; extern int HalfLength = 12; extern int Price = PRICE_WEIGHTED; extern double BandsDeviations = 3; extern bool Interpolate = true; extern bool alertsOn = false; extern bool alertsOnCurrent = false; extern bool alertsOnHighLow = true; extern bool alertsMessage = true; extern bool alertsSound = false; extern bool alertsEmail = false; double tmBuffer[]; double upBuffer[]; double dnBuffer[]; double wuBuffer[]; double wdBuffer[]; double upArrow[]; double dnArrow[]; string IndicatorFileName; bool calculatingTma = false; bool returningBars = false; int timeFrame; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int init() { timeFrame = stringToTimeFrame(TimeFrame); HalfLength = MathMax(HalfLength,1); IndicatorBuffers(7); SetIndexBuffer(0,tmBuffer); SetIndexDrawBegin(0,HalfLength); SetIndexBuffer(1,upBuffer); SetIndexDrawBegin(1,HalfLength); SetIndexBuffer(2,dnBuffer); SetIndexDrawBegin(2,HalfLength); SetIndexBuffer(3,dnArrow); SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(5,242); SetIndexBuffer(4,upArrow); SetIndexStyle(4,DRAW_ARROW); SetIndexArrow(6,241); SetIndexBuffer(5,wuBuffer); SetIndexBuffer(6,wdBuffer); if (TimeFrame=="calculateTma") { calculatingTma=true; return(0); } if (TimeFrame=="returnBars") { returningBars=true; return(0); } IndicatorFileName = WindowExpertName(); return(0); } int deinit() { return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // int start() { int counted_bars=IndicatorCounted(); int i,limit; if(counted_bars<0) return(-1); if(counted_bars>0) counted_bars--; limit=MathMin(Bars-1,Bars-counted_bars+HalfLength); if (returningBars) { tmBuffer[0] = limit; return(0); } if (calculatingTma) { calculateTma(limit); return(0); } if (timeFrame > Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,IndicatorFileName,"returnBars",0,0)*timeFrame/Period())); for(i = limit; i >= 0; i--) { int shift1 = iBarShift(NULL,timeFrame,Time[i]); datetime time1 = iTime (NULL,timeFrame,shift1); tmBuffer[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",HalfLength,Price,BandsDeviations,0,shift1); upBuffer[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",HalfLength,Price,BandsDeviations,1,shift1); dnBuffer[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",HalfLength,Price,BandsDeviations,2,shift1); upArrow[i] = EMPTY_VALUE; dnArrow[i] = EMPTY_VALUE; if (High[i+1]>upBuffer[i+1] && Close[i+1]>Open[i+1] && Close[i]<Open[i]) upArrow[i] = High[i]+iATR(NULL,0,20,i); if ( Low[i+1]<dnBuffer[i+1] && Close[i+1]<Open[i+1] && Close[i]>Open[i]) dnArrow[i] = High[i]-iATR(NULL,0,20,i); if (timeFrame <= Period() || shift1==iBarShift(NULL,timeFrame,Time[i-1])) continue; if (!Interpolate) continue; for(int n = 1; i+n < Bars && Time[i+n] >= time1; n++) continue; double factor = 1.0 / n; for(int k = 1; k < n; k++) { tmBuffer[i+k] = k*factor*tmBuffer[i+n] + (1.0-k*factor)*tmBuffer[i]; upBuffer[i+k] = k*factor*upBuffer[i+n] + (1.0-k*factor)*upBuffer[i]; dnBuffer[i+k] = k*factor*dnBuffer[i+n] + (1.0-k*factor)*dnBuffer[i]; } } if (alertsOn) { if (alertsOnCurrent) int forBar = 0; else forBar = 1; int timeBar = iBarShift(NULL,0,iTime(NULL,timeFrame,forBar)); double up0 = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",HalfLength,Price,BandsDeviations,1,forBar); double dn0 = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",HalfLength,Price,BandsDeviations,2,forBar); double up1 = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",HalfLength,Price,BandsDeviations,1,forBar+1); double dn1 = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",HalfLength,Price,BandsDeviations,2,forBar+1); if (alertsOnHighLow) { double hi0 = iHigh(NULL,timeFrame,forBar); double hi1 = iHigh(NULL,timeFrame,forBar+1); double lo0 = iLow( NULL,timeFrame,forBar); double lo1 = iLow( NULL,timeFrame,forBar+1); if (hi0 > up0 && hi1 < up1) doAlert(timeBar,"high penetrated upper bar"); if (lo0 < dn0 && lo1 > dn1) doAlert(timeBar,"low penetrated lower bar"); } else { double cl0 = iClose(NULL,timeFrame,forBar); double cl1 = iClose(NULL,timeFrame,forBar); if (cl0 > up0 && cl1 < up1) doAlert(timeBar,"close penetrated upper bar"); if (cl0 < dn0 && cl1 > dn1) doAlert(timeBar,"close penetrated lower bar"); } } return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void calculateTma(int limit) { int i,j,k; double FullLength = 2.0*HalfLength+1.0; for (i=limit; i>=0; i--) { double sum = (HalfLength+1)*iMA(NULL,0,1,0,MODE_SMA,Price,i); double sumw = (HalfLength+1); for(j=1, k=HalfLength; j<=HalfLength; j++, k--) { sum += k*iMA(NULL,0,1,0,MODE_SMA,Price,i+j); sumw += k; if (j<=i) { sum += k*iMA(NULL,0,1,0,MODE_SMA,Price,i-j); sumw += k; } } tmBuffer[i] = sum/sumw; double diff = iMA(NULL,0,1,0,MODE_SMA,Price,i)-tmBuffer[i]; if (i> (Bars-HalfLength-1)) continue; if (i==(Bars-HalfLength-1)) { upBuffer[i] = tmBuffer[i]; dnBuffer[i] = tmBuffer[i]; if (diff>=0) { wuBuffer[i] = MathPow(diff,2); wdBuffer[i] = 0; } else { wdBuffer[i] = MathPow(diff,2); wuBuffer[i] = 0; } continue; } if(diff>=0) { wuBuffer[i] = (wuBuffer[i+1]*(FullLength-1)+MathPow(diff,2))/FullLength; wdBuffer[i] = wdBuffer[i+1]*(FullLength-1)/FullLength; } else { wdBuffer[i] = (wdBuffer[i+1]*(FullLength-1)+MathPow(diff,2))/FullLength; wuBuffer[i] = wuBuffer[i+1]*(FullLength-1)/FullLength; } upBuffer[i] = tmBuffer[i] + BandsDeviations*MathSqrt(wuBuffer[i]); dnBuffer[i] = tmBuffer[i] - BandsDeviations*MathSqrt(wdBuffer[i]); } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void doAlert(int forBar, string doWhat) { static string previousAlert=""; static datetime previousTime; string message; if (previousAlert!=doWhat || previousTime!=Time[forBar]) { previousAlert = doWhat; previousTime = Time[forBar]; message= StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," THA : ",doWhat); if (alertsMessage) Alert(message); if (alertsEmail) SendMail(StringConcatenate(Symbol(),"TMA "),message); if (alertsSound) PlaySound("alert2.wav"); } } int stringToTimeFrame(string tfs) { for(int l = StringLen(tfs)-1; l >= 0; l--) { int char1 = StringGetChar(tfs,l); if((char1 > 96 && char1 < 123) || (char1 > 223 && char1 < 256)) tfs = StringSetChar(tfs, l, char1 - 32); else if(char1 > -33 && char1 < 0) tfs = StringSetChar(tfs, l, char1 + 224); } int tf=0; if (tfs=="M1" || tfs=="1") tf=PERIOD_M1; if (tfs=="M5" || tfs=="5") tf=PERIOD_M5; if (tfs=="M15"|| tfs=="15") tf=PERIOD_M15; if (tfs=="M30"|| tfs=="30") tf=PERIOD_M30; if (tfs=="H1" || tfs=="60") tf=PERIOD_H1; if (tfs=="H4" || tfs=="240") tf=PERIOD_H4; if (tfs=="D1" || tfs=="1440") tf=PERIOD_D1; if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1; if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1; if (tf==0 || tf<Period()) tf=Period(); return(tf); }
TriangularMa centered envelope_mtf+alerts 2 nmc
//------------------------------------------------------------------ #property copyright "www.forex-tsd.com" #property link "www.forex-tsd.com" //------------------------------------------------------------------ #property indicator_chart_window #property indicator_buffers 2 #property indicator_color1 IndianRed #property indicator_color2 CadetBlue // // // // // extern string TimeFrame = "current time frame"; extern int HalfLength = 46; extern int Price = PRICE_CLOSE; extern int EnvelopeShift = 0; extern double Deviation = 0.10; extern string note = "turn on Alert = true; turn off = false"; extern bool alertsOn = true; extern bool alertsOnCurrent = false; extern bool alertsMessage = true; extern bool alertsSound = true; extern bool alertsEmail = false; extern bool alertsNotify = false; extern string soundFile = "alert2.wav"; // // // // // double UpEnv[]; double DnEnv[]; double tmBuffer[]; double trend[]; // // // // // string indicatorFileName; bool calculateValue; bool returnBars; int timeFrame; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // int init() { IndicatorBuffers(4); HalfLength=MathMax(HalfLength,1); SetIndexBuffer(0,UpEnv); SetIndexDrawBegin(0,HalfLength); SetIndexBuffer(1,DnEnv); SetIndexDrawBegin(1,HalfLength); SetIndexBuffer(2,tmBuffer); SetIndexBuffer(3,trend); // // // // // indicatorFileName = WindowExpertName(); returnBars = TimeFrame=="returnBars"; if (returnBars) return(0); calculateValue = TimeFrame=="calculateValue"; if (calculateValue) return(0); timeFrame = stringToTimeFrame(TimeFrame); SetIndexShift(0,EnvelopeShift * timeFrame/Period()); SetIndexShift(1,EnvelopeShift * timeFrame/Period()); // // // // // IndicatorShortName(timeFrameToString(timeFrame)+" Tma centered envelopes ("+HalfLength+")"); return(0); } int deinit() { return(0); } //+------------------------------------------------------------------ //| //+------------------------------------------------------------------ // // int start() { int counted_bars=IndicatorCounted(); int i,j,k,limit; if(counted_bars<0) return(-1); if(counted_bars>0) counted_bars--; limit=MathMin(Bars-1,Bars-counted_bars+HalfLength); if (returnBars) { UpEnv[0] = limit+1; return(0); } // // // // // if (calculateValue || timeFrame==Period()) { for (i=limit; i>=0; i--) { double sum = (HalfLength+1)*iMA(NULL,0,1,0,MODE_SMA,Price,i); double sumw = (HalfLength+1); for (j=1, k=HalfLength; j<=HalfLength; j++, k--) { sum += k*iMA(NULL,0,1,0,MODE_SMA,Price,i+j); sumw += k; if (j<=i) { sum += k*iMA(NULL,0,1,0,MODE_SMA,Price,i-j); sumw += k; } } tmBuffer[i] = sum/sumw; UpEnv[i] = (1+Deviation/100)*tmBuffer[i]; DnEnv[i] = (1-Deviation/100)*tmBuffer[i]; trend[i] = trend[i+1]; if (Close[i]>DnEnv[i]) trend[i] = 1; if (Close[i]<UpEnv[i]) trend[i] =-1; } manageAlerts(); return(0); } // // // // // limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period())); for(i=limit; i>=0; i--) { int y = iBarShift(NULL,timeFrame,Time[i]); UpEnv[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateTma",HalfLength,Price,EnvelopeShift,Deviation,0,y); DnEnv[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateTma",HalfLength,Price,EnvelopeShift,Deviation,1,y); trend[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateTma",HalfLength,Price,EnvelopeShift,Deviation,3,y); } manageAlerts(); return(0); } //+------------------------------------------------------------------- //| //+------------------------------------------------------------------- // // // // // string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"}; int iTfTable[] = {1,5,15,30,60,240,1440,10080,43200}; // // // // // int stringToTimeFrame(string tfs) { tfs = StringUpperCase(tfs); for (int i=ArraySize(iTfTable)-1; i>=0; i--) if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period())); return(Period()); } string timeFrameToString(int tf) { for (int i=ArraySize(iTfTable)-1; i>=0; i--) if (tf==iTfTable[i]) return(sTfTable[i]); return(""); } // // // // // string StringUpperCase(string str) { string s = str; for (int length=StringLen(str)-1; length>=0; length--) { int tchar = StringGetChar(s, length); if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256)) s = StringSetChar(s, length, tchar - 32); else if(tchar > -33 && tchar < 0) s = StringSetChar(s, length, tchar + 224); } return(s); } // // // // // void manageAlerts() { if (!calculateValue && alertsOn) { if (alertsOnCurrent) int whichBar = 0; else whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar)); if (trend[whichBar] != trend[whichBar+1]) { if (trend[whichBar] == 1) doAlert(whichBar,"Buy"); if (trend[whichBar] ==-1) doAlert(whichBar,"Sell"); } } } // // // // // void doAlert(int forBar, string doWhat) { static string previousAlert="nothing"; static datetime previousTime; string message; if (previousAlert != doWhat || previousTime != Time[forBar]) { previousAlert = doWhat; previousTime = Time[forBar]; // // // // // message = StringConcatenate(Symbol()," ",timeFrameToString(Period())," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Tma centered envelopes ",doWhat); if (alertsMessage) Alert(message); if (alertsEmail) SendMail(StringConcatenate(Symbol()," Tma centered envelopes "),message); if (alertsNotify) SendNotification(message); if (alertsSound) PlaySound(soundFile); } }
TriangularMA centered abands alerts 2
TriangularMa centered envelope_mtf+alerts 2 nmc
Все спрятано тут
void calculateTma(int limit) { int i,j,k; double FullLength = 2.0*HalfLength+1.0; for (i=limit; i>=0; i--) { double sum = (HalfLength+1)*iMA(NULL,0,1,0,MODE_SMA,Price,i); double sumw = (HalfLength+1); for(j=1, k=HalfLength; j<=HalfLength; j++, k--) { sum += k*iMA(NULL,0,1,0,MODE_SMA,Price,i+j); sumw += k; if (j<=i) { sum += k*iMA(NULL,0,1,0,MODE_SMA,Price,i-j); sumw += k; } } tmBuffer[i] = sum/sumw; double diff = iMA(NULL,0,1,0,MODE_SMA,Price,i)-tmBuffer[i]; if (i> (Bars-HalfLength-1)) continue; if (i==(Bars-HalfLength-1)) { upBuffer[i] = tmBuffer[i]; dnBuffer[i] = tmBuffer[i]; if (diff>=0) { wuBuffer[i] = MathPow(diff,2); wdBuffer[i] = 0; } else { wdBuffer[i] = MathPow(diff,2); wuBuffer[i] = 0; } continue; } if(diff>=0) { wuBuffer[i] = (wuBuffer[i+1]*(FullLength-1)+MathPow(diff,2))/FullLength; wdBuffer[i] = wdBuffer[i+1]*(FullLength-1)/FullLength; } else { wdBuffer[i] = (wdBuffer[i+1]*(FullLength-1)+MathPow(diff,2))/FullLength; wuBuffer[i] = wuBuffer[i+1]*(FullLength-1)/FullLength; } upBuffer[i] = tmBuffer[i] + BandsDeviations*MathSqrt(wuBuffer[i]); dnBuffer[i] = tmBuffer[i] - BandsDeviations*MathSqrt(wdBuffer[i]); } }
Если более точно, то вот тут
if (j<=i) { sum += k*iMA(NULL,0,1,0,MODE_SMA,Price,i-j); sumw += k; }
Можно точно так же использовать ЕМА с нужным периодом.
И мульти таймфреймные индикаторы нужны только ручной торговли. Советник может обратиться сразу же к нужному таймфрейму.
Так Вы создаете только большие тормоза в работе.
Можно точно так же использовать ЕМА с нужным периодом.
И мульти таймфреймные индикаторы нужны только ручной торговли. Советник может обратиться сразу же к нужному таймфрейму.
Так Вы создаете только большие тормоза в работе.
То есть из-за этой формулы пересечения не будет? И ничего нельзя сделать?
Почему. Пересечение будет. Просто индикатор перерисовывает. И вроде было, а вроде и нет.
В советнике, если параметры индикатора не меняются, обычно корректно рассчитываются только нулевой и первый бар. Остальные берутся из буфера.
Визуально пересечение было, фактически советник этого не увидел.
Почему. Пересечение будет. Просто индикатор перерисовывает. И вроде было, а вроде и нет.
В советнике, если параметры индикатора не меняются, обычно корректно рассчитываются только нулевой и первый бар. Остальные берутся из буфера.
Визуально пересечение было, фактически советник этого не увидел.
- Бесплатные приложения для трейдинга
- 8 000+ сигналов для копирования
- Экономические новости для анализа финансовых рынков
Вы принимаете политику сайта и условия использования
Здравствуйте. Пишу советник состоящий из двух индикаторов TriangularMa centered envelope_mtf+alerts 2 nmc и TriangularMA centered abands alerts 2. Нужно чтобы открывалась и закрывалась сделка по пересечению.
А у меня получается совсем не так как надо. Помогите мне написать условия.