Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Needs help to figure out what's wrong with GetAtmStrategyRealizedProfitLoss

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

    Needs help to figure out what's wrong with GetAtmStrategyRealizedProfitLoss

    I created a strategy to place orders with the AtmStrategyCreate() method. So far everything works perfectly except when comes the time to record the realised PnL. Most of the time the PnL returned by the GetAtmStrategyRealizedProfitLoss() method is exact but in some cases it is not. It returns 0 as PnL and can't figure out why.

    Here is the portion of the code i use in the OnBarUpdate event. Attached to this comment, there are two screen captures. One shows the Tab order in Ninja showing a buy order with a small profit and the results returned by the GetAtmStrategyRealizedProfitLoss command in the output window. The problem occurs when i close the trade manually by pressing the Close button with the middle mouse button. Any idea wha's going on?

    Code:
    #region MonitorOrderStatus
    //-
    if (orderId.Length > 0)
    {
    
    string[] status = GetAtmStrategyEntryOrderStatus(orderId);
    
    if (status.GetLength(0) > 0)
    {
    if (status[2] == "Filled") {
    IsTradeFilled = true;
    IsStopReadyToMove = true;
    countOnBarUpdateSinceTradeFilled = -1;
    }
    // If the order state is terminal, reset the order id value
    if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
    orderId = string.Empty;
    }
    }
    else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
    {
    // when the trade is over, record the PnL
    PnL_atmStrategy = GetAtmStrategyRealizedProfitLoss( atmStrategyId );
    Print("PnL_atmStrategy read: " + PnL_atmStrategy + " at bar " + CurrentBar);
    // reset the strategy id
    atmStrategyId = string.Empty;
    }
    //-
    #endregion
    Attached Files

    #2
    Hi, thanks for posting.

    GetAtmStrategyRealizedProfitLoss will only report a PnL if the stop or target is filled. NinjaScript strategies were not designed to work with manual trading from chart trader, so if that is a required action the realized PnL will need to be calculated using an Addon-style Account object to detect when trades open and close through OnExecutionUpdate, see here for example documentation:

    https://ninjatrader.com/support/help...ount_class.htm

    Kind regards,
    -ChrisL

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    45 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    21 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    31 views
    1 like
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    50 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    42 views
    0 likes
    Last Post CarlTrading  
    Working...
    X