2024-04-06 13:35:52 +00:00
|
|
|
#region Using declarations
|
|
|
|
using System;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel;
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net.Sockets;
|
|
|
|
using System.Text;
|
2024-04-23 10:39:09 +00:00
|
|
|
using System.Threading;
|
2024-04-06 13:35:52 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Windows;
|
|
|
|
using System.Windows.Input;
|
|
|
|
using System.Windows.Media;
|
|
|
|
using System.Xml.Serialization;
|
|
|
|
using NinjaTrader.Cbi;
|
|
|
|
using NinjaTrader.Gui;
|
|
|
|
using NinjaTrader.Gui.Chart;
|
|
|
|
using NinjaTrader.Gui.SuperDom;
|
|
|
|
using NinjaTrader.Gui.Tools;
|
|
|
|
using NinjaTrader.Data;
|
|
|
|
using NinjaTrader.NinjaScript;
|
|
|
|
using NinjaTrader.Core.FloatingPoint;
|
|
|
|
using NinjaTrader.NinjaScript.DrawingTools;
|
|
|
|
using NinjaTrader.Core;
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
//This namespace holds Indicators in this folder and is required. Do not change it.
|
|
|
|
namespace NinjaTrader.NinjaScript.Indicators
|
|
|
|
{
|
2024-04-23 10:39:09 +00:00
|
|
|
class MarketDepthStream
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
2024-04-23 10:39:09 +00:00
|
|
|
public ConcurrentDictionary<float, int> Asks { get; private set; }
|
|
|
|
public ConcurrentDictionary<float, int> Bids { get; private set; }
|
2024-04-06 13:35:52 +00:00
|
|
|
|
2024-04-23 10:39:09 +00:00
|
|
|
static readonly Dictionary<string, string> MonthCodes = new Dictionary<string, string>
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
2024-04-08 14:45:22 +00:00
|
|
|
{"01", "F"},
|
|
|
|
{"02", "G"},
|
|
|
|
{"03", "H"},
|
|
|
|
{"04", "J"},
|
|
|
|
{"05", "K"},
|
|
|
|
{"06", "M"},
|
|
|
|
{"07", "N"},
|
|
|
|
{"08", "Q"},
|
|
|
|
{"09", "U"},
|
|
|
|
{"10", "V"},
|
|
|
|
{"11", "X"},
|
|
|
|
{"12", "Z"}
|
|
|
|
};
|
|
|
|
|
2024-04-23 10:39:09 +00:00
|
|
|
public async Task StreamMarketDepth(Instrument instrument, int maxLevels, string host, int port, CancellationToken cancellationToken)
|
2024-04-08 14:45:22 +00:00
|
|
|
{
|
2024-04-23 10:39:09 +00:00
|
|
|
Asks = new ConcurrentDictionary<float, int>();
|
|
|
|
Bids = new ConcurrentDictionary<float, int>();
|
|
|
|
|
2024-04-08 14:45:22 +00:00
|
|
|
string symbol = GetIQFeedSymbol(instrument);
|
|
|
|
|
2024-04-20 14:05:59 +00:00
|
|
|
using (TcpClient client = new TcpClient(host, port))
|
2024-04-06 13:35:52 +00:00
|
|
|
using (NetworkStream stream = client.GetStream())
|
|
|
|
{
|
|
|
|
StringBuilder data = new StringBuilder();
|
|
|
|
|
2024-04-23 10:39:09 +00:00
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
|
|
|
byte[] buffer = new byte[4096];
|
|
|
|
int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
|
|
|
|
data.Append(Encoding.UTF8.GetString(buffer, 0, bytesRead));
|
|
|
|
|
|
|
|
string[] messages = data.ToString().Split('\n');
|
|
|
|
|
|
|
|
if (data.ToString().EndsWith("\n"))
|
|
|
|
{
|
|
|
|
data.Clear();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data.Clear();
|
|
|
|
data.Append(messages[messages.Length - 1]);
|
|
|
|
Array.Resize(ref messages, messages.Length - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var message in messages)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(message))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.Contains("S,SERVER CONNECTED"))
|
|
|
|
{
|
|
|
|
await SendCommand(stream, "S,SET PROTOCOL,6.2");
|
2024-04-11 01:50:38 +00:00
|
|
|
string command = "WPL," + symbol + ",";
|
|
|
|
if (maxLevels > 0)
|
|
|
|
command += maxLevels.ToString();
|
|
|
|
await SendCommand(stream, command);
|
2024-04-06 13:35:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessMarketDepthMessage(message.Trim());
|
2024-04-23 09:16:42 +00:00
|
|
|
TrimMarketDepth(maxLevels);
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 10:39:09 +00:00
|
|
|
private string GetIQFeedSymbol(Instrument instrument)
|
2024-04-08 14:45:22 +00:00
|
|
|
{
|
2024-04-08 14:47:16 +00:00
|
|
|
// Example: ES 06-24
|
2024-04-08 14:45:22 +00:00
|
|
|
string masterInstrument = instrument.MasterInstrument.Name;
|
|
|
|
string monthYear = instrument.FullName.Split(' ')[1];
|
|
|
|
|
|
|
|
string month = monthYear.Substring(0, 2);
|
|
|
|
string year = monthYear.Substring(3, 2);
|
|
|
|
|
|
|
|
string monthCode = MonthCodes[month];
|
|
|
|
|
|
|
|
return "@" + masterInstrument + monthCode + year;
|
|
|
|
}
|
|
|
|
|
2024-04-23 10:39:09 +00:00
|
|
|
private async Task SendCommand(NetworkStream stream, string command)
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
|
|
|
byte[] commandBytes = Encoding.UTF8.GetBytes(command + "\r\n");
|
|
|
|
await stream.WriteAsync(commandBytes, 0, commandBytes.Length);
|
|
|
|
}
|
|
|
|
|
2024-04-23 10:39:09 +00:00
|
|
|
private void ProcessMarketDepthMessage(string message)
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
|
|
|
string[] fields = message.Split(',');
|
|
|
|
|
|
|
|
if (fields[0] == "7" || fields[0] == "8")
|
|
|
|
{ // Price Update and Price Summary Messages
|
|
|
|
string symbol = fields[1];
|
|
|
|
string side = fields[2];
|
|
|
|
float price = float.Parse(fields[3]);
|
|
|
|
int levelSize = int.Parse(fields[4]);
|
|
|
|
|
|
|
|
if (side.ToUpper() == "B")
|
|
|
|
{
|
|
|
|
Bids.AddOrUpdate(price, levelSize, (p, oldValue) => levelSize);
|
|
|
|
}
|
|
|
|
else if (side.ToUpper() == "A")
|
|
|
|
{
|
|
|
|
Asks.AddOrUpdate(price, levelSize, (p, oldValue) => levelSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fields[0] == "9")
|
|
|
|
{ // Price Delete Messages
|
|
|
|
string side = fields[2];
|
|
|
|
float price = float.Parse(fields[3]);
|
|
|
|
if (side.ToUpper() == "B")
|
|
|
|
{
|
|
|
|
int levelSize;
|
|
|
|
Bids.TryRemove(price, out levelSize);
|
|
|
|
}
|
|
|
|
else if (side.ToUpper() == "A")
|
|
|
|
{
|
|
|
|
int levelSize;
|
|
|
|
Asks.TryRemove(price, out levelSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-23 09:16:42 +00:00
|
|
|
|
2024-04-23 10:39:09 +00:00
|
|
|
private void TrimMarketDepth(int maxLevels)
|
2024-04-23 09:16:42 +00:00
|
|
|
{
|
|
|
|
if (maxLevels <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Asks.Count > maxLevels)
|
|
|
|
{
|
|
|
|
var sortedLevels = Asks.Keys.OrderBy(x => x).ToList();
|
|
|
|
if (sortedLevels.Count > maxLevels)
|
|
|
|
{
|
|
|
|
// Remove the highest asks.
|
|
|
|
var levelsToRemove = sortedLevels.Skip(maxLevels).ToList();
|
|
|
|
foreach (var level in levelsToRemove)
|
|
|
|
{
|
|
|
|
int value;
|
|
|
|
Asks.TryRemove(level, out value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Bids.Count > maxLevels)
|
|
|
|
{
|
|
|
|
var sortedLevels = Bids.Keys.OrderBy(x => x).ToList();
|
|
|
|
if (sortedLevels.Count > maxLevels)
|
|
|
|
{
|
|
|
|
// Remove the lowest bids.
|
|
|
|
var levelsToRemove = sortedLevels.Take(sortedLevels.Count - maxLevels).ToList();
|
|
|
|
foreach (var level in levelsToRemove)
|
|
|
|
{
|
|
|
|
int value;
|
|
|
|
Bids.TryRemove(level, out value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class MarketDepth : Indicator
|
|
|
|
{
|
2024-04-23 10:39:09 +00:00
|
|
|
private MarketDepthStream Stream = new MarketDepthStream();
|
|
|
|
private CancellationTokenSource StreamCancellation;
|
|
|
|
|
2024-04-06 13:35:52 +00:00
|
|
|
protected override void OnStateChange()
|
|
|
|
{
|
|
|
|
if (State == State.SetDefaults)
|
|
|
|
{
|
|
|
|
Description = @"";
|
|
|
|
Name = "Market Depth";
|
|
|
|
Calculate = Calculate.OnEachTick;
|
|
|
|
IsOverlay = true;
|
|
|
|
DisplayInDataBox = true;
|
|
|
|
DrawOnPricePanel = true;
|
|
|
|
DrawHorizontalGridLines = false;
|
|
|
|
DrawVerticalGridLines = false;
|
|
|
|
PaintPriceMarkers = false;
|
|
|
|
ScaleJustification = ScaleJustification.Right;
|
2024-04-09 15:08:11 +00:00
|
|
|
IsSuspendedWhileInactive = false;
|
2024-04-11 01:50:38 +00:00
|
|
|
MaxLevels = 0;
|
2024-04-09 15:08:11 +00:00
|
|
|
MaxWidth = 500;
|
2024-04-23 10:39:09 +00:00
|
|
|
BidStroke = new Stroke(Brushes.LimeGreen, DashStyleHelper.Solid, 3);
|
|
|
|
AskStroke = new Stroke(Brushes.Red, DashStyleHelper.Solid, 3);
|
2024-04-20 14:05:59 +00:00
|
|
|
Host = "127.0.0.1";
|
|
|
|
Port = 9200;
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
else if (State == State.DataLoaded)
|
|
|
|
{
|
2024-04-23 10:39:09 +00:00
|
|
|
StreamCancellation = new CancellationTokenSource();
|
|
|
|
Task.Run(() => Stream.StreamMarketDepth(Instrument, MaxLevels, Host, Port, StreamCancellation.Token));
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
else if (State == State.Historical)
|
|
|
|
{
|
|
|
|
SetZOrder(-1); // Display behind bars on chart.
|
|
|
|
}
|
2024-04-23 10:39:09 +00:00
|
|
|
else if (State == State.Terminated)
|
|
|
|
{
|
|
|
|
if (StreamCancellation != null)
|
|
|
|
{
|
|
|
|
StreamCancellation.Cancel();
|
|
|
|
StreamCancellation.Dispose();
|
|
|
|
}
|
|
|
|
}
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
|
|
|
|
{
|
|
|
|
base.OnRender(chartControl, chartScale);
|
|
|
|
|
2024-04-23 10:39:09 +00:00
|
|
|
if (Stream.Asks == null || Stream.Bids == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Stream.Asks.Count() <= 0 || Stream.Bids.Count() <= 0)
|
2024-04-06 13:35:52 +00:00
|
|
|
return;
|
|
|
|
|
2024-04-07 13:32:44 +00:00
|
|
|
// Find the max volume for visually scaling the indicator.
|
2024-04-23 10:39:09 +00:00
|
|
|
long maxVolume = Math.Max(Stream.Asks.Values.ToList().DefaultIfEmpty(0).Max(),
|
|
|
|
Stream.Bids.Values.ToList().DefaultIfEmpty(0).Max());
|
2024-04-06 13:35:52 +00:00
|
|
|
|
2024-04-23 10:39:09 +00:00
|
|
|
foreach (var Ask in Stream.Asks.ToArray())
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
|
|
|
float price = Ask.Key;
|
|
|
|
int volume = Ask.Value;
|
|
|
|
|
2024-04-09 15:08:11 +00:00
|
|
|
int length = (int)((double)volume / maxVolume * MaxWidth);
|
2024-04-06 13:35:52 +00:00
|
|
|
|
|
|
|
int y = chartScale.GetYByValue(price);
|
2024-04-23 10:39:09 +00:00
|
|
|
DrawHorizontalLine((ChartPanel.X + ChartPanel.W) - length, ChartPanel.X + ChartPanel.W, y, AskStroke);
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 10:39:09 +00:00
|
|
|
foreach (var Bid in Stream.Bids.ToArray())
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
|
|
|
float price = Bid.Key;
|
|
|
|
int volume = Bid.Value;
|
|
|
|
|
2024-04-09 15:08:11 +00:00
|
|
|
int length = (int)((double)volume / maxVolume * MaxWidth);
|
2024-04-06 13:35:52 +00:00
|
|
|
|
|
|
|
int y = chartScale.GetYByValue(price);
|
2024-04-23 10:39:09 +00:00
|
|
|
DrawHorizontalLine((ChartPanel.X + ChartPanel.W) - length, ChartPanel.X + ChartPanel.W, y, BidStroke);
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnBarUpdate() {}
|
|
|
|
|
|
|
|
private void DrawHorizontalLine(int startX, int endX, int y, Stroke stroke)
|
|
|
|
{
|
|
|
|
SharpDX.Direct2D1.Brush dxBrush = stroke.Brush.ToDxBrush(RenderTarget);
|
|
|
|
RenderTarget.DrawLine(
|
|
|
|
new SharpDX.Vector2(startX, y),
|
|
|
|
new SharpDX.Vector2(endX, y),
|
|
|
|
dxBrush, stroke.Width, stroke.StrokeStyle
|
|
|
|
);
|
|
|
|
dxBrush.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string DisplayName
|
|
|
|
{
|
|
|
|
get { return Name; }
|
|
|
|
}
|
2024-04-09 15:08:11 +00:00
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
2024-04-11 01:50:38 +00:00
|
|
|
[Display(Name = "Max Levels", Description = "The maximum number of levels to display (0 for unlimited)", Order = 1, GroupName = "Market Depth")]
|
|
|
|
public int MaxLevels { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "Max Width", Description = "The maximum width of the market depth levels in pixels", Order = 2, GroupName = "Market Depth")]
|
2024-04-09 15:08:11 +00:00
|
|
|
public int MaxWidth
|
|
|
|
{ get; set; }
|
2024-04-20 14:05:59 +00:00
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
2024-04-23 10:39:09 +00:00
|
|
|
[Display(Name = "Bid Level", Description = "Color and style of bid levels", Order = 3, GroupName = "Market Depth")]
|
|
|
|
public Stroke BidStroke { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "Ask Level", Description = "Color and style of ask levels", Order = 4, GroupName = "Market Depth")]
|
|
|
|
public Stroke AskStroke { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "IQFeed Host", Description = "The host on which the IQFeed connection is running", Order = 5, GroupName = "Market Depth")]
|
2024-04-20 14:05:59 +00:00
|
|
|
public string Host { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
2024-04-23 10:39:09 +00:00
|
|
|
[Display(Name = "IQFeed Port", Description = "The port on which the IQFeed connection is listening", Order = 6, GroupName = "Market Depth")]
|
2024-04-20 14:05:59 +00:00
|
|
|
public int Port { get; set; }
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#region NinjaScript generated code. Neither change nor remove.
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.Indicators
|
|
|
|
{
|
|
|
|
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
|
|
|
|
{
|
|
|
|
private MarketDepth[] cacheMarketDepth;
|
2024-04-23 10:39:09 +00:00
|
|
|
public MarketDepth MarketDepth(int maxLevels, int maxWidth, Stroke bidStroke, Stroke askStroke, string host, int port)
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
2024-04-23 10:39:09 +00:00
|
|
|
return MarketDepth(Input, maxLevels, maxWidth, bidStroke, askStroke, host, port);
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 10:39:09 +00:00
|
|
|
public MarketDepth MarketDepth(ISeries<double> input, int maxLevels, int maxWidth, Stroke bidStroke, Stroke askStroke, string host, int port)
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
|
|
|
if (cacheMarketDepth != null)
|
|
|
|
for (int idx = 0; idx < cacheMarketDepth.Length; idx++)
|
2024-04-23 10:39:09 +00:00
|
|
|
if (cacheMarketDepth[idx] != null && cacheMarketDepth[idx].MaxLevels == maxLevels && cacheMarketDepth[idx].MaxWidth == maxWidth && cacheMarketDepth[idx].BidStroke == bidStroke && cacheMarketDepth[idx].AskStroke == askStroke && cacheMarketDepth[idx].Host == host && cacheMarketDepth[idx].Port == port && cacheMarketDepth[idx].EqualsInput(input))
|
2024-04-06 13:35:52 +00:00
|
|
|
return cacheMarketDepth[idx];
|
2024-04-23 10:39:09 +00:00
|
|
|
return CacheIndicator<MarketDepth>(new MarketDepth(){ MaxLevels = maxLevels, MaxWidth = maxWidth, BidStroke = bidStroke, AskStroke = askStroke, Host = host, Port = port }, input, ref cacheMarketDepth);
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
|
|
|
|
{
|
|
|
|
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
|
|
|
|
{
|
2024-04-23 10:39:09 +00:00
|
|
|
public Indicators.MarketDepth MarketDepth(int maxLevels, int maxWidth, Stroke bidStroke, Stroke askStroke, string host, int port)
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
2024-04-23 10:39:09 +00:00
|
|
|
return indicator.MarketDepth(Input, maxLevels, maxWidth, bidStroke, askStroke, host, port);
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 10:39:09 +00:00
|
|
|
public Indicators.MarketDepth MarketDepth(ISeries<double> input , int maxLevels, int maxWidth, Stroke bidStroke, Stroke askStroke, string host, int port)
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
2024-04-23 10:39:09 +00:00
|
|
|
return indicator.MarketDepth(input, maxLevels, maxWidth, bidStroke, askStroke, host, port);
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.Strategies
|
|
|
|
{
|
|
|
|
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
|
|
|
|
{
|
2024-04-23 10:39:09 +00:00
|
|
|
public Indicators.MarketDepth MarketDepth(int maxLevels, int maxWidth, Stroke bidStroke, Stroke askStroke, string host, int port)
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
2024-04-23 10:39:09 +00:00
|
|
|
return indicator.MarketDepth(Input, maxLevels, maxWidth, bidStroke, askStroke, host, port);
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 10:39:09 +00:00
|
|
|
public Indicators.MarketDepth MarketDepth(ISeries<double> input , int maxLevels, int maxWidth, Stroke bidStroke, Stroke askStroke, string host, int port)
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
2024-04-23 10:39:09 +00:00
|
|
|
return indicator.MarketDepth(input, maxLevels, maxWidth, bidStroke, askStroke, host, port);
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|