Ejemplo n.º 1
0
        bool convert(Converter con, string filename, int tradesize, string sym)
        {
            int  bads      = 0;
            int  thistotal = _ticksprocessed;
            bool g         = true;
            // get output filename
            string convertname = string.Empty;
            // setup writing to output
            TikWriter outfile = null;
            // setup input file
            StreamReader infile           = null;
            int          _date            = 0;
            int          cqgdecimalplaces = 2;

            try
            {
                // open input file
                switch (con)
                {
                case Converter.TradeStation:
                    infile = new StreamReader(filename);
                    // read in and ignore header of input file
                    infile.ReadLine();
                    break;

                case Converter.eSignal_EPF:
                    infile = new StreamReader(filename);
                    // ignore header
                    SecurityImpl esec = eSigTick.InitEpf(infile);
                    _sym = esec.Symbol;
                    break;

                case Converter.CQG:
                    infile           = new StreamReader(filename);
                    cqgdecimalplaces = (int)this._cqgdecimalplacesinput.Value;
                    // no header
                    break;

                case Converter.TradingPhysicsTnS:
                case Converter.TradingPhysicsTV:
                    string   file     = System.IO.Path.GetFileName(filename);
                    string[] date_sym = file.Split('_');
                    string[] sym_ext  = date_sym[1].Split('.');
                    string   datestr  = date_sym[0];
                    int.TryParse(datestr, out _date);
                    _sym   = sym_ext[0];
                    infile = new StreamReader(filename);
                    infile.ReadLine();    //discard header line
                    break;

                case Converter.QCollector_eSignal:
                    infile = new StreamReader(filename);
                    // no header in file
                    break;

                case Converter.MultiCharts:
                    // The symbol for data being imported is obtained from the filename
                    // Selected files for import must be named SYMBOL.ext - eg AAPL.txt, GOOG.txt
                    _sym   = System.IO.Path.GetFileNameWithoutExtension(filename);
                    infile = new StreamReader(filename);
                    infile.ReadLine();     // ignore first line header of input file
                    break;
                }
            }
            catch (Exception ex) { debug("error reading input header:" + ex.Message); g = false; }
            // setup previous tick and current tick
            Tick pk = new TickImpl();
            Tick k  = null;

            do
            {
                try
                {
                    // get next tick from the file

                    switch (con)
                    {
                    case Converter.CQG:
                        k = CQG.parseline(infile.ReadLine(), tradesize, cqgdecimalplaces);
                        break;

                    case Converter.eSignal_EPF:
                        k = eSigTick.FromStream(_sym, infile);
                        break;

                    case Converter.TradeStation:
                        k = TradeStation.parseline(infile.ReadLine(), sym);
                        break;

                    case Converter.TradingPhysicsTnS:
                        k = TradingPhysicsTnS.parseline(infile.ReadLine(), _sym, _date);
                        break;

                    case Converter.TradingPhysicsTV:
                        k = TradingPhysicsTV.parseline(infile.ReadLine(), _sym, _date);
                        break;

                    case Converter.QCollector_eSignal:
                        k = QCollector.parseline(infile.ReadLine(), sym);
                        break;

                    case Converter.MultiCharts:
                        k = MultiCharts.parseline(infile.ReadLine(), _sym);
                        break;
                    }
                }
                catch (Exception ex) { bads++;  continue; }
                if (k == null)
                {
                    debug("Invalid converter: " + con.ToString());
                    return(false);
                }
                // bad tick
                if (k.date == 0)
                {
                    bads++; continue;
                }
                // if dates don't match, we need to write new output file
                if (k.date != pk.date)
                {
                    try
                    {
                        // if the outfile was open previously, close it
                        if (outfile != null)
                        {
                            outfile.Close();
                        }
                        // get path from input
                        string path = Path.GetDirectoryName(filename) + "\\";
                        // setup new file
                        outfile = new TikWriter(path, k.symbol, k.date);
                        // report progress
                        progress((double)_ticksprocessed / _approxtotal);
                    }
                    catch (Exception ex) { debug(ex.Message); g = false; }
                }
                try
                {
                    // write the tick
                    outfile.newTick(k);
                    // save this tick as previous tick
                    pk = k;
                    // count the tick as processed
                    _ticksprocessed++;
                }
                catch (Exception ex) { debug("error writing output tick: " + ex.Message); g = false; }
            }
            // keep going until input file is exhausted
            while (!infile.EndOfStream);
            // close output file
            if (outfile == null)
            {
                debug("Tick file was never opened, likely that input file in wrong format.");
            }
            else
            {
                debug("Saved: " + outfile.Filepath);
                outfile.Close();
            }
            // close input file
            infile.Close();
            // get percentage of good ticks
            double goodratio = (_ticksprocessed - thistotal - bads) / (_ticksprocessed - (double)thistotal);

            if (goodratio < GOODRATIO)
            {
                debug("Less than " + GOODRATIO.ToString("P0") + " good ticks");
                g = false;
            }
            // return status
            return(g);
        }