Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unrealized PnL Backtesting

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

    Unrealized PnL Backtesting

    Hi traders!
    I have a problem with the unrealized PnL in a strategy analyzer!
    This code work's in the real time, but not in backtesting, why?
    Thank's in advance

    double cumProfit = Performance.AllTrades.TradesPerformance.Currency.C umProfit;
    double curPosition = Position.GetProfitLoss( Close[0], PerformanceUnit.Currency );
    double totCumProfit = cumProfit + curPosition ;

    {
    do something
    }

    #2
    Hello,

    Thank you for the question.

    I would like to ask, what is the output you are getting from these methods in backtest vs realtime? are they reporting incorrectly or as zeros, can you provide more details on what you are seeing or the output you are getting?

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

    Comment


      #3
      Hi Jesse, thank's forma the reply!
      I would Like to close the position if a daily gain or loss is hit.
      I write this..
      (Reset Performance.AllTrades.TradesPerformance.Currency.C umProfit at the first bar..)
      double cumProfit = Performance.AllTrades.TradesPerformance.Currency.C umProfit;
      double curPosition = Position.GetProfitLoss( Close[0], PerformanceUnit.Currency );
      double totCumProfit = cumProfit + curPosition ;

      {
      if(totCumProfit > dailygain ||
      TotCumProfit < dailyloss)
      \\buy or sell..
      }
      The code is OK because realtime works..only in backtet this noto work..

      Comment


        #4
        Hello,

        Thank you for the reply.

        I am still unsure on the specific output you are getting regarding this, I will test the code provided but it would be more helpful if you could tell me what you are seeing as the output values. Are the values reporting incorrect, as zeros, can you provide more specific details on the question?

        Edit:

        I have tested the supplied code and have values in backtest along with realtime, I would need further clarification on what the actual problem is that you are having, along with any output that can show what is happening.

        I look forward to being of further assistance.
        Last edited by NinjaTrader_Jesse; 09-17-2015, 08:57 AM.
        JesseNinjaTrader Customer Service

        Comment


          #5
          if ( Bars.FirstBarOfSession == true )

          {
          Performance.AllTrades.TradesPerformance.Currency.C umProfit = 0;

          }

          double cumProfit = Performance.AllTrades.TradesPerformance.Currency.C umProfit;
          double curPosition = Position.GetProfitLoss( Close[0], PerformanceUnit.Currency );
          double totCumProfit = cumProfit + curPosition ;

          if( Position.MarketPosition != MarketPosition.Flat &&
          ( totCumProfit > dailyProfit ||
          totCumProfit < -dailyLoss ))

          {
          if( Position.MarketPosition == MarketPosition.Long )

          {
          _Close= SubmitOrder( );
          CancelAllOrders( true, true );
          }

          This is the code.. In realtime it works,only in backtest not close the position.

          My goal is to close position if the sum of Realized and unrealized is greater or lower than daily target or stop.

          Comment


            #6
            Hello,

            Thank you for providing more of the code.

            Have you printed the values you are getting to determine why the condition is not working? This would be the only way to determine why this statement is not working. Because I only have a general idea of how this is being run on your end, it would be best for you to test this on your end to find the problem.

            I would suggest starting with Print() statements to figure out what part of the condition is different in backtest vs realtime.
            Printing the Variables would be a good start, so something like the following would work to determine what exact values the condition consists of:

            Code:
            Print(Position.MarketPosition + " " + totCumProfit + " > " + dailyProfit + " || " + totCumProfit + " < " + -dailyLoss);
            This would print to the Tools -> Output window where you can view the value as they are evaluated. This would tell you which part of the condition is true and which part is not.

            Please let me know if I may be of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Thank's for the replay Jesse! I attached two screenshot of the situation in backtest!
              In the output window i look the daily target is strike, but in the strategy analyzer it's hold the position..
              Attached Files

              Comment


                #8
                Hello,

                Thank you for the reply.

                Are you saying that the difference between backtest and realtime is that in backtest the position is held until ExitOnClose where in realtime this does not occur? Can you confirm this is what you were saying in your prior post?

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

                Comment


                  #9
                  Sorry, i don't understand.
                  If the position is long and i sell while the trade is on (in the output window i have the sell condition), in the strategy analyzer we can't sell??

                  I use ever ExitOnClose = true and COBC=true.
                  Last edited by bergimax; 09-17-2015, 12:35 PM.

                  Comment


                    #10
                    I find the problem!
                    I sell and than i use cancelallorders, but if i reverse the two script it works! thank's for the assistance Jesse!

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by pechtri, 06-22-2023, 02:31 AM
                    9 responses
                    122 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by frankthearm, 04-18-2024, 09:08 AM
                    16 responses
                    66 views
                    0 likes
                    Last Post NinjaTrader_Clayton  
                    Started by habeebft, Today, 01:18 PM
                    1 response
                    5 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by benmarkal, Today, 12:52 PM
                    2 responses
                    14 views
                    0 likes
                    Last Post benmarkal  
                    Started by f.saeidi, Today, 01:38 PM
                    1 response
                    8 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Working...
                    X