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

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
    Chris L.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by andrewtrades, Today, 04:57 PM
    1 response
    5 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by chbruno, Today, 04:10 PM
    0 responses
    4 views
    0 likes
    Last Post chbruno
    by chbruno
     
    Started by josh18955, 03-25-2023, 11:16 AM
    6 responses
    436 views
    0 likes
    Last Post Delerium  
    Started by FAQtrader, Today, 03:35 PM
    0 responses
    7 views
    0 likes
    Last Post FAQtrader  
    Started by rocketman7, Today, 09:41 AM
    5 responses
    19 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Working...
    X