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

Getting Realized profit/loss each trade

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

    Getting Realized profit/loss each trade

    I have a custom orders object that contains all my stop orders across two BarsArray, both on same contact, but on differing time frames. I am doing this to manage each of my trades / legs across two different timeframes and multiple diff stop prices.

    For instance, I may have 5 entries on BarsArray[0] and 5 entries on BarsArray[1]. I want to get the Realized Profit/Loss when each trade closes.

    My first thought was to use onPositionUpdate() with position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]); But, I am confused here. ..

    My first questions:
    1) Is the position object associated with a given BarsArray (i.e. only 5 of the 10 entries) or all entries across that contract (BarsArray[0] and BarsArray[1]) ; all 10 entries?

    2) Does the GetUnrealizedProfitLoss have the Realized Profit Loss in the following scenario

    Code:
    OnPositionUpdate(...)
    {
      if (position.MarketPosition == MarketPosition.Flat && marketPosition == MarketPosition.Flat)
      {
        profit = position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]);
      }
    }
    Or should I be using onExecutionUpdate() with the SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].ProfitCurrency:

    Code:
    OnExecutionUpdate(...)
    {
      if (execution.Order.OrderState == OrderState.Filled && (execution.Order.OrderAction == OrderAction.Sell || execution.Order.OrderAction == OrderAction.BuyToCover) )
      {
        //get the closed trade profit/loss
        profitLoss = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].ProfitCurrency;
      }
    Am I guaranteed that SystemPerformance is updated at this point?

    Or do I have to calculate profit based on entry and exit price?

    Thanks,

    #2
    Hello tornadoatc,

    1) Is the position object associated with a given BarsArray (i.e. only 5 of the 10 entries) or all entries across that contract (BarsArray[0] and BarsArray[1]) ; all 10 entries?
    The position would be associated with the series which it was for, generally if you are using a multiseries script you would need to use the plural Positions object to access all position information at any given time. The position which is passed in from the override would be specifically the position which changed which may not include your total strategy position across all instruments.



    2) Does the GetUnrealizedProfitLoss have the Realized Profit Loss in the following scenario
    For fill based logic that generally should go in OnExecutionUpdate however this collection would not be updated at that point, this would also not be up to date in OnPositionUpdate so you owuld need to calculate it yourself in that situation.


    I look forward to being of further assistance.


    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello Jesse,

      When can SystemPerformance object be confidently accessed? Is this only avail from OnBarUpdate?

      From your above statements regarding OnPositionUpdate() and considering MultiDataSeries Scenarios on the same contract you suggest that the state of the positions object in OnPositionUpdate() would be from the first data series or the second. How do you know which ?? Can you access BarsInProgress from OnPositionUpdate()? I don't see any property on the Position object which would tell me this.

      Consider scenario where Strategy is loaded on chart with ES 30 min bars and the AddDataSeries(BarsPeriodType.Minute, 90); is added.
      Last edited by tornadoatc; 10-16-2019, 05:41 PM.

      Comment


        #4
        Hello tornadoatc,

        When can SystemPerformance object be confidently accessed? Is this only avail from OnBarUpdate?
        It would be after it has been updated with trade performance. In the prior examples that does not allow enough time for anything to be recorded in the collection. The next OnBarUpdate call should contain the information.

        ?? Can you access BarsInProgress from OnPositionUpdate()? I don't see any property on the Position object which would tell me this.
        The passed in Position object would have the Instrument as a property which can be used.


        Consider scenario where Strategy is loaded on chart with ES 30 min bars and the AddDataSeries(BarsPeriodType.Minute, 90); is added.
        This scenario is described in the Positions object page when using the Positions object this is the same as the primary or you would just use the primary instruments position index. For the OnPositionUpdate override if the instrument is the same we will just see the position reported, otherwise for a different instrument you may need to use the Instrument property on the position object to find out what position that is for. Alternatively you can use the Positions object directly with the BarsInProgress index if you wanted to find a specific series position. https://ninjatrader.com/support/help...?positions.htm


        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by ghoul, Today, 06:02 PM
        0 responses
        7 views
        0 likes
        Last Post ghoul
        by ghoul
         
        Started by Barry Milan, Yesterday, 10:35 PM
        6 responses
        19 views
        0 likes
        Last Post Barry Milan  
        Started by DanielSanMartin, Yesterday, 02:37 PM
        2 responses
        13 views
        0 likes
        Last Post DanielSanMartin  
        Started by DJ888, 04-16-2024, 06:09 PM
        4 responses
        13 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by terofs, Today, 04:18 PM
        0 responses
        12 views
        0 likes
        Last Post terofs
        by terofs
         
        Working...
        X