I have web socket endpoint and want to initialize and get it called continuously after every time when data is available from api.
I have created consciously running azure web job but not sure how/where I need to add web socket ticker service
code which will run continuously and get data from api.
Program.cs
class Program
{
static void Main()
{
var config = new JobHostConfiguration();
if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}
var host = new JobHost(config);
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
}
TickerService.cs
class TickerService
{
private Ticker _ticker; // THIS OBJECT YOU WILL GET IT FROM KITECONNECT
private string _myAccessToken;
private UInt32[] intrument_tokent = new UInt32[] { 256265 };
public TickerService(string myAccessToken)
{
this._myAccessToken = myAccessToken; // THIS TOKEN IS ONE WHICH WE GET AFTER LOGIN.
initTicker();
}
private void initTicker()
{
_ticker = new Ticker("apikey", this._myAccessToken);
_ticker.OnTick += OnTick;
_ticker.OnReconnect += OnReconnect;
_ticker.OnNoReconnect += OnNoReconnect;
_ticker.OnError += OnError;
_ticker.OnClose += OnClose;
_ticker.OnConnect += OnConnect;
_ticker.OnOrderUpdate += OnOrderUpdate;
_ticker.EnableReconnect(Interval: 5, Retries: 50);
//init and connect to web socket api
_ticker.Connect();
// Subscribing to BANKNIFTY and setting mode to FULL MODE
_ticker.Subscribe(Tokens: intrument_tokent); // <- THIS METHOD USED FOR SUBSCRIPTION
_ticker.SetMode(Tokens: intrument_tokent, Mode: Constants.MODE_FULL); // <- THIS METHOD USED FOR SETTING MODE
}
private void OnTokenExpire()
{
Console.WriteLine("Need to login again");
}
private void OnConnect()
{
Console.WriteLine("Connected ticker");
}
private void OnClose()
{
Console.WriteLine("Closed ticker");
}
private void OnError(string Message)
{
Console.WriteLine("Error: " + Message);
}
private void OnNoReconnect()
{
Console.WriteLine("Not reconnecting");
}
private void OnReconnect()
{
Console.WriteLine("Reconnecting");
}
private void OnTick(Tick TickData)
{
ProcessTick(TickData);
}
private void OnOrderUpdate(Order OrderData)
{
Console.WriteLine("OrderUpdate " + Utils.JsonSerialize(OrderData));
}
//Method wch listen data , here you will get LTP
private void ProcessTick(Tick tickData)
{
try
{
var candleTickData = new List<CandleTickData>();
var tickDataHis = new CandleTickData()
{
InstrumentID = tickData.InstrumentToken,
Close = tickData.LastPrice,
High = tickData.LastPrice,
Low = tickData.LastPrice,
Open = tickData.LastPrice,
TimeStamp = tickData.Timestamp.HasValue ? tickData.Timestamp.Value : DateTime.Now,
Volume = tickData.Volume
};
candleTickData.Add(tickDataHis);
// TODO:->Logic here to generate and process 5 min candle
}
catch (Exception ex)
{
}
}
}
class CandleTickData
{
public uint InstrumentID { get; set; }
public decimal Close { get; set; }
public decimal High { get; set; }
public decimal Low { get; set; }
public decimal Open { get; set; }
public DateTime TimeStamp { get; set; }
public uint Volume { get; set; }
}
Question - what is the correct way to implement TickerService.cs into Program.cs
In the console slave program, the program runs from top to bottom, so you only need to define TickerService once.
Like:
class Program
{
static void Main()
{
var config = new JobHostConfiguration();
if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}
var host = new JobHost(config);
// The following code ensures that the WebJob will be running continuously
TickerService _tickerservice=new TickerService();
while(true){
// The specific judgment conditions are based on actual scenarios.
if(_tickerservice._myAccessToken==null){
_tickerservice=new TickerService();
}
}
host.RunAndBlock();
}
}
We can't find Ticker
package, therefore, only methods for general scenarios are provided for reference only.