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 NullPointStrategies, 03-13-2026, 05:17 AM
    0 responses
    88 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    151 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    80 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    53 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    62 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X