• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

long and short without OCO in managed approach

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

    long and short without OCO in managed approach

    Hello NinjaTrader Team,

    I have this peace of code for going long and short simultaniosly as a simple range breakout system:

    Code:
     
    high = Highs[1][0];
    low = Lows[1][0];
    
    var longOrder = EnterLongStopMarket(0, true, 1, high + 1, "LongRBO");
    
    if (longOrder != null)
    {
           longOrder.Oco = Guid.NewGuid().ToString();
           _orders.Add(longOrder);
    }
    
    var shortOrder = EnterShortStopMarket(0, true, 1, low - 1, "ShortRBO");
    
    if (shortOrder != null)
    {
                    shortOrder.Oco = Guid.NewGuid().ToString();
                    _orders.Add(shortOrder);
    }
    The SL and TPs are set in the OnExecutionUpdate Event

    Code:
      protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, 
                MarketPosition marketPosition, string orderId, DateTime time)
            {
                if(marketPosition == MarketPosition.Long)
                {
                    SetStopLoss(CalculationMode.Price, high - MaxStop);
                    SetProfitTarget(CalculationMode.Price, high + MaxStop * TakeProfitFaktor);
                }
    
                if (marketPosition == MarketPosition.Short)
                {
                    SetStopLoss(CalculationMode.Price, low + MaxStop);
                    SetProfitTarget(CalculationMode.Price, low - MaxStop * TakeProfitFaktor);
                }
    
            }

    Now, the problem is short orders never work, they get canceled when the Long order gets triggered.
    Setting different OCO strings didn't solve the problem.

    What am I doing wrong here?

    Best regards,
    Johnny

    #2
    Hello Johnny90,

    Thanks for your post.

    The Managed Approach will not allow multiple entries in opposite directions, and does not allow for manual OCO handling. If you would like to accomplish either of these, we suggest using the Unmanaged Approach.

    Please see the Managed Approach's Internal Rules that prevent unwanted positions here - https://ninjatrader.com/support/help...antedPositions

    Unmanaged Approach - https://ninjatrader.com/support/help...d_approach.htm

    Please also note that Set methods prep NinjaTrader to submit the profit target and stop loss after the Enter method gets a fill. They should be fired when the strategy is flat and before the next entry gets fired and should not be used in OnExecutionUpdate. Please see the example below for properly using Set methods.

    SamplePriceModification - https://ninjatrader.com/support/help...of_stop_lo.htm

    If you want to fire your own Profit Target and Stop Loss in OnExecutionUpdate, I recommend following the SampleOnOrderUpdate strategy. I have also attached an unmanaged version that follows the same type of approach.

    SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

    Please let me know if I can be of further assistance.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      Hello Jim,

      thanks for your fast reply. After I copied your code it works mostly but sometimes I get this strange wrong timestamps in the strategy analyzer, leading to an error when submitting SL order because the SL price trades under/over current price bar.
      Click image for larger version

Name:	errorInBacktest.png
Views:	1
Size:	110.1 KB
ID:	1067083


      Any idea what's causing this issue?
      I've attached the strategy if your want to try reproducing this on your side.

      Best regards,
      Johnny




      Attached Files

      Comment


        #4
        Hello Johnny90,

        Thanks for your message.

        The UnmanagedTemplate example utilizes intrabar granularity for more accurate order fills. Essentially this means we submit an order to a smaller data series, like a single tick data series to get a more accurate order fill.

        I noticed you changed this data series to a 60 minute data series, so the the 60 minute data series is being used to simulate the order fill rather than a smaller data series. You could submit the order to BarsInProgress 0, or you can ensure that you are submitting the order to the BarsInProgress of a smaller time frame. I have included some resources for intrabar granularity and how it is used.

        SubmitOrderUnmanaged - https://ninjatrader.com/support/help...runmanaged.htm

        SampleIntrabarbacktest example - https://ninjatrader.com/support/help...ipt_strate.htm

        Multi Time Frame and Instruments - https://ninjatrader.com/support/help...nstruments.htm

        I would suggest testing this out, and then to compare against the original UnmanagedTemplate strategy if you observe any other strange behaviors with your strategy that is not seen in the original.

        We look forward to being of further assistance.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hello Jim,

          ah thanks for the explanation. My Strategy is not working in Tickchart, so I added a 1 minute DataSeries and it's working now.
          Just a small recommendation for feature development: Would be nice of having optional OCO string handling in the managed mode like I tried in post #1, so a lot of code through the unmanaged approach could be saved.

          Best regards,
          Johnny
          Last edited by Johnny90; 08-14-2019, 05:35 AM.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by ct_fx, Today, 05:14 AM
          0 responses
          6 views
          0 likes
          Last Post ct_fx
          by ct_fx
           
          Started by Beauregard, Today, 04:36 AM
          0 responses
          5 views
          0 likes
          Last Post Beauregard  
          Started by fx.practic, Today, 04:34 AM
          0 responses
          7 views
          0 likes
          Last Post fx.practic  
          Started by thomaspartyof10, Today, 02:33 AM
          0 responses
          9 views
          0 likes
          Last Post thomaspartyof10  
          Started by ArmKnuckle, Today, 01:59 AM
          0 responses
          13 views
          0 likes
          Last Post ArmKnuckle  
          Working...
          X