//+------------------------------------------------------------------+ //| Spread.mq5 | //| Copyright 2015, Mikalas | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2015, Mikalas" #property link "http://www.mql5.com" #property version "1.15" // #property indicator_separate_window #property indicator_buffers 2 #property indicator_plots 2 //--- plot Label1 #property indicator_label1 "Curr spread" #property indicator_type1 DRAW_LINE #property indicator_color1 clrYellow #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- plot Label2 #property indicator_label2 "Midle" #property indicator_type2 DRAW_LINE #property indicator_color2 clrDeepPink #property indicator_style2 STYLE_SOLID #property indicator_width2 1 //--- Levels #property indicator_level1 0 #property indicator_level2 0 #property indicator_level3 0 //--- #property indicator_levelwidth 1 //--- #property indicator_levelstyle STYLE_DOT //--- ENUM_TIMEFRAMES time_frame; //Таймфрейм // string sec_symbol; // input string StartData = "2015.03.16"; //Дата начала расчёта индикатора //--- indicator buffers double MainBuffer[], MidleBuffer[]; double sec_close[1]; double mid_spread; double max_value; double min_value; // ulong spread_cnt; //--- datetime start_time; datetime end_time; datetime sec_times[]; // int sec_bars; int mem_bars; int fail_cnt; int next_month; // bool b_found; // //+------------------------------------------------------------------+ //| Indicator Expert set second symbol function | //+------------------------------------------------------------------+ string SetSecSymbol( const string aSymbol ) { int str_tire = 0; int str_tochka = 0; ushort let_symbol; string str_month, str_year; long aYear; int str_size = StringLen( aSymbol ); for( int i = 0; i < str_size; i++ ) { let_symbol = StringGetCharacter( aSymbol, i ); if ( let_symbol == '-' ) { str_tire = i; } if ( let_symbol == '.' ) { str_tochka = i; } } if ( ( str_tire != 0 ) && ( str_tochka != 0 ) ) { str_month = StringSubstr( aSymbol, str_tire + 1, str_tochka - str_tire - 1 ); str_year = StringSubstr( aSymbol, str_tochka + 1, str_size - str_tochka - 1 ); if ( str_month == "12" ) { str_month = IntegerToString( next_month ); aYear = StringToInteger( str_year ); aYear = aYear + 1; str_year = IntegerToString( aYear ); } else { long aMonth = StringToInteger( str_month ); aMonth = aMonth + next_month; if ( aMonth > 12 ) { aMonth = aMonth - 12; aYear = StringToInteger( str_year ); aYear = aYear + 1; str_year = IntegerToString( aYear ); } str_month = IntegerToString( aMonth ); } } //--- Set new symbol return( StringSubstr( aSymbol, 0, str_tire + 1 ) + str_month + "." + str_year ); } //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ bool CheckBrent( const string a_symbol ) { ushort let_symbol; int str_size = StringLen( a_symbol ); for( int i = 0; i < str_size; i++ ) { let_symbol = StringGetCharacter( a_symbol, i ); if ( let_symbol == '-' ) { string str_info = StringSubstr( a_symbol, 0, i ); //--- if ( str_info == "BR" ) { return( true ); } } } return( false ); } //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { mem_bars = 0; mid_spread = 0.0; spread_cnt = 0; time_frame = PERIOD_CURRENT; next_month = 3; max_value = -999999999994.994; min_value = 999999999994.994; //--- if ( CheckBrent( _Symbol ) ) next_month = 1; //--- sec_symbol = SetSecSymbol( _Symbol ); //--- SymbolSelect( sec_symbol, true ); //--- end_time = datetime( SymbolInfoInteger( _Symbol, SYMBOL_EXPIRATION_TIME ) ); start_time = datetime( StringToTime( StartData ) + 19 * 3600 + 10 * 60 ); // } //--- IndicatorSetInteger( INDICATOR_DIGITS, 0 ); IndicatorSetString( INDICATOR_SHORTNAME, "Spread" ); //--- SetIndexBuffer( 0, MainBuffer, INDICATOR_DATA ); PlotIndexSetDouble( 0, PLOT_EMPTY_VALUE, EMPTY_VALUE ); ArraySetAsSeries( MainBuffer, true ); //--- SetIndexBuffer( 1, MidleBuffer, INDICATOR_DATA ); PlotIndexSetDouble( 1, PLOT_EMPTY_VALUE, EMPTY_VALUE ); ArraySetAsSeries( MidleBuffer, true ); //--- IndicatorSetInteger( INDICATOR_LEVELCOLOR, 0, clrLime ); IndicatorSetInteger( INDICATOR_LEVELCOLOR, 1, clrAqua ); IndicatorSetInteger( INDICATOR_LEVELCOLOR, 2, clrLime ); //--- if ( ( TimeCurrent() - start_time ) < 60 ) { Print( "Слишком мал промежуток времени!" ); // return( INIT_FAILED ); } fail_cnt = 0; sec_bars = 0; uint start_tick; //--- while ( ( sec_bars < 1 ) && ( fail_cnt < 3 ) ) { sec_bars = GetBars( sec_symbol, time_frame, start_time, end_time ); start_tick = GetTickCount(); while ( !CheckTimer( start_tick, 10 ) ) { fail_cnt--; fail_cnt++; } fail_cnt++; } //--- return( INIT_SUCCEEDED ); } //+------------------------------------------------------------------+ // Custom indicator Check timer function | //+------------------------------------------------------------------+ bool CheckTimer( const uint start_value, const uint per_value ) { uint end_value = GetTickCount(); if ( end_value < start_value ) { if ( ( start_value - end_value ) >= per_value ) return( true ); } else { if ( ( end_value - start_value ) >= per_value ) return( true ); } return( false ); } //+------------------------------------------------------------------+ //| Custom indicator Get local data function | //+------------------------------------------------------------------+ int GetLocalData( const string a_symbol, ENUM_TIMEFRAMES a_period, datetime start_date, datetime end_date ) { datetime times[1]; //--- long first_date = SeriesInfoInteger( a_symbol, PERIOD_M1, SERIES_TERMINAL_FIRSTDATE ); if ( first_date > 0 ) { Print( "GetLocalData: Первая дата в терминале есть." ); //--- force timeseries build CopyTime( a_symbol, a_period, datetime( first_date ) + PeriodSeconds( a_period ), 1, times ); //--- check date fail_cnt = 0; //--- while ( fail_cnt < 3 ) { uint start_tick = GetTickCount(); //--- while ( !CheckTimer( start_tick, 5 ) ) { first_date = SeriesInfoInteger( a_symbol, PERIOD_M1, SERIES_FIRSTDATE ); if ( first_date > 0 ) break; } fail_cnt++; } //--- if ( first_date > 0 && first_date <= long( start_date ) ) { bool is_sync = bool( SeriesInfoInteger( a_symbol, a_period, SERIES_SYNCHRONIZED ) ); //--- if ( is_sync ) { Print( "GetLocalData: Серия синхронизирована." ); return( Bars( a_symbol, a_period, start_date, end_date ) ); } } } Print( "GetLocalData: Не удалось построить таймсерию!" ); return( 0 ); } //+------------------------------------------------------------------+ //| Custom indicator Get server data function | //+------------------------------------------------------------------+ int LoadServerData( const string a_symbol, ENUM_TIMEFRAMES period, const datetime start_date, const datetime end_date ) { datetime times[1]; // SeriesInfoInteger( a_symbol, PERIOD_M1, SERIES_SERVER_FIRSTDATE ); fail_cnt = 0; //--- while ( fail_cnt < 10 ) { uint start_tick = GetTickCount(); //--- if ( SymbolIsSynchronized( a_symbol ) ) { Print( "LoadServerData: Символ синхронизирован." ); return( GetLocalData( a_symbol, period, start_date, end_date ) ); } else { SeriesInfoInteger( a_symbol, PERIOD_M1, SERIES_SERVER_FIRSTDATE ); while ( !CheckTimer( start_tick, 20 ) ) { fail_cnt--; fail_cnt++; } fail_cnt++; } } Print( "LoadServerData: Не удалось загрузить историю с сервера!" ); return( 0 ); } //+------------------------------------------------------------------+ //| Custom indicator Get bars function | //+------------------------------------------------------------------+ int GetBars( string symbol, ENUM_TIMEFRAMES period, const datetime start_date, const datetime end_date ) { //---Check for symbol present if ( !SymbolInfoInteger( symbol, SYMBOL_SELECT ) ) { ResetLastError(); //--- if ( GetLastError() != ERR_MARKET_UNKNOWN_SYMBOL ) { SymbolSelect( symbol, true ); } else { Print( "GetBars: Неизвестный символ - ", symbol ); return( 0 ); } } //---Check program if ( MQL5InfoInteger( MQL5_PROGRAM_TYPE ) == PROGRAM_INDICATOR && Period() == period && Symbol() == symbol ) { Print( "GetBars: Не пройдена проверка типа программы!" ); return( 0 ); } //--- if ( SymbolIsSynchronized( symbol ) ) { //Print( "GetBars: Символ синхронизирован." ); //---Check series syncronization bool is_sync = bool( SeriesInfoInteger( symbol, period, SERIES_SYNCHRONIZED ) ); if ( is_sync ) { Print( "GetBars: Серия синхронизирована." ); return( Bars( symbol, period, start_date, end_date ) ); } else { Print( "GetBars: Локальная загрузка..." ); return( GetLocalData( symbol, period, start_date, end_date ) ); } } else { Print( "GetBars: Загрузка с сервера..." ); return( LoadServerData( symbol, period, start_date, end_date ) ); } //--- return( 0 ); } //+------------------------------------------------------------------+ //| 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[]) { int nStartBar = rates_total - prev_calculated; if ( nStartBar == 0 ) return( rates_total ); //--- ArraySetAsSeries( time, true ); ArraySetAsSeries( close, true ); ArraySetAsSeries( sec_times, true ); //--- fail_cnt = 0; sec_bars = 0; uint start_tick; //--- while ( ( sec_bars < 1 ) && ( fail_cnt < 5 ) ) { sec_bars = GetBars( sec_symbol, time_frame, start_time, end_time ); start_tick = GetTickCount(); while ( !CheckTimer( start_tick, 10 ) ) { fail_cnt--; fail_cnt++; } fail_cnt++; } //--- if ( sec_bars < 1 ) { Print( "OnCalculate: Не получены бары по символу ", sec_symbol ); return( prev_calculated ); } else { sec_bars = CopyTime( sec_symbol, time_frame, end_time, sec_bars, sec_times ); if ( sec_bars < 1 ) { Print( "OnCalculate: Не скопированы тийминги по символу ", sec_symbol ); return( prev_calculated ); } } Print( "OnCalculate: Данные получены. Символ ", sec_symbol ); //--- for ( int i = nStartBar - 1; i >= 0; i-- ) { } //--- return( rates_total ); } //+------------------------------------------------------------------+