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

Prevent Overtrading

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

    Prevent Overtrading

    Hello,
    What would be the equivalent of :
    Code:
    [FONT=Consolas][COLOR=#0000ff]protected override void[/COLOR] OnBarUpdate()[/FONT]
    [FONT=Consolas]{[/FONT]
    [FONT=Consolas][COLOR=#008000]   // If the profit on real-time trades is > $1000 stop trading[/COLOR][/FONT]
    [FONT=Consolas]   [COLOR=#0000ff]if[/COLOR] (SystemPerformance.RealTimeTrades.TradesPerformance.Currency.CumProfit > 1000)[/FONT]
    [FONT=Consolas]        return;[/FONT]
    [FONT=Consolas]}[/FONT]
    for a simulation account to be back tested?

    Alternatively,
    I tried using AccountItem via:

    Code:
    If(AccounItem.GrossRealizedProfitLoss > 500)
    return;
    But I get an error cs0019: Operator '>' cannot be applied to operands of type "Ninjatrader.Cbi.AccountItem" and "int"

    #2
    Hello aaadetos,

    The SystemPerformance is the same for sim accounts as for live accounts.

    This is giving you the performance of the trades that have been completed that were submitted from this instance of the script.

    It is calculated the same way as a live account.

    Below is a publicly available link to the help guide on SystemPerformance.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello,
      This isn't working on a backtest of historical data. Yes, I also explicitly set:
      Code:
      IncludeTradeHistoryInBacktest=true;

      Thank you.
      Last edited by aaadetos; 10-12-2017, 01:08 PM.

      Comment


        #4
        Hello aaadetos,

        Thanks for the follow up.

        You may use SystemPerformance.AllTrades to get a trade collection object that you can loop through. You may do whatever you wish to this data while the script is running. The RealTimeTrades object is strictly for real-time trades made by the strategy.

        Example:

        Code:
        protected override void OnBarUpdate()
        {
          if (SystemPerformance.AllTrades.Count > 1)
          {
           //Get the last and first trade.
              Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
              Trade firstTrade = SystemPerformance.AllTrades[0];
         
              Print("The last trade profit is " + lastTrade.ProfitPercent);
              Print("The first trade profit is " + firstTrade.ProfitPercent);
          }
        }
        Please see the following links for more information:
        https://ninjatrader.com/support/help...?alltrades.htm - AllTrades
        https://ninjatrader.com/support/help...collection.htm - TradeCollection
        https://ninjatrader.com/support/help...erformance.htm - TradesPerformance

        Please let us know if we may be of any further assistance.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Thanks Chris.
          I already noted this in my original post. So the question again is, what is the equivalent for historical trades? I just want to be able to do a backtest is all. How do I, in testing historical data (as I would in a back test), stop trading after some certain currency level? I think AccountItem also only works on realtime trades...What script can I use to test this? I hope I'm being clear. Thanks again.

          Comment


            #6
            Hello aaadetos,

            Thanks for the reply.

            I am not using SystemPerformance.RealTimeTrades in my example, I am using SystemPerformance.AllTrades. If you have IncludeTradeHistoryInBacktest be set to true, you will be able to keep track of each trade execution in the backtest.

            Please let me know if I may assist further.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Thanks Chris...still not working.
              I replaced RealTimeTrades with AllTrades and changed the CumProfit amount from 1000 to 1.
              Still not working.

              Comment


                #8
                Hello aaadetos,

                I put together a sample which saves the running PL of the strategy to a variable which is checked against initial account size, and when the strategy is down 10k, it will no longer process. This will work in backtests.

                You would want to enable the bool CutLosses.

                Please let us know if you need further assistance.
                Attached Files
                Alan P.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks @NinjaTrader_AlanP and @NinjaTraderChrisL.
                  After I reviewed Alan's code, I saw that all along, I had added the SystemPerformance.AllTrades code at the end of the strategy in OnBarupdate() rather than at the beginning. Add it before the strategy itself, and presto it works!!

                  Alan, I don't understand what xAccountSize is. I don't see it initialized anywhere, so I assumed it to be a typo, such that
                  Code:
                  cutOff = XAccountSize - MaxLossToHaltTrading
                  , rather than
                  Code:
                  xAccountSize-MaxLossToHaltTrading
                  , like you wrote; but when I modified it for the problem I'm trying to solve, it didn't solve it, while using AllTrades before the strategy did.

                  However, there is a new problem...How do I track DailyPL? DailyPL isn't one of the TradePerformance values and CumProfit will still run as many trades per day until the CumProfit is reached, which isn't my intent. My intent is to stop daily trading once a certain realized profitloss is attained. Otherwise, is there a way to initialize or manipulate the CumProfit each day?

                  Thanks again.
                  Last edited by aaadetos; 10-19-2017, 09:38 AM.

                  Comment


                    #10
                    Hello aaadetos,

                    The code I provided will stop trading once a certain loss is reached. You would have to modify the operator if you wanted it to stop trading once a certain period of profit was reached.

                    XAccountSize is declared on line 165.

                    To track Daily PL you could create a data series and assign that data series the value of xAccountSize on each day.

                    Please let us know if you need further assistance.
                    Alan P.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by ZenCortexCLICK, Today, 04:58 AM
                    0 responses
                    1 view
                    0 likes
                    Last Post ZenCortexCLICK  
                    Started by sidlercom80, 10-28-2023, 08:49 AM
                    172 responses
                    2,279 views
                    0 likes
                    Last Post sidlercom80  
                    Started by Irukandji, Yesterday, 02:53 AM
                    2 responses
                    17 views
                    0 likes
                    Last Post Irukandji  
                    Started by adeelshahzad, Today, 03:54 AM
                    0 responses
                    4 views
                    0 likes
                    Last Post adeelshahzad  
                    Started by Barry Milan, Yesterday, 10:35 PM
                    3 responses
                    13 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Working...
                    X