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

Set Order object to an ExitShortLimit order method

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

    Set Order object to an ExitShortLimit order method

    Hello,

    I´m having trouble using Order objects set to ExitShortLimit for my target and stoploss in an "Advanced Managed Approach" since I don´t want to use SetProfitTarget and SetStopLoss methods. My aim is to have these orders cancel each other once either is filled but cannot figure out why they are not being placed.


    Here is my code:




    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class AddSeriesTest : Strategy
    {

    #region Variables
    private Order EntryOrder = null;
    private Order TargetOrder = null;
    private Order StopLossOrder = null;
    #endregion

    protected override void OnStateChange(){
    if (State == State.SetDefaults)
    {
    Description = @"";
    Name = "AddSeriesTest";
    Calculate = Calculate.OnBarClose;
    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 = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 0;
    IsInstantiatedOnEachOptimizationIteration = true;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;

    }


    }

    protected override void OnBarUpdate()
    {

    if(BarsInProgress == 0 && ToDay(Time[0]) == 20131212 && ToTime(Time[0]) == ToTime(9,32,00) )
    {

    EntryOrder = EnterShortLimit(BarsInProgress,true, 1, Close[0],"Entry");
    TargetOrder = ExitShortLimit(BarsInProgress, true, 1, Close[0] - 1.5 , "ProfitTarget", "Entry");
    StopLossOrder = ExitShortLimit(BarsInProgress, true, 1, Close[0] + 2.5 , "StopLoss", "Entry");
    }
    }




    }

    }

    #2
    Hello Orden33,

    Thanks for your post.

    Our SampleOnOrderUpdate strategy and associated documentation can show you how to properly use Order objects and the OnExecutionUpdate and OnOrderUpdate events and accomplish your goal.

    The issue with your strategy is that it is submitting entry and exit orders at the same time when the Managed Approach requires that the strategy is in a position before you can submit an exit order.

    Please be sure to note the internal rules of the Managed Approach in addition to the resources involved with SampleOnOrderUpdate.

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

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

    Please let us know if you have any questions on these materials.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi Jim, thanks for your reply,

      I have now made the change so the ExitShortLimit orders set when the OnExecutionUpdate method for the entry order is executed but still causes conflict.


      Here is the code:



      namespace NinjaTrader.NinjaScript.Strategies
      {
      public class AddSeriesTest : Strategy
      {

      #region Variables
      private Order EntryOrder = null;
      private Order TargetOrder = null;
      private Order StopLossOrder = null;
      private double EntryPrice;
      #endregion

      protected override void OnStateChange(){
      if (State == State.SetDefaults)
      {
      Description = @"";
      Name = "AddSeriesTest";
      Calculate = Calculate.OnBarClose;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.UniqueEntries;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 30;
      IsFillLimitOnTouch = false;
      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
      OrderFillResolution = OrderFillResolution.Standard;
      Slippage = 0;
      StartBehavior = StartBehavior.WaitUntilFlat;
      TimeInForce = TimeInForce.Gtc;
      TraceOrders = false;
      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      BarsRequiredToTrade = 0;
      IsInstantiatedOnEachOptimizationIteration = true;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;

      }


      }

      protected override void OnBarUpdate()
      {
      /////////////////// Submits order if on a specific bar in the asigned Bars
      if(BarsInProgress == 0 && ToDay(Time[0]) == 20131212 && ToTime(Time[0]) == ToTime(9,32,00) )
      {
      // SetProfitTarget(CalculationMode.Ticks,6);
      // SetStopLoss(CalculationMode.Ticks,9);
      EntryPrice = Close[0];
      Print(EntryPrice);
      EntryOrder = EnterShortLimit(BarsInProgress,true, 1, EntryPrice,"Entry");

      }
      }


      protected override void OnExecutionUpdate (Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
      {

      if(execution.Order.Name == "Entry")
      {

      TargetOrder = ExitShortLimit(BarsInProgress, true, 1, (EntryPrice - 10) , "ProfitTarget", "Entry");
      StopLossOrder = ExitShortLimit(BarsInProgress, true, 1, (EntryPrice + 20) , "StopLoss", "Entry");
      }

      if(execution.Order.Name == "ProfitTarget")
      {

      Print(@"PT executed");

      }

      if(execution.Order.Name == "StopLoss")
      {

      Print(@"SL Executed");
      }
      }

      }

      }

      Comment


        #4
        Hello Orden33,

        I'm not sure what issue you are running into when you mention a conflict.

        Since the SampleOnOrderUpdate is a working example, I would recommend modifying that strategy as needed so the strategy is modelled to use these methods appropriately, and if you incur any issues, you can compare against the original example to check your work.

        Also, as a tip for posting code on the forums, you can place your code in [CODE] blocks so it is properly formatted and easier to read.

        I'm confident that you will be able to accomplish your goal by closely modelling the example. If you are having any issues using the sample provided, please let me know how you are testing so I can do the same on my end and give further feedback.

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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by algospoke, Yesterday, 06:40 PM
        2 responses
        19 views
        0 likes
        Last Post algospoke  
        Started by ghoul, Today, 06:02 PM
        3 responses
        14 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by jeronymite, 04-12-2024, 04:26 PM
        3 responses
        45 views
        0 likes
        Last Post jeronymite  
        Started by Barry Milan, Yesterday, 10:35 PM
        7 responses
        20 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by AttiM, 02-14-2024, 05:20 PM
        10 responses
        180 views
        0 likes
        Last Post jeronymite  
        Working...
        X