267 lines
8.9 KiB
C#
267 lines
8.9 KiB
C#
#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>();
|
|
static string symbol = "@NQM24";
|
|
|
|
public static async Task StreamMarketDepth()
|
|
{
|
|
using (TcpClient client = new TcpClient("127.0.0.1", 9200))
|
|
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");
|
|
await SendCommand(stream, "WPL," + symbol + ",200");
|
|
continue;
|
|
}
|
|
|
|
ProcessMarketDepthMessage(message.Trim());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
IsSuspendedWhileInactive = true;
|
|
}
|
|
else if (State == State.Configure)
|
|
{
|
|
}
|
|
else if (State == State.DataLoaded)
|
|
{
|
|
Task.Run(() => MarketDepthProcessor.StreamMarketDepth());
|
|
}
|
|
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;
|
|
|
|
// Find the max volume for visually scaling the indicator.
|
|
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;
|
|
|
|
int length = (int)((double)volume / maxVolume * 500);
|
|
|
|
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;
|
|
|
|
int length = (int)((double)volume / maxVolume * 500);
|
|
|
|
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; }
|
|
}
|
|
}
|
|
}
|
|
|
|
#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()
|
|
{
|
|
return MarketDepth(Input);
|
|
}
|
|
|
|
public MarketDepth MarketDepth(ISeries<double> input)
|
|
{
|
|
if (cacheMarketDepth != null)
|
|
for (int idx = 0; idx < cacheMarketDepth.Length; idx++)
|
|
if (cacheMarketDepth[idx] != null && cacheMarketDepth[idx].EqualsInput(input))
|
|
return cacheMarketDepth[idx];
|
|
return CacheIndicator<MarketDepth>(new MarketDepth(), input, ref cacheMarketDepth);
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
|
|
{
|
|
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
|
|
{
|
|
public Indicators.MarketDepth MarketDepth()
|
|
{
|
|
return indicator.MarketDepth(Input);
|
|
}
|
|
|
|
public Indicators.MarketDepth MarketDepth(ISeries<double> input )
|
|
{
|
|
return indicator.MarketDepth(input);
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace NinjaTrader.NinjaScript.Strategies
|
|
{
|
|
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
|
|
{
|
|
public Indicators.MarketDepth MarketDepth()
|
|
{
|
|
return indicator.MarketDepth(Input);
|
|
}
|
|
|
|
public Indicators.MarketDepth MarketDepth(ISeries<double> input )
|
|
{
|
|
return indicator.MarketDepth(input);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|