/// <summary>
 /// ctor
 /// </summary>
 /// <param name="responseListener"></param>
 public TableListener(ResponseListener responseListener)
 {
     mOrderMonitor = null;
     mResponseListener = responseListener;
     mTradesTable = null;
     mRequestID = string.Empty;
 }
        /// <summary>
        /// Get initial Trades state
        /// </summary>
        /// <param name="tableManager"></param>
        /// <returns>TradesTable</returns>
        private static O2GTradesTable GetTradesTable(O2GTableManager tableManager)
        {
            O2GTradesTable tradesTable = null;

            tradesTable = (O2GTradesTable)tableManager.getTable(O2GTableType.Trades);
            return(tradesTable);
        }
Esempio n. 3
0
        /// <summary>
        /// Get orders data for closing all positions
        /// </summary>
        /// <param name="tableManager"></param>
        /// <param name="sAccountID"></param>
        /// <returns></returns>
        private static Dictionary <string, CloseOrdersData> GetCloseOrdersData(O2GTableManager tableManager, string sAccountID)
        {
            Dictionary <string, CloseOrdersData> closeOrdersData = new Dictionary <string, CloseOrdersData>();
            O2GTradeRow    trade       = null;
            O2GTradesTable tradesTable = (O2GTradesTable)tableManager.getTable(O2GTableType.Trades);

            for (int i = 0; i < tradesTable.Count; i++)
            {
                trade = tradesTable.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);
        }
Esempio n. 4
0
 /// <summary>
 /// ctor
 /// </summary>
 /// <param name="responseListener"></param>
 public TableListener(ResponseListener responseListener)
 {
     mOrderMonitor     = null;
     mResponseListener = responseListener;
     mTradesTable      = null;
     mRequestID        = string.Empty;
 }
Esempio n. 5
0
        private IEnumerable <BaseRow> GetRows(O2GTradesTable table)
        {
            var rows = new List <TradeTableRow>();

            for (var i = 0; i < table.Count; i++)
            {
                rows.Add(this.GetRow <TradeTableRow, O2GTradeTableRow>(table.getRow(i)));
            }

            return(rows);
        }
        // Print orders table using IO2GEachRowListener
        public static void PrintTrades(O2GTableManager tableManager, string sAccountID)
        {
            O2GTradesTable tradesTable = (O2GTradesTable)tableManager.getTable(O2GTableType.Trades);

            if (tradesTable.Count == 0)
            {
                Console.WriteLine("Table is empty!");
            }
            else
            {
                tradesTable.forEachRow(new EachRowListener(sAccountID));
            }
        }
Esempio n. 7
0
        public void UnsubscribeEvents(O2GTableManager manager)
        {
            O2GOrdersTable       ordersTable       = (O2GOrdersTable)manager.getTable(O2GTableType.Orders);
            O2GTradesTable       tradesTable       = (O2GTradesTable)manager.getTable(O2GTableType.Trades);
            O2GMessagesTable     messagesTable     = (O2GMessagesTable)manager.getTable(O2GTableType.Messages);
            O2GClosedTradesTable closedTradesTable = (O2GClosedTradesTable)manager.getTable(O2GTableType.ClosedTrades);

            ordersTable.unsubscribeUpdate(O2GTableUpdateType.Insert, this);
            ordersTable.subscribeUpdate(O2GTableUpdateType.Update, this);
            ordersTable.unsubscribeUpdate(O2GTableUpdateType.Delete, this);
            tradesTable.unsubscribeUpdate(O2GTableUpdateType.Insert, this);
            tradesTable.unsubscribeUpdate(O2GTableUpdateType.Update, this);
            closedTradesTable.unsubscribeUpdate(O2GTableUpdateType.Insert, this);
            messagesTable.unsubscribeUpdate(O2GTableUpdateType.Insert, this);
        }
