ninjatrader/indicators/MarketDepth.cs

385 lines
14 KiB
C#
Raw Normal View History

#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;
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 MarketDepthStream
{
public ConcurrentDictionary<float, int> Asks { get; private set; }
public ConcurrentDictionary<float, int> Bids { get; private set; }
static readonly Dictionary<string, string> MonthCodes = new Dictionary<string, string>
{
{"01", "F"},
{"02", "G"},
{"03", "H"},
{"04", "J"},
{"05", "K"},
{"06", "M"},
{"07", "N"},
{"08", "Q"},
{"09", "U"},
{"10", "V"},
{"11", "X"},
{"12", "Z"}
};
public async Task StreamMarketDepth(Instrument instrument, int maxLevels, string host, int port, CancellationToken cancellationToken)
{
Asks = new ConcurrentDictionary<float, int>();
Bids = new ConcurrentDictionary<float, int>();
string symbol = GetIQFeedSymbol(instrument);
using (TcpClient client = new TcpClient(host, port))
using (NetworkStream stream = client.GetStream())
{
StringBuilder data = new StringBuilder();
while (!cancellationToken.IsCancellationRequested)
{
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");
string command = "WPL," + symbol + ",";
if (maxLevels > 0)
command += maxLevels.ToString();
await SendCommand(stream, command);
continue;
}
ProcessMarketDepthMessage(message.Trim());
TrimMarketDepth(maxLevels);
}
}
}
}
private string GetIQFeedSymbol(Instrument instrument)
{
2024-04-08 14:47:16 +00:00
// Example: ES 06-24
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;
}
private async Task SendCommand(NetworkStream stream, string command)
{
byte[] commandBytes = Encoding.UTF8.GetBytes(command + "\r\n");
await stream.WriteAsync(commandBytes, 0, commandBytes.Length);
}
private 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);
}
}
}
private 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);
}
}
}
}
}
public class MarketDepth : Indicator
{
private MarketDepthStream Stream = new MarketDepthStream();
private CancellationTokenSource StreamCancellation;
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;
IsSuspendedWhileInactive = false;
MaxLevels = 0;
MaxWidth = 500;
BidStroke = new Stroke(Brushes.LimeGreen, DashStyleHelper.Solid, 3);
AskStroke = new Stroke(Brushes.Red, DashStyleHelper.Solid, 3);
Host = "127.0.0.1";
Port = 9200;
}
else if (State == State.DataLoaded)
{
StreamCancellation = new CancellationTokenSource();
Task.Run(() => Stream.StreamMarketDepth(Instrument, MaxLevels, Host, Port, StreamCancellation.Token));
}
else if (State == State.Historical)
{
SetZOrder(-1); // Display behind bars on chart.
}
else if (State == State.Terminated)
{
if (StreamCancellation != null)
{
StreamCancellation.Cancel();
StreamCancellation.Dispose();
}
}
}
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
{
base.OnRender(chartControl, chartScale);
if (Stream.Asks == null || Stream.Bids == null)
return;
if (Stream.Asks.Count() <= 0 || Stream.Bids.Count() <= 0)
return;
// Find the max volume for visually scaling the indicator.
long maxVolume = Math.Max(Stream.Asks.Values.ToList().DefaultIfEmpty(0).Max(),
Stream.Bids.Values.ToList().DefaultIfEmpty(0).Max());
foreach (var Ask in Stream.Asks.ToArray())
{
float price = Ask.Key;
int volume = Ask.Value;
int length = (int)((double)volume / maxVolume * MaxWidth);
int y = chartScale.GetYByValue(price);
DrawHorizontalLine((ChartPanel.X + ChartPanel.W) - length, ChartPanel.X + ChartPanel.W, y, AskStroke);
}
foreach (var Bid in Stream.Bids.ToArray())
{
float price = Bid.Key;
int volume = Bid.Value;
int length = (int)((double)volume / maxVolume * MaxWidth);
int y = chartScale.GetYByValue(price);
DrawHorizontalLine((ChartPanel.X + ChartPanel.W) - length, ChartPanel.X + ChartPanel.W, y, BidStroke);
}
}
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; }
}
[NinjaScriptProperty]
[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")]
public int MaxWidth
{ get; set; }
[NinjaScriptProperty]
[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")]
public string Host { get; set; }
[NinjaScriptProperty]
[Display(Name = "IQFeed Port", Description = "The port on which the IQFeed connection is listening", Order = 6, GroupName = "Market Depth")]
public int Port { get; set; }
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private MarketDepth[] cacheMarketDepth;
public MarketDepth MarketDepth(int maxLevels, int maxWidth, Stroke bidStroke, Stroke askStroke, string host, int port)
{
return MarketDepth(Input, maxLevels, maxWidth, bidStroke, askStroke, host, port);
}
public MarketDepth MarketDepth(ISeries<double> input, int maxLevels, int maxWidth, Stroke bidStroke, Stroke askStroke, string host, int port)
{
if (cacheMarketDepth != null)
for (int idx = 0; idx < cacheMarketDepth.Length; idx++)
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))
return cacheMarketDepth[idx];
return CacheIndicator<MarketDepth>(new MarketDepth(){ MaxLevels = maxLevels, MaxWidth = maxWidth, BidStroke = bidStroke, AskStroke = askStroke, Host = host, Port = port }, input, ref cacheMarketDepth);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.MarketDepth MarketDepth(int maxLevels, int maxWidth, Stroke bidStroke, Stroke askStroke, string host, int port)
{
return indicator.MarketDepth(Input, maxLevels, maxWidth, bidStroke, askStroke, host, port);
}
public Indicators.MarketDepth MarketDepth(ISeries<double> input , int maxLevels, int maxWidth, Stroke bidStroke, Stroke askStroke, string host, int port)
{
return indicator.MarketDepth(input, maxLevels, maxWidth, bidStroke, askStroke, host, port);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.MarketDepth MarketDepth(int maxLevels, int maxWidth, Stroke bidStroke, Stroke askStroke, string host, int port)
{
return indicator.MarketDepth(Input, maxLevels, maxWidth, bidStroke, askStroke, host, port);
}
public Indicators.MarketDepth MarketDepth(ISeries<double> input , int maxLevels, int maxWidth, Stroke bidStroke, Stroke askStroke, string host, int port)
{
return indicator.MarketDepth(input, maxLevels, maxWidth, bidStroke, askStroke, host, port);
}
}
}
#endregion