Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Measure Risk/Reward ratio before entry

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

    Measure Risk/Reward ratio before entry

    Hi all,

    I'm currently working on a strategy and 1 of the criteria must be to measure the Risk/Reward ratio before it enters a trade. For this we need to know the stoploss and Target 1 of course.

    For my stoploss I'm using the lowest low of the past 96 bars and get 10 pips below that price.
    Target 1 = EMA

    if (CCI(256)[0] > -50 && CCI(256)[1] > -50 && CCI(256)[2] > -50 && CrossAbove(CCI(256), -50, 3) && ((EMA(10)[0] - Close[0]) / ((Close[0] - Low[LowestBar(Low, 96)] - 10 * TickSize) >= 0.90))
    {
    EnterLong(30000, "LongEntry");
    }


    This is the code I'm trying out. The green letters work as I want it to be. From there I'm trying to add the next "block" of code, which is the Risk/Reward criteria.
    The red letters are the code I entered to try and measure the Risk/Reward ratio as an entry criteria. Unfortunately this doesn't work as I want it to. As you can see I'm using it as a mathematical formula ( Reward / Risk >= 0.90)
    Note that I'm a beginner with NT script coding.

    Many thanks in advance for any comments and/or help.

    Dennis

    #2
    Originally posted by Aqua-Life View Post
    Hi all,

    I'm currently working on a strategy and 1 of the criteria must be to measure the Risk/Reward ratio before it enters a trade. For this we need to know the stoploss and Target 1 of course.

    For my stoploss I'm using the lowest low of the past 96 bars and get 10 pips below that price.
    Target 1 = EMA

    if (CCI(256)[0] > -50 && CCI(256)[1] > -50 && CCI(256)[2] > -50 && CrossAbove(CCI(256), -50, 3) && ((EMA(10)[0] - Close[0]) / ((Close[0] - Low[LowestBar(Low, 96)] - 10 * TickSize) >= 0.90))
    {
    EnterLong(30000, "LongEntry");
    }


    This is the code I'm trying out. The green letters work as I want it to be. From there I'm trying to add the next "block" of code, which is the Risk/Reward criteria.
    The red letters are the code I entered to try and measure the Risk/Reward ratio as an entry criteria. Unfortunately this doesn't work as I want it to. As you can see I'm using it as a mathematical formula ( Reward / Risk >= 0.90)
    Note that I'm a beginner with NT script coding.

    Many thanks in advance for any comments and/or help.

    Dennis
    Hi Dennis

    The important thing (as was always drummed into me when I studied math) is to avoid a division by zero. No system - esp. Ninja - likes this!

    One way to avoid this for a Reward/Risk calculation, is to calculate instead:

    Reward/(Reward + Risk)

    If you make sure that that Reward > 0 and Risk >= 0, the denominator in this ratio can never be zero.

    Please see this thread:

    Comment


      #3
      Hello Aqua-Life,

      Thank you for your post.

      Try printing the values to the Output window (Tools > Output) with Print(). Check the values are not 0 as arbuthnot explained.

      Print(): http://www.ninjatrader.com/support/h.../nt7/print.htm

      Comment


        #4
        Originally posted by arbuthnot View Post
        Hi Dennis

        The important thing (as was always drummed into me when I studied math) is to avoid a division by zero. No system - esp. Ninja - likes this!

        One way to avoid this for a Reward/Risk calculation, is to calculate instead:

        Reward/(Reward + Risk)

        If you make sure that that Reward > 0 and Risk >= 0, the denominator in this ratio can never be zero.

        Please see this thread:

        http://www.ninjatrader.com/support/f...ad.php?t=67122
        Check your bracketing: there seems to be an extra open bracket in the denominator (what you are identifying as risk). You have also not taken into account the possibility that the way you have written that risk could result in a zero or negative value, both of which would be illogical.

        Comment


          #5
          Originally posted by NinjaTrader_PatrickH View Post
          Hello Aqua-Life,

          Thank you for your post.

          Try printing the values to the Output window (Tools > Output) with Print(). Check the values are not 0 as arbuthnot explained.

          Print(): http://www.ninjatrader.com/support/h.../nt7/print.htm
          I've printed the results:

          Code:
          2,59548054691857
          23,7746087436332
          14,9976687952119
          9,5328433485387
          15,6620380110831
          1,72959776392555
          8,61496439151117
          5,48507978793138
          3,83451184029052
          0,409610141086572
          -0,553463015274238
          11,9375696518232
          -0,304254756638895
          1,2748687992195
          0,881375177159153
          6,21843468115507
          28738495080130,5
          1,71631253089167
          1,96451850968512
          0,645903927130346
          0,977084326274902
          7,38874884169002
          10,1488560933393
          -24,2216307329195
          1,67917941964157
          4,50818242001844
          0,619691723749716
          1,63484110494011
          2,51705043698998
          3,24581549139235
          4,82477491373241
          7,16155258006637
          2,85185416134339
          2,0718510069061
          0,442522670830845
          9,74521930332406
          0,207152034125863
          22,4847553974676
          2,23561849279801
          2,34449449156063
          3,12272870170668
          1,35197257144551
          0,270426109584433
          3,22914963313514
          2,18894209252652
          0,948768463588976
          4,45303775447019
          3,99898854018261
          2,63169449820358
          I've also used the coding of arbuthnot and this is the new code, with the output results of that formula.

          Code:
          Print((EMA(100)[0] - Close[0]) / ((EMA(100)[0] - Close[0])+((Close[0] - Low[LowestBar(Low, 96)] - 10 * TickSize))));
          Code:
          0,721873060651927
          0,959636093132773
          0,937490892404317
          0,905058874711287
          0,939983332210932
          0,63364565533573
          0,895995454659939
          0,845799892568634
          0,793153883362936
          0,290583991379937
          -1,23945615750988
          0,922705730140044
          -0,437307706437284
          0,560414209231276
          0,468473905608777
          0,861465810224665
          0,999999999999965
          0,631853850163649
          0,66267709352025
          0,392431123398853
          0,494204679734558
          0,88079271189641
          0,910304699277854
          1,04306329781493
          0,626751387880628
          0,818451909587146
          0,382598561604723
          0,620470472346479
          0,71567083898409
          0,764473985733171
          0,828319546281108
          0,877474293011065
          0,740384771044594
          0,674463378024582
          0,306769993830299
          0,90693535685301
          0,171603930797224
          0,957419186060255
          0,690940077692766
          0,701001152035573
          0,75744220094181
          0,574824973666506
          0,21286252505696
          0,763545845678985
          0,686416381676055
          0,486855407051109
          0,816615977180749
          0,799959533421241
          0,724646442454163

          Comment


            #6
            Hello Dennis,

            Thank you for your response.

            Are these the values you expect?

            Comment


              #7
              Yes they are. I didn't know about the Print() coding. It's really usefull to check if a part of your coding is activated or not.

              After some trial and error I've managed to implement the coding into my entry criteria.
              Thanks for all the help! Now I'm ready to start builing the next part of the strategy on this coding. I'm trying to walk first before I start running.

              Comment


                #8
                Hi all,

                In a follow up on the previous coding problems I'm currently trying to add my next entry filter. This is the entry code I'm currently using for a LONG position;

                Code:
                if (CCI(256)[0] > -50 && CCI(256)[1] > -50 && CCI(256)[2] > -50 && CrossAbove(CCI(256), -50, 3) && [COLOR=Red]((EMA(10)[0] - Close[0]) / (Close[0] - (Low[LowestBar(Low, 96)] - 10 * TickSize))) >= 0.90[/COLOR])
                            {    
                                LongEntryOrder = EnterLong(30000, "LongEntry");
                                DrawArrowUp("My up arrow" + CurrentBar, false, 0, Low[0] - 5 * TickSize, Color.Lime);
                            }
                As you can see I'm checking if CCI > -100 for the past 3 bars. The red part is the R/R measurement. As you can see I want the R/R ratio to be at least 0,90 before I want to enter a trade. But now the thing I can't figure out yet;

                IF the R/R ratio is below 0,9, but all the other rules for entry are met, I want my strategy to check on the next bar if;

                1) Target 1 wouldn't have hit yet. (Target 1 = EMA 50 period)
                2) My stoploss wouldn't have hit yet (StopLoss = 10 ticks below the lowest low of the last 96 bars counted from the entry bar)
                3) CCI remains above -100
                4) R/R ratio is at least 1.10

                I want the strategy to keep checking these 4 requirements and enter a LONG position if all 4 requirements are met. If 1 of the 4 are false, I want the strategy to reset completely and start checking again for the original entry requirements. But I have no idea what logic to use to keep checking this every bar and still keep in mind the first entry requirements.

                I hope I'm making some sense here. To clear things up, I've added a chart where I'm illustrating what I just described.
                I've also added my whole coding.

                Thank you in advance for any help!

                Code:
                #region Using declarations
                using System;
                using System.ComponentModel;
                using System.Diagnostics;
                using System.Drawing;
                using System.Drawing.Drawing2D;
                using System.Xml.Serialization;
                using NinjaTrader.Cbi;
                using NinjaTrader.Data;
                using NinjaTrader.Indicator;
                using NinjaTrader.Gui.Chart;
                using NinjaTrader.Strategy;
                #endregion
                
                // This namespace holds all strategies and is required. Do not change it.
                namespace NinjaTrader.Strategy
                {
                    /// <summary>
                    /// Enter the description of your strategy here
                    /// </summary>
                    [Description("Enter the description of your strategy here")]
                    public class CCItest : Strategy
                    {
                        #region Variables
                        // Wizard generated variables
                        // User defined variables (add any user defined variables below)
                        
                        private IOrder LongEntryOrder = null;
                        private IOrder ShortEntryOrder = null;
                        private IOrder LongScalper = null;
                        private IOrder LongTarget1 = null;
                        private IOrder ShortScalper = null;
                        private IOrder ShortTarget1 = null;
                        
                        #endregion
                
                        /// <summary>
                        /// This method is used to configure the strategy and is called once before any strategy method is called.
                        /// </summary>
                        protected override void Initialize()
                        {
                            CalculateOnBarClose = true;
                            ExitOnClose = false;
                            EntriesPerDirection = 1;
                            EntryHandling = EntryHandling.AllEntries;
                        }
                
                        /// <summary>
                        /// Called on each bar update event (incoming tick)
                        /// </summary>
                        protected override void OnBarUpdate()
                        {
                            // LONG entry condition set 
                            if (CCI(256)[0] > -50 && CCI(256)[1] > -50 && CCI(256)[2] > -50 && CrossAbove(CCI(256), -50, 3) && ((EMA(10)[0] - Close[0]) / (Close[0] - (Low[LowestBar(Low, 96)] - 10 * TickSize))) >= 0.90)
                            {    
                                LongEntryOrder = EnterLong(10000, "LongEntry");
                                DrawArrowUp("My up arrow" + CurrentBar, false, 0, Low[0] - 5 * TickSize, Color.Lime);
                            }
                        
                            // SHORT entry condition set
                            if (CCI(256)[0] < 50 && CCI(256)[1] < 50 && CCI(256)[2] < 50 && CrossBelow(CCI(256), 50, 3) && ((Close[0] - EMA(10)[0]) / ((High[HighestBar(High, 96)] + 10 * TickSize) - Close[0])) >= 0.90)
                            {
                                ShortEntryOrder = EnterShort(10000, "ShortEntry");
                                DrawArrowDown("My down arrow" + CurrentBar, false, 0, High[0] + 5 * TickSize, Color.Lime);
                                }
                            
                            
                            
                            
                            
                            
                
                            
                            
                            
                            // LONG StopLoss condition set
                            if (Position.MarketPosition == MarketPosition.Long)
                            {
                                if (Position.Quantity == 10000)
                                {
                                    if (EMA(50)[0] <= Position.AvgPrice)
                                    {
                                        ExitLong(0, 10000, "ExitEMALong", "");
                                    }
                                }
                            }
                                
                            // SHORT StopLoss condition set
                            if (Position.MarketPosition == MarketPosition.Short)
                            {
                                if (Position.Quantity == 10000)
                                {
                                    if (EMA(50)[0] >= Position.AvgPrice)
                                    {
                                        ExitShort(0, 10000, "ExitEMAShort", "");
                                    }
                                }
                            }
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            // LONG Scalper condition set
                            if (Position.MarketPosition == MarketPosition.Long)
                            {
                                if (Position.Quantity == 10000)
                                {
                                    LongScalper = ExitLongLimit(0, true, 10000, EMA(10)[0] - 2 * TickSize, "LongScalper", "");
                                }
                            }
                
                            // SHORT Scalper condition set
                            if (Position.MarketPosition == MarketPosition.Short)
                            {
                                if (Position.Quantity == 10000)
                                {
                                    ShortScalper = ExitShortLimit(0, true, 10000, EMA(10)[0] + 2 * TickSize, "ShortScalper", "");
                                }
                            }
                
                        }
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        protected override void OnExecution (IExecution execution)
                        {
                            if (LongEntryOrder != null && LongEntryOrder == execution.Order)
                            {
                                if (Position.Quantity == 10000)
                                {
                                    ExitLongStop(0, true, 10000, Low[LowestBar(Low, 96)] - 10 * TickSize, "StoppedOutLong", "");
                                }
                            }
                            
                            if (ShortEntryOrder != null && ShortEntryOrder == execution.Order)
                            {
                                if (Position.Quantity == 10000)
                                {
                                    ExitShortStop(0, true, 10000, High[HighestBar(High, 96)] + 10 * TickSize, "StoppedOutShort", "");
                                }
                            }
                
                                
                        }
                    }
                        #region Properties
                        #endregion
                }
                Attached Files

                Comment


                  #9
                  Aqua,

                  I would suggest using a mode flag and break your code into modes. You could even use an enum if you wanted more then a true/false mode.

                  bool isEntryConditionTriggered;


                  As the entry condition becomes true and you enter the market set isEntryConditionTriggered = true;. Now you would use the code while your in the market to monitor the trade as you specified.

                  As the trade is closed and or you want to go back to looking for the entry order again set isEntryConditionTriggered back to false. This will have your code now check for entry conditions again on each bar.

                  Comment


                    #10
                    Hello Brett,

                    I'm not quite sure if I'm following you. Could you please explain a little bit more about the following words you mentioned?

                    "mode flag"
                    "enum"

                    Besides that, when I'm using the isEntryConditionTriggered in my coding I get CS0103 alerts.

                    Code:
                    if (CCI(256)[0] > -50 && CCI(256)[1] > -50 && CCI(256)[2] > -50 && CrossAbove(CCI(256), -50, 3) && [COLOR=Red]((EMA(10)[0] - Close[0]) / (Close[0] - (Low[LowestBar(Low, 96)] - 10 * TickSize))) >= 0.90[/COLOR])            
                    {                    
                    LongEntryOrder = EnterLong(30000, "LongEntry");                 
                    DrawArrowUp("My up arrow" + CurrentBar, false, 0, Low[0] - 5 * TickSize, Color.Lime);            
                    isEntryConditionTriggered = true; 
                    }

                    Comment


                      #11
                      Hi Aqua-Life,

                      The isEntryConditionTriggered will need to be declared as a bool in the #region Variables section.

                      Has this variable been declared?

                      private bool isEntryConditionTriggered = false;

                      Also, isEntryConditionTriggered is the mode flag. When the flag is true, you would continue checking the last condition for an entry. When this is false, the other conditions will need to be checked again.

                      An enum is a way of having a variable that has different values. This could also be used as a mode flag. You can check the state of this value and have logic that triggers for each state.

                      Below is a link to an example of using enums.
                      http://www.ninjatrader.com/support/f...ead.php?t=3420
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        No it was not, but now I understand how to use this. Thanks.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Kaledus, Today, 01:29 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post Kaledus
                        by Kaledus
                         
                        Started by PaulMohn, Today, 12:36 PM
                        1 response
                        16 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by yertle, Yesterday, 08:38 AM
                        8 responses
                        36 views
                        0 likes
                        Last Post ryjoga
                        by ryjoga
                         
                        Started by rdtdale, Today, 01:02 PM
                        1 response
                        6 views
                        0 likes
                        Last Post NinjaTrader_LuisH  
                        Started by alifarahani, Today, 09:40 AM
                        3 responses
                        18 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Working...
                        X