Esempio n. 8
0
        public Trades(MainForm CurrentForm, O2GTableManager mTblMgr)
        {
            CreateTable();

            CurrentForm.PopulateTable(TradesTable);


            while (mTblMgr.getStatus() != O2GTableManagerStatus.TablesLoaded && mTblMgr.getStatus() != O2GTableManagerStatus.TablesLoadFailed)
            {
                Thread.Sleep(50);
            }
            O2GTradesTable   table    = (O2GTradesTable)mTblMgr.getTable(O2GTableType.Trades);
            TradesListener   listener = new TradesListener();
            O2GTradeTableRow row      = null;

            for (int i = 0; i < table.Count; i++)
            {
                DataRow CurrentRow = TradesTable.NewRow();
                TradesTable.Rows.Add(CurrentRow);
            }

            CurrentForm.GetTableData(table, listener, row, mTblMgr);
        }
Esempio n. 9
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(O2GTableManager tableManager, string sAccountID, string sOfferID)
        {
            bool           bHasTrade   = false;
            O2GTradeRow    trade       = null;
            O2GTradesTable tradesTable = (O2GTradesTable)tableManager.getTable(O2GTableType.Trades);

            for (int i = 0; i < tradesTable.Count; i++)
            {
                trade = tradesTable.getRow(i);
                if (trade.AccountID.Equals(sAccountID) && trade.OfferID.Equals(sOfferID))
                {
                    bHasTrade = true;
                    break;
                }
            }
            if (!bHasTrade)
            {
                return(null);
            }
            else
            {
                return(trade);
            }
        }
Esempio n. 10
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 bool GetCloseOrdersData(O2GTableManager tableManager, string sAccountID, string sOfferID, out CloseOrdersData closeOrdersData)
        {
            bool bIsTradesFound = false;

            closeOrdersData = new CloseOrdersData();
            O2GTradesTable tradesTable = (O2GTradesTable)tableManager.getTable(O2GTableType.Trades);

            for (int i = 0; i < tradesTable.Count; i++)
            {
                O2GTradeRow trade = tradesTable.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);
        }
        static void Main(string[] args)
        {
            O2GSession            session          = null;
            SessionStatusListener statusListener   = null;
            ResponseListener      responseListener = null;

            try
            {
                Console.WriteLine("OpenPositionNetting sample\n");

                ArgumentParser argParser = new ArgumentParser(args, "OpenPositionNetting");

                argParser.AddArguments(ParserArgument.Login,
                                       ParserArgument.Password,
                                       ParserArgument.Url,
                                       ParserArgument.Connection,
                                       ParserArgument.SessionID,
                                       ParserArgument.Pin,
                                       ParserArgument.Instrument,
                                       ParserArgument.BuySell,
                                       ParserArgument.Lots,
                                       ParserArgument.AccountID);

                argParser.ParseArguments();

                if (!argParser.AreArgumentsValid)
                {
                    argParser.PrintUsage();
                    return;
                }

                argParser.PrintArguments();

                LoginParams  loginParams  = argParser.LoginParams;
                SampleParams sampleParams = argParser.SampleParams;

                session = O2GTransport.createSession();
                session.useTableManager(O2GTableManagerMode.Yes, null);
                statusListener = new SessionStatusListener(session, loginParams.SessionID, loginParams.Pin);
                session.subscribeSessionStatus(statusListener);
                statusListener.Reset();
                session.login(loginParams.Login, loginParams.Password, loginParams.URL, loginParams.Connection);
                if (statusListener.WaitEvents() && statusListener.Connected)
                {
                    responseListener = new ResponseListener();
                    TableListener tableListener = new TableListener(responseListener);
                    session.subscribeResponse(responseListener);

                    O2GTableManager       tableManager  = session.getTableManager();
                    O2GTableManagerStatus managerStatus = tableManager.getStatus();
                    while (managerStatus == O2GTableManagerStatus.TablesLoading)
                    {
                        Thread.Sleep(50);
                        managerStatus = tableManager.getStatus();
                    }

                    if (managerStatus == O2GTableManagerStatus.TablesLoadFailed)
                    {
                        throw new Exception("Cannot refresh all tables of table manager");
                    }
                    O2GAccountRow account = GetAccount(tableManager, sampleParams.AccountID);
                    if (account == null)
                    {
                        if (string.IsNullOrEmpty(sampleParams.AccountID))
                        {
                            throw new Exception("No valid accounts");
                        }
                        else
                        {
                            throw new Exception(string.Format("The account '{0}' is not valid", sampleParams.AccountID));
                        }
                    }
                    sampleParams.AccountID = account.AccountID;

                    O2GOfferRow offer = GetOffer(tableManager, sampleParams.Instrument);
                    if (offer == null)
                    {
                        throw new Exception(string.Format("The instrument '{0}' is not valid", sampleParams.Instrument));
                    }

                    O2GTradesTable tradesTable = GetTradesTable(tableManager);
                    if (tradesTable == null)
                    {
                        throw new Exception("Cannot get trades table");
                    }
                    tableListener.SetTradesTable(tradesTable);

                    O2GLoginRules loginRules = session.getLoginRules();
                    if (loginRules == null)
                    {
                        throw new Exception("Cannot get login rules");
                    }

                    O2GTradingSettingsProvider tradingSettingsProvider = loginRules.getTradingSettingsProvider();
                    int iBaseUnitSize = tradingSettingsProvider.getBaseUnitSize(sampleParams.Instrument, account);
                    int iAmount       = iBaseUnitSize * sampleParams.Lots;

                    tableListener.SubscribeEvents(tableManager);

                    O2GRequest request = CreateTrueMarketOrderRequest(session, offer.OfferID, sampleParams.AccountID, iAmount, sampleParams.BuySell);
                    if (request == null)
                    {
                        throw new Exception("Cannot create request");
                    }
                    responseListener.SetRequestID(request.RequestID);
                    tableListener.SetRequestID(request.RequestID);
                    session.sendRequest(request);
                    if (responseListener.WaitEvents())
                    {
                        System.Threading.Thread.Sleep(1000); // Wait for the balance update
                        Console.WriteLine("Done!");
                    }
                    else
                    {
                        throw new Exception("Response waiting timeout expired");
                    }

                    tableListener.UnsubscribeEvents(tableManager);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.ToString());
            }
            finally
            {
                if (session != null)
                {
                    if (statusListener.Connected)
                    {
                        statusListener.Reset();
                        session.logout();
                        statusListener.WaitEvents();
                        if (responseListener != null)
                        {
                            session.unsubscribeResponse(responseListener);
                        }
                        session.unsubscribeSessionStatus(statusListener);
                    }
                    session.Dispose();
                }
            }
        }
