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

Simple strategy issue with two positions w Profit target and Trailing stop loss

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

    Simple strategy issue with two positions w Profit target and Trailing stop loss

    Folks,

    Simple stratey that enters two (e.g. P1, P2) same direction orders with individual profit target and trail stop loss. Both the positions are entered correctly, I can see individual Profit target and Stop loss orders for both the P1 and P2 positions on the chart trader.
    Issue: When either profit target or stop order for P1 is hit, both profit target and stop order for P2 entry disappear/cancelled.

    I have spent quite a few days on looking for solution to no avail. I would really appreciate if your help in figuring out why Profit/Stop orders for P2 are cancelled when P1 profit/stop orders are hit.

    OnStateChange
    {
    Calculate = Calculate.OnBarClose;
    OrderFillResolution = OrderFillResolution.Standard;
    StartBehavior = StartBehavior.WaitUntilFlat;​
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelCloseIgnoreRejects ;

    //I have tried all the following combinations but same issue
    //1.
    EntriesPerDirection = 2;
    EntryHandling = EntryHandling.AllEntries;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;

    //2. Tried this also
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.UniqueEntries;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    }

    OnBarUpdate()
    {
    .....
    //Pseudocode

    double p1ProfitTicks = (currentAtr * 40);
    double p1TailStopTicks =(currentAtr * 40);
    double p2ProfitTicks = (currentAtr * 200);
    double p2TailStopTicks =(currentAtr * 100);
    SetTrailStop(@"P1",CalculationMode.Ticks, p1TailStopTicks, false);
    SetTrailStop(@"P2", CalculationMode.Ticks, p2TailStopTicks, false);
    SetProfitTarget(@"P1", CalculationMode.Ticks, p1ProfitTicks);
    SetProfitTarget(@"P2", CalculationMode.Ticks, p2ProfitTicks);

    if (long condition && Position.MarketPosition == MarketPosition.Flat)
    {
    EnterLong(1, @"P1");
    EnterLong(1, @"P2");
    }

    if (short condition && Position.MarketPosition == MarketPosition.Flat)
    {
    EnterShort(1, @"P1");
    EnterShort(1, @"P2");
    }

    .....
    }

    TIA for your attention and help!​
    Last edited by dejafutures; 02-13-2024, 10:30 AM.

    #2
    Hello dejafutures,

    From the given information I don't see why that would happen, have you tried to use TraceOrders to see if any order handling errors are being presented in the output window?

    I tried the following and see that the targets can fill independently meaning if the P1 target fills the P2 targets are still active.


    Code:
    protected override void OnStateChange()
    {
       if (State == State.SetDefaults)
       {
          EntriesPerDirection = 2;
          EntryHandling = EntryHandling.UniqueEntries;
          StopTargetHandling = StopTargetHandling.PerEntryExecution;
       }
    }
    
    private bool doOnce;
    protected override void OnBarUpdate()
    {
       if(State == State.Historical) return;
       if(doOnce == false)
       {
          SetTrailStop(@"P1",CalculationMode.Ticks, 20, false);
          SetTrailStop(@"P2", CalculationMode.Ticks, 30, false);
          SetProfitTarget(@"P1", CalculationMode.Ticks, 20);
          SetProfitTarget(@"P2", CalculationMode.Ticks, 30);
    
          EnterLong(1, @"P1");
          EnterLong(1, @"P2");
          doOnce = true;
       }
    }​
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you for your quick help. I see in your example both P1 and P2 have identical profit and stop targets and both P1 and P2 hit at the same time. Can you please try if P2 profit/SL targets are farther than P1 targets. In my case P2 position remains active while both profit and SL orders are cancelled when P1 profit/SL is hit. Btw, how do I use TraceOrders to diagnose this?

      I have RealtimeErrorHandling = RealtimeErrorHandling.StopCancelCloseIgnoreRejects ;
      Do you think this may be an issue?
      Last edited by dejafutures; 02-13-2024, 11:35 AM.

      Comment


        #4
        Hello dejafutures,

        The targets in that sample are not identical, the offset for the targets are 20 and 30 ticks so they wont be hit at the same time. You can use that test on a chart and also move one of the targets manually to be close to the price so it fills, that will cancel the associated stop order and leave open the other 2 targets.

        RealtimeErrorHandling would not be related to this.
        JesseNinjaTrader Customer Service

        Comment


          #5
          You are correct. I created a simple sma cross strategy and P1/P2 stop/profit orders are handled correctly. It seems I need to use TraceOrders to figure out what's going on with my offending strategy. Would you be able to point to good resource on use of TraceOrder to debug such situations.
          Thanks for your help again!

          Comment


            #6
            Hello dejafutures,

            You can read about trace orders in the following link. You just need to set the property to true, if you are using the strategy builder this property is found in the initial default properties screen. After setting the value to true you will need to remove the strategy from where it is applied and then re apply it.

            https://ninjatrader.com/support/helpGuides/nt8/traceorders2.htm
            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by rhyminkevin, Today, 04:58 PM
            3 responses
            47 views
            0 likes
            Last Post Anfedport  
            Started by iceman2018, Today, 05:07 PM
            0 responses
            5 views
            0 likes
            Last Post iceman2018  
            Started by lightsun47, Today, 03:51 PM
            0 responses
            7 views
            0 likes
            Last Post lightsun47  
            Started by 00nevest, Today, 02:27 PM
            1 response
            14 views
            0 likes
            Last Post 00nevest  
            Started by futtrader, 04-21-2024, 01:50 AM
            4 responses
            50 views
            0 likes
            Last Post futtrader  
            Working...
            X