100 lines
2.5 KiB
C#
100 lines
2.5 KiB
C#
using Backend.MarketDataRequest;
|
|
using IBApi;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
|
|
namespace Backend
|
|
{
|
|
public static class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
var Gateway = new IBGatewayControl();
|
|
try
|
|
{
|
|
Gateway.Start("127.0.0.1", 4002, 1);
|
|
}
|
|
catch
|
|
{
|
|
Console.WriteLine("Invalid connection to IB Gateway. Please ensure gateway is running and connected.");
|
|
Environment.Exit(1);
|
|
}
|
|
|
|
while (!Gateway.IsApiReady)
|
|
{
|
|
Thread.Sleep(50);
|
|
}
|
|
|
|
// ---- TEST HISTORICAL DATA REQUEST ----
|
|
|
|
var HistoricalRequest = new HistoricalEquityRequest
|
|
{
|
|
RequestId = 1,
|
|
Symbol = "NVDA",
|
|
EndDateTime = "20260311 11:00:00 US/Eastern",
|
|
Duration = "3600 S",
|
|
BarSize = HistoricalBarSize.FiveMins,
|
|
WhatToShow = HistoricalWhatToShow.Trades,
|
|
UseRth = true,
|
|
KeepUpToDate = false,
|
|
FormatDate = 1
|
|
};
|
|
|
|
List<Bar> Bars = Gateway.RequestHistoricalBlocking(HistoricalRequest, TimeSpan.FromSeconds(15));
|
|
|
|
Console.WriteLine();
|
|
Console.WriteLine($"Received {Bars.Count} bars. Continue program here.");
|
|
|
|
// ---- TEST LIVE DATA REQUEST ----
|
|
|
|
var liveRequest = new LiveEquityRequest
|
|
{
|
|
RequestId = 2,
|
|
Symbol = "NVDA"
|
|
};
|
|
|
|
var live = Gateway.RequestLiveEquityBlocking(
|
|
liveRequest,
|
|
TimeSpan.FromSeconds(15));
|
|
|
|
Console.WriteLine();
|
|
Console.WriteLine("=== Live Quote ===");
|
|
Console.WriteLine($"Symbol: {live.Symbol}");
|
|
Console.WriteLine($"Last Price: {live.LastPrice}");
|
|
Console.WriteLine($"Volume: {live.Volume}");
|
|
Console.WriteLine($"Previous Close: {live.PreviousClose}");
|
|
Console.WriteLine($"Open: {live.Open}");
|
|
|
|
|
|
// ---- FUNDAMENTALS DATA REQUEST ----
|
|
// DOES NOT WORK, NEEDS EXTRA DATA SUBSCRIPTION
|
|
/*
|
|
var fundamentalsRequest = new FundamentalsEquityRequest
|
|
{
|
|
RequestId = 3,
|
|
Symbol = "NVDA"
|
|
};
|
|
|
|
try
|
|
{
|
|
var fundamentals = Gateway.RequestFundamentalsBlocking(
|
|
fundamentalsRequest,
|
|
TimeSpan.FromSeconds(15));
|
|
|
|
Console.WriteLine();
|
|
Console.WriteLine("=== Fundamentals ===");
|
|
Console.WriteLine($"Symbol: {fundamentals.Symbol}");
|
|
Console.WriteLine($"Market Cap: {fundamentals.MarketCap}");
|
|
Console.WriteLine($"P/E Ratio: {fundamentals.PERatio}");
|
|
Console.WriteLine($"Dividend Yield: {fundamentals.DividendYield}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Fundamentals request failed.");
|
|
}
|
|
*/
|
|
Gateway.Disconnect();
|
|
}
|
|
}
|
|
} |