Getting Today's Data from MarketWatch (too slow)

To add comments, please log in or register
Guilherme Moniz Caixeta
150
Guilherme Moniz Caixeta  

Hello,

I am trying to export just the today's data from every stock from my watch list (around 250 assets) using the following code (based on https://www.mql5.com/pt/code/1252).

void OnStart()
{
        uint start = GetTickCount();

//--- Get symbols from list
        string strSymbols = SymbolsList;
        if ( strSymbols == "all" || strSymbols == "" )
        {
                strSymbols = "";
                for ( int s = SymbolsTotal( true )-1; s >= 0; s -- )
                {
                        StringAdd( strSymbols, SymbolName( s, true ) );
                        if ( s != 0 ) StringAdd( strSymbols, "," );
                }
        }

        string  SymbolsName[];
        int             SymbolsCount = StringToArray( strSymbols, SymbolsName );
        if ( SymbolsCount <= 0 )
        {
                Alert( "Invalid SymbolsList (\"", SymbolsList, "\")!" );
                return;
        }

//--- Get TFs from list
        string strTimeFrames = TimeFramesList;
        if ( strTimeFrames == "all" || strTimeFrames == "" ) strTimeFrames = "M1,M5,M15,M30,H1,H4,D1";

        string  PeriodsName[];
        int             PeriodsCount = StringToArray( strTimeFrames, PeriodsName );
        if ( PeriodsCount <= 0 )
        {
                Alert( "Invalid TimeFramesList (\"", TimeFramesList, "\")!" );
                return;
        }

//--- Get bars count
        int BarsCount = BarsToDownload, MaxBars = TerminalInfoInteger( TERMINAL_MAXBARS );
        if ( BarsCount <= 0 || BarsCount > MaxBars ) BarsCount = MaxBars;

//--- 
        int files_count = 0;
        for ( int s = 0; s < SymbolsCount; s ++ )
        {
                for ( int p = 0; p < PeriodsCount; p ++ )
                {
                   Comment( "Downloading history and writing files: ", DoubleToString( (PeriodsCount*s+p)/double(SymbolsCount*PeriodsCount)*100.0, 1 ), "% complete..." );
                        CheckLoadHistory( SymbolsName[s], StringToPeriod( PeriodsName[p] ), BarsCount );
                        if ( WriteHistoryToFile( SymbolsName[s], StringToPeriod( PeriodsName[p] ), BarsCount ) ) files_count ++;
                }
        }

//---
        Alert( "History export finished within ", DoubleToString( (GetTickCount() - start)/1000.0, 1 ), " sec! ", IntegerToString( files_count ), " files have been written to:\n", 
                                TerminalInfoString( TERMINAL_DATA_PATH ), "\\MQL5\\Files\\History (" + AccountInfoString( ACCOUNT_SERVER ) + ")\\" );
        Comment( "" );
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool WriteHistoryToFile( string symbol, ENUM_TIMEFRAMES period, int bars_count )
{
        uint start = GetTickCount();

        CFileTxt FileTxt;

        MqlRates rates_array[];
        ArraySetAsSeries( rates_array, true );

        int copy_count = CopyRates( symbol, period, 0, bars_count, rates_array );
        if ( copy_count < 0 ) return(false);

        int digits =(int)SymbolInfoInteger( symbol, SYMBOL_DIGITS );
        int period_s = PeriodSeconds( period ) / 60;

        // Open a file
        FileTxt.Open( "History (" + AccountInfoString( ACCOUNT_SERVER ) + ")\\" + symbol + PeriodToString ( period   ) + ".txt", FILE_WRITE );

//---
   int          version         = 400;
   string       c_copyright     = "Copyright 2012-2013, komposter";
   int          i_unused                [14];


//---
         for ( int i = copy_count-1; i >= 0; i -- )
        {
                string str_write = "";
                StringConcatenate( str_write
                                                                        , symbol
                                                                        , ",D"
                                                                        , ",",TimeToString( rates_array[i].time, TIME_DATE )
                           , ",", TimeToString( rates_array[i].time, TIME_MINUTES )
                                                                        , ",", DoubleToString( rates_array[i].open      , digits )
                                                                        , ",", DoubleToString( rates_array[i].high      , digits )
                                                                        , ",", DoubleToString( rates_array[i].low               , digits )
                                                                        , ",", DoubleToString( rates_array[i].close     , digits )
                                                                        , ",", DoubleToString( rates_array[i].real_volume, 0 )
                                                                        , ",", DoubleToString( rates_array[i].tick_volume, 0 )
                                                                        , "\n" );
                FileTxt.WriteString( str_write );


        }

        Print( symbol, ", ", EnumToString( period ), ": ", IntegerToString( copy_count ), " bars have been written to \"", FileTxt.FileName());
        FileTxt.Close();


        return(true);
}


However, it takes too long (more than 20 minutes to download just 1 bar). Is there any suggestion in order to make this faster ?

Keith Watford
Moderator
17949
Keith Watford  
Guilherme Moniz Caixeta:

I am trying to export just the today's date from every stock from my watch list (around 250 assets) using the following code (based on  https://www.mql5.com/pt/code/1252).

Why? Isn't the date the same for all of them?

Guilherme Moniz Caixeta
150
Guilherme Moniz Caixeta  
Keith Watford:

Why? Isn't the date the same for all of them?

I would like to export today's data (Time, open, low, high, close (at the moment), volume) 

Keith Watford
Moderator
17949
Keith Watford  
Guilherme Moniz Caixeta:

I would like to export today's data (Time, open, low, high, close (at the moment), volume) 

I thought it was a bit weird. I see that you have now edited your post and replaced date with data.

To add comments, please log in or register