Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

When to manually capture profit/loss from active trade?

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

    When to manually capture profit/loss from active trade?

    Hi all,

    I originally took some inspiration from JoyDeep on working a manual profit/loss system into my code. It works well, but I think I have it placed too early before a reversal trade is performed.

    So, I'm working in a COBC = false, unmanaged environment. I have profit or loss calculated right before closing an order with the following code:

    Code:
    private void StartLong_Reversal()
    			{
    				if (longOrder == null)
    				{
    				//  Capture the profit and/or loss of this trade before executing the next market order.
    					trade_ProfitLoss_Current = Math.Round(Position.GetProfitLoss(Close[0], PerformanceUnit.Currency),2);
    					trade_ProfitLoss_Totals = trade_ProfitLoss_Totals + trade_ProfitLoss_Current;
    					
    				//  Update individual profit/loss totals
    					if (trade_ProfitLoss_Current > 0)	{trade_ProfitLoss_GrossProfit = Math.Round(trade_ProfitLoss_GrossProfit + trade_ProfitLoss_Current,2);	}
    					else if (trade_ProfitLoss_Current < 0)	{trade_ProfitLoss_GrossLoss = Math.Round(trade_ProfitLoss_GrossLoss + trade_ProfitLoss_Current,2);	}	
    					
    				//  Capture the trend of the current trade for the output window
    					trade_PreviousTrend = 1;
    					
    				//  Sell your current long shares to go short later.
    					longOrder = SubmitOrder(0, OrderAction.Sell, OrderType.Market, numberofShares, 0, 0, "", "trendingLong");
    					longOrder_reversal = true;			//  Activate the longOrder_reversal flag for processing in iExecution	
    					ResetAllVariables();				//  Reset all applicable variables
    				}
    			}
    With this, I get a fairly accurate total of either the gross profit or loss of the trade.
    What I'm wondering is, should these two paragraphs of code....:

    Code:
    //  Capture the profit and/or loss of this trade before executing the next market order.
    					trade_ProfitLoss_Current = Math.Round(Position.GetProfitLoss(Close[0], PerformanceUnit.Currency),2);
    					trade_ProfitLoss_Totals = trade_ProfitLoss_Totals + trade_ProfitLoss_Current;
    					
    				//  Update individual profit/loss totals
    					if (trade_ProfitLoss_Current > 0)	{trade_ProfitLoss_GrossProfit = Math.Round(trade_ProfitLoss_GrossProfit + trade_ProfitLoss_Current,2);	}
    					else if (trade_ProfitLoss_Current < 0)	{trade_ProfitLoss_GrossLoss = Math.Round(trade_ProfitLoss_GrossLoss + trade_ProfitLoss_Current,2);	}
    ... be placed in OnOrderUpdate(IOrder order) or OnExecution(IExecution execution), and checking for a specific order state?

    FYI, the reason I'm doing this and NOT using the Performance class of variables is that I couldn't get RealTime totals from each trade to output. So I went with manually tracking everything within the script and it's going great... except for this one part.

    I'm also not against using the Performance class If anyone has some sample code or references you could point me too. I'd be more than happy to try out some alternative ways of getting this done.

    Thanks in advance for any replies!

    Yours,
    Spider

    #2
    Hello Spider,

    We have two different samples that use the Performance that you may view as references.
    * http://www.ninjatrader.com/support/f...ead.php?t=4084
    * http://www.ninjatrader.com/support/f...ad.php?t=19289

    If those do not help could you clarify what values that you could not get to work and what you are expecting compared to the values you are getting?
    JCNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    670 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    379 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    111 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    575 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    582 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X