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 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:	129
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




      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 agclub, Yesterday, 08:57 PM
          2 responses
          16 views
          0 likes
          Last Post agclub
          by agclub
           
          Started by cre8able, 04-17-2024, 04:16 PM
          6 responses
          56 views
          0 likes
          Last Post cre8able  
          Started by Mindset, 05-06-2023, 09:03 PM
          13 responses
          293 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by kaywai, 09-01-2023, 08:44 PM
          4 responses
          600 views
          0 likes
          Last Post joselube001  
          Started by dpolyakov, 03-29-2024, 11:30 AM
          3 responses
          135 views
          2 likes
          Last Post sonia0101  
          Working...
          X