C
Curious
Hi,
I have a program that gets the prices of stocks in real-time. The
program consists of the following key components:
// Constructor
public GetRealTimeStockPrice()
{
// Step 1
foreach (IWatchList portfolio in
WatchListManager.GetPortfolios())
{
portfolio.OpenStocks(new
ListDataReadyDelegate(RegisterExistingStocks));
}
// Step 2
foreach (IWatchList portfolio in
WatchListManager.GetPortfolios())
{
portfolio.OpenStocks(new
ListDataReadyDelegate(OnStocksDownloaded));
}
}
void RegisterExistingStocks(IWatchList portfolio)
{
PriceForStock stockPrice;
string CurrentTicker;
foreach (IWatchEntry stock in portfolio.Stocks)
{
CurrentTicker = stock.Ticker;
if (!UniqueTickerRegistered(CurrentTicker))
{
stockPrice = new PriceForEntry(CurrentTicker);
mPriceCheckList.Add(stockPrice);
}
}
}
void OnStocksDownloaded(IWatchList portfolio)
{
string ticker;
foreach (IWatchEntry stock in portfolio.Stocks)
{
ticker = stock.Ticker;
if (!PriceObtained(ticker))
{
stock.SymbolData.Trade += new
TradeDelegate(SymbolData_Trade);
}
else // un-subscribe to the event once it's done
{
stock.SymbolData.Trade -= new
TradeDelegate(SymbolData_Trade);
}
}
}
Question 1:
I want to make sure that Step 1 happens before Step 2 in
"GetRealTimeStockPrice". Can anyone tell me if that is the case? (I
understand that with delegates, when it actually gets executed is out
of control.)
Question 2:
I was advised to use the structure below to get all of the existing
stocks in the porfolios:
foreach (IWatchList portfolio in
WatchListManager.GetPortfolios())
{
portfolio.OpenStocks(new
ListDataReadyDelegate(......));
}
Alternatively, I could use the following approach instead:
foreach (IWatchList portfolio in
WatchListManager.GetPortfolios())
{
foreach (IWatchEntry stock in portfolio.Stocks)
{
// blah blah blah
}
}
However, I was told by using "OpenStocks" with a delegate as its
parameter, it guarantees to get all of the stocks while the other
approach of looping through each stock may not work if the data for
the stock(s) are not immediately available.
Can anyone explain why using the delegate guarantees that the data are
available?
Question 3:
Is the way I un-subscribe to the "SymbolData_Trade" event correct?
if (!PriceObtained(ticker))
{
stock.SymbolData.Trade += new
TradeDelegate(SymbolData_Trade);
}
else // un-subscribe to the event once it's done
{
stock.SymbolData.Trade -= new
TradeDelegate(SymbolData_Trade);
}
I want to make sure that this removes the stock for which
"PriceObtained(ticker) == true" from the subscription. In other words,
once the price of a stock is obtained, it should be removed from the
watch list so that it won't react to the "SymbolData_Trade" event
anymore.
Would appreciate knowledgable people's advice!
I have a program that gets the prices of stocks in real-time. The
program consists of the following key components:
// Constructor
public GetRealTimeStockPrice()
{
// Step 1
foreach (IWatchList portfolio in
WatchListManager.GetPortfolios())
{
portfolio.OpenStocks(new
ListDataReadyDelegate(RegisterExistingStocks));
}
// Step 2
foreach (IWatchList portfolio in
WatchListManager.GetPortfolios())
{
portfolio.OpenStocks(new
ListDataReadyDelegate(OnStocksDownloaded));
}
}
void RegisterExistingStocks(IWatchList portfolio)
{
PriceForStock stockPrice;
string CurrentTicker;
foreach (IWatchEntry stock in portfolio.Stocks)
{
CurrentTicker = stock.Ticker;
if (!UniqueTickerRegistered(CurrentTicker))
{
stockPrice = new PriceForEntry(CurrentTicker);
mPriceCheckList.Add(stockPrice);
}
}
}
void OnStocksDownloaded(IWatchList portfolio)
{
string ticker;
foreach (IWatchEntry stock in portfolio.Stocks)
{
ticker = stock.Ticker;
if (!PriceObtained(ticker))
{
stock.SymbolData.Trade += new
TradeDelegate(SymbolData_Trade);
}
else // un-subscribe to the event once it's done
{
stock.SymbolData.Trade -= new
TradeDelegate(SymbolData_Trade);
}
}
}
Question 1:
I want to make sure that Step 1 happens before Step 2 in
"GetRealTimeStockPrice". Can anyone tell me if that is the case? (I
understand that with delegates, when it actually gets executed is out
of control.)
Question 2:
I was advised to use the structure below to get all of the existing
stocks in the porfolios:
foreach (IWatchList portfolio in
WatchListManager.GetPortfolios())
{
portfolio.OpenStocks(new
ListDataReadyDelegate(......));
}
Alternatively, I could use the following approach instead:
foreach (IWatchList portfolio in
WatchListManager.GetPortfolios())
{
foreach (IWatchEntry stock in portfolio.Stocks)
{
// blah blah blah
}
}
However, I was told by using "OpenStocks" with a delegate as its
parameter, it guarantees to get all of the stocks while the other
approach of looping through each stock may not work if the data for
the stock(s) are not immediately available.
Can anyone explain why using the delegate guarantees that the data are
available?
Question 3:
Is the way I un-subscribe to the "SymbolData_Trade" event correct?
if (!PriceObtained(ticker))
{
stock.SymbolData.Trade += new
TradeDelegate(SymbolData_Trade);
}
else // un-subscribe to the event once it's done
{
stock.SymbolData.Trade -= new
TradeDelegate(SymbolData_Trade);
}
I want to make sure that this removes the stock for which
"PriceObtained(ticker) == true" from the subscription. In other words,
once the price of a stock is obtained, it should be removed from the
watch list so that it won't react to the "SymbolData_Trade" event
anymore.
Would appreciate knowledgable people's advice!