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

Long Positions entering correctly but Not Shorts?

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

    Long Positions entering correctly but Not Shorts?

    Hi,

    Long Positions are entering correctly on this strategy but not Shorts?
    Positions are supposed to enter at the desired time.

    Am I doing something incorrectly?


    /// <summary>
    /// Enter the description of your strategy here
    /// </summary>
    [Description("Enter the description of your strategy here")]
    public class Timetest : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int myInput0 = 1; // Default setting for MyInput0
    // 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()
    {
    CalculateOnBarClose = false;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (ToTime(Time[0]) > ToTime(8, 0, 0)
    && ToTime(Time[0]) < ToTime(8, 20, 0))
    {
    EnterLong(100000, "Long");
    }

    // Condition set 2
    if (Position.Quantity > 0
    && ToTime(Time[0]) > ToTime(10, 20, 0))
    {
    ExitLong("", "");
    }

    // Condition set 3
    if (ToTime(Time[0]) > ToTime(11, 0, 0)
    && ToTime(Time[0]) < ToTime(11, 20, 0))
    {
    EnterShort(100000, "Short");
    }

    // Condition set 4
    if (Position.Quantity < 0
    && ToTime(Time[0]) > ToTime(11, 40, 0))
    {
    ExitShort("", "");
    }
    }
    Attached Files

    #2
    Set 4. Position quantity would never be < 0. It would be a positive number regardless if you are long or short. If you want to check against a short position you want to check for Position.MarketPosition == MarketPosition.Short.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      That's helpful. Thank You very much.

      Comment


        #4
        Is this what I should be using to exit a position based on the PNL from within the Strategy?



        // Condition set 3
        if (Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) >= 150
        && Position.MarketPosition == MarketPosition.Long)
        {
        ExitLong("Profit", "");
        }

        // Condition set 4
        if (Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) <= -300
        && Position.MarketPosition == MarketPosition.Long)
        {
        ExitLong("Stop Loss", "");
        }

        Comment


          #5
          Yes, that should do it tschalli27.
          BertrandNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by thumper57, Yesterday, 04:30 PM
          3 responses
          12 views
          0 likes
          Last Post thumper57  
          Started by DT215, 01-14-2023, 07:59 PM
          4 responses
          132 views
          1 like
          Last Post NinjaTrader_BrandonH  
          Started by ETFVoyageur, Yesterday, 12:52 AM
          2 responses
          32 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by Skifree, Yesterday, 02:50 PM
          1 response
          12 views
          0 likes
          Last Post NinjaTrader_Kimberly  
          Started by owen5819, Yesterday, 02:24 PM
          2 responses
          16 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Working...
          X