Esempio n. 12
0
 public void SetTradesTable(O2GTradesTable tradesTable)
 {
     mTradesTable = tradesTable;
 }
Esempio n. 13
0
        // **AFTER LOG IN**
        // Background worker for table listeners
        private void priceBW_DoWork(object sender, DoWorkEventArgs e)
        {
            Console.WriteLine("Entered BW");
            // Enable timers.
            pastTimer.Enabled = true;
            pastTimer.Start();
            oneSecTimer.Enabled = true;
            oneSecTimer.Start();

            mSession.useTableManager(O2GTableManagerMode.Yes, null);

            tableManager  = mSession.getTableManager();
            managerStatus = tableManager.getStatus();

            while (managerStatus == O2GTableManagerStatus.TablesLoading)
            {
                Console.WriteLine("Tables are loading...");
                Thread.Sleep(50);
                managerStatus = tableManager.getStatus();
            }
            if (managerStatus == O2GTableManagerStatus.TablesLoadFailed)
            {
                Console.WriteLine("Tabled loading failed");
                this.Invoke(new MethodInvoker(delegate { actiBox.AppendText("WARNING: LOADING TABLES FAILED!" + Environment.NewLine); }));
                return;
            }
            // Check Accounts Table and Grab Information
            try
            {
                O2GLoginRules loginRules = mSession.getLoginRules();
                Console.WriteLine("Tables are loaded!");
                // Check if Accounts table is loaded automatically
                if (loginRules != null && loginRules.isTableLoadedByDefault(O2GTableType.Accounts))
                {
                    Console.WriteLine("Login rules != null");
                    // If table is loaded, use getTableRefreshResponse method
                    O2GResponse accountsResponse             = loginRules.getTableRefreshResponse(O2GTableType.Accounts);
                    O2GResponseReaderFactory responseFactory = mSession.getResponseReaderFactory();
                    if (responseFactory != null)
                    {
                        Console.WriteLine("Grabbing account data....");
                        O2GAccountsTableResponseReader accountsReader = responseFactory.createAccountsTableReader(accountsResponse);

                        for (int i = 0; i < accountsReader.Count; i++)
                        {
                            account      = accountsReader.getRow(i);
                            accountValue = account.Balance;
                            this.Invoke(new MethodInvoker(delegate { accountValueBox.Text = "$" + Convert.ToString(accountValue); }));
                            availLev = account.Balance;
                            this.Invoke(new MethodInvoker(delegate { accountLevBox.Text = "$" + Convert.ToString(availLev); }));
                            sAccountID  = account.AccountID.ToString();
                            amountLimit = account.AmountLimit;
                            baseSize    = account.BaseUnitSize;
                        }
                    }
                }
                else
                {
                    // If table is not loaded, use createRefreshTableRequest method
                    O2GRequestFactory requestFactory = mSession.getRequestFactory();
                    if (requestFactory != null)
                    {
                        O2GRequest request = requestFactory.createRefreshTableRequest(O2GTableType.Accounts);
                        mSession.sendRequest(request);
                        Thread.Sleep(1000);
                    }
                }
            }
            catch (Exception acctErr)
            {
                Console.WriteLine(acctErr);
            }

            // Check if all 20 pairs needed are subscribed to on the account.
            try
            {
                O2GLoginRules loginRules = mSession.getLoginRules();

                if (loginRules != null && loginRules.isTableLoadedByDefault(O2GTableType.Offers))
                {
                    O2GResponse offersResponse = loginRules.getTableRefreshResponse(O2GTableType.Offers);
                    O2GResponseReaderFactory responseFactory = mSession.getResponseReaderFactory();
                    if (responseFactory != null)
                    {
                        Console.WriteLine("Triggering offers row");
                        O2GOffersTableResponseReader offersReader = responseFactory.createOffersTableReader(offersResponse);

                        for (int i = 0; i < offersReader.Count; i++)
                        {
                            O2GOfferRow offers = offersReader.getRow(i);

                            string checkOffer = offers.OfferID;
                        }
                    }
                }
                // Get MMR prices and set position initial cost
                Console.WriteLine("Grabbing MMR");
                this.Invoke(new MethodInvoker(delegate { actiBox.AppendText("Retrieving Pair MMR..." + Environment.NewLine); }));
                O2GTradingSettingsProvider settingResponse = loginRules.getTradingSettingsProvider();
                audcadMMR     = settingResponse.getMMR("AUD/CAD", account);
                audcadInitial = shortLot * audcadMMR;
                audjpyMMR     = settingResponse.getMMR("AUD/JPY", account);
                audjpyInitial = shortLot * audjpyMMR;
                audusdMMR     = settingResponse.getMMR("AUD/USD", account);
                audusdInitial = shortLot * audusdMMR;
                cadjpyMMR     = settingResponse.getMMR("CAD/JPY", account);
                cadjpyInitial = shortLot * cadjpyMMR;
                chfjpyMMR     = settingResponse.getMMR("CHF/JPY", account);
                chfjpyInitial = shortLot * chfjpyMMR;
                euraudMMR     = settingResponse.getMMR("EUR/AUD", account);
                euraudInitial = shortLot * euraudMMR;
                eurcadMMR     = settingResponse.getMMR("EUR/CAD", account);
                eurcadInitial = shortLot * eurcadMMR;
                eurchfMMR     = settingResponse.getMMR("EUR/CHF", account);
                eurchfInitial = shortLot * eurchfMMR;
                eurjpyMMR     = settingResponse.getMMR("EUR/JPY", account);
                eurjpyInitial = shortLot * eurjpyMMR;
                eurusdMMR     = settingResponse.getMMR("EUR/USD", account);
                eurusdInitial = shortLot * eurusdMMR;
                gbpaudMMR     = settingResponse.getMMR("GBP/AUD", account);
                gbpaudInitial = shortLot * gbpaudMMR;
                gbpchfMMR     = settingResponse.getMMR("GBP/CHF", account);
                gbpchfInitial = shortLot * gbpchfMMR;
                gbpjpyMMR     = settingResponse.getMMR("GBP/JPY", account);
                gbpjpyInitial = shortLot * gbpjpyMMR;
                gbpnzdMMR     = settingResponse.getMMR("GBP/NZD", account);
                gbpnzdInitial = shortLot * gbpnzdMMR;
                gbpusdMMR     = settingResponse.getMMR("GBP/USD", account);
                gbpusdInitial = shortLot * gbpusdMMR;
                nzdjpyMMR     = settingResponse.getMMR("NZD/JPY", account);
                nzdjpyInitial = shortLot * nzdjpyMMR;
                nzdusdMMR     = settingResponse.getMMR("NZD/USD", account);
                nzdusdInitial = shortLot * nzdusdMMR;
                usdcadMMR     = settingResponse.getMMR("USD/CAD", account);
                usdcadInitial = shortLot * usdcadMMR;
                usdchfMMR     = settingResponse.getMMR("USD/CHF", account);
                usdchfInitial = shortLot * usdchfMMR;
                usdjpyMMR     = settingResponse.getMMR("USD/JPY", account);
                usdjpyInitial = shortLot * usdjpyMMR;

                // Set highest move to 0
                highestMove = 0;

                // Set all move values to 0;
            }
            catch (Exception mmrErr)
            {
                Console.WriteLine(mmrErr);
            }

            Console.WriteLine("Initializing needed table events.");
            // Initiate Table Getters
            O2GOffersTable offersTable = (O2GOffersTable)tableManager.getTable(O2GTableType.Offers);

            accountsTable = (O2GAccountsTable)tableManager.getTable(O2GTableType.Accounts);
            O2GTradesTable tradesTable = (O2GTradesTable)tableManager.getTable(O2GTableType.Trades);

            //O2GOrdersTable ordersTable = (O2GOrdersTable)tableManager.getTable(O2GTableType.Orders);
            //O2GClosedTradesTable closedTradesTable = (O2GClosedTradesTable)tableManager.getTable(O2GTableType.ClosedTrades);
            //O2GMessagesTable messagesTable = (O2GMessagesTable)tableManager.getTable(O2GTableType.Messages);
            //O2GSummaryTable summaryTable = (O2GSummaryTable)tableManager.getTable(O2GTableType.Summary);


            // Trigger Table Events for Subscription
            offersTable.RowChanged   += new EventHandler <RowEventArgs>(offersTable_RowChanged);
            accountsTable.RowChanged += new EventHandler <RowEventArgs>(accountsTable_RowChanged);
            tradesTable.RowChanged   += new EventHandler <RowEventArgs>(tradesTable_RowChanged);
            //ordersTable.RowChanged += new EventHandler<RowEventArgs>(ordersTable_RowChanged);
            //closedTradesTable.RowChanged += new EventHandler<RowEventArgs>(closedTradesTable_RowChanged);
            //messagesTable.RowChanged += new EventHandler<RowEventArgs>(messagesTable_RowChanged);
            //summaryTable.RowChanged += new EventHandler<RowEventArgs>(summaryTable_RowChanged);

            // Check pair subscription status, and add if needed.

            this.Invoke(new MethodInvoker(delegate { actiBox.AppendText("Connection Established.... Monitoring Pairs..." + Environment.NewLine); }));
            // Start Past Price Timer
            pastTimer.Start();
        }
Esempio n. 14
0
 public void SetTradesTable(O2GTradesTable tradesTable)
 {
     mTradesTable = tradesTable;
 }