Automate download of Realtime Trade and MarketDepth stocks demonstration
using System;
using System.Windows.Forms;
using SmartQuant.FIX;
using SmartQuant.Data;
using SmartQuant.Instruments;
using SmartQuant.Providers;
public class Script
{
static void Main()
{
// connect provider
IMarketDataProvider provider = ProviderManager.MarketDataProviders[ProviderId.IB];
provider.Connect(10 * 1000);
if (!provider.IsConnected)
{
MessageBox.Show("Cannot connect to provider!");
return;
}
// setup bar factory if needed
provider.BarFactory.Reset();
provider.BarFactory.Items.Clear();
provider.BarFactory.Items.Add(BarType.Time, 60, true); // 1min bars
provider.BarFactory.Items.Add(BarType.Time, 300, true); // 5min bars
provider.BarFactory.Enabled = true;
// subscribe to events
provider.NewTrade += new TradeEventHandler(OnNewTrade);
provider.NewQuote += new QuoteEventHandler(OnNewQuote);
provider.NewBar += new BarEventHandler (OnNewBar );
// get instruments
InstrumentList instruments = new InstrumentList();
foreach (Instrument instrument in InstrumentManager.Instruments)
{
if (instrument.SecurityType == SecurityType.CommonStock)
instruments.Add(instrument);
}
// request market data
foreach (Instrument instrument in instruments)
{
instrument.RequestMarketData(
provider,
MarketDataType.Trade | MarketDataType.Quote);
}
// wait
MessageBox.Show("Press Ok button to stop.");
// unsubscribe from events
provider.NewTrade -= new TradeEventHandler(OnNewTrade);
provider.NewQuote -= new QuoteEventHandler(OnNewQuote);
provider.NewBar -= new BarEventHandler (OnNewBar );
// cancel market data
foreach (Instrument instrument in instruments)
{
instrument.CancelMarketData(
provider,
MarketDataType.Trade | MarketDataType.Quote);
}
// reset bar factory
provider.BarFactory.Enabled = false;
provider.BarFactory.Reset();
// disconnect provider
provider.Disconnect();
// flush memory data
DataManager.Server.Flush();
}
static void OnNewTrade(object sender, TradeEventArgs args)
{
Instrument instrument = args.Instrument as Instrument;
instrument.Add(args.Trade);
}
static void OnNewQuote(object sender, QuoteEventArgs args)
{
Instrument instrument = args.Instrument as Instrument;
instrument.Add(args.Quote);
}
static void OnNewBar(object sender, BarEventArgs args)
{
Instrument instrument = args.Instrument as Instrument;
instrument.Add(args.Bar);
}
}
using System.Windows.Forms;
using SmartQuant.FIX;
using SmartQuant.Data;
using SmartQuant.Instruments;
using SmartQuant.Providers;
public class Script
{
static void Main()
{
// connect provider
IMarketDataProvider provider = ProviderManager.MarketDataProviders[ProviderId.IB];
provider.Connect(10 * 1000);
if (!provider.IsConnected)
{
MessageBox.Show("Cannot connect to provider!");
return;
}
// setup bar factory if needed
provider.BarFactory.Reset();
provider.BarFactory.Items.Clear();
provider.BarFactory.Items.Add(BarType.Time, 60, true); // 1min bars
provider.BarFactory.Items.Add(BarType.Time, 300, true); // 5min bars
provider.BarFactory.Enabled = true;
// subscribe to events
provider.NewTrade += new TradeEventHandler(OnNewTrade);
provider.NewQuote += new QuoteEventHandler(OnNewQuote);
provider.NewBar += new BarEventHandler (OnNewBar );
// get instruments
InstrumentList instruments = new InstrumentList();
foreach (Instrument instrument in InstrumentManager.Instruments)
{
if (instrument.SecurityType == SecurityType.CommonStock)
instruments.Add(instrument);
}
// request market data
foreach (Instrument instrument in instruments)
{
instrument.RequestMarketData(
provider,
MarketDataType.Trade | MarketDataType.Quote);
}
// wait
MessageBox.Show("Press Ok button to stop.");
// unsubscribe from events
provider.NewTrade -= new TradeEventHandler(OnNewTrade);
provider.NewQuote -= new QuoteEventHandler(OnNewQuote);
provider.NewBar -= new BarEventHandler (OnNewBar );
// cancel market data
foreach (Instrument instrument in instruments)
{
instrument.CancelMarketData(
provider,
MarketDataType.Trade | MarketDataType.Quote);
}
// reset bar factory
provider.BarFactory.Enabled = false;
provider.BarFactory.Reset();
// disconnect provider
provider.Disconnect();
// flush memory data
DataManager.Server.Flush();
}
static void OnNewTrade(object sender, TradeEventArgs args)
{
Instrument instrument = args.Instrument as Instrument;
instrument.Add(args.Trade);
}
static void OnNewQuote(object sender, QuoteEventArgs args)
{
Instrument instrument = args.Instrument as Instrument;
instrument.Add(args.Quote);
}
static void OnNewBar(object sender, BarEventArgs args)
{
Instrument instrument = args.Instrument as Instrument;
instrument.Add(args.Bar);
}
}