Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Code not working as it should

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

    Code not working as it should

    I am just starting to use Ninja. I started to practice by just seeing if i can buy and sell based on price. Have not even gotten to the indicator part yet. Trying to understand the coding. All i want to do is go short or long, if CLose > open and Close < open. I attached my code below. But, Either the short will work or the long will work, but not both. Also, i want only to trade between 10:15 and 3:30 and only for the day of replay.

    Any help would be great.

    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// LongShort
    /// </summary>
    [Description("LongShort ")]
    public class LetsTradeIt : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int stopLoss = 8; // Default setting for StopLoss
    private int profitTarget = 6; // Default setting for ProfitTarget
    private int ordQuantity = 5; // Default setting for OrdQuantity
    // User defined variables (add any user defined variables below)
    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    SetStopLoss("", CalculationMode.Ticks, StopLoss, false);
    SetProfitTarget("", CalculationMode.Ticks, ProfitTarget);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    IOrder entryOrderSH = null;


    Print("### Bar [" + Time[0].ToString() +"], Open ["+ Open[0] +"], Close ["+ Close[0] +"], High ["+ High[0] +"], Low [" +Low[0] + "].");
    if (ToTime(Time[0]) > ToTime(10, 0, 0) && ToTime(Time[0]) < ToTime(15,30,0) )
    {

    // Condition set 1
    if (Close[0] < Open[0])
    {
    Print ( "************* In Short Logic ***********************");
    entryOrderSH = EnterShortLimit(0,true,OrdQuantity, High[0] + -2 * TickSize, "ShortHigh " + Time[0].ToString()+ "");
    EnterShortLimit(0,true,OrdQuantity, Open[0] + -2 * TickSize, "ShortOpen " + Time[0].ToString()+ "");
    Print ( " *** rtnCode for Short High [" + entryOrderSH.ToString() + "]");

    }

    if ( Close[0] > Open[0] )
    {
    Print ( "+++++++++++++++++ In Long Logic ++++++++++++++++++");
    // entryOrderSH = EnterLongLimit(0,true,OrdQuantity,Low[0]+ 2*TickSize,"GreenLow"+Time[0].ToString());
    // EnterLongLimit(0,true,OrdQuantity,Open[0]+ 2*TickSize,"GreenOpen"+Time[0].ToString());
    // Print ( " +++ rtnCode for Long Low [" + entryOrderSH.ToString() + "]");



    }


    }
    }

    #region Properties
    [Description("")]
    [Category("Parameters")]
    public int StopLoss
    {
    get { return stopLoss; }
    set { stopLoss = Math.Max(8, value); }
    }

    [Description("")]
    [Category("Parameters")]
    public int ProfitTarget
    {
    get { return profitTarget; }
    set { profitTarget = Math.Max(6, value); }
    }

    [Description("")]
    [Category("Parameters")]
    public int OrdQuantity
    {
    get { return ordQuantity; }
    set { ordQuantity = Math.Max(5, value); }
    }
    #endregion
    }
    }

    #2
    check your LOG tab for any errors. Maybe something strange is going on that is corrupting the strategy.
    mrlogik
    NinjaTrader Ecosystem Vendor - Purelogik Trading

    Comment


      #3
      I have checked the log and there are no errors.
      If i run the code for the short side while the Long side is commented out, it works, if i run the Long side while the Short side is commented, it works.
      But, when I run both while none are commented out, then the long side does not work.

      You can try running the code against any day on a 2 min chart.

      Comment


        #4
        I suspect you are trying to submit two entry orders simultaneously. This is currently now allowed and the second order placed is ignored.

        Review the bottom part of this page for Internal Order Handling Rules.

        RayNinjaTrader Customer Service

        Comment


          #5
          The way i have it coded, it can place as many short or Long order as it wants as long as it is defined in the "MaxOrders Per Side" parameter.
          I have been able to place 10+ short orders or Long orders. Again, the problem seems to be I can only do Longs or Shorts Not both.

          Comment


            #6
            Right. You cannot be long and short at the same time. Please see the link Ray provided for more information. Scroll down to the end of the page to review the handling rules.
            Josh P.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            648 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            369 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            108 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            572 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            574 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X