// should call StartSubsriber() after calling Start()
        public bool Start()
        {
            try
            {
                if (!IsStarted)
                {
                    if (!m_monitorUtilities.IsMonitoring)
                    {
                        PositionMonitorUtilities.Info(String.Format("Cannot start {0} before starting monitor", Name));
                    }
                    else
                    {
                        RefreshEventArgs args = new RefreshEventArgs(true);
                        UpdateTables(args);
                        m_monitorUtilities.OnRefresh += utilities_OnRefresh;
                        IsStarted = true;
                    }
                }
            }
            catch (Exception ex)
            {
                PositionMonitorUtilities.Error("Unable to start " + Name, ex);
                IsStarted = false;
            }

            return(IsStarted);
        }
 private void utilities_OnRefresh(object sender, RefreshEventArgs e)
 {
     if (IsStarted)
     {
         UpdateTables(e);
     }
 }
Esempio n. 3
0
        private void RefreshNoncriticalTables(RefreshEventArgs args)
        {
            try
            {
                HugoDataSet.PortfolioSnapshotIdsDataTable snapshotIds = GetSnapshotIds(null, ref m_snapshotIdsUpdateTime);
                HugoDataSet.TradesDataTable trades = GetTrades(null, null, ref m_tradesUpdateTime);

                lock (m_tableLock)
                {
                    // if there are no changes, an empty table is returned by the above methods
                    if ((snapshotIds.Count > 0) || (m_snapshotIds == null))
                    {
                        m_snapshotIds           = snapshotIds;
                        args.SnapshotIdsUpdated = true;
                    }
                    if ((trades.Count > 0) || (m_trades == null))
                    {
                        m_trades           = trades;
                        args.TradesUpdated = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Error("Unable to refresh non-critical data tables", ex);
            }
        }
Esempio n. 4
0
        private RefreshEventArgs RefreshTables(bool initializing = false)
        {
            RefreshEventArgs args = new RefreshEventArgs();

            try
            {
                RefreshCriticalTables(args);

                // skip non-critical tables if initializing, so we will start up sooner
                if (!initializing)
                {
#if !DEBUG
                    TakeRequiredSnapshots();
#endif
                    RefreshNoncriticalTables(args);

                    // try again before sleeping, in case above Refresh took a long time
#if !DEBUG
                    TakeRequiredSnapshots();
#endif
                }
            }
            catch (Exception ex)
            {
                Error("Unable to refresh data tables", ex);
            }
            return(args);
        }
Esempio n. 5
0
        private void RefreshCriticalTables(RefreshEventArgs args)
        {
            try
            {
                HugoDataSet.AccountDataDataTable      accountData      = GetAccountData(ref m_accountsUpdateTime);
                HugoDataSet.CurrentPositionsDataTable currentPositions = GetCurrentPositions();;
                HugoDataSet.IndexWeightsDataTable     indexWeights     = GetIndexWeights();
                HugoDataSet.TradingScheduleDataTable  tradingSchedule  = GetTradingSchedule();

                lock (m_tableLock)
                {
                    // if there are no changes, an empty table is returned by the above methods
                    if (accountData.Count > 0)
                    {
                        if (AccountLimit > 0)
                        {
                            for (int i = accountData.Count - 1; i >= AccountLimit; i--)
                            {
                                accountData.Rows.RemoveAt(i);
                            }
                        }
                        m_accountData        = accountData;
                        args.AccountsUpdated = true;
                    }
                    if (currentPositions.Count > 0)
                    {
                        m_currentPositions    = currentPositions;
                        args.PositionsUpdated = true;
                    }
                    if ((indexWeights.Count > 0) || (m_indexWeights == null))
                    {
                        m_indexWeights           = indexWeights;
                        args.IndexWeightsUpdated = true;
                    }
                    if ((tradingSchedule.Count > 0) || (m_tradingSchedule == null))
                    {
                        m_tradingSchedule           = tradingSchedule;
                        args.TradingScheduleUpdated = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Error("Unable to refresh critical data tables", ex);
            }
        }
        private void UpdateTables(RefreshEventArgs e)
        {
            string msg = String.Format("{0} refreshed portfolio:", Name);

            if (e.PositionsUpdated)
            {
                UpdatePositions(m_monitorUtilities.GetCurrentPositionsForAccount(AccountName));
                msg += String.Format(" {0} positions,", m_portfolio.Count);
            }

            if (e.IndexWeightsUpdated)
            {
                UpdateIndices(m_monitorUtilities.GetIndexWeightsForAccount(AccountName));
                msg += " index weights,";
            }

            if (e.AccountsUpdated)
            {
                m_accountData = m_monitorUtilities.GetDataForAccount(AccountName);
                if (m_accountData != null)
                {
                    UpdateBenchmark(BenchmarkSymbol, m_accountData.IndexFlag ? QuoteType.Index : QuoteType.Stock);
                    msg += " account info,";
                }
            }

            if (e.TradesUpdated)
            {
                m_trades          = m_monitorUtilities.GetTradesForAccount(AccountName);
                DividendsReceived = -m_trades.Where(x => x.TradeType == "RecDiv").Sum(x => x.Change_in_Cost);
                msg += String.Format(" {0} trades,", NumberOfTrades);
            }

            if (e.SnapshotIdsUpdated)
            {
                m_snapshotIds = m_monitorUtilities.GetSnapshotsForAccount(AccountName);
                msg          += " snapshots";
            }

            PositionMonitorUtilities.Debug(msg);
            if (m_refreshEventHandler != null)
            {
                m_refreshEventHandler(this, new EventArgs());
            }
        }
Esempio n. 7
0
        private void AsyncRefresh(object o, DoWorkEventArgs e)
        {
            try
            {
                RefreshEventArgs args = (RefreshEventArgs)e.Argument;
                while (IsMonitoring)
                {
                    if ((m_refreshEventHandler != null) && args.TablesUpdated)
                    {
                        Debug("Tables refreshed");
                        m_refreshEventHandler(null, args);
                    }
                    else
                    {
                        Debug("No updates to tables");
                    }

                    System.Threading.Thread.Sleep(RefreshMs);
                    if (IsMonitoring)
                    {
                        args = RefreshTables();
                    }

                    // make sure quote subscriber is running for each account
                    bool quoteServerIsUp = IsUsingQuoteFeed;
                    foreach (AccountPortfolio account in GetAllAccountPortfolios())
                    {
                        if (account.IsStarted && quoteServerIsUp)
                        {
                            quoteServerIsUp = account.StartSubscriber();
                        }
                    }
                }
                StopMonitor();
            }
            catch (Exception ex)
            {
                Error("Exception in refresh thread", ex);
                StopMonitor(ex);
            }
        }
Esempio n. 8
0
 public bool StartMonitor()
 {
     if (!IsMonitoring)
     {
         try
         {
             if (IsInitialized)
             {
                 Info("Starting monitor");
                 RefreshEventArgs args = RefreshTables();
                 if (args.TablesUpdated)
                 {
                     if (BuildAccountPortfolios())
                     {
                         System.Threading.Thread.Sleep(RefreshMs); // wait one cycle to give quotes a chance to come in
                         IsMonitoring = true;
                         BackgroundWorker worker = new BackgroundWorker();
                         worker.DoWork += new DoWorkEventHandler(AsyncRefresh);
                         worker.RunWorkerAsync(args);
                         Info("Monitor started");
                     }
                 }
             }
             else
             {
                 Info("Cannot start monitor before it is initialized");
             }
         }
         catch (Exception ex)
         {
             Error("Error trying to start Monitor", ex);
             IsMonitoring = false;
         }
     }
     return(IsMonitoring);
 }