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

Daily profit target

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

    #16
    Hi Jesse,
    I probably should of included that on my earlier post. Here it is attached.
    Thanks for the help,
    Andrew
    Attached Files

    Comment


      #17
      Hello adrew99,

      I looked at the changes however I am not certain what you are trying to do with the WeeklyAccumulated value, it is not being set/reset. You do set it one time from State.Defaults but that is not a valid time to gather the value, you would need to move that code into OnBarUpdate. The comments from post #13 would apply here and still need to be changed in your script.

      Lets walk through how the existing accumulation works, that may help to highlight how any other modified accumulation would also need to work.

      The existing accumulation works by checking if it is the reset time and if so storing the days cumulative profit to the DailyAccumulated variable. This is "reset" each day by subtracting this value from the current total cumulative profit.
      Code:
      SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit - DailyAccumulated
      That gives the Difference which is for today. The trade performance values are totals for the time the script was running, if you need a single days value you need to cut the other values out of that total to get your daily value.

      If you are trying to do a similar reset on a weekly scale you can essentially duplicate the daily reset logic. The only part that would change is the time you store the new value to the variable. Rather than using WeeklyAccumulated = 0; it would need to look like the daily reset and be:

      Code:
      WeeklyAccumulated = SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit;


      Then the other logic you have makes sense where it is subtracting the value from the variable:

      Code:
      if ((SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit - WeeklyAccumulated > Weekly_Profit)


      Another item which comes up with your sample is checking a specific day of week. This code will not run if the day of week is not a trading day, for example if Saturday is not a trading day for the instrument being used the code won't work as you expect.

      One other item is that by storing the value only once per week the conditions to stop trading will only work in the following week. If you hit your limit on Tuesday that wont matter until next Sunday, if you want to have it update each day you would have to accumulate the value each day just like the daily limit. I posted about that in post #13 with the sample showing it being accumulated with the daily value.


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

      Comment


        #18
        I think im more confused now then I was before.
        Firstly, In reality, this part of the strategy will be useless in realtime trading, as the system, either yours (nt8) or mine (data feed) restarts over the weekend. So I'm building this into the strategy so that when im running back testing thru playback connection, I dont have to adjust it each week, and can run it via playback for months without touching it. This bring me to the whole point of a "weekly" limit. I'm trying to have the system stop trading if during the current week it reaches it's profit or loss target, in this case 1200. Then, Reset itself on Sunday at first bar of session and trade again for the week. So in your statement above, yes, if the limit is hit on Tuesday, I dont want it to take a position until the the following Sunday, when it resets it's weekly accumulated to ZERO.

        Now for the coding part,
        It's my understanding, that using the example file I sent early, towards the bottom, im asking the system to both reset the Daily accumulated and at the same time, have the days profit/loss added to the weekly accumulated and stored as a variable.

        In your reply above, if I move the Weekly accumulated value from the state.default to the onbarupdate, it would zero out every time there is an update.

        I guess the simplest way to resolve this would be, if you were writing code to have a Weekly profit target hit, and then stop trading until the next week when it resets, what would that look like?


        Comment


          #19
          Hello adrew99 ,

          I guess the simplest way to resolve this would be, if you were writing code to have a Weekly profit target hit, and then stop trading until the next week when it resets, what would that look like?
          What we have been discussing in this thread would be the syntax that would be needed. There is already a lot of information in this thread so it may be helpful to do a more simple test. To move forward from this point it may be easiest to explore the existing daily profit syntax sample and modify it to just do weekly to highlight the syntax used.: https://ninjatrader.com/support/help...nce_statis.htm

          In the sample you could make the same modification we had discussed or adding a DayOfWeek condition to the existing IsFirstBarOfSession condition. This would change the existing daily reset logic into weekly logic. The values in the script would also likely need to be updated to include more trades or more profit/loss to accommodate the added trading days.

          Once that change is made you could test the weekly reset to see it working. This type of logic would be needed if you wanted to do a weekly reset.


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

          Comment


            #20
            This can work for daily as well , removing reset every sunday or monday.....

            public class -->

            Code:
             /// Weekly Target
            private double WeeklyAccumulated = 0;
            state.setdefaults --->

            Code:
             // Weekly Target
            
            WeeklyLossLimit = -200;
            WeeklyProfitLimit = 200;
            OnBarUpdate --->

            Code:
             /// Weekly Target
            if (Time[0].DayOfWeek == DayOfWeek.Sunday || Time[0].DayOfWeek == DayOfWeek.Monday)
            if (Bars.IsFirstBarOfSession)
            
            {
            // Store the strategy's prior cumulated realized profit
            
            WeeklyAccumulated = SystemPerformance.RealTimeTrades.TradesPerformance .Currency.CumProfit;
            /* NOTE: Using . " AllTrades " will include both historical virtual trades as well as real-time trades.
            For only count profits from real-time trades use . " RealTimeTrades " */
            }
            if (SystemPerformance.RealTimeTrades.TradesPerformanc e.Currency.CumProfit - WeeklyAccumulated >= WeeklyProfitLimit
            || SystemPerformance.RealTimeTrades.TradesPerformance .Currency.CumProfit - WeeklyAccumulated <= WeeklyLossLimit)
            {
            Draw.TextFixed(this, "limitText", "Weekly loss / profit reached"
            , TextPosition.BottomRight);
            
            // Halt further processing of our strategy
            return;
            }
            else
            {
            RemoveDrawObject("limitText");
            }
            Properties -->

            Code:
             [NinjaScriptProperty]
            [Display(Name = "WeeklyLossLimit", Description = "Weekly Loss Limit", Order = 3, GroupName = "Target")]
            public double WeeklyLossLimit
            { get; set; }
            
            [NinjaScriptProperty]
            [Display(Name = "WeeklyProfitLimit", Description = "Weekly Profit Limit", Order = 4, GroupName = "Target")]
            public double WeeklyProfitLimit
            { get; set; }
            Last edited by bcomas; 09-11-2020, 05:01 AM.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by rtwave, 04-12-2024, 09:30 AM
            2 responses
            19 views
            0 likes
            Last Post rtwave
            by rtwave
             
            Started by tsantospinto, 04-12-2024, 07:04 PM
            5 responses
            67 views
            0 likes
            Last Post tsantospinto  
            Started by cre8able, Today, 03:20 PM
            0 responses
            6 views
            0 likes
            Last Post cre8able  
            Started by Fran888, 02-16-2024, 10:48 AM
            3 responses
            49 views
            0 likes
            Last Post Sam2515
            by Sam2515
             
            Started by martin70, 03-24-2023, 04:58 AM
            15 responses
            115 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Working...
            X