• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Daily loss limit examples

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

    #16
    Hello Trader17,

    I have let Chelsea know about your interest in a ready to test strategy that can demonstrate this behavior in addition to using another Calculate mode. The changes involved to this strategy would be minimal as long as it is understood how an intrabar strategy operates.

    To have the example above calculate intrabar, you can set Calculate to OnEachTick or OnPriceChange. Any logic that you would want to have process on bar closes would then be placed within a check for if (IsFirstTickOfBar) and BarsAgo 1 references would be used. I.E. Close[1] in IsFirstTickOfBar would represent the bar that had just closed. A BarsAgo value of 0 would represent the developing bar's value.

    IsFirstTickOfBar - https://ninjatrader.com/support/help...ttickofbar.htm

    Calculate - https://ninjatrader.com/support/help.../calculate.htm

    If you change the strategy to Calculate.OnEachTick or OnPriceChange, line 53 if (Bars.IsFirstBarOfSession) will be true for each tick of that bar. I would suggest adding an IsFirstTickOfBar check with that condition to ensure it only happens once on that bar.

    Please let us know if you have any questions.
    JimNinjaTrader Customer Service

    Comment


      #17
      Thank you. The logic can be On Bar Close. I just want to change the Loss Exit to On Tick so it can exit a position before a bar closes.
      Thank you.

      Comment


        #18
        Hello Trader17,

        You can then set Calculate.OnEachTick or OnPriceChange and then place your trading logic in a check for IsFirstTickOfBar. BarsAgo 0 indexes should then be BarsAgo 1 indexes since you are referencing the bar that had just closed within that logic.

        The block of code checking daily PnL would not have to be inside any logic for IsFirstTickOfBar and can still use BarsAgo 0 references because it is calculating on each tick or price change.

        Code:
        // if in a position and the realized day's PnL plus the position PnL is greater than the loss limit then exit the order
        if (Position.MarketPosition == MarketPosition.Long
                && (currentPnL + Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])) <= -LossLimit)
        {
            //Print((currentPnL+Position.GetProfitLoss(Close[0], PerformanceUnit.Currency)) + " - " + -LossLimit);
            // print to the output window if the daily limit is hit in the middle of a trade
            Print("daily limit hit, exiting order " + Time[0].ToString());
            ExitLong("Daily Limit Exit", "long1");
        }
        We look forward to being of further assistance.
        JimNinjaTrader Customer Service

        Comment


          #19
          Thanks. Will make an attempt to do so. Hopefully Chelsea will get a chance to update it soon too.

          Comment


            #20
            So instead of using On Bar Update we use On Each Tick and in the logic use a condition like if Is First Tick of Bar = True?
            Thanks.

            Comment


              #21
              Hello Trader17,

              OnBarUpdate is a data processing that we regularly use for NinjaScripts. It is controlled by the Calculate setting.

              When Calculate is set to OnEachTick or OnPriceChange, the developing bar (BarsAgo 0 I.E. Close[0]) will constantly be updated in OnBarUpdate with each new tick or price change.

              When Calculate is set to OnBarClose, the developing bar will not update on each tick/price change, and we will be working with the bar that had just closed instead of the developing bar.

              In other words, when using Calculate.OnBarClose, Close[0] will update only when a bar closes. With OnEachTick/OnPriceChange, Close[0] will update with each tick or price change.

              Calculating on bar closes can be done with Calculate.OnEachTick/OnPriceChange if you add a check for IsFirstTick of bar and use BarsAgo 1 references within that block.

              I encourage you to test the Help Guide example code with prints to observe how OnBarUpdate works using different Calculate modes.

              Demo - https://drive.google.com/file/d/1KdV...w?usp=drivesdk

              Please let me know if you have any additional questions.
              JimNinjaTrader Customer Service

              Comment


                #22
                Thanks for the detailed example. Will try it myself and also hopefully wait till Chelsea updates his example too. Buy any strategy technically is constantly running the entire script on each tick and checking for things needed to be done at the close of a bar or intra bar, correct?
                Thank you.

                Comment


                  #23
                  Hello Trader17,

                  When you use a different Calculate mode, the behavior changes for the NinjaScript for how OnBarUpdate develops new bars. A strategy will not update intrabar behind-the-scenes if you are using Calculate.OnBarClose, and it will only update for bar closes.

                  If you simulate OnBarClose when using OnEachTick/OnPriceChange by checking previous bars on IsFirstTickOfBar, then this would be the case.

                  Please let me know if I can be of further assistance.
                  JimNinjaTrader Customer Service

                  Comment


                    #24
                    Thanks Jim. Saw the video too. In there I see in the logic you just used IsFirstTickOfBar. Can it also be written like this? if ( IsFirstTickOfBar == True ) or will I be wrong?
                    Thanks.

                    Comment


                      #25
                      Hello Trader17,

                      Bools return a true or false value and checking if (YourBool == true) is equivalent to checking if(YourBool). It can be very helpful and will at times be necessary to test out the concept to observe the behavior. For example, you could create a couple conditions that print a message if the condition becomes true.

                      if (IsFirstTickOfBar) Print("Condition 1 This is true"");

                      if (IsFirstTickOfBar == true) Print("Condition 2 This is true");

                      We look forward to being of further assistance.
                      JimNinjaTrader Customer Service

                      Comment


                        #26
                        Thanks. Are both statements acceptable as correct in Ninja Script?

                        So in the Script by Chelsea I basically change it ti calculate on each tick or price change and both use the common term IsFirstTickOfBar? And while doing so now the bar currently forming will take "0" as its index and the bar that just closed will take "1' off which I need to calculate the strategy. Correct? As when we use Calculate on bar close then "0" is the one that just closed.

                        Thanks. Ninja Rocks!!!

                        Comment


                          #27
                          Originally posted by NinjaTrader_Jim View Post
                          Hello Trader17,

                          You can then set Calculate.OnEachTick or OnPriceChange and then place your trading logic in a check for IsFirstTickOfBar. BarsAgo 0 indexes should then be BarsAgo 1 indexes since you are referencing the bar that had just closed within that logic.

                          The block of code checking daily PnL would not have to be inside any logic for IsFirstTickOfBar and can still use BarsAgo 0 references because it is calculating on each tick or price change.

                          Code:
                          // if in a position and the realized day's PnL plus the position PnL is greater than the loss limit then exit the order
                          if (Position.MarketPosition == MarketPosition.Long
                          && (currentPnL + Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])) <= -LossLimit)
                          {
                          //Print((currentPnL+Position.GetProfitLoss(Close[0], PerformanceUnit.Currency)) + " - " + -LossLimit);
                          // print to the output window if the daily limit is hit in the middle of a trade
                          Print("daily limit hit, exiting order " + Time[0].ToString());
                          ExitLong("Daily Limit Exit", "long1");
                          }
                          We look forward to being of further assistance.
                          And in this code the first line that uses the Close[0] technically is not a close of the bar forming as I will use OnTickUpdate but just the term "close" is used, correct? How do you get the code in a block in the post? Am I supposed to copy and paste it and put it between Code and /Code?

                          Thanks.
                          Last edited by Trader17; 04-15-2019, 01:32 PM.

                          Comment


                            #28
                            Also using the same code I guess I can also put a limit on Daily Profit as GetUnrealizedProfitLoss returns a positive number for profit and negative number for loss. So I can add another input like ProfitLimit similar to LossLimit if I need to stop trading after achieving a set amount of profit for the day, correct?

                            How do I make a duplicate of this strategy to play around with?

                            Thanks a lot.
                            Last edited by Trader17; 04-15-2019, 01:33 PM.

                            Comment


                              #29
                              Hello Trader17,

                              I have seen your messages.

                              I am approaching the end of my shift and I will be replying tomorrow during our regular office hours.

                              Thanks for your patience.
                              JimNinjaTrader Customer Service

                              Comment


                                #30
                                Hello Trader17,

                                Thanks for your patience.

                                So in the Script by Chelsea I basically change it ti calculate on each tick or price change and both use the common term IsFirstTickOfBar? And while doing so now the bar currently forming will take "0" as its index and the bar that just closed will take "1' off which I need to calculate the strategy. Correct? As when we use Calculate on bar close then "0" is the one that just closed.
                                Changing the script to Calculate.OnEachTick or Calculate.OnPriceChange will have the script's OnBarUpdate method iterate with each new tick or price change. BarsAgo 0 values for Price Series and plots (Close[0]) will update with each new iteration instead of having OnBarUpdate iterate once when the bar closes (Calculate.OnBarClose.) If you would like to have some code calculate on bar closes when using Calculate.OnEachTick or Calculate.OnPriceChange, you will want to use BarsAgo 1 references on the first tick of a new bar. See IsFirstTickOfBar linked in post #16.


                                And in this code the first line that uses the Close[0] technically is not a close of the bar forming as I will use OnTickUpdate but just the term "close" is used, correct? How do you get the code in a block in the post? Am I supposed to copy and paste it and put it between Code and /Code?
                                Close[0] represents the data point for the close of the bar. When calculating on each tick or on price changes, this value is updated with each new tick or price change. Once the bar closes, then the index will shift (I.E. Close[0] becomes Close[1].) There is no OnTickUpdate method. You can use [CODE] blocks similar to [QUOTE] blocks to post formatted code.


                                Also using the same code I guess I can also put a limit on Daily Profit as GetUnrealizedProfitLoss returns a positive number for profit and negative number for loss. So I can add another input like ProfitLimit similar to LossLimit if I need to stop trading after achieving a set amount of profit for the day, correct?

                                How do I make a duplicate of this strategy to play around with?
                                Yes, you will have full control over the strategy logic and could model similar behavior for a daily profit behavior. You can open your strategy in the NinjaScript Editor, right click and select Save As to create a duplicate strategy.

                                We look forward to being of further assistance.
                                JimNinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by dodo-t, Today, 08:22 AM
                                1 response
                                6 views
                                0 likes
                                Last Post NinjaTrader_JoshG  
                                Started by superg3, Today, 12:33 AM
                                1 response
                                9 views
                                0 likes
                                Last Post NinjaTrader_ChrisL  
                                Started by bebbus, Today, 04:50 AM
                                1 response
                                4 views
                                0 likes
                                Last Post NinjaTrader_JoshG  
                                Started by Longhornmark, Today, 06:52 AM
                                1 response
                                5 views
                                0 likes
                                Last Post NinjaTrader_Jim  
                                Started by yeshujbp, Today, 05:33 AM
                                1 response
                                3 views
                                0 likes
                                Last Post NinjaTrader_JoshG  
                                Working...
                                X