Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How can I prevent a second trade being triggered in the same direction?

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

    How can I prevent a second trade being triggered in the same direction?

    I am pleased with some results with a reversion to mean strategy. The big problem that I have is if a trend gets under way, a losing trade can be followed by another trade triggered in the same direction, or even a string of trades. The conditions for the trigger are still true. I can't change that.

    What I need is a way to disallow a second trade in the same direction while a trend is still in place without disallowing a trade in the opposite direction. Maybe disallow it for a number of bars after which a trend would probably be exhausted. I am only capable of using the wizard, though I could copy a code. The strategy does not require a strict alternation of long and short trades as conditions are not satisfied in that way.

    I have tried changing the entry conditions from using <,> to 'crosses above' an extreme, but this does not remove the problem.

    I bet I am not the only one with this problem. Can anyone lend a hand with this?

    Steve

    #2
    Hi Steve,

    This is done with custom coding. Some work remains yet to turn that description into something the computer can understand. You have to define the sequence you want first and then can adapt into code. Commonly through bool flags, although the strategy wizard can also be used for sequences using user variables. A sample is available here:
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      You seem to be saying that if you are long, you do not want to enter a second long trade, and the same for short trades. If that is correct, then just use a position filter.

      Code:
      if  (Position.MarketPosition != MarketPosition.Long)[INDENT]{
            //Trade logic to enter long or not
      }
      [/INDENT]else if  (Position.MarketPosition != MarketPosition.Short)[INDENT]{
            //Trade logic to enter short or not
      }[/INDENT]

      Comment


        #4
        If that was the scenario he wanted, he can control with EntriesPerDirection property. I believe he's looking into situations where the long trade has closed, yet doesn't want to accept another long trade until some other conditions have passed. This requires a refinement on the conditions.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_RyanM View Post
          If that was the scenario he wanted, he can control with EntriesPerDirection property. I believe he's looking into situations where the long trade has closed, yet doesn't want to accept another long trade until some other conditions have passed. This requires a refinement on the conditions.
          Actually Ryan, that is why I did not suggest EntriesPerDirection.

          The snippet I wrote takes care of what you describe, because if the trade has closed, then
          Position.MarketPosition != MarketPosition.Long is true and the trade logic block is entered, and on the other hand if a trade is on, then the trade block is bypassed.

          Any further refinement would be handled inside the trade logic block itself.
          Last edited by koganam; 02-18-2011, 06:16 PM.

          Comment


            #6
            Hi kogonam and Ryan

            Thank you both for your replies. Kogonam, your reply was very helpful concerning another problem that I had on another occasion which I am going to find very useful. Ryan, I read through the file of the script that you attached and I see what you are saying about a sequence of events.

            I don’t know if I require specific user variables here,…
            As for sequence, Condition 1 = exit long, Condition 2 =indicator can trigger a short entry any time after exit, ie Condition 1. Condition 3 = indicator can trigger a long entry after say 10 bars after Condition 1. Would I then need a Condition 4 = exit short for the reverse, followed by Conditions 5 and 6?

            An alternative for me to explore is if a long trade exits below the entry price, then I can not go long till there has been a short trade ie following a loss, I can not go in the same direction cosecutively.

            I have my ideas, but I lack the knowledge of how to write the script. Some more relevant references might help. It’s a big challenge outside of the wizard.


            Steve

            Comment


              #7
              Steve, the basic outline would be for example : if your conditions trigger a long trade include a check to a user defined variable / bool flag that would check if it's permissioned to go long - this would be the case only if the previous trade was a short trade, signified by setting yet another user variable / bool flag for example to value 1 if that was the case. Then as you take the long trade, you would reset the user variable permissioning shorts and there you go....
              BertrandNinjaTrader Customer Service

              Comment


                #8
                I am having an issue with bools as well. Here is my code for a long entry:

                private bool ADXClose = false;

                // If Bull/Bear Slope ends reset condition & draw dot
                if (!Bullslope && !Bearslope)
                {
                DrawDot("ADX Close" + CurrentBar, true, 0, High[0] + 2, Color.Turquoise);
                ADXClose = true;
                }

                (I know that the above condition is working because the DrawDot prints a dot)

                // Bull Long1 Quicky Entry
                if (Close[0] > VMAZones (9, 2, 18).Lower[0]
                && TrendStrengthA(VC.NinjaScript.Utility.MovingAverag eType.VWMA, 200, 20, 10).TrendStrength[0] > 60
                && ToTime(Time[0]) > ToTime(7, 30, 0)
                && ToTime(Time[0]) < ToTime(14, 15, 0)
                && ADXClose == true)

                {
                EnterLong(DefaultQuantity, "Bull Long2");
                }

                // Bull Long2 Quicky Exit
                if (TrendStrengthA(VC.NinjaScript.Utility.MovingAvera geType.VWMA, 200, 20, 10).TrendStrength[0] < 60// for quick turn arounds
                ||(ADX(10)[2] < ADX(10)[1]//Rising
                && ADX(10)[0] < ADX(10)[1]))//Falling exits when ADX falls
                {
                ExitLong("Exit Bull Long2", "Bull Long2");
                ADXClose = false;
                }

                The entry fires ok until I add the bool. Any suggestions about what I'm doing wrong?
                Last edited by CaptainAmericaXX; 03-16-2011, 07:32 PM.

                Comment


                  #9
                  I am having an issue with bools as well. Here is my code for a long entry:

                  private bool ADXClose = false;

                  // If Bull/Bear Slope ends reset condition & draw dot
                  if (!Bullslope && !Bearslope)
                  {
                  DrawDot("ADX Close" + CurrentBar, true, 0, High[0] + 2, Color.Turquoise);
                  ADXClose = true;
                  }

                  (I know that the above condition is working because the DrawDot prints a dot)

                  // Bull Long1 Quicky Entry
                  if (Close[0] > VMAZones (9, 2, 18).Lower[0]
                  && TrendStrengthA(VC.NinjaScript.Utility.MovingAverag eType.VWMA, 200, 20, 10).TrendStrength[0] > 60
                  && ToTime(Time[0]) > ToTime(7, 30, 0)
                  && ToTime(Time[0]) < ToTime(14, 15, 0)
                  && ADXClose == true)

                  {

                  EnterLong(DefaultQuantity, "Bull Long2");
                  }

                  // Bull Long2 Quicky Exit
                  if (TrendStrengthA(VC.NinjaScript.Utility.MovingAvera geType.VWMA, 200, 20, 10).TrendStrength[0] < 60// for quick turn arounds
                  ||(ADX(10)[2] < ADX(10)[1]//Rising
                  && ADX(10)[0] < ADX(10)[1]))//Falling)//exits when ADX falls
                  {
                  ExitLong("Exit Bull Long2", "Bull Long2");
                  ADXClose = false;
                  }

                  The entry fires ok until I add the bool. Any suggestions about what I'm doing wrong?

                  Comment


                    #10
                    It just looks like your entire condition never validates to true. You might want to use some Print statements and examine the output.

                    All of your loops execute on every time the OnBarUpdate() event triggers, so I would venture to think that the Exit loop resets your ADXClose to false at some point, before your entry loop is ever entered, after which your entry loop condition is always going to be false. In other words, as written, you will only get an entry if and only if, after your first loop runs, your exit loop does not run before your entry loop.

                    Re-examine your logic, and write it out, in plain English, exactly as to what sequence of events you are seeking. Once that is done, I might take a look at how you may want to restructure your code.

                    As a first step, you may want to put a market position filter on your exit condition, so that it never runs, unless you are already long.

                    Code:
                    if  (Position.MarketPosition == MarketPosition.Long)
                    {
                          //Trade logic for the exit loop
                    }
                    Of course, you can just add the market position filter to the current if statement, to make a compound if statement

                    Comment


                      #11
                      After checking my code I see that I need to change some conditions. This might be the topic of a new conversation, but it does deal with bools. Is there a way to have a bool and a double communicate? I need to say something like:
                      if (Close[1] != bool conditon
                      && Close[0] == bool conditon
                      {
                      TradeOK
                      }

                      Comment


                        #12
                        No, that doesn't make sense to the compiler and you will see programming errors. It's similar to saying:
                        if (14 == true)
                        Ryan M.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by terofs, Yesterday, 04:18 PM
                        1 response
                        21 views
                        0 likes
                        Last Post terofs
                        by terofs
                         
                        Started by CommonWhale, Today, 09:55 AM
                        1 response
                        3 views
                        0 likes
                        Last Post NinjaTrader_Erick  
                        Started by Gerik, Today, 09:40 AM
                        2 responses
                        7 views
                        0 likes
                        Last Post Gerik
                        by Gerik
                         
                        Started by RookieTrader, Today, 09:37 AM
                        2 responses
                        12 views
                        0 likes
                        Last Post RookieTrader  
                        Started by alifarahani, Today, 09:40 AM
                        1 response
                        7 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Working...
                        X