Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Problem with VAH - VAL - POC

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Problem with VAH - VAL - POC

    Hello, I would like to create an indicator for the VAH, VAL and the POC. However, nothing is displayed in the chart. Unfortunately, I don't know whether this is due to the calculation or the plotting. I would be grateful if someone could help me.

    Code:
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Reflection;
    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.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;
    using NinjaTrader.Gui.Tools;
    #endregion
    
    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class myIndicator : Indicator
    {
    
    
    
    
    
    //--- VAH, VAL, POC -----------------------------------------------------------------
    private Dictionary<double, double> volumeByPrice = new Dictionary<double, double>();
    private double pocPrice = 0;
    private double vahPrice = 0;
    private double valPrice = 0;
    
    
    
    
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"";
    Name = "VAH VAL POC";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    IsAutoScale = false;
    IsSuspendedWhileInactive = false;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    
    
    
    
    //---Plots für VAH, VAL, POC--------------------------------------------------------------------
    
    AddPlot(Brushes.Orange, "POC");
    AddPlot(Brushes.Blue, "VAH");
    AddPlot(Brushes.Blue, "VAL");
    
    //----------------------------------------------------------------------------------------------
    
    
    }
    
    
    else if (State == State.DataLoaded)
    
    {
    
    }
    
    }
    
    
    protected override void OnBarUpdate()
    {
    
    // Ignoriere historische Daten
    if (Bars.IsFirstBarOfSession && CurrentBar > 0)
    {
    pocPrice = vahPrice = valPrice = 0;
    volumeByPrice.Clear();
    
    }
    
    //DrawLines();
    
    
    
    
    }
    
    
    
    
    private void CalculateVolumeProfile()
    {
    Print("CalculateVolumeProfile aufgerufen für Bar: " + CurrentBar);
    
    
    volumeByPrice.Clear();
    double sessionHigh = double.MinValue;
    double sessionLow = double.MaxValue;
    
    // Annahme: Die Analyse erfolgt für die aktuelle Sitzung
    for (int i = 0; i < BarsArray[0].Count; i++)
    {
    double highPrice = High[i];
    double lowPrice = Low[i];
    double volume = Volume[i];
    sessionHigh = Math.Max(sessionHigh, highPrice);
    sessionLow = Math.Min(sessionLow, lowPrice);
    
    // Annahme: Verteilung des Volumens auf alle Preise zwischen High und Low der Bar
    double priceRange = highPrice - lowPrice;
    int priceLevels = (int)Math.Ceiling(priceRange / TickSize) + 1;
    for (int level = 0; level < priceLevels; level++)
    {
    double price = lowPrice + level * TickSize;
    double distributedVolume = volume / priceLevels;
    
    if (volumeByPrice.ContainsKey(price))
    {
    volumeByPrice[price] += distributedVolume;
    }
    else
    {
    volumeByPrice.Add(price, distributedVolume);
    }
    }
    }
    
    // Analyse des Volumenprofils zur Bestimmung von POC, VAH, VAL
    AnalyzeVolumeProfile();
    }
    
    
    private void AnalyzeVolumeProfile()
    {
    double totalVolume = 0;
    double maxVolume = 0;
    foreach (var pair in volumeByPrice)
    {
    totalVolume += pair.Value;
    if (pair.Value > maxVolume)
    {
    maxVolume = pair.Value;
    pocPrice = pair.Key;
    }
    }
    
    // Bestimmen des Volumens, das 70% des Gesamtvolumens entspricht
    double targetVolume = totalVolume * 0.7;
    double cumulativeVolume = 0;
    List<KeyValuePair<double, double>> sortedVolume = new List<KeyValuePair<double, double>>(volumeByPrice);
    sortedVolume.Sort((a, b) => a.Key.CompareTo(b.Key));
    
    // Sammle alle Preislevel, die innerhalb der Value Area fallen
    List<double> pricesInValueArea = new List<double>();
    foreach (var pair in sortedVolume)
    {
    cumulativeVolume += pair.Value;
    pricesInValueArea.Add(pair.Key);
    if (cumulativeVolume >= targetVolume) break;
    }
    
    // Bestimme VAH und VAL
    vahPrice = pricesInValueArea.Max();
    valPrice = pricesInValueArea.Min();
    }
    
    
    /*private void DrawLines()
    {
    // Löscht alte Zeichnungen nur, wenn eine neue Session beginnt
    if (Bars.IsFirstBarOfSession && CurrentBar > 0)
    {
    RemoveDrawObjects();
    }
    
    // Zeichnen von VAH, VAL und POC nur, wenn sie Werte haben
    if (pocPrice > 0 && vahPrice > 0 && valPrice > 0)
    {
    Draw.Line(this, "POCLine" + CurrentBar, false, 0, pocPrice, -100, pocPrice, Brushes.Orange, DashStyleHelper.Dash, 5);
    Draw.Line(this, "VAHLine" + CurrentBar, false, 0, vahPrice, -100, vahPrice, Brushes.Blue, DashStyleHelper.Dash, 5);
    Draw.Line(this, "VALLine" + CurrentBar, false, 0, valPrice, -100, valPrice, Brushes.Blue, DashStyleHelper.Dash, 5);
    }
    }*/
    
    
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
    base.OnRender(chartControl, chartScale);
    
    // Stellen Sie sicher, dass die Preise aktualisiert wurden
    if (pocPrice > 0 && vahPrice > 0 && valPrice > 0)
    {
    // Konvertieren der Preise in Y-Koordinaten
    float pocY = chartScale.GetYByValue(pocPrice);
    float vahY = chartScale.GetYByValue(vahPrice);
    float valY = chartScale.GetYByValue(valPrice);
    
    // Erstellen des DashStyle
    var strokeStyleProperties = new SharpDX.Direct2D1.StrokeStyleProperties()
    {
    DashStyle = SharpDX.Direct2D1.DashStyle.Dash
    };
    using (var strokeStyle = new SharpDX.Direct2D1.StrokeStyle(RenderTarget.Factory , strokeStyleProperties))
    {
    // Erstellen von SharpDX-Pinseln für die Linien
    using (var pocBrush = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.DarkCyan))
    using (var vahValBrush = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.Orange))
    {
    // Zeichnen der Linien
    RenderTarget.DrawLine(new SharpDX.Vector2(0, pocY), new SharpDX.Vector2(chartControl.CanvasRight, pocY), pocBrush, 5, strokeStyle);
    RenderTarget.DrawLine(new SharpDX.Vector2(0, vahY), new SharpDX.Vector2(chartControl.CanvasRight, vahY), vahValBrush, 5, strokeStyle);
    RenderTarget.DrawLine(new SharpDX.Vector2(0, valY), new SharpDX.Vector2(chartControl.CanvasRight, valY), vahValBrush, 5, strokeStyle);
    }
    }
    }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    ​​

    #2
    Hello IntyRocket,

    If nothing is appearing in the chart that may be due to the logic you are using, are you seeing any errors in the NinjaScript output window when running the script?

    The best way forward would be to add Print statements into your code so you can observe how the logic is working and the values being used. That would be part of the process when creating custom items and having it not work as expected, this is considered debugging the code. You can find a guide that goes over how to use prints an debug scripts here:



    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by rhyminkevin, Today, 04:58 PM
    3 responses
    47 views
    0 likes
    Last Post Anfedport  
    Started by iceman2018, Today, 05:07 PM
    0 responses
    5 views
    0 likes
    Last Post iceman2018  
    Started by lightsun47, Today, 03:51 PM
    0 responses
    7 views
    0 likes
    Last Post lightsun47  
    Started by 00nevest, Today, 02:27 PM
    1 response
    14 views
    0 likes
    Last Post 00nevest  
    Started by futtrader, 04-21-2024, 01:50 AM
    4 responses
    50 views
    0 likes
    Last Post futtrader  
    Working...
    X