Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Recalculation before an order is submitted in nj script strategy

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

    Recalculation before an order is submitted in nj script strategy

    I have a variable called account value and I have been struggling to implement the following:


    If a trade closed with positive PNL, add the PNL $ amount to account value variable and then use updated account value to calculate new quantity of shares for the next trade.

    If a trade closed with negative PNL, do not update account value.


    The problem is that my strategy is always long or short. For example, lets lets say we trade price and 200ma crossover. Suppose the price is below 200ma and I have been short, now the price crosses above and I have two trades instantaneously: 1) close the short 2) initiate the long.

    However, before the long order is sent out, I want to add the PNL from my short trade, get updated account value, and then recalculate the number of shares to trade (so its always the same % of the account valule), and only then, initiate the long.


    I tried onorderupdate() and onexecutionupdate() and went thru a dozen of code variations but cant achieve the recalculation to happen between trades (so the part in italics). I think its because the trades are basically sent instantaneously and the system doesnt have time to do calculations in between


    Does anyone have any advice on how to solve it?

    Ideally, if someone could edit the template sma crossever startegy that NT provides, to achieve proper order execution like described above, that would be truly amazing! I imagine for someone who knows how to do it it will take 1-2 minutes..​

    #2
    Hello ChartTourist,

    I can provide an example of processing the trade PnL as the position changes.
    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


    You will want to submit an exit order. Detect that exit order has filled in OnExecutionUpdate(), and then calculate the PnL and quantity, and submit the new entry order (all in OnExecutionUpdate()).

    This means instead of immediately submitting the new entry to reverse, you will want to wait until the exit order fills first, then submit the new entry.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Will give it a try.

      Thank you!

      Comment


        #4
        Unfortunately I couldnt make it work. My AccountValue didn't change from the initial set up value at the beginning of the strategy.

        I tried including the code here, but the forum CODE WRAP doesnt handle codes well so I am doing two screenshots instead.


        Here is a simple crossover entry condition for a short position and then exit condition from the same short (I have the same one for the longs, but not including as it here for brevity):
        Number of shares is calculated at the beginning of the strategy as AccountValue/Close[0]

        Click image for larger version  Name:	image.png Views:	0 Size:	33.8 KB ID:	1279268


        And here I am showing how I am trying to updated AccountValue:​

        Click image for larger version  Name:	image.png Views:	0 Size:	249.3 KB ID:	1279267


        Let me know if its clear or if I need to provide more details!

        Thank you.​

        Comment


          #5
          HelloChartTourist,

          Use prints to understand the behavior.

          Is the condition evaluating as true?

          Print the values used in the condition to confirm these are what you expect.

          Print the value of AccountValue before the new value is accumulated.
          Print the ProfitCurrency value.
          Print the value of AccountValue after the new value is accumulated.

          Below is a link to a forum post on using prints to understand behavior.
          https://ninjatrader.com/support/foru...121#post791121

          I think the condition is not evaluating as true. The OrderId is not the signalName. I think you want to be comparing the execution.Name.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            So instead if doing recalculations after each trade I decided to just do it at the end of the day, but I think I found a bug in how SystemPerformance.AllTrades.TradesPerformance.CumP rofit is handled.

            Below I am checking if its 15.59 and if performance of my strategy increased account value (MAV variable) by more than 5%. If it did, I recalculate the share count (CS variable).
            Click image for larger version  Name:	image.png Views:	0 Size:	35.8 KB ID:	1279304
            To achieve that I was using SystemPerformance.AllTrades.TradesPerformance.CumP rofit thinking that it would provide the same number as the Strategy Analyzer's cumulative profit chart
            Click image for larger version  Name:	image.png Views:	0 Size:	86.0 KB ID:	1279305

            However, instead of properly computing CumProfit like the chart does, the script function computes a running sum of individual profit values, which is incorrect.

            Notice how on the chart, my cumulative profit rose about 5% somewhere in March, therefore, my first share count recalculation should have happened in March.

            But if you look at the trade table, it shows that my first share recalculation happened on the 11th trade in January. The reason it happened in January is because the sum of individual profits is >5%, but of course the true cumlative profit is nowhere near 5%.
            Click image for larger version  Name:	image.png Views:	0 Size:	71.6 KB ID:	1279306

            So two questions:

            1) Is it a bug or user error?
            2) How can I access proper running cumulative profit of my strategy?

            Comment


              #7
              Hello ChartTourist,

              Do you have the Analysis display Period set to Trades?
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                No it was left as default Daily period

                Comment


                  #9
                  Hello ChartTourist,

                  Set the Period to Trades if you want to see the cumulative pnl for each trade.
                  Chelsea B.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by NullPointStrategies, Today, 05:17 AM
                  0 responses
                  44 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  124 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  65 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  42 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  46 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X