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 CarlTrading, 03-31-2026, 09:41 PM
    1 response
    77 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    40 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    63 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    63 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    53 views
    0 likes
    Last Post CarlTrading  
    Working...
    X