Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Order won't execute?

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

    Order won't execute?

    If I get a 1 lot position (long) and apply this strategy, why won't it close the position on the cross of the 5EMA and 30EMA?

    ///</summary>
    protectedoverridevoid Initialize()
    {
    CalculateOnBarClose =
    true;
    }
    ///<summary>
    /// Called on each bar update event (incoming tick)
    ///</summary>
    protectedoverridevoid OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(EMA(30), EMA(5), 1))
    {
    ExitLong(
    "", "");
    }
    }
    #region Properties
    [Description(
    "")]
    [Category(
    "Parameters")]
    publicint MyInput0
    {
    get { return myInput0; }
    set { myInput0 = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #2
    You strategy does not enter an order -> there is nothing to exit.

    Comment


      #3
      Dierk.....

      If I entered the order directly from the "Orders" tab, I have a position. Won't the stgrategy see that I have a position and execute the "ExitLong" based on my indicators and strategy?

      Comment


        #4
        No, NinjaScript strategies only can manage position entered by themselves.

        Comment


          #5
          No it will not.

          Here is more info on the differences between a strategy and account position.

          RayNinjaTrader Customer Service

          Comment


            #6
            Thanks Ray! Any idea how you can initiate a "long" trade via a strategy (instantly) and apply the "ExitLong" part and make sure it only does it once? (instead of placing an order via the "Orders" tab and trying to run the ExitLong strategy from below?)

            Thanks!

            Comment


              #7
              You can just do something like
              Code:
              if (CurrentBar == 1)
                   EnterLong();
              That will generate a virtual simulated position since it is running on historical bars. Then you want to do something like
              Code:
              if (Historical)
                  return;
              That will prevent your ExitLong() from executing on historical bars and basically it will now run on real-time data only. When it finds the exit position it will submit the order. Your strategy position will become flat and your account position will also be exited. Now, of course, if you run statistics on your strategy it will be off since it is calculated based on your virtual long position as opposed to your real account position, but it gets the job done.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Thanks a bunch Josh!

                Comment


                  #9
                  Any idea why when I run this, it doesn't generate an order???? I won't EnterLong, order ExitLong???

                  ================================================== ===

                  {
                  SetStopLoss(
                  "", CalculationMode.Ticks, 30, false);

                  CalculateOnBarClose =
                  true;


                  TraceOrders =
                  true;


                  if (Historical)
                  return;
                  }
                  ///<summary>
                  /// Called on each bar update event (incoming tick)
                  ///</summary>
                  protectedoverridevoid OnBarUpdate()
                  {
                  // Condition set 1
                  if ((Position.MarketPosition == MarketPosition.Flat)
                  && (CrossBelow(EMA(
                  5), EMA(50), 1)))
                  {
                  EnterShort(DefaultQuantity,
                  "Short 1a");
                  }
                  // Condition set 2
                  if ((Position.MarketPosition == MarketPosition.Short)
                  && (CrossBelow(EMA(
                  50), EMA(5), 1)))
                  {
                  ExitLong(
                  "Exit Short 1a", "");
                  }
                  // Condition set 3
                  if ((Position.MarketPosition == MarketPosition.Short)
                  && (CrossBelow(EMA(
                  30), EMA(5), 1)))
                  {
                  ExitLong(
                  "Exit Short 1a", "");
                  }
                  }

                  Comment


                    #10
                    I don't see an EnterLong, and I'd guess that "CrossBelow(EMA(5), EMA(50), 1))" is false, so you don't get any order.

                    Comment


                      #11
                      Actually, it is the EnterShort that won't execute...sorry...typo.....??

                      ================================================== =

                      {
                      SetStopLoss("", CalculationMode.Ticks, 30, false
                      );

                      CalculateOnBarClose =
                      true
                      ;


                      TraceOrders =
                      true;


                      if (Historical)
                      return
                      ;
                      }
                      ///
                      <summary>
                      ///
                      Called on each bar update event (incoming tick)
                      ///
                      </summary>
                      protectedoverridevoid
                      OnBarUpdate()
                      {
                      // Condition set 1
                      if
                      ((Position.MarketPosition == MarketPosition.Flat)
                      && (CrossBelow(EMA(
                      5), EMA(50), 1
                      )))
                      {
                      EnterShort(DefaultQuantity,
                      "Short 1a"
                      );
                      }
                      // Condition set 2
                      if
                      ((Position.MarketPosition == MarketPosition.Short)
                      && (CrossBelow(EMA(
                      50), EMA(5), 1
                      )))
                      {
                      ExitShort(
                      "Exit Short 1a", ""
                      );
                      }
                      // Condition set 3
                      if
                      ((Position.MarketPosition == MarketPosition.Short)
                      && (CrossBelow(EMA(
                      30), EMA(5), 1
                      )))
                      {
                      ExitShort(
                      "Exit Short 1a", ""
                      );
                      }
                      }

                      Comment


                        #12
                        Code looks fine so you will have to resort to debugging to determine what is happening at run time.

                        RayNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        558 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        324 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        101 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        545 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        547 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X