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 and Short orders interrupting each other... why?

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

    Long and Short orders interrupting each other... why?

    Hi there, I've got a problem I can't seem to get my head around.
    I have a strategy that I built with the builder and the compiled to be able to make manual modifications...

    However the problem is that the the long and short orders cancel each other out. So while a long position is open, a short position gets opened, canceling the long position. What I'd like is for each trade to COMPLETE in line with the TP and SL conditions.
    Any suggestion please? I've tried using the unrealized profit loss criterion but it's completely ignored. This is really weird and usually I don't have that issue, so maybe I messed up something

    Thanks!

    Code:
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = true;
    RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    Code:
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    if (State == State.Historical)
    return;
    
    if (CurrentBars[0] < 1)
    return;
    
    // Set 1
    if ((CrossAbove(RelativeVigorIndex1.Default, RelativeVigorIndex2.Signal, 1))
    && BarsSinceExitExecution() > 2 || BarsSinceExitExecution() == -1
    && (Position.GetUnrealizedProfitLoss(PerformanceUnit. Currency, Close[0]) == 0)
    // TradingHours
    && (Times[0][0].TimeOfDay <= new TimeSpan(20, 0, 0)
    && (Times[0][0].TimeOfDay >= new TimeSpan(2, 0, 0))
    && (GetCurrentAsk(0) > SMA1[0])))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"golong");
    }
    
    // Set 2
    if ((CrossBelow(RelativeVigorIndex3.Default, RelativeVigorIndex4.Signal, 1))
    && BarsSinceExitExecution() > 2 || BarsSinceExitExecution() == -1
    && (Position.GetUnrealizedProfitLoss(PerformanceUnit. Currency, Close[0]) == 0)
    // TradingHours
    && (Times[0][0].TimeOfDay <= new TimeSpan(20, 0, 0)
    && (Times[0][0].TimeOfDay >= new TimeSpan(2, 0, 0))
    && (GetCurrentAsk(0) < SMA2[0])))
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), @"goshort");
    }
    
    }

    #2
    I don't believe you can be both long and short the same instrument at the same time. Which is why one closes when the other is triggered

    Comment


      #3
      Hello Oracletrades,

      This correct, a strategy cannot have two positions and cannot be long and short at the same time.

      Calling EnterShort when in a long position will close the position and enter a short position. Calling EnterLong when in a short position will close the position and enter a long position.

      I recommend using two accounts with the strategy logic separated in two strategies that take opposite positions on each account if this is your goal.
      https://ninjatrader.com/support/foru...660#post792660

      Regarding placing orders in opposite directions at the same time to bracket the market, this is not possible with the managed approach used by the Strategy Builder.
      Bracking the market would require an unmanaged script using SubmitOrderUnmanaged to place the orders.
      Below is a link to an example.
      https://ninjatrader.com/support/foru...579#post770579
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Thanks but all I wanted is to have the open trade stay open, even if there are other signals to open a short trade when I'm long... So not as complex as you suggest, just want to have an open position stay open and continue... Isn't there code to say "wait while another trade is open"?

        Comment


          #5
          Hello Oracletrades,

          With the managed approach It would depend on what orders are already working and whether a position is open.

          Below is a link to the help guide on the internal order handling rules.


          Use TraceOrders in the NinjaScript Output window to see when orders are ignored due to the internal order handling rules (or ignored for other reasons like not using isLiveUntilCancelled as true).


          Your logic would decide when orders are placed. If you do not want to submit orders when a position is open, require in the conditions of your logic for the Position.MarketPosition to be MarketPosition.Flat.


          The unmanaged approach would allow you place any orders you wanted in any direction at any time.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Thanks, this here is what I was looking for:

            Your logic would decide when orders are placed. If you do not want to submit orders when a position is open, require in the conditions of your logic for the Position.MarketPosition to be MarketPosition.Flat.
            https://ninjatrader.com/support/help...tionupdate.htm

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by bortz, 11-06-2023, 08:04 AM
            47 responses
            1,611 views
            0 likes
            Last Post aligator  
            Started by jaybedreamin, Today, 05:56 PM
            0 responses
            9 views
            0 likes
            Last Post jaybedreamin  
            Started by DJ888, 04-16-2024, 06:09 PM
            6 responses
            19 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by Jon17, Today, 04:33 PM
            0 responses
            6 views
            0 likes
            Last Post Jon17
            by Jon17
             
            Started by Javierw.ok, Today, 04:12 PM
            0 responses
            22 views
            0 likes
            Last Post Javierw.ok  
            Working...
            X