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

Script to track profit and loss range on a strategy

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

    Script to track profit and loss range on a strategy

    I would like to track the range of the PNL from trades on the chart that I am running my strategy. I need to know the maximum profit and maximum loss range at any given time for the trading session. How would I do that?

    #2
    Hello EminiMES,

    The Position.GetUnrealizedProfitLoss() can provide the unrealized PnL, while the SystemPerformance collection will provide the realized PnL.


    Hello, I've updated the DailyLossLimit and DailyLosLimitMultiTrade examples that were posted on the forum for NinjaTrader 7 for NinjaTrader 8. These are often requested and I felt they are good examples to have for NT8. DailyLossLimitExample_NT7 - http://ninjatrader.com/support/forum...241#post451241 (http://ninjatrader


    To track the highest high or lowest low, use a variable.

    If the new value is higher than what is saved to the variable, save the new value to the variable.
    if (newPnL > savedPnL)
    savedPnL = newPnL;
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks ChelseaB... new to script. Can that be made into an indicator that would calculate at the end of the bar like the Range indicator? If so....How?

      Comment


        #4
        Hello EminiMES,

        Below is a link to a forum post with helpful resources on getting started with C# and NinjaScript.


        This would calculate based on the Calculate setting. If Calculate is OnBarClose this would calculate when the bar closes. If Calculate is OnPriceChange this would calculate when the price changes.

        The position itself is updated when OnPositionUpdate() is run.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks....

          Comment


            #6
            Hi EminiMES,

            One thing I have done to help with a similar concept is to use Account.Get(AccountItem.NetLiquidation, Currency.UsDollar). and after every bar (1 min / 5 min, etc) I use the print function to output my current account value (IF Close>= Open OR IF Close<= Open, THEN print the above code for net liquidation - if you create this strategy in the builder and any print as the actions, replace inside the print the net liquidation code (it's not in the builder). This helps because I paste these values into Excel, create a graph that then shows me a bar by bar value of my account - you would be able to see the max range from this, and by counting the x axis (1,2,3) against the bar time, you'd be able to look back at your chart and see when your account was the lowest/highest on the chart and can deduce which trades led you there. Hope this helps!

            Comment


              #7
              Thanks Austiner87.......

              Comment


                #8
                Hello EminiMES,

                Indicators are not Strategies and do not designed to placed orders or have an internal position.

                The Position and SystemPerformance are exclusive to NinjaScript Strategies. Try this code in a strategy that places orders instead of an indicator.


                Through the Addon approach an indicator can check the Account position and get the Account RealizedPnL.

                However, this would not the the Strategy performance you have said you were looking for in post #1.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  I would like an indicator to track the profit and loss on each bar. Not place orders. The strategy script I use does that already but I have to enter the range manually for the daily profit target and daily loss target. I am trying to determine the daily profit and loss range in order to determine the best manual inputs in the strategy.

                  I used the same code as the range indicator since that is how I want the indicator to appear on the chart but then got stuck with using Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0] as you suggested. In the range indicator it uses just Value[0] = High[0] - Low[0];. I just don't know how to incorporate Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0] and if (newPnL > savedPnL) savedPnL = newPnL; as you suggested.

                  Comment


                    #10
                    Thanks ChelseaB,

                    I just reread your post and now understand that Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0] can't be used in a indicator. I guess I would like to use plot then instead in the startegy. I currently plot the profit target in the strategy so I understand how that works and will take a stab at it inside the strategy.

                    Comment


                      #11
                      Hi EminiMES,

                      you could alternatively create an action for a drawing object for 'Text' on your chart that tells you the net liquidation of your accoutn each time you exit/enter a trade, or at an interval (like a 5 min bar). This would tell you your account value at each trade or interval, so if you looked back at the chart, you could see when your account was at the highest or lowest. This would accomplish something close to what you want to create on the indicator.

                      remember to offset to the Y axis to make it clear

                      Comment


                        #12
                        Hello EminiMES,

                        You can send values from a strategy to a hosted indicator for plotting.

                        See the reference sample SampleStrategyPlot in the help guide.
                        https://ninjatrader.com/support/help..._a_ninjasc.htm

                        If you are not specifically trying to set plots with AddPlot() which specifically must be done in an indicator, then any drawing with Draw methods can be drawn directly from the strategy.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          ClelseaB..

                          I use the following to determine if it is ok to trade based on the cumulative daily profit and loss.
                          if (

                          ((SystemPerformance.AllTrades.TradesPerformance.Cu rrency.CumProfit &gt;= DPTarg)
                          || (SystemPerformance.AllTrades.TradesPerformance.Cur rency.CumProfit &lt;= DSTarg)))

                          OkToTrade = false;

                          I use the following to draw profit target lines on each bar when in a position.

                          // In Short Position, drawing lines
                          Draw.Line(this, "profittarget" + CurrentBar, false, 1, Low[1] - (Tsz*TickSize), -4, Low[1] - (Tsz*TickSize) , Brushes.Red, DashStyleHelper.Dot, 4, DrawOnPricePanel);

                          ProfitTarget[0] = Low[1] - (Tsz*TickSize);

                          How would I draw a line to track the SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit?

                          Comment


                            #14
                            Thanks Austiner87..

                            I have not used text before. Please explain the process to do what you suggested.

                            Comment


                              #15
                              Hello EminiMES,

                              The CumProfit is a double which can be used for the start and end price.

                              Code:
                              Draw.Line(this, "MyProfit" + CurrentBar, true, 5, SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit, 0, SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit, Brushes.Blue);
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Haiasi, 04-25-2024, 06:53 PM
                              2 responses
                              17 views
                              0 likes
                              Last Post Massinisa  
                              Started by Creamers, Today, 05:32 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post Creamers  
                              Started by Segwin, 05-07-2018, 02:15 PM
                              12 responses
                              1,786 views
                              0 likes
                              Last Post Leafcutter  
                              Started by poplagelu, Today, 05:00 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post poplagelu  
                              Started by fx.practic, 10-15-2013, 12:53 AM
                              5 responses
                              5,408 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Working...
                              X