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 Rapine Heihei, Today, 08:19 PM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by Rapine Heihei, Today, 08:25 PM
        0 responses
        6 views
        0 likes
        Last Post Rapine Heihei  
        Started by f.saeidi, Today, 08:01 PM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by Rapine Heihei, Today, 07:51 PM
        0 responses
        8 views
        0 likes
        Last Post Rapine Heihei  
        Started by frslvr, 04-11-2024, 07:26 AM
        5 responses
        98 views
        1 like
        Last Post caryc123  
        Working...
        X