Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

trailing stop

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

    trailing stop

    hello,
    i'm using some of this code : https://ninjatraderecosystem.com/use...lingstop_101b/ as a template with a few modifications of my own
    but i'm running into some trouble and can't solve it,
    most of the time all is well, except when the trend is going down, and i'm having my strategy re-entering an entry (while having another entry open), and one of the entries hit stop-loss, then when it entering again at a lower price (while i'm still having another open entry), the stoploss and trailing stop are calculated by the Position.AveragePrice (to remind you when the trend is down) , the stoploss is way above the lower price so it never hit it.

    my question is, how would i change the calculation by the last position price and not the average?

    thanks for anyone who can help

    #2
    Hello bugo11,

    Thanks for your post and welcome to the NinjaTrader forums!

    The strategy was written for single order entry only and this is why it is able to use Prosition.AveragePrice because the average of a single entry is the entry price.

    To now add multiple entries you would have add a method to query the order object to see the actual entry price of each order. This does require that you use "signal names" to be able to differential the order objects You could do this in OnOrderUpdate(), reference: https://ninjatrader.com/support/help...rderupdate.htm

    To check the individual order price you would (In OnOrderUpdate()) check the "name" of the order and check for the status of "Filled" and then you can obtain the orders AveragePrice. Here is a brief example:

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

    if (order.Name == "myTest" && orderState == OrderState.Filled)
    {
    Print ("Name: "+order.Name+" Filled at: "+ order.AverageFillPrice);
    }

    if (order.Name == "myTest2" && orderState == OrderState.Filled)
    {
    Print ("Name : "+order.Name + " Filled at: "+ order.AverageFillPrice);
    }

    }


    In your case, once you have tested you would need to create a couple double variables at the class level that you can then assign the order price to in OnOrderUpdate().

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    87 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    132 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    65 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    118 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    67 views
    0 likes
    Last Post PaulMohn  
    Working...
    X