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

How to Prevent Closing a Position before Stop/Target Reached

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

    How to Prevent Closing a Position before Stop/Target Reached

    Hello,

    I have created a strategy that has a condition to go long or go short based on simple criteria and the long/short criteria are the inverse of each other. It is based on an SMA crossover strategy. I am using an EnterLong() and EnterShort() order for each condition and I have declared a SetStopLoss and SetProfitTarget value declared for each condition.

    If I am in a long position, I want to ensure that the position will not close until the target or stop value is reached, even if a short signal appears. In addition, if there is a short signal and I am currently in a long position, I also do not want NT to place another order to go short until the long has reached the target or stop (meaning I only want 1 futures contact to be bought a one time).

    Based on the current code (below), it seems that if I am in a long position and a short signal appears, it will close my position out and reverse. I am trying to figure out what rule I am missing to prevent the long form being closed or an additional order from being placed.

    Thanks for any information you can provide,
    -Dave



    protected override void Initialize()
    {


    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.UniqueEntries;
    CalculateOnBarClose = false;
    }


    protected override void OnBarUpdate()
    {


    if (long criteria)

    {
    EnterLong(1, "long");
    SetStopLoss(CalculationMode. ...);
    SetProfitTarget(CalculationMode ...);
    }

    // Condition set 2
    if (short criteria)
    {
    EnterShort(DefaultQuantity, "Short");
    SetStopLoss(CalculationMode ...);
    SetProfitTarget(CalculationMode ...);
    }

    #2
    Hello NewEngTrader,

    Thank you for your post and welcome to the NinjaTrader Support Forum!

    Add Position.MarketPosition == MarketPosition.Flat to the conditions:
    Code:
    if (long criteria && Position.MarketPosition == MarketPosition.Flat)
    
    {
    EnterLong(1, "long");
    SetStopLoss(CalculationMode. ...);
    SetProfitTarget(CalculationMode ...);
    }
    
    // Condition set 2
    if (short criteria && Position.MarketPosition == MarketPosition.Flat)
    {
    EnterShort(DefaultQuantity, "Short");
    SetStopLoss(CalculationMode ...);
    SetProfitTarget(CalculationMode ...);
    }
    For information on Position please visit the following link: http://www.ninjatrader.com/support/h...7/position.htm

    Please let me know if I may be of further assistance.

    Comment


      #3
      Originally posted by NewEngTrader View Post
      Hello,

      I have created a strategy that has a condition to go long or go short based on simple criteria and the long/short criteria are the inverse of each other. It is based on an SMA crossover strategy. I am using an EnterLong() and EnterShort() order for each condition and I have declared a SetStopLoss and SetProfitTarget value declared for each condition.

      If I am in a long position, I want to ensure that the position will not close until the target or stop value is reached, even if a short signal appears. In addition, if there is a short signal and I am currently in a long position, I also do not want NT to place another order to go short until the long has reached the target or stop (meaning I only want 1 futures contact to be bought a one time).

      Based on the current code (below), it seems that if I am in a long position and a short signal appears, it will close my position out and reverse. I am trying to figure out what rule I am missing to prevent the long form being closed or an additional order from being placed.

      Thanks for any information you can provide,
      -Dave



      protected override void Initialize()
      {


      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.UniqueEntries;
      CalculateOnBarClose = false;
      }


      protected override void OnBarUpdate()
      {


      if (long criteria)

      {
      EnterLong(1, "long");
      SetStopLoss(CalculationMode. ...);
      SetProfitTarget(CalculationMode ...);
      }

      // Condition set 2
      if (short criteria)
      {
      EnterShort(DefaultQuantity, "Short");
      SetStopLoss(CalculationMode ...);
      SetProfitTarget(CalculationMode ...);
      }
      What you have written will do what you want. There have been many arguments and much caviling on these fora about the fact that in the presence of protective orders, the Managed approach for orders will not reverse a trade on the same bar if CalculateOnBarClose is true, or indeed at all, if there is no cancellation of said orders.

      Comment


        #4
        Thank you

        Thank you for your feed back and suggestions!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by DJ888, 04-16-2024, 06:09 PM
        6 responses
        18 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by Jon17, Today, 04:33 PM
        0 responses
        1 view
        0 likes
        Last Post Jon17
        by Jon17
         
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        6 views
        0 likes
        Last Post Javierw.ok  
        Started by timmbbo, Today, 08:59 AM
        2 responses
        10 views
        0 likes
        Last Post bltdavid  
        Started by alifarahani, Today, 09:40 AM
        6 responses
        41 views
        0 likes
        Last Post alifarahani  
        Working...
        X