• 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

SampleOnOrderUpdate ==> Short

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

    SampleOnOrderUpdate ==> Short

    Hello to the Forum,
    please support me with the following question:

    I'm trying to rewrite SampleOnOrderUpdate to the short position.

    For this I use the following code:

    PHP Code:

    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class 
    SampleOnOrderUpdate Strategy
        
    {
            private 
    Order entryOrder                        null;
            private 
    Order stopOrder                         null;
            private 
    Order targetOrder                       null;

            private 
    Order entryOrderShort                    null;
            private 
    Order stopOrderShort                    null;
            private 
    Order targetOrderShort                  null;

            
    bool tradeLongtradeShorttestBreakEven;
            
    double deltaBreakEven;

            protected 
    override void OnStateChange()
            {
                
    #region settings

                
    if (State == State.SetDefaults)
                {
                    
    Description                        = @"Sample ";
                    
    Name                            "SampleOnOrderUpdate";
                    
    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                     false;
                    
    RealtimeErrorHandling           RealtimeErrorHandling.StopCancelClose;
                    
    StopTargetHandling              StopTargetHandling.PerEntryExecution;
                    
    BarsRequiredToTrade             20;
                }

                else if (
    State == State.Realtime)
                {
                    if (
    entryOrder  != nullentryOrder =  GetRealtimeOrder(entryOrder);
                    if (
    stopOrder   != nullstopOrder =   GetRealtimeOrder(stopOrder);
                    if (
    targetOrder != nulltargetOrder GetRealtimeOrder(targetOrder);

                    
    //  Short
                    
    if (entryOrderShort != null)
                    
    entryOrderShort GetRealtimeOrder(entryOrderShort);
                    if (
    stopOrderShort != null)
                    
    stopOrderShort =  GetRealtimeOrder(stopOrderShort);
                    if (
    targetOrderShort!= null)
                    
    targetOrderShort GetRealtimeOrder(targetOrderShort);
                }

                
    #endregion
            
    }

            protected 
    override void OnBarUpdate()
            {
                
    tradeLong false;
                
    tradeShort true;

                if (
    State == State.Realtime)
                {
                
    #region Long    

                
    if (tradeLong// OCO ==> ++
                
    {    
                    if (
    entryOrder == null && Close[0] > Open[0]
                    && 
    CurrentBar BarsRequiredToTrade)
                    {
    EnterLong(1"MyEntry");}

                    
    // BreakEven
                    
    if (Position.MarketPosition == MarketPosition.Long
                    
    && Close[0] >= Position.AveragePrice 5)
                    {
                        if (
    stopOrder != null
                        
    && stopOrder.StopPrice Position.AveragePrice)
                        {
                            
    stopOrder ExitLongStopMarket(0truestopOrder.Quantity,
                            
    Position.AveragePrice 3"MyStop""MyEntry"); // Trailing ++
                        
    }
                    }    
                }

                
    #endregion

                #region Short

                
    if (tradeShort)
                {    
                    if (
    Position.AveragePrice 0deltaBreakEven =
                    
    Position.AveragePrice 10;
                    
    testBreakEven Close[0] < (deltaBreakEven);

                    if (
    entryOrderShort == null && Close[0] < Open[0]
                    && 
    CurrentBar BarsRequiredToTrade)
                    {
    EnterShort(1"MyEntryShort");}

                    
    // BreakEven
                    
    if (Position.MarketPosition == MarketPosition.Short && testBreakEven)     
                        {
                            if (
    stopOrderShort != null
                            
    && stopOrderShort.StopPrice Position.AveragePrice)
                            {
                                
    stopOrderShort ExitShortStopMarket(0true,
                                
    stopOrderShort.Quantity,
                                
    Position.AveragePrice"TrailStop""MyEntryShort");
                            }
                        }
                    }
                    
    #endregion

                
    }
            }

            protected 
    override void OnOrderUpdate(Order order,
            
    double limitPricedouble stopPriceint quantityint filled,
            
    double averageFillPriceOrderState orderState,
            
    DateTime timeErrorCode errorstring nativeError)
            {
                
    #region Long

                
    if (order.Name == "MyEntry")
                {    
                    
    entryOrder order;
                    if (
    order.OrderState == OrderState.Cancelled
                    
    && order.Filled == 0entryOrder null;
                }
                
    #endregion

                #region Short
                
    if (order.Name == "MyEntryShort")
                {    
                    
    entryOrderShort order;
                    if (
    order.OrderState == OrderState.Cancelled
                    
    && order.Filled == 0entryOrderShort null;
                }
                
    #endregion
            
    }

            protected 
    override void OnExecutionUpdate(Execution execution,
            
    string executionIddouble priceint quantity,
            
    MarketPosition marketPositionstring orderIdDateTime time)
            {
                
    #region Long
                
    if (entryOrder != null && entryOrder == execution.Order)
                {
                    if (
    execution.Order.OrderState == OrderState.Filled
                    
    || execution.Order.OrderState == OrderState.PartFilled
                    
    || (execution.Order.OrderState == OrderState.Cancelled
                    
    && execution.Order.Filled 0))
                    {
                        
    stopOrder ExitLongStopMarket(0trueexecution.Order.Filled,
                        
    execution.Order.AverageFillPrice "MyStop""MyEntry");
                        
    targetOrder ExitLongLimit(0trueexecution.Order.Filled,
                        
    execution.Order.AverageFillPrice 15 "MyTarget""MyEntry");
                        if (
    execution.Order.OrderState != OrderState.PartFilled)
                            
    entryOrder null;
                    }
                }

                if ((
    stopOrder != null && stopOrder == execution.Order)
                || (
    targetOrder != null && targetOrder == execution.Order))
                {
                    if (
    execution.Order.OrderState == OrderState.Filled
                    
    || execution.Order.OrderState == OrderState.PartFilled)
                    {
                        
    stopOrder nulltargetOrder null;
                    }
                }
                
    #endregion

                #region Short

                
    if (entryOrderShort != null && entryOrderShort == execution.Order)
                {
                    if (
    execution.Order.OrderState == OrderState.Filled
                        
    || execution.Order.OrderState == OrderState.PartFilled
                        
    || (execution.Order.OrderState == OrderState.Cancelled
                        
    && execution.Order.Filled 0))
                    {
                        
    stopOrderShort ExitShortStopMarket(0trueexecution.Order.Filled,
                        
    execution.Order.AverageFillPrice 10 "InitStop""MyEntryShort");
                        
    targetOrderShort ExitShortLimit(0trueexecution.Order.Filled,
                        
    execution.Order.AverageFillPrice 20 "Target""MyEntryShort");
                        if (
    execution.Order.OrderState != OrderState.PartFilled)
                            
    entryOrderShort null;
                    }
                }

                if ((
    stopOrderShort != null && stopOrderShort == execution.Order)
                || (
    targetOrderShort != null && targetOrderShort == execution.Order))
                {
                    if (
    execution.Order.OrderState == OrderState.Filled
                    
    || execution.Order.OrderState == OrderState.PartFilled)
                    {
                        
    stopOrderShort nulltargetOrderShort null;
                    }
                }    

                
    #endregion
            
    }
        }


    For the calculation of the break-even I cannot use PositionAveragePrice + x as on the LongPosition.
    PositionAveragePrice - x sends a continuous signal to the broker; the position is opened and immediately closed again.

    I therefore calculate the Delta (EntryPrice - x) and use the condition testBreakEven. This enables me to control the trading process correctly.
    Is this procedure correct?

    Unlike the long position, OCO does not work with this programming.
    I get two stop prices. How can I change this?

    Thank you very much for your support!

    #2
    Hello user10,

    Position.AveragePrice is the entry price for the position.
    https://ninjatrader.com/support/help...erageprice.htm

    When you mention the position becomes immediately closed, is the exit order filling?
    If you set the price to a further distance below the current bid does the order still fill immediately?

    For a long position, placing the exit a specific number of ticks below the Position.AveragePrice would be correct.
    https://ninjatrader.com/support/help...8/ticksize.htm

    Below I am providing a link to a few examples I have created of custom exit order movements.
    https://ninjatrader.com/support/foru...269#post802269

    For OCO, you would need to switch to using the unmanaged approach and submit orders with SubmitOrderUnamanged().
    https://ninjatrader.com/support/help...d_approach.htm
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello Chelsea,

      thank you for your advice.

      I have now changed my code as follows:

      PHP Code:
      if (stopOrderShort != null && stopOrderShort.OrderState == OrderState.Accepted
          
      || stopOrderShort.OrderState == OrderState.Working
          
      && stopOrderShort.StopPrice Position.AveragePrice)
      {
          
      // Modifies stop-loss to breakeven
          
      ExitShortStopMarket(0truestopOrderShort.Quantity,
              
      Position.AveragePrice 1"TrailStop""MyEntryShort");
          
      // Cancel InitOrder
          
      CancelOrder(stopOrderShort);

      Is that correct?

      Yours sincerely

      Comment


        #4
        Hello user10,

        You mentioned the issue is the order becomes immediately closed, is this correct?

        I am seeing that CancelOrder() is being called.

        Are you trying to cancel the order you are currently placing?

        For a exit short stop order this would be a buy order. A buy stop must be above the market price.
        Is Position.AveragePrice - 1 above the current ask price?
        If not, this order would be rejected for being on the wrong side of the market.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by cassiano_sanches, Today, 07:37 PM
        0 responses
        6 views
        0 likes
        Last Post cassiano_sanches  
        Started by terminatorT1000, Today, 06:40 PM
        0 responses
        6 views
        0 likes
        Last Post terminatorT1000  
        Started by Giudiver, Today, 04:29 PM
        0 responses
        9 views
        0 likes
        Last Post Giudiver  
        Started by mlarocco, 03-03-2018, 03:54 PM
        12 responses
        1,300 views
        0 likes
        Last Post SawDr
        by SawDr
         
        Started by TakingStock12, Today, 04:05 PM
        2 responses
        17 views
        0 likes
        Last Post NinjaTrader_ShawnB  
        Working...
        X