//+------------------------------------------------------------------+ //| TrendIndicatorADX_SAR.mq5 | //| Copyright 2014, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2014, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property version "1.00" //#property indicator_separate_window #property indicator_chart_window #property indicator_buffers 1 #property indicator_plots 1 //--- plot tred #property indicator_label1 "sar tf sup" #property indicator_type1 DRAW_LINE #property indicator_color1 clrRed #property indicator_style1 STYLE_SOLID #property indicator_width1 1 #property indicator_maximum 1.5 #property indicator_minimum -1.5 //#include //CExpert ExtExpert; input double SARstep=0.02; //il passo, ovvero il punto iniziale di posizionamento dello stop input double SARMaximum=0.2;//o spostamento massimo, che controlla il movimento del punto SAR rispetto al movimento del prezzo. int h_SAR; ENUM_TIMEFRAMES timeframeSup; int PeriodSeconds_TF1; int PeriodSeconds_TF2; //--- indicator buffers double tredBuffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ /*void OnStart() { //ChartRedraw(); } */ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,tredBuffer,INDICATOR_DATA); Print("Timeframe=",Period()); if( Period()== PERIOD_M1 ) timeframeSup= PERIOD_M5; else if(Period()== PERIOD_M5 ) timeframeSup= PERIOD_M15; else if(Period()== PERIOD_M15) timeframeSup= PERIOD_M30; else if(Period()== PERIOD_M30) timeframeSup= PERIOD_H1; else if(Period()== PERIOD_H1 ) timeframeSup= PERIOD_H4; else if(Period()== PERIOD_H4 ) timeframeSup= PERIOD_D1; else if(Period()== PERIOD_D1 ) timeframeSup= PERIOD_W1; else if(Period()== PERIOD_W1 ) timeframeSup= PERIOD_MN1; else timeframeSup= Period(); Print("timeframe sup=",timeframeSup); h_SAR= iSAR(NULL,timeframeSup,SARstep,SARMaximum); if (h_SAR== INVALID_HANDLE) Print("Errore nell'Handle dell'indicatore iSAR"); else Print("Init OK"); PeriodSeconds_TF1=PeriodSeconds(); PeriodSeconds_TF2=PeriodSeconds(timeframeSup); //ExtExpert.Refresh(); //--- return(INIT_SUCCEEDED); } void OnDeinit(const int motivo){ IndicatorRelease(h_SAR); } //+------------------------------------------------------------------+ //| 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[]) { //--- double SAR[1]; double CLOSE[1]; datetime TIME[1]; //--- return value of prev_calculated for next call //if (market_closed(Symbol())) { Print("Mercato chiuso"); ChartRedraw();} else {Print("Mercato aperto");ChartRedraw();} int start = PeriodSeconds_TF2; //ArrayRange(CLOSE,0); //start =0; for (int i=(int)MathMax(prev_calculated-1,start); i 1) { datetime begin=0; datetime end=0; datetime now=TimeCurrent(); uint session_index=0; MqlDateTime today; TimeToStruct(now,today); if( SymbolInfoSessionTrade(symbol,(ENUM_DAY_OF_WEEK ) today.day_of_week,session_index,begin,end) == true) { string snow=TimeToString(now,TIME_MINUTES|TIME_SECONDS); string sbegin=TimeToString(begin,TIME_MINUTES|TIME_SECONDS); string send=TimeToString(end-1,TIME_MINUTES|TIME_SECONDS); now=StringToTime(snow); begin=StringToTime(sbegin); end=StringToTime(send); if(now >= begin && now <= end) return false; return true; } } return false; } //----------------------------------------------------------------- int iBarShift(string symbol,ENUM_TIMEFRAMES miotimeframe,datetime time,bool exact=false) { datetime LastBar; if(!SeriesInfoInteger(symbol,miotimeframe,SERIES_LASTBAR_DATE,LastBar)) { //-- Sometimes SeriesInfoInteger with SERIES_LASTBAR_DATE return an error, //-- so we try an other method datetime opentimelastbar[1]; if(CopyTime(symbol,miotimeframe,0,1,opentimelastbar)==1) LastBar=opentimelastbar[0]; else return(-1); } int shift=Bars(symbol,miotimeframe,time,LastBar); datetime checkcandle[1]; //-- If time requested doesn't match opening time of a candle, //-- we need a correction of shift value if(CopyTime(symbol,miotimeframe,time,1,checkcandle)==1) { if(checkcandle[0]==time) return(shift-1); else if(exact && time>checkcandle[0]+PeriodSeconds(miotimeframe)) return(-1); else return(shift); /* Can be replaced by the following statement for more concision return(checkcandle[0]==time ? shift-1 : (exact && time>checkcandle[0]+PeriodSeconds(timeframe) ? -1 : shift)); */ } return(-1); }