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;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
class MarketDepthProcessor
|
|
|
|
{
|
|
|
|
public static ConcurrentDictionary<float, int> Asks = new ConcurrentDictionary<float, int>();
|
|
|
|
public static ConcurrentDictionary<float, int> Bids = new ConcurrentDictionary<float, int>();
|
|
|
|
|
2024-04-08 14:45:22 +00:00
|
|
|
static 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-20 14:05:59 +00:00
|
|
|
public static async Task StreamMarketDepth(Instrument instrument, int maxLevels, string host, int port)
|
2024-04-08 14:45:22 +00:00
|
|
|
{
|
2024-04-18 14:39:20 +00:00
|
|
|
Asks.Clear();
|
|
|
|
Bids.Clear();
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
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-08 14:45:22 +00:00
|
|
|
static string GetIQFeedSymbol(Instrument instrument)
|
|
|
|
{
|
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-06 13:35:52 +00:00
|
|
|
static async Task SendCommand(NetworkStream stream, string command)
|
|
|
|
{
|
|
|
|
byte[] commandBytes = Encoding.UTF8.GetBytes(command + "\r\n");
|
|
|
|
await stream.WriteAsync(commandBytes, 0, commandBytes.Length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ProcessMarketDepthMessage(string message)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
static void TrimMarketDepth(int maxLevels)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
|
|
|
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-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.Configure)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (State == State.DataLoaded)
|
|
|
|
{
|
2024-04-20 14:05:59 +00:00
|
|
|
Task.Run(() => MarketDepthProcessor.StreamMarketDepth(Instrument, MaxLevels, Host, Port));
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
else if (State == State.Historical)
|
|
|
|
{
|
|
|
|
SetZOrder(-1); // Display behind bars on chart.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
|
|
|
|
{
|
|
|
|
base.OnRender(chartControl, chartScale);
|
|
|
|
|
|
|
|
if (MarketDepthProcessor.Asks.Count() <= 0 || MarketDepthProcessor.Bids.Count() <= 0)
|
|
|
|
return;
|
|
|
|
|
2024-04-07 13:32:44 +00:00
|
|
|
// Find the max volume for visually scaling the indicator.
|
2024-04-06 13:35:52 +00:00
|
|
|
long maxVolume = Math.Max(MarketDepthProcessor.Asks.Values.ToList().DefaultIfEmpty(0).Max(),
|
|
|
|
MarketDepthProcessor.Bids.Values.ToList().DefaultIfEmpty(0).Max());
|
|
|
|
|
|
|
|
foreach (var Ask in MarketDepthProcessor.Asks.ToArray())
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
DrawHorizontalLine((ChartPanel.X + ChartPanel.W) - length, ChartPanel.X + ChartPanel.W, y, new Stroke(Brushes.Red, DashStyleHelper.Solid, 3));
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var Bid in MarketDepthProcessor.Bids.ToArray())
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
DrawHorizontalLine((ChartPanel.X + ChartPanel.W) - length, ChartPanel.X + ChartPanel.W, y, new Stroke(Brushes.LimeGreen, DashStyleHelper.Solid, 3));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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]
|
|
|
|
[Display(Name = "IQFeed Host", Description = "The host on which the IQFeed connection is running", Order = 3, GroupName = "Market Depth")]
|
|
|
|
public string Host { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "IQFeed Port", Description = "The port on which the IQFeed connection is listening", Order = 4, GroupName = "Market Depth")]
|
|
|
|
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-20 14:05:59 +00:00
|
|
|
public MarketDepth MarketDepth(int maxLevels, int maxWidth, string host, int port)
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
2024-04-20 14:05:59 +00:00
|
|
|
return MarketDepth(Input, maxLevels, maxWidth, host, port);
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
|
2024-04-20 14:05:59 +00:00
|
|
|
public MarketDepth MarketDepth(ISeries<double> input, int maxLevels, int maxWidth, 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-20 14:05:59 +00:00
|
|
|
if (cacheMarketDepth[idx] != null && cacheMarketDepth[idx].MaxLevels == maxLevels && cacheMarketDepth[idx].MaxWidth == maxWidth && 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-20 14:05:59 +00:00
|
|
|
return CacheIndicator<MarketDepth>(new MarketDepth(){ MaxLevels = maxLevels, MaxWidth = maxWidth, 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-20 14:05:59 +00:00
|
|
|
public Indicators.MarketDepth MarketDepth(int maxLevels, int maxWidth, string host, int port)
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
2024-04-20 14:05:59 +00:00
|
|
|
return indicator.MarketDepth(Input, maxLevels, maxWidth, host, port);
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
|
2024-04-20 14:05:59 +00:00
|
|
|
public Indicators.MarketDepth MarketDepth(ISeries<double> input , int maxLevels, int maxWidth, string host, int port)
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
2024-04-20 14:05:59 +00:00
|
|
|
return indicator.MarketDepth(input, maxLevels, maxWidth, host, port);
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.Strategies
|
|
|
|
{
|
|
|
|
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
|
|
|
|
{
|
2024-04-20 14:05:59 +00:00
|
|
|
public Indicators.MarketDepth MarketDepth(int maxLevels, int maxWidth, string host, int port)
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
2024-04-20 14:05:59 +00:00
|
|
|
return indicator.MarketDepth(Input, maxLevels, maxWidth, host, port);
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
|
2024-04-20 14:05:59 +00:00
|
|
|
public Indicators.MarketDepth MarketDepth(ISeries<double> input , int maxLevels, int maxWidth, string host, int port)
|
2024-04-06 13:35:52 +00:00
|
|
|
{
|
2024-04-20 14:05:59 +00:00
|
|
|
return indicator.MarketDepth(input, maxLevels, maxWidth, host, port);
|
2024-04-06 13:35:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|