Exemplo n.º 1
0
        /// <summary>
        /// Get initial Trades state
        /// </summary>
        /// <param name="session"></param>
        /// <param name="sAccountID"></param>
        /// <param name="responseListener"></param>
        /// <returns>TradesTable</returns>
        private static O2GTradesTableResponseReader GetTradesTable(O2GSession session, string sAccountID, ResponseListener responseListener)
        {
            O2GTradesTableResponseReader tradesTable    = null;
            O2GRequestFactory            requestFactory = session.getRequestFactory();

            if (requestFactory == null)
            {
                throw new Exception("Cannot create request factory");
            }
            O2GRequest refreshTrades = requestFactory.createRefreshTableRequestByAccount(O2GTableType.Trades, sAccountID);

            responseListener.SetRequestID(refreshTrades.RequestID);
            session.sendRequest(refreshTrades);
            if (!responseListener.WaitEvents())
            {
                throw new Exception("Response waiting timeout expired");
            }
            O2GResponse tradeResponse = responseListener.GetResponse();

            if (tradeResponse != null)
            {
                O2GResponseReaderFactory readerFactory = session.getResponseReaderFactory();
                if (readerFactory != null)
                {
                    tradesTable = readerFactory.createTradesTableReader(tradeResponse);
                }
            }
            return(tradesTable);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get orders data for closing all positions
        /// </summary>
        /// <param name="session"></param>
        /// <param name="sAccountID"></param>
        /// <param name="sOfferID"></param>
        /// <param name="responseListener"></param>
        /// <returns></returns>
        private static bool GetCloseOrdersData(O2GSession session, string sAccountID, string sOfferID, ResponseListener responseListener, out CloseOrdersData closeOrdersData)
        {
            O2GRequestFactory requestFactory = session.getRequestFactory();

            if (requestFactory == null)
            {
                throw new Exception("Cannot create request factory");
            }
            O2GRequest request = requestFactory.createRefreshTableRequestByAccount(O2GTableType.Trades, sAccountID);

            responseListener.SetRequestID(request.RequestID);
            session.sendRequest(request);
            if (!responseListener.WaitEvents())
            {
                throw new Exception("Response waiting timeout expired");
            }
            O2GResponse response       = responseListener.GetResponse();
            bool        bIsTradesFound = false;

            closeOrdersData = new CloseOrdersData();
            if (response != null)
            {
                O2GResponseReaderFactory readerFactory = session.getResponseReaderFactory();
                if (readerFactory != null)
                {
                    O2GTradesTableResponseReader tradesResponseReader = readerFactory.createTradesTableReader(response);
                    for (int i = 0; i < tradesResponseReader.Count; i++)
                    {
                        O2GTradeRow trade = tradesResponseReader.getRow(i);
                        if (!trade.OfferID.Equals(sOfferID))
                        {
                            continue;
                        }
                        bIsTradesFound = true;
                        string sBuySell = trade.BuySell;
                        // Set opposite side
                        OrderSide side = (sBuySell.Equals(Constants.Buy) ? OrderSide.Sell : OrderSide.Buy);
                        if (closeOrdersData.OfferID.Equals(sOfferID))
                        {
                            OrderSide currentSide = closeOrdersData.Side;
                            if (currentSide != OrderSide.Both && currentSide != side)
                            {
                                closeOrdersData.Side = OrderSide.Both;
                            }
                        }
                        else
                        {
                            closeOrdersData.OfferID   = sOfferID;
                            closeOrdersData.AccountID = sAccountID;
                            closeOrdersData.Side      = side;
                        }
                    }
                }
            }
            return(bIsTradesFound);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get orders data for closing all positions
        /// </summary>
        /// <param name="session"></param>
        /// <param name="sAccountID"></param>
        /// <param name="responseListener"></param>
        /// <returns></returns>
        private static Dictionary <string, CloseOrdersData> GetCloseOrdersData(O2GSession session, string sAccountID, ResponseListener responseListener)
        {
            Dictionary <string, CloseOrdersData> closeOrdersData = new Dictionary <string, CloseOrdersData>();
            O2GRequestFactory requestFactory = session.getRequestFactory();

            if (requestFactory == null)
            {
                throw new Exception("Cannot create request factory");
            }
            O2GRequest request = requestFactory.createRefreshTableRequestByAccount(O2GTableType.Trades, sAccountID);

            responseListener.SetRequestID(request.RequestID);
            session.sendRequest(request);
            if (!responseListener.WaitEvents())
            {
                throw new Exception("Response waiting timeout expired");
            }
            O2GResponse response = responseListener.GetResponse();

            if (response != null)
            {
                O2GResponseReaderFactory readerFactory = session.getResponseReaderFactory();
                if (readerFactory != null)
                {
                    O2GTradesTableResponseReader tradesResponseReader = readerFactory.createTradesTableReader(response);
                    for (int i = 0; i < tradesResponseReader.Count; i++)
                    {
                        O2GTradeRow trade    = tradesResponseReader.getRow(i);
                        string      sOfferID = trade.OfferID;
                        string      sBuySell = trade.BuySell;
                        // Set opposite side
                        OrderSide side = (sBuySell.Equals(Constants.Buy) ? OrderSide.Sell : OrderSide.Buy);

                        if (closeOrdersData.ContainsKey(sOfferID))
                        {
                            OrderSide currentSide = closeOrdersData[sOfferID].Side;
                            if (currentSide != OrderSide.Both && currentSide != side)
                            {
                                closeOrdersData[sOfferID].Side = OrderSide.Both;
                            }
                        }
                        else
                        {
                            CloseOrdersData data = new CloseOrdersData(sAccountID, side);
                            closeOrdersData.Add(sOfferID, data);
                        }
                    }
                }
            }
            return(closeOrdersData);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Find the first opened position by AccountID and OfferID
        /// </summary>
        /// <param name="session"></param>
        /// <param name="sAccountID"></param>
        /// <param name="sOfferID"></param>
        /// <param name="responseListener"></param>
        /// <returns></returns>
        private static O2GTradeRow GetTrade(O2GSession session, string sAccountID, string sOfferID, ResponseListener responseListener)
        {
            O2GTradeRow       trade          = null;
            bool              bHasTrade      = false;
            O2GRequestFactory requestFactory = session.getRequestFactory();

            if (requestFactory == null)
            {
                throw new Exception("Cannot create request factory");
            }
            O2GRequest request = requestFactory.createRefreshTableRequestByAccount(O2GTableType.Trades, sAccountID);

            responseListener.SetRequestID(request.RequestID);
            session.sendRequest(request);
            if (!responseListener.WaitEvents())
            {
                throw new Exception("Response waiting timeout expired");
            }
            O2GResponse response = responseListener.GetResponse();

            if (response != null)
            {
                O2GResponseReaderFactory readerFactory = session.getResponseReaderFactory();
                if (readerFactory != null)
                {
                    O2GTradesTableResponseReader tradesResponseReader = readerFactory.createTradesTableReader(response);
                    for (int i = 0; i < tradesResponseReader.Count; i++)
                    {
                        trade = tradesResponseReader.getRow(i);
                        if (sOfferID.Equals(trade.OfferID))
                        {
                            bHasTrade = true;
                            break;
                        }
                    }
                }
            }
            if (!bHasTrade)
            {
                return(null);
            }
            else
            {
                return(trade);
            }
        }
Exemplo n.º 5
0
        public bool ExistsPositionFor(string symbol)
        {
            O2GResponseReaderFactory factory = session.Session.getResponseReaderFactory();
            // Gets first account from login.
            O2GLoginRules loginRules = session.Session.getLoginRules();
            O2GResponse   response   = loginRules.getTableRefeshResponse(O2GTable.ClosedTrades);

            O2GTradesTableResponseReader tradesReader = factory.createTradesTableReader(response);

            for (int i = 0; i < tradesReader.Count; i++)
            {
                O2GTradeRow tradeRow = tradesReader.getRow(i);
                Console.WriteLine("Trades---");
                Console.WriteLine(tradeRow.OpenQuoteID);
                Console.WriteLine(tradeRow.OfferID);
            }

            return(false);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Print trades table for account
        /// </summary>
        /// <param name="session"></param>
        /// <param name="sAccountID"></param>
        /// <param name="responseListener"></param>
        private static void PrintTrades(O2GSession session, string sAccountID, ResponseListener responseListener)
        {
            O2GRequestFactory requestFactory = session.getRequestFactory();

            if (requestFactory == null)
            {
                throw new Exception("Cannot create request factory");
            }
            O2GRequest request = requestFactory.createRefreshTableRequestByAccount(O2GTableType.Trades, sAccountID);

            if (request != null)
            {
                Console.WriteLine("Trades table for account {0}", sAccountID);
                responseListener.SetRequestID(request.RequestID);
                session.sendRequest(request);
                if (!responseListener.WaitEvents())
                {
                    throw new Exception("Response waiting timeout expired");
                }
                O2GResponse response = responseListener.GetResponse();
                if (response != null)
                {
                    O2GResponseReaderFactory     responseReaderFactory = session.getResponseReaderFactory();
                    O2GTradesTableResponseReader responseReader        = responseReaderFactory.createTradesTableReader(response);
                    for (int i = 0; i < responseReader.Count; i++)
                    {
                        O2GTradeRow tradeRow = responseReader.getRow(i);
                        Console.WriteLine("TradeID: {0}, Amount: {1}, Dividends: {2}", tradeRow.TradeID, tradeRow.Amount, tradeRow.Dividends);
                    }
                }
                else
                {
                    throw new Exception("Cannot get response");
                }
            }
            else
            {
                throw new Exception("Cannot create request");
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Get trade by order ID
        /// </summary>
        private static O2GTradeRow FindPosition(O2GSession session, string sAccountID, string sOrderID, ResponseListener responseListener)
        {
            O2GRequestFactory requestFactory = session.getRequestFactory();

            if (requestFactory == null)
            {
                throw new Exception("Cannot create request factory");
            }
            O2GRequest request = requestFactory.createRefreshTableRequestByAccount(O2GTableType.Trades, sAccountID);

            if (request != null)
            {
                responseListener.SetRequestID(request.RequestID);
                session.sendRequest(request);
                if (!responseListener.WaitEvents())
                {
                    throw new Exception("Response waiting timeout expired");
                }
                O2GResponse tradeResponse = responseListener.GetResponse();
                if (tradeResponse != null)
                {
                    if (tradeResponse.Type == O2GResponseType.GetTrades)
                    {
                        O2GResponseReaderFactory     responseReaderFactory = session.getResponseReaderFactory();
                        O2GTradesTableResponseReader responseReader        = responseReaderFactory.createTradesTableReader(tradeResponse);
                        for (int i = 0; i < responseReader.Count; i++)
                        {
                            O2GTradeRow tradeRow = responseReader.getRow(i);
                            if (sOrderID.Equals(tradeRow.OpenOrderID))
                            {
                                return(tradeRow);
                            }
                        }
                    }
                }
            }
            return(null);
        }