Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Applying a MACD to CVD

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

    Applying a MACD to CVD

    I'm trying to apply a macd to cvd. Any help as to where I'm going wrong would be appreciated:

    region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    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.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class CVDMACD : Strategy
    {
    private OrderFlowCumulativeDelta cumulativeDelta;
    private MACD macd;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "CVDMACD";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    cumulativeDelta = OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, DeltaType.Cumulative, 0);
    macd = MACD(cumulativeDelta.Delta, 12, 26, 9);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < BarsRequiredToTrade)
    return;

    // Long condition: MACD line crosses above signal line
    if (CrossAbove(macd.Default, macd.Avg, 1))
    {
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    EnterLong("Long Entry");
    }
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    ExitShort("Exit Short");
    EnterLong("Long Entry");
    }
    }
    // Short condition: MACD line crosses below signal line
    else if (CrossBelow(macd.Default, macd.Avg, 1))
    {
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    EnterShort("Short Entry");
    }
    else if (Position.MarketPosition == MarketPosition.Long)
    {
    ExitLong("Exit Long");
    EnterShort("Short Entry");
    }
    }
    }
    }
    }​

    #2
    Hello jourdale,

    The orderflow items are not like most of the other indicators, to use those items you need to make sure you are doing exactly what is shown in the help guide for those indicators. you are missing some of the requirements like the AddDataSeries statement and BarsInprogess conditions to update the indicator.



    You would need to make your code like the second example in the help guide if you plan to store the indicator as a variable. Once you have your script like the help guide and see the test value is able to print you could pass the series to another indicator like you have but that code would go inside the OnBarUpdate BarsInProgress 0 section and not State.Configure.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    45 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    21 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    31 views
    1 like
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    50 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    42 views
    0 likes
    Last Post CarlTrading  
    Working...
    X