コード例 #1
0
        public static MarketDataEventArg ProcessMarketData(FXSession connection, O2GResponse response)
        {
            try
            {
                O2GResponseReaderFactory            rrfactory = connection.Session.getResponseReaderFactory();
                O2GMarketDataSnapshotResponseReader mReader   = rrfactory.createMarketDataSnapshotReader(response);

                var d = new SortedList <DateTime, Tick>(mReader.Count);

                for (int i = 0; i < mReader.Count; i++)
                {
                    // information like reader.getDate(i), reader.getBidOpen(i), reader.getBidHigh(i), reader.getBidLow(i), reader.getBidClose(i), reader.getVolume(i) is now available
                    //Console.WriteLine(i + ":" + mReader.getDate(i).ToString() + ":" + mReader.getBidOpen(i));
                    //create a quantum of ticks for the market data
                    var tick = new Tick(
                        mReader.getBid(i),
                        mReader.getBidOpen(i),
                        mReader.getBidHigh(i),
                        mReader.getBidLow(i),
                        mReader.getBidClose(i),
                        mReader.getAsk(i),
                        mReader.getAskOpen(i),
                        mReader.getAskHigh(i),
                        mReader.getAskLow(i),
                        mReader.getAskClose(i),
                        mReader.getVolume(i),
                        mReader.getDate(i));

                    d.Add(mReader.getDate(i), tick);
                }

                var q = new Quantum(d);
                return(new MarketDataEventArg(q));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(new MarketDataEventArg(new Quantum(new SortedList <DateTime, Tick>(300))));
            }
        }
コード例 #2
0
        private IEnumerable <BaseRow> GetGenericResponse(O2GMarketDataSnapshotResponseReader reader, string requestID)
        {
            var rows = new List <MarketDataRow>();

            // Try to obtain the instrument from a saved list of market data requests.
            var instrument = this.MarketDataInstrumentGetter(requestID);

            for (var i = 0; i < reader.Count; i++)
            {
                var row = new MarketDataRow();

                row.Instrument = instrument;

                if (reader.isBar)
                {
                    row.AskClose = reader.getAskClose(i);
                    row.AskHigh  = reader.getAskHigh(i);
                    row.AskLow   = reader.getAskLow(i);
                    row.AskOpen  = reader.getAskOpen(i);
                    row.BidClose = reader.getBidClose(i);
                    row.BidHigh  = reader.getBidHigh(i);
                    row.BidLow   = reader.getBidLow(i);
                    row.BidOpen  = reader.getBidOpen(i);
                }
                else
                {
                    row.Ask = reader.getAsk(i);
                    row.Bid = reader.getBid(i);
                }

                row.IsBar  = reader.isBar;
                row.Time   = reader.getDate(i);
                row.Volume = reader.getVolume(i);

                rows.Add(row);
            }

            return(rows);
        }
コード例 #3
0
        private static void GetHistoryPrices(O2GSession session, string sInstrument, string sTimeframe, DateTime dtFrom, DateTime dtTo, ResponseListener responseListener)
        {
            try
            {
                StreamWriter m_data = new StreamWriter(OutData);

                string m_string_to_write = "";

                O2GRequestFactory factory   = session.getRequestFactory();
                O2GTimeframe      timeframe = factory.Timeframes[sTimeframe];
                if (timeframe == null)
                {
                    throw new Exception(string.Format("Timeframe '{0}' is incorrect!", sTimeframe));
                }
                O2GRequest request = factory.createMarketDataSnapshotRequestInstrument(sInstrument, timeframe, 300);
                DateTime   dtFirst = dtTo;
                do // cause there is limit for returned candles amount
                {
                    factory.fillMarketDataSnapshotRequestTime(request, dtFrom, dtFirst, false);
                    responseListener.SetRequestID(request.RequestID);
                    session.sendRequest(request);
                    if (!responseListener.WaitEvents())
                    {
                        throw new Exception("Response waiting timeout expired");
                    }
                    // shift "to" bound to oldest datetime of returned data
                    O2GResponse response = responseListener.GetResponse();
                    if (response != null && response.Type == O2GResponseType.MarketDataSnapshot)
                    {
                        O2GResponseReaderFactory readerFactory = session.getResponseReaderFactory();
                        if (readerFactory != null)
                        {
                            O2GMarketDataSnapshotResponseReader reader = readerFactory.createMarketDataSnapshotReader(response);
                            if (reader.Count > 0)
                            {
                                if (DateTime.Compare(dtFirst, reader.getDate(0)) != 0)
                                {
                                    dtFirst = reader.getDate(0); // earliest datetime of returned data

                                    for (int nData = reader.Count - 1; nData > -1; nData--)
                                    {
                                        // reader.getDate(0);

                                        m_string_to_write = reader.getDate(nData).ToString() + ";" +
                                                            reader.getAsk(nData).ToString() + ";" +
                                                            reader.getAskOpen(nData).ToString() + ";" +
                                                            reader.getAskClose(nData).ToString() + ";" +
                                                            reader.getAskLow(nData).ToString() + ";" +
                                                            reader.getAskHigh(nData).ToString() + ";" +

                                                            reader.getBid(nData).ToString() + ";" +
                                                            reader.getBidOpen(nData).ToString() + ";" +
                                                            reader.getBidClose(nData).ToString() + ";" +
                                                            reader.getBidLow(nData).ToString() + ";" +
                                                            reader.getBidHigh(nData).ToString() + ";" +

                                                            reader.getVolume(nData).ToString() + ";" +

                                                            reader.getLastBarTime().ToString() + ";" +
                                                            reader.getLastBarVolume().ToString();


                                        m_data.WriteLine(m_string_to_write);
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                            else
                            {
                                Console.WriteLine("0 rows received");
                                break;
                            }
                        }
                        // PrintPrices(session, response);
                    }
                    else
                    {
                        break;
                    }
                } while (dtFirst > dtFrom);

                m_data.Close();
            }
            catch (Exception e)
            {
                int ErrorCounter = 0;

                if (ErrorCounter > 5)
                {
                    LogDirector.DoAction(4, e);
                }
                else
                {
                    ErrorCounter++;
                    LogDirector.DoAction(2, e);
                    GetHistoryPrices(session, sInstrument, sTimeframe, dtFrom, dtTo, responseListener);
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Print history data from response.
        /// </summary>
        /// <param name="communicator">The price history communicator.</param>
        /// <param name="response">The response. Cannot be null.</param>
        public static void PrintPrices(IPriceHistoryCommunicator communicator, IPriceHistoryCommunicatorResponse response)
        {
            // use O2GMarketDataSnapshotResponseReader to extract price data from the response object
            O2GMarketDataSnapshotResponseReader reader = communicator.createResponseReader(response);

            for (int i = 0; i < reader.Count; i++)
            {
                if (reader.isBar)
                {
                    Console.WriteLine("DateTime={0}, BidOpen={1}, BidHigh={2}, BidLow={3}, BidClose={4}, AskOpen={5}, AskHigh={6}, AskLow={7}, AskClose={8}, Volume={9}",
                                      reader.getDate(i), reader.getBidOpen(i), reader.getBidHigh(i), reader.getBidLow(i), reader.getBidClose(i),
                                      reader.getAskOpen(i), reader.getAskHigh(i), reader.getAskLow(i), reader.getAskClose(i), reader.getVolume(i));
                }
                else
                {
                    Console.WriteLine("DateTime={0}, Bid={1}, Ask={2}", reader.getDate(i), reader.getBid(i), reader.getAsk(i));
                }
            }
        }
コード例 #5
0
        private void GetHistoryPrices(O2GSession O2GSession, string Instrument, string Interval, DateTime DtFrom, DateTime DtTo)
        {
            m_responseListener = new ResponseListener(O2GSession);
            O2GSession.subscribeResponse(m_responseListener);

            O2GRequestFactory factory = O2GSession.getRequestFactory();

            O2GTimeframeCollection timeframecollection = factory.Timeframes;
            O2GTimeframe           Timeframe           = timeframecollection[Interval];

            if (Timeframe == null)
            {
                throw new Exception(string.Format("Timeframe '{0}' is incorrect!", Timeframe));
            }

            O2GRequest request  = factory.createMarketDataSnapshotRequestInstrument(Instrument, Timeframe, 300);
            DateTime   DtFirst  = DtTo;
            DateTime   DatePrec = System.DateTime.MinValue;

            //TimeSpan PricesTimeSpan = System.TimeSpan.MinValue;

            //if (Interval == "m5")
            //{
            //    PricesTimeSpan = new TimeSpan(0, 0, 5, 0, 0);
            //}

            do // cause there is limit for returned candles amount
            {
                factory.fillMarketDataSnapshotRequestTime(request, DtFrom, DtFirst, false);
                m_responseListener.SetRequestID(request.RequestID);
                O2GSession.sendRequest(request);

                if (!m_responseListener.WaitEvents())
                {
                    throw new Exception("Response waiting timeout expired");
                }
                // shift "to" bound to oldest datetime of returned data
                O2GResponse response = m_responseListener.GetResponse();
                if (response != null && response.Type == O2GResponseType.MarketDataSnapshot)
                {
                    O2GResponseReaderFactory readerFactory = O2GSession.getResponseReaderFactory();
                    if (readerFactory != null)
                    {
                        O2GMarketDataSnapshotResponseReader reader = readerFactory.createMarketDataSnapshotReader(response);
                        if (reader.Count > 0)
                        {
                            if (DateTime.Compare(DtFirst, reader.getDate(0)) != 0)
                            {
                                DtFirst = reader.getDate(0); // earliest datetime of returned data

                                for (int nData = reader.Count - 1; nData > -1; nData--)
                                {
                                    if (reader.getDate(nData) != DatePrec)
                                    {
                                        m_datetime.Add(reader.getDate(nData));

                                        m_ask.Add(reader.getAsk(nData));
                                        m_askopen.Add(reader.getAskOpen(nData));
                                        m_askclose.Add(reader.getAskClose(nData));
                                        m_askhigh.Add(reader.getAskHigh(nData));
                                        m_asklow.Add(reader.getAskLow(nData));

                                        m_bid.Add(reader.getBid(nData));
                                        m_bidopen.Add(reader.getBidOpen(nData));
                                        m_bidclose.Add(reader.getBidClose(nData));
                                        m_bidhigh.Add(reader.getBidHigh(nData));
                                        m_bidlow.Add(reader.getBidLow(nData));
                                    }
                                    //else
                                    //{
                                    //    break;
                                    //}

                                    DatePrec = reader.getDate(nData);
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            Console.WriteLine("0 rows received");
                            break;
                        }
                    }
                    // PrintPrices(session, response);
                }
                else
                {
                    break;
                }
            } while (DtFirst > DtFrom);

            m_datetime.Reverse();

            m_ask.Reverse();
            m_askopen.Reverse();
            m_askclose.Reverse();
            m_askhigh.Reverse();
            m_asklow.Reverse();

            m_bid.Reverse();
            m_bidopen.Reverse();
            m_bidclose.Reverse();
            m_bidhigh.Reverse();
            m_bidlow.Reverse();
        }