/// <summary> /// Listener: Price history request is completed /// </summary> /// <param name="request"></param> /// <param name="response"></param> public void onRequestCompleted(IPriceHistoryCommunicatorRequest request, IPriceHistoryCommunicatorResponse response) { // try to find the collection (must be created at the moment when the request is sent) PeriodCollection coll = null; mHistories.TryGetValue(request, out coll); if (coll != null) { // create a reader for the price history data O2GMarketDataSnapshotResponseReader reader = mPriceHistoryCommunicator.createResponseReader(response); // and read all bars for (int i = 0; i < reader.Count; i++) { coll.Add(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)); } // finally notify the collection that all bars are added, so it can // add all ticks collected while the request was being executed // and start update the data by forthcoming ticks (for alive collections) coll.Finish(reader.getLastBarTime(), reader.getLastBarVolume()); } // now we can remove collection from our cache and send it to the application mHistories.Remove(request); if (coll != null && OnCollectionLoaded != null) { OnCollectionLoaded(coll); } }
/// <summary> /// Request historical prices for the specified timeframe of the specified period. /// </summary> /// <param name="communicator">The price history communicator.</param> /// <param name="instrument">The instrument.</param> /// <param name="timeframe">The timeframe.</param> /// <param name="from">From-date.</param> /// <param name="to">To-date</param> /// <param name="quotesCount">The quotes count.</param> /// <param name="responseListener">The response listener.</param> public static void GetHistoryPrices(IPriceHistoryCommunicator communicator, string instrument, string timeframe, DateTime from, DateTime to, int quotesCount, ResponseListener responseListener) { if (!communicator.isReady()) { Console.WriteLine("History communicator is not ready."); return; } // create timeframe entity ITimeframeFactory timeframeFactory = communicator.TimeframeFactory; O2GTimeframe timeframeObj = timeframeFactory.create(timeframe); // create and send a history request IPriceHistoryCommunicatorRequest request = communicator.createRequest(instrument, timeframeObj, from, to, quotesCount); responseListener.SetRequest(request); communicator.sendRequest(request); // wait results responseListener.Wait(); // print results if any IPriceHistoryCommunicatorResponse response = responseListener.GetResponse(); if (response != null) { PrintPrices(communicator, response); } }
/// <summary> /// Listener: request failed. /// </summary> /// <param name="request"></param> /// <param name="error"></param> public void onRequestFailed(IPriceHistoryCommunicatorRequest request, PriceHistoryError error) { mHistories.Remove(request); if (OnErrorEvent != null) { OnErrorEvent(error.Message); } }
/// <summary> /// Listener: Price history request is completed. /// </summary> /// <param name="request"></param> /// <param name="response"></param> public void onRequestCompleted(IPriceHistoryCommunicatorRequest request, IPriceHistoryCommunicatorResponse response) { if (mRequest == request) { mResponse = response; mRequest = null; mSyncResponseEvent.Set(); } }
/// <summary> /// Requests the historical data /// </summary> /// <param name="instrument">The instrument</param> /// <param name="timeframe">The timeframe</param> /// <param name="from">Date/time to get the data from</param> /// <param name="to">Date/time to get the data to</param> public void RequestHistory(string instrument, O2GTimeframe timeframe, DateTime from, DateTime to) { IPriceHistoryCommunicatorRequest request = mPriceHistoryCommunicator.createRequest(instrument, timeframe, from, to, -1); // We don't need a pre-created collection for pure historical data, but just lets create it // to handle the response in the same way. PeriodCollection collection = new PeriodCollection(instrument, timeframe.ID, false, this); mHistories[request] = collection; mPriceHistoryCommunicator.sendRequest(request); // ... to response handling see onRequestCompleted in Price Communicator Event handler section }
/// <summary> /// Listener: when the request is cancelled. /// </summary> /// <param name="request"></param> public void onRequestCancelled(IPriceHistoryCommunicatorRequest request) { if (mRequest == request) { Console.WriteLine("Request cancelled."); mResponse = null; mRequest = null; mSyncResponseEvent.Set(); } }
/// <summary> /// Listener: request failed. /// </summary> /// <param name="request"></param> /// <param name="error"></param> public void onRequestFailed(IPriceHistoryCommunicatorRequest request, PriceHistoryError error) { if (mRequest == request) { Console.WriteLine("Request failed: " + error); mResponse = null; mRequest = null; mSyncResponseEvent.Set(); } }
/// <summary> /// Gets the historical data up to now and subscribe it for price updates /// </summary> /// <param name="instrument">The instrument</param> /// <param name="timeframe">The timeframe</param> /// <param name="from">The date/time to get the data from</param> public void RequestAndSubscribeHistory(string instrument, O2GTimeframe timeframe, DateTime from) { IPriceHistoryCommunicatorRequest request = mPriceHistoryCommunicator.createRequest(instrument, timeframe, from, Candleworks.PriceHistoryMgr.Constants.ZERODATE, -1); // here we create a collection right now in order to let it subscribe for all ticks (offer changes) // which may occur while the request is being executed, so no data is lost because of gap while the server // is already collected all data, but the client isn't started to update collection yet. PeriodCollection collection = new PeriodCollection(instrument, timeframe.ID, true, this); mHistories[request] = collection; mPriceHistoryCommunicator.sendRequest(request); // ... to response handling see onRequestCompleted in Price Communicator Event handler section }
/// <summary> /// Sets request. /// </summary> /// <param name="request"></param> public void SetRequest(IPriceHistoryCommunicatorRequest request) { mResponse = null; mRequest = request; }
/// <summary> /// Listener: when the request is cancelled /// </summary> /// <param name="request"></param> public void onRequestCancelled(IPriceHistoryCommunicatorRequest request) { // simply remove the request from the list of waiting histories mHistories.Remove(request); }
/// <summary> /// Request historical prices for the specified timeframe of the specified period /// and then show live prices. /// </summary> /// <param name="communicator">The price history communicator.</param> /// <param name="instrument">The instrument.</param> /// <param name="timeframe">The timeframe.</param> /// <param name="from">From-date.</param> /// <param name="to">To-date</param> /// <param name="quotesCount">The quotes count.</param> /// <param name="responseListener">The response listener.</param> /// <param name="session">The trading session.</param> /// <param name="sessionListener">The trading session listener.</param> public static void GetLivePrices(IPriceHistoryCommunicator communicator, string instrument, string timeframe, DateTime from, DateTime to, int quotesCount, ResponseListener responseListener, O2GSession session, SessionStatusListener sessionListener) { if (!communicator.isReady()) { Console.WriteLine("History communicator is not ready."); return; } // create timeframe entity ITimeframeFactory timeframeFactory = communicator.TimeframeFactory; O2GTimeframe timeframeObj = timeframeFactory.create(timeframe); // check timeframe for ticks if (O2GTimeframeUnit.Tick == timeframeObj.Unit) { throw new Exception("Application works only for bars. Don't use tick as timeframe."); } // load Offers table and start ticks listening PriceUpdateController priceUpdateController = new PriceUpdateController(session, instrument); if (!priceUpdateController.Wait()) { return; } // create period collection bool alive = true; PeriodCollection periods = new PeriodCollection(instrument, timeframe, alive, priceUpdateController); PeriodCollectionUpdateObserver livePriceViewer = new PeriodCollectionUpdateObserver(periods); // create and send a history request IPriceHistoryCommunicatorRequest request = communicator.createRequest(instrument, timeframeObj, from, to, quotesCount); responseListener.SetRequest(request); communicator.sendRequest(request); // wait results responseListener.Wait(); IPriceHistoryCommunicatorResponse response = responseListener.GetResponse(); O2GMarketDataSnapshotResponseReader reader = communicator.createResponseReader(response); if (response != null) { ProcessHistoricalPrices(communicator, response, ref periods); } // finally notify the collection that all bars are added, so it can // add all ticks collected while the request was being executed // and start update the data by forthcoming ticks periods.Finish(reader.getLastBarTime(), reader.getLastBarVolume()); // continue update the data until cancelled by a user Console.WriteLine("\nPress ENTER to cancel.\n\n"); Console.ReadKey(); livePriceViewer.Unsubscribe(); priceUpdateController.Unsubscribe(); }