// Profitabul Levels for NinjaTrader 8 // // Draws your Profitabul key levels (call wall, put wall, zero gamma, max gamma strike, // key gamma level) as horizontal lines on the chart and refreshes them every minute. // The API key is kept in your workspace file and never shown on the chart. // // Install: copy this file to Documents\NinjaTrader 8\bin\Custom\Indicators, // open the NinjaScript Editor and press F5 to compile, then add // "Profitabul Levels" to a chart. Paste an API key from Profitabul, // Settings > Integrations (a read-only key is enough) into the indicator's // API key setting. // // Uses GET https://compute.profitabul.ai/v1/plugin/levels?symbol=, // which answers one "label,price" line per level. #region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Globalization; using System.Net.Http; using System.Threading.Tasks; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Gui; using NinjaTrader.NinjaScript; using NinjaTrader.NinjaScript.DrawingTools; #endregion namespace NinjaTrader.NinjaScript.Indicators { public class ProfitabulLevels : Indicator { private static readonly HttpClient Http = new HttpClient { Timeout = TimeSpan.FromSeconds(15) }; private readonly object sync = new object(); private List> levels = new List>(); private bool levelsChanged; private DateTime lastFetchUtc = DateTime.MinValue; private bool fetching; private string lastError; public override string DisplayName { get { return "Profitabul Levels"; } } protected override void OnStateChange() { if (State == State.SetDefaults) { Name = "Profitabul Levels"; Description = "Profitabul key levels: call wall, put wall, zero gamma, max gamma strike and key gamma level."; Calculate = Calculate.OnPriceChange; IsOverlay = true; DisplayInDataBox = false; DrawOnPricePanel = true; IsSuspendedWhileInactive = true; ApiKey = string.Empty; Symbol = string.Empty; RefreshSeconds = 60; ApiBaseUrl = "https://compute.profitabul.ai"; CallWallBrush = Brushes.LimeGreen; PutWallBrush = Brushes.Red; OtherBrush = Brushes.Gold; } } protected override void OnBarUpdate() { if (State != State.Realtime && CurrentBar < Count - 2) return; if (!fetching && (DateTime.UtcNow - lastFetchUtc).TotalSeconds >= Math.Max(30, RefreshSeconds)) { fetching = true; lastFetchUtc = DateTime.UtcNow; Task.Run(FetchAsync); } List> snapshot = null; lock (sync) { if (levelsChanged) { snapshot = new List>(levels); levelsChanged = false; } } if (snapshot == null) return; RemoveDrawObjects(); foreach (var level in snapshot) { Brush brush = level.Key == "Call wall" ? CallWallBrush : level.Key == "Put wall" ? PutWallBrush : OtherBrush; string tag = "Profitabul " + level.Key; Draw.HorizontalLine(this, tag, level.Value, brush, DashStyleHelper.Dash, 2); Draw.Text(this, tag + " label", false, level.Key + " " + level.Value.ToString("0.##", CultureInfo.InvariantCulture), 0, level.Value, 8, brush, new SimpleFont("Arial", 11), System.Windows.TextAlignment.Right, Brushes.Transparent, Brushes.Transparent, 0); } } private string RootSymbol() { if (!string.IsNullOrWhiteSpace(Symbol)) return Symbol.Trim().ToUpperInvariant(); // "NQ 12-26" -> "NQ"; micros map to their full-size root. string root = Instrument.MasterInstrument.Name.ToUpperInvariant(); if (root == "MNQ") return "NQ"; if (root == "MES") return "ES"; if (root == "M2K") return "RTY"; if (root == "MGC") return "GC"; return root; } private async Task FetchAsync() { try { if (string.IsNullOrWhiteSpace(ApiKey)) { lastError = "Add your Profitabul API key in the indicator settings."; return; } var url = ApiBaseUrl.TrimEnd('/') + "/v1/plugin/levels?symbol=" + Uri.EscapeDataString(RootSymbol()); using (var request = new HttpRequestMessage(HttpMethod.Get, url)) { request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + ApiKey.Trim()); request.Headers.TryAddWithoutValidation("User-Agent", "Profitabul-NinjaTrader/1.0"); using (var response = await Http.SendAsync(request).ConfigureAwait(false)) { string body = await response.Content.ReadAsStringAsync().ConfigureAwait(false); if (!response.IsSuccessStatusCode) { lastError = "Profitabul answered " + (int)response.StatusCode; return; } var parsed = new List>(); foreach (var raw in body.Split('\n')) { string line = raw.Trim(); if (line.Length == 0 || line.StartsWith("#")) continue; int comma = line.LastIndexOf(','); double price; if (comma > 0 && double.TryParse(line.Substring(comma + 1), NumberStyles.Float, CultureInfo.InvariantCulture, out price)) parsed.Add(new KeyValuePair(line.Substring(0, comma), price)); } lock (sync) { levels = parsed; levelsChanged = true; } lastError = null; } } } catch (Exception ex) { lastError = ex.Message; } finally { fetching = false; if (lastError != null) Print("Profitabul Levels: " + lastError); } } #region Properties [Display(Name = "API key", Description = "From Profitabul, Settings > Integrations. A read-only key is enough.", Order = 1, GroupName = "Profitabul")] public string ApiKey { get; set; } [Display(Name = "Symbol override", Description = "Leave empty to use the chart's instrument (ES, NQ, RTY, GC, SPX, QQQ ...).", Order = 2, GroupName = "Profitabul")] public string Symbol { get; set; } [Range(30, 3600)] [Display(Name = "Refresh (seconds)", Order = 3, GroupName = "Profitabul")] public int RefreshSeconds { get; set; } [Browsable(false)] public string ApiBaseUrl { get; set; } [XmlIgnoreAttribute] [Display(Name = "Call wall color", Order = 4, GroupName = "Colors")] public Brush CallWallBrush { get; set; } [Browsable(false)] public string CallWallBrushSerializable { get { return Serialize.BrushToString(CallWallBrush); } set { CallWallBrush = Serialize.StringToBrush(value); } } [XmlIgnoreAttribute] [Display(Name = "Put wall color", Order = 5, GroupName = "Colors")] public Brush PutWallBrush { get; set; } [Browsable(false)] public string PutWallBrushSerializable { get { return Serialize.BrushToString(PutWallBrush); } set { PutWallBrush = Serialize.StringToBrush(value); } } [XmlIgnoreAttribute] [Display(Name = "Other levels color", Order = 6, GroupName = "Colors")] public Brush OtherBrush { get; set; } [Browsable(false)] public string OtherBrushSerializable { get { return Serialize.BrushToString(OtherBrush); } set { OtherBrush = Serialize.StringToBrush(value); } } #endregion } }