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

Confused about trace order output

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

    Confused about trace order output

    my strategy sets the stoploss/profit target statically and calls EnterShortStopLimit() within an indicator event. however, in the trace order output i am confused why there are cancelled profit target orders and if the profit target was reached why that price is not indicated in the output i.e. averageFillPrice. below is the TO output and code that generates the orders. any help is appreciated as i am new to trading and trying to wrap my head around how/when these orders are created/filled/cancelled.

    OUTPUT
    =====

    VALID Trend -- Strength: Strongest Direction Down Bar Index: 97
    bar index: 97 short: price 3952.5, limit 3952.25, stop 3952

    1/3/2023 6:56:42 AM Strategy 'MySecondEntryStrategy/-1': Entered internal SubmitOrderManaged() method at 1/3/2023 6:56:42 AM: BarsInProgress=0 Action=SellShort OrderType=StopLimit Quantity=1 LimitPrice=3952.25 StopPrice=3952.00 SignalName='short97' FromEntrySignal=''

    FILLED Order -- (Name: short97 Limit: 3952.25 Stop: 3952) was filled for 3952.25

    1/3/2023 7:00:25 AM Strategy 'MySecondEntryStrategy/-1: Cancelled pending exit order, since associated position is closed, orderId='NT-00005-156' account='Backtest' name='Profit target' orderState=Working instrument='ES SEP23' orderAction=BuyToCover orderType='Limit' limitPrice=3951.25 stopPrice=0 quantity=1 tif=Gtc oco='NT-00002-156' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-01-03 07:00:25' gtd='2099-12-01' statementDate='2023-09-05'

    1/3/2023 7:00:25 AM Strategy 'MySecondEntryStrategy/-1': Cancelled OCO paired order: BarsInProgress=0, orderId='NT-00005-156' account='Backtest' name='Profit target' orderState=Cancelled instrument='ES SEP23' orderAction=BuyToCover orderType='Limit' limitPrice=3951.25 stopPrice=0 quantity=1 tif=Gtc oco='NT-00002-156' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-01-03 07:00:25' gtd='2099-12-01' statementDate='2023-09-05'


    CODE
    ====

    protected override void OnStateChange() {
    if (State == State.SetDefaults) {
    SetDefaults();
    }
    else if (State == State.Configure) {
    ClearOutputWindow();
    TraceOrders = TraceMyOrders;
    _direction = Direction;
    _positionSize = PositionSize;
    _entryIndicator = MySecondEntryIndicator(Direction);
    _entryIndicator.EntryAlertEvent += EntryAlertEvent;
    AddChartIndicator(_entryIndicator);

    SetStopLoss(CalculationMode.Ticks, 8);
    SetProfitTarget(CalculationMode.Ticks, 4);
    }
    }

    private void EntryAlertEvent(object sender, EntryAlertEventArgs e) {
    var price = e.Price.Value;
    var barIndex = e.Price.BarIndex;
    double entryStopPrice = 0;
    double entryLimitPrice = 0;
    string signalName = "{0}" + barIndex;
    switch (Direction) {
    case PositionDirection.Long:
    // implement later
    break;
    case PositionDirection.Short:
    entryLimitPrice = price - TickSize;
    entryStopPrice = (price - (2 * TickSize));
    signalName = string.Format(signalName, "short");

    if (_shortEntryOrder != null) {
    Print(string.Format(
    "CANCELLED Order -- (Name: {0} Limit: {1} Stop: {2}) was cancelled because it was not filled prior to entering a
    new short position.",
    _shortEntryOrder.Name,
    _shortEntryOrder.LimitPrice,
    _shortEntryOrder.StopPrice
    ));
    CancelOrder(_shortEntryOrder);
    _shortEntryOrderName = string.Empty;
    _shortEntryOrder = null;
    }

    _shortEntryOrderName = signalName;
    _shortEntryOrder = EnterShortStopLimit(0, true, PositionSize, entryLimitPrice, entryStopPrice, signalName);
    Print(string.Format("bar index: {0} short: price {1}, limit {2}, stop {3} ",
    e.Price.BarIndex, price, entryLimitPrice, entryStopPrice));

    break;
    }
    }

    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice,
    int quantity, int filled, double averageFillPrice, OrderState orderState,
    DateTime time, ErrorCode error, string nativeError) {

    if (order.Name == _shortEntryOrderName && orderState == OrderState.Filled)
    Print(string.Format("FILLED Order -- (Name: {0} Limit: {1} Stop: {2}) was filled for {3}",
    order.Name,
    order.LimitPrice,
    order.StopPrice,
    order.AverageFillPrice
    ));

    // One time only, as we transition from historical. Convert any old historical order object
    // references to the live order submitted to the real-time account.
    if (_shortEntryOrder != null && _shortEntryOrder.IsBacktestOrder && State == State.Realtime)
    _shortEntryOrder = GetRealtimeOrder(_shortEntryOrder);

    // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
    // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment
    // is not gauranteed to be complete if it is referenced immediately after submitting
    else if (order.Name == _shortEntryOrderName && orderState != OrderState.Filled)
    _shortEntryOrder = order;


    // Null Entry order if filled or cancelled. We do not use the Order objects after the order is
    // filled, so we can null it here
    if (_shortEntryOrder != null && _shortEntryOrder == order) {
    if (order.OrderState == OrderState.Cancelled && order.Filled == 0) {
    _shortEntryOrderName = string.Empty;
    _shortEntryOrder = null;
    }
    if (order.OrderState == OrderState.Filled)
    _shortEntryOrderName = string.Empty;
    _shortEntryOrder = null;
    }
    }

    #2
    Hello love2code2trade,

    Thanks for your post.

    The TraceOrder line "Cancelled pending exit order, since associated position is closed" indicates that the pending exit order (ProfitTarget) was canceled because the entry order position was closed.

    1/3/2023 7:00:25 AM Strategy 'MySecondEntryStrategy/-1: Cancelled pending exit order, since associated position is closed, orderId='NT-00005-156' account='Backtest' name='Profit target' orderState=Working instrument='ES SEP23' orderAction=BuyToCover orderType='Limit' limitPrice=3951.25 stopPrice=0 quantity=1 tif=Gtc oco='NT-00002-156' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-01-03 07:00:25' gtd='2099-12-01' statementDate='2023-09-05'

    1/3/2023 7:00:25 AM Strategy 'MySecondEntryStrategy/-1': Cancelled OCO paired order: BarsInProgress=0, orderId='NT-00005-156' account='Backtest' name='Profit target' orderState=Cancelled instrument='ES SEP23' orderAction=BuyToCover orderType='Limit' limitPrice=3951.25 stopPrice=0 quantity=1 tif=Gtc oco='NT-00002-156' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-01-03 07:00:25' gtd='2099-12-01' statementDate='2023-09-05'


    Note that any orders that are linked with OCO will be canceled if the other order is filled. For example, if the stop loss is filled then the profit target order would be canceled.

    You could review the full TraceOrders output and use prints to better understand exactly how your strategy is behaving and managing orders.

    Below is a link to a forum post that demonstrates how to use prints and TraceOrders to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121
    Brandon H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by sugalt, 04-30-2024, 04:02 AM
    2 responses
    13 views
    0 likes
    Last Post sugalt
    by sugalt
     
    Started by merc410, Today, 03:41 AM
    0 responses
    3 views
    0 likes
    Last Post merc410
    by merc410
     
    Started by Ndakotan1313, 03-14-2024, 05:02 PM
    2 responses
    60 views
    0 likes
    Last Post blaise_code  
    Started by claxxical, 05-30-2017, 12:30 PM
    37 responses
    4,457 views
    0 likes
    Last Post Padan
    by Padan
     
    Started by SugarDefwebsite, Today, 02:18 AM
    0 responses
    4 views
    0 likes
    Last Post SugarDefwebsite  
    Working...
    X