Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy help

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

    Strategy help

    Hello, I would like to develop an strat thats about to enter at volume increase only at pivot candles (opposite candle), using ema 50 as filter for bullish or bearish entries, also with arrows marking the candles with increase of volume regarding previous candle.

    So far ChatGPT help me with the code, but the strat doesnt make any entry but the Output log is correct:

    region Using declarations
    using System;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript.Strategies;
    using NinjaTrader.NinjaScript.Indicators;
    #endregion

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class VolumeBreakoutScalp : Strategy
    {
    private EMA ema50;
    private bool reentryAllowed = false;
    private bool inReentry = false;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Name = "VolumeBreakoutScalp";
    Calculate = Calculate.OnBarClose;
    IsInstantiatedOnEachOptimizationIteration = false;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IncludeCommission = true;
    }
    else if (State == State.Configure)
    {
    SetStopLoss("VolReversalLong", CalculationMode.Ticks, 32, false);
    SetStopLoss("VolReversalShort", CalculationMode.Ticks, 32, false);
    SetStopLoss("ReentryLong", CalculationMode.Ticks, 32, false);
    SetStopLoss("ReentryShort", CalculationMode.Ticks, 32, false);
    }
    else if (State == State.DataLoaded)
    {
    ema50 = EMA(Close, 50);
    }
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar < 2)
    return;

    // Filtro horario opcional
    // if (ToTime(Time[0]) < 093500 || ToTime(Time[0]) > 154500)
    // return;

    double currentVolume = Volume[0];
    double prevVolume = Volume[1];
    double currentClose = Close[0];
    double prevClose = Close[1];
    double emaValue = ema50[0];

    // Debug
    Print("Vol actual: " + currentVolume + " | Vol anterior: " + prevVolume);
    Print("Close actual: " + currentClose + " | EMA: " + emaValue);

    // Dirección contraria (vela opuesta)
    bool bullishBar = Close[0] > Open[0];
    bool bearishBar = Close[0] < Open[0];
    bool previousBullish = Close[1] > Open[1];
    bool previousBearish = Close[1] < Open[1];

    // Long setup: vela verde, mayor volumen que anterior, la anterior era roja y estamos sobre la EMA
    if (!inReentry && Position.MarketPosition == MarketPosition.Flat)
    {
    if (currentVolume > prevVolume && bullishBar && previousBearish && Close[0] > emaValue)
    {
    EnterLong(1, "VolReversalLong");
    }

    // Short setup: vela roja, mayor volumen, anterior verde, y estamos bajo la EMA
    else if (currentVolume > prevVolume && bearishBar && previousBullish && Close[0] < emaValue)
    {
    EnterShort(1, "VolReversalShort");
    }
    }

    // Reentrada tras stop
    if (reentryAllowed && Position.MarketPosition == MarketPosition.Flat)
    {
    if (currentVolume > prevVolume && bullishBar && previousBearish && Close[0] > emaValue)
    {
    EnterLong(2, "ReentryLong");
    inReentry = true;
    reentryAllowed = false;
    }
    else if (currentVolume > prevVolume && bearishBar && previousBullish && Close[0] < emaValue)
    {
    EnterShort(2, "ReentryShort");
    inReentry = true;
    reentryAllowed = false;
    }
    }
    }

    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity,
    MarketPosition marketPosition, string orderId, DateTime time)
    {
    if (execution.Order == null || execution.Order.OrderState != OrderState.Filled)
    return;

    // Si una orden principal fue cerrada y estamos flat → permitir reentrada
    if ((execution.Order.Name == "VolReversalLong" || execution.Order.Name == "VolReversalShort") &&
    Position.MarketPosition == MarketPosition.Flat)
    {
    reentryAllowed = true;
    Print("Reentrada habilitada");
    }

    // Si una reentrada fue ejecutada → desactivar flag
    if (execution.Order.Name == "ReentryLong" || execution.Order.Name == "ReentryShort")
    {
    inReentry = false;
    Print("Reentrada completada");
    }
    }
    }
    }​

    There is something wrong in this code?
    Why entries are not taking?

    Thx in advance

    #2
    Hello DoomNM,

    Thank you for your post.

    If you are using an external AI tool those generally cannot create valid NinjaScript, I have included some information about that further below. Regarding your strategy not taking trades when expected, to understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order.

    The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

    The debugging print output should clearly show what the condition is, what time the conditions are being compared, all values being compared, and how they are being compared.

    Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    Further, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

    After enabling TraceOrders remove the instance of the strategy from the Configured list in the Strategies window and add a new instance of the strategy from the Available list.

    I am happy to assist you with analyzing the output from the output window.

    Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

    Below is a link to a support article that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.

    https://support.ninjatrader.com/s/ar...nd-TraceOrders



    From our experience at this time, ChatGPT and other AI models are not quite adequate to reliably and consistently generate valid compilable NinjaScripts that function as the user intends. We often find that the AI-generated code will call non-existent properties and methods, use improper classes or inheritance, exclude necessary components such as using statements, and may have incorrect logic.
    We highly encourage that you create a new NinjaScript yourself using the NinjaScript Editor, and use the code generated by ChatGpt as more as suggestions and guide than the actual code generated.

    Below is a link to a forum thread that discusses ChatGPT and it's limitations.


    While it would not be within our support model to correct these scripts at user request, we would be happy to provide insight for any direct specific inquiries you may have if you would like to create this script yourself. Our support is able to assist with finding resources in our help guide as well as simple examples, and we are happy to assist with guiding you through the debugging process to assist you with understanding unexpected behavior.

    Below I am providing a link to a support article with helpful resources on getting started with NinjaScript and C#.


    You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    19 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    119 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    63 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    41 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    45 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X