Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SampleOnOrderUpdate ==> Short

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

  • NinjaTrader_ChelseaB
    replied
    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.

    Leave a comment:


  • user10
    replied
    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

    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    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

    Leave a comment:


  • user10
    started a topic SampleOnOrderUpdate ==> Short

    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!

Latest Posts

Collapse

Topics Statistics Last Post
Started by ct, Today, 04:16 AM
1 response
3 views
0 likes
Last Post ct
by ct
 
Started by aeadon, Today, 02:27 AM
1 response
5 views
0 likes
Last Post NinjaTrader_Jason  
Started by op1031, Today, 01:52 AM
0 responses
1 view
0 likes
Last Post op1031
by op1031
 
Started by Aramunno, Yesterday, 10:11 PM
2 responses
18 views
0 likes
Last Post Aramunno  
Started by sdauteuil, Yesterday, 09:38 PM
0 responses
6 views
0 likes
Last Post sdauteuil  
Working...
X