Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 yertle, Yesterday, 08:38 AM
                        7 responses
                        28 views
                        0 likes
                        Last Post yertle
                        by yertle
                         
                        Started by bmartz, 03-12-2024, 06:12 AM
                        2 responses
                        21 views
                        0 likes
                        Last Post bmartz
                        by bmartz
                         
                        Started by funk10101, Today, 12:02 AM
                        0 responses
                        5 views
                        0 likes
                        Last Post funk10101  
                        Started by gravdigaz6, Yesterday, 11:40 PM
                        1 response
                        9 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Started by MarianApalaghiei, Yesterday, 10:49 PM
                        3 responses
                        11 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Working...
                        X