Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

strategy not working

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

    strategy not working

    Hello,I am trying to make a strategy but it seems not working ,can I have a light where my code is damaged please
    thanks ,any help is welcome

    Summary of rules:
    Price must be above the 200 SMA for a trade to be triggered.
    The first red candle detected triggers a Buy Stop order 1 tick above the high of that candle.if not triggered buy next red candle
    Ignore the other red candles after the first is traded
    target 20ticks stop15
    Close the position if the price closes below the 10 EMA.

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

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class BuyStopAboveFirstRedCandleCloseBelowEMA : Strategy
    {
    private bool firstRedCandleFound = false;
    private double buyStopPrice; //
    private SMA SMA1;
    private EMA EMA1;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Stratégie qui place un ordre Buy Stop 1 tick au-dessus de la première bougie rouge si le prix est au-dessus de la SMA 200, et ferme le trade si le prix ferme sous l'EMA 10.";
    Name = "BuyStopAboveFirstRedCandleCloseBelowEMA";
    Calculate = Calculate.OnBarClose; // Calculer à la fermeture de chaque bougie
    }
    else if (State == State.DataLoaded)
    {
    SMA1 = SMA(Close,200);
    EMA1 = EMA(Close, 10);
    }
    }

    protected override void OnBarUpdate()
    {

    if (Position.MarketPosition == MarketPosition.Long)
    &&(Close[0] < ema10[0])
    {
    ExitLong("CloseBelowEMA", "BuyStopFirstRedCandle");
    }



    if (firstRedCandleFound)
    return;


    if (Close[0] > sma200[0])
    && (Close[0] < Open[0])
    {
    firstRedCandleFound = true;
    buyStopPrice = High[0] + TickSize;


    EnterLongStopMarket(1, buyStopPrice, "BuyStopFirstRedCandle");
    }
    }
    }


    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time)
    {

    if (order.OrderState == OrderState.Filled || order.OrderState == OrderState.Cancelled)
    {
    firstRedCandleFound = false;
    }
    }
    }
    }
    ​​

    #2
    I think the logic to reset firstRedCandleFound should be in OnExecutionUpdate.

    You could also try to step through with a debugger. Steps:

    - get Microsoft Visual Studio Community, whichever is the latest version. Should be free
    - add "using System.Diagnostics;" in the Using declarations region
    - in OnBarUpdate do something like if (CurrentBar == 0) Debugger.Break(); This will cause the execution to stop when it gets to that point when you've attached with the debugger, and then you can start stepping through the code one line at a time.
    - make sure you compile with DebugMode. Right click on the editor to set that
    - To attach the debugger, you need to go to the debug tab in Visual Studio and select "attach to process" before you start executing your strategy. The process name will usually be whatever is in the chart tab, e.g. "ES 09-24" or something along those lines.
    - after getting into the debugger, put a breakpoint at the beginning of onBarUpdate to jump to the next bar with Continue in Visual Studio.

    Comment


      #3
      Hello janio973,

      Thank you for your post.

      If the strategy is applied to a chart, is there data with new bars appearing on the chart the Strategy is applied to?

      Is the strategy showing as enabled on the Strategies tab of the Control Center?

      If the strategy is in the Strategy Analyzer, is there data appearing on the Chart Display of the Strategy Analyzer?

      Importantly, are there errors appearing on the Log tab of the Control Center?


      Try testing the Sample MA Crossover strategy included with NinjaTrader on the same chart or Strategy Analyzer using the same instrument, bar type, interval, and date range.

      Do you see results with the Sample MA Crossover strategy? (This would confirm for us data is available and the issue is with the logic in the custom script)


      If the strategy is getting data and the strategy is enabled or a backtest is run with no errors in the Log tab of the Control Center, and the Sample MA Crossover is returning results, then would likely indicate the logic conditions in the custom strategy did not evaluate as true or orders are being ignored or cancelled.

      In order to better understand how the code is working, it will be necessary to use Print to see how the conditions are evaluating and enable TraceOrders to see if orders are being submitted, ignored, rejected, or cancelled.

      I'm also including a link to a forum post with further suggestions on debugging a script.


      Enable TraceOrders, print the time of the bar and all values used in the conditions that submit entry orders. Include labels for all values and comparison operators.

      Let me know if you need any assistance creating a print or enabling TraceOrders.

      Save the output from the output window to a text file and provide this with your reply.

      I'll be happy to assist with analyzing the output.​

      Comment


        #4
        Thanks silverm3170​ and NinjaTrader_Gaby
        The strategy doenst appear,not selectionable, when I try to use it on my chart
        I will try the advises above and come back

        Comment


          #5
          If your strategies are not showing up in the available Strategy list then that suggests there is an issue with the strategies themselves. Specifically, there is likely an error in OnStateChange().

          Keep in mind that while it may compile successfully, the compiler cannot check "run time" logic errors, which can only occur when you run (or load) the strategy.

          A good first step here is to check the "Log" tab of the Ninjatrader Control center and look for any errors related to the strategies as if there is a run time error is would show in the log tab.

          If there are no log errors, check (in the Ninjascript editor open each strategy) to see if the strategies have the same name (IE you made a copy of the strategy and made your changes but unintentionally left the name the same as an existing one).

          In general, you want the filename of the strategy (this is what Ninjascript editor uses to open the script) to be the same as the public class name and most importantly that inside OnStateChange and within State.SetDefaults, the Name = field (This is what the drop-down or strategy selector uses) shows the same name as the public class name.

          Please let us know if you need further assistance. ​

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          58 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          133 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          73 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          45 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          50 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X