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

Need Help with my TrendTrader Strategy

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

    Need Help with my TrendTrader Strategy

    Can anyone see flaws in my code logic? If you could please help me fix it i've been working on it for long and want to get it done. It will be greatly appreciated, and ill upload it to the ninja trader forums downloads after The title of the strategy is 'golongorshort' when you upload it, its compiled successfully. It just doesn't have any back testing resaults and i can't figure out why.

    Kind Regards

    #region Variables
    // Wizard generated variables
    private bool onlyGoLong = true; // Default setting for OnlyGoLong
    private int aDXStrength = 18; // Default setting for ADXStrength
    private int trailingStopLoss = 30; // Default setting for TrailingStopLoss
    private int stopLoss = 30; // Default setting for StopLoss
    // User defined variables (add any user defined variables below)
    #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()
    {
    SetStopLoss("", CalculationMode.Ticks, StopLoss, false);
    SetTrailStop("", CalculationMode.Ticks, TrailingStopLoss, false);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (OnlyGoLong == true
    && CrossAbove(MACD(12, 26, 9).Avg, MACD(12, 26, 9).Avg, 1)
    && CrossAbove(ADX(14), ADXStrength, 1)
    && CrossAbove(DM(14).DiPlus, DM(14).DiMinus, 1)
    && CrossAbove(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1))
    {
    EnterLong(DefaultQuantity, "");
    }

    // Condition set 2
    if (OnlyGoLong == false
    && CrossBelow(MACD(12, 26, 9).Avg, MACD(12, 26, 9).Avg, 1)
    && CrossBelow(DM(14).DiPlus, DM(14).DiMinus, 1)
    && CrossBelow(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1)
    && CrossAbove(ADX(14), ADXStrength, 1))
    {
    EnterShort(DefaultQuantity, "");
    }

    // Condition set 3
    if (CrossBelow(ADX(14), ADXStrength, 1)
    || CrossBelow(MACD(12, 26, 9).Avg, MACD(12, 26, 9).Avg, 1)
    || CrossBelow(DM(14).DiPlus, DM(14).DiMinus, 1)
    || CrossBelow(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1))
    {
    ExitLong("", "");
    }

    // Condition set 4
    if (CrossAbove(DM(14).DiPlus, DM(14).DiMinus, 1)
    || CrossBelow(ADX(14), ADXStrength, 1)
    || CrossAbove(MACD(12, 26, 9).Avg, MACD(12, 26, 9).Avg, 1)
    || CrossAbove(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1))
    {
    ExitShort("", "");
    }
    }

    #region Properties
    [Description("Only take Long trades, if false only take Short trades")]
    [GridCategory("Parameters")]
    public bool OnlyGoLong
    {
    get { return onlyGoLong; }
    set { onlyGoLong = value; }
    }

    [Description("Trend Strength")]
    [GridCategory("Parameters")]
    public int ADXStrength
    {
    get { return aDXStrength; }
    set { aDXStrength = Math.Max(1, value); }
    }

    [Description("Traling Stop Loss")]
    [GridCategory("Parameters")]
    public int TrailingStopLoss
    {
    get { return trailingStopLoss; }
    set { trailingStopLoss = Math.Max(1, value); }
    }

    [Description("StopLoss")]
    [GridCategory("Parameters")]
    public int StopLoss
    {
    get { return stopLoss; }
    set { stopLoss = Math.Max(1, value); }
    }
    #endregion
    }
    }
    Attached Files

    #2
    Originally posted by cameo86 View Post
    Can anyone see flaws in my code logic? If you could please help me fix it i've been working on it for long and want to get it done. It will be greatly appreciated, and ill upload it to the ninja trader forums downloads after The title of the strategy is 'golongorshort' when you upload it, its compiled successfully. It just doesn't have any back testing resaults and i can't figure out why.

    Kind Regards

    #region Variables
    // Wizard generated variables
    private bool onlyGoLong = true; // Default setting for OnlyGoLong
    private int aDXStrength = 18; // Default setting for ADXStrength
    private int trailingStopLoss = 30; // Default setting for TrailingStopLoss
    private int stopLoss = 30; // Default setting for StopLoss
    // User defined variables (add any user defined variables below)
    #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()
    {
    SetStopLoss("", CalculationMode.Ticks, StopLoss, false);
    SetTrailStop("", CalculationMode.Ticks, TrailingStopLoss, false);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (OnlyGoLong == true
    && CrossAbove(MACD(12, 26, 9).Avg, MACD(12, 26, 9).Avg, 1)
    && CrossAbove(ADX(14), ADXStrength, 1)
    && CrossAbove(DM(14).DiPlus, DM(14).DiMinus, 1)
    && CrossAbove(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1))
    {
    EnterLong(DefaultQuantity, "");
    }

    // Condition set 2
    if (OnlyGoLong == false
    && CrossBelow(MACD(12, 26, 9).Avg, MACD(12, 26, 9).Avg, 1)
    && CrossBelow(DM(14).DiPlus, DM(14).DiMinus, 1)
    && CrossBelow(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1)
    && CrossAbove(ADX(14), ADXStrength, 1))
    {
    EnterShort(DefaultQuantity, "");
    }

    // Condition set 3
    if (CrossBelow(ADX(14), ADXStrength, 1)
    || CrossBelow(MACD(12, 26, 9).Avg, MACD(12, 26, 9).Avg, 1)
    || CrossBelow(DM(14).DiPlus, DM(14).DiMinus, 1)
    || CrossBelow(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1))
    {
    ExitLong("", "");
    }

    // Condition set 4
    if (CrossAbove(DM(14).DiPlus, DM(14).DiMinus, 1)
    || CrossBelow(ADX(14), ADXStrength, 1)
    || CrossAbove(MACD(12, 26, 9).Avg, MACD(12, 26, 9).Avg, 1)
    || CrossAbove(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1))
    {
    ExitShort("", "");
    }
    }

    #region Properties
    [Description("Only take Long trades, if false only take Short trades")]
    [GridCategory("Parameters")]
    public bool OnlyGoLong
    {
    get { return onlyGoLong; }
    set { onlyGoLong = value; }
    }

    [Description("Trend Strength")]
    [GridCategory("Parameters")]
    public int ADXStrength
    {
    get { return aDXStrength; }
    set { aDXStrength = Math.Max(1, value); }
    }

    [Description("Traling Stop Loss")]
    [GridCategory("Parameters")]
    public int TrailingStopLoss
    {
    get { return trailingStopLoss; }
    set { trailingStopLoss = Math.Max(1, value); }
    }

    [Description("StopLoss")]
    [GridCategory("Parameters")]
    public int StopLoss
    {
    get { return stopLoss; }
    set { stopLoss = Math.Max(1, value); }
    }
    #endregion
    }
    }
    You have listed 4 almost unrelated conditions, albeit all based on price, where you want to see a crossover on the same exact bar. IOW, you have 8 lines, arrived at differently from the same underlying data, that you expect to be pairwise coincident at the same time. I would say that the chances are pretty close to nil that you will ever get a trade. You need to refine your conditions. It may help to write them out in plain language first, instead of math. That often reveals just how restrictive a set of conditions actually are.

    Comment


      #3
      Problem Spotted

      Your MACD conditions never get hit because they are the same value, they will never cross.
      You have
      CrossAbove(MACD(12, 26, 9).Avg, MACD(12, 26, 9).Avg, 1)

      What you need is

      CrossAbove(MACD(12, 26, 9).Avg, MACD(12, 26, 9).Avg[1], 1)which pulls the prior MACD value.

      Returns MACD value
      MACD(int fast, int slow, int smooth)[int barsAgo]


      the look behind variable 1 as the last argument in your CrossAbove, is only looking for a cross this bar, or prior bar, but using the current or prior bar values, so the logic as is is essentially

      If 1 crosses above 1 in this period
      or 1 crosses above 1 in the last period

      1 will never cross 1

      Do this every place you use MACD as a condition and you will have backtest results.

      Comment


        #4
        Conditions wont trigger all together

        FYI,

        I ran backtesting after making a correction on your formula for 10 year on daily AA symbol.

        All your conditions (after fixed) do get hit individually at least once, however when you combine

        1 && 2 && 3 && 4
        They are never hit together.

        I would use one condition, get a trade, then add another, see if you get a trade, then add another, etc.. incrementally so that you can isolate the unlikely conditions that prevent a trade from firing.

        Comment


          #5
          Thanks for your reply

          Ok i did the MACD fix, thanks for that but still didn't get any resaults. Im going to re-work the code later tonight after running some figures on what can work. I did this mainly for the idea of the daily chart trend. I do want it for all frames. Thanks again for your help everyone, very much appreciated.


          Kind Regards,

          Cameron

          Comment


            #6
            What I like to do is take one condition
            if(some condition)
            Print("Some condition hit");
            turn the output window on and apply the strategy in a chart, you will see if it gets hit.
            Then add new conditions incrementally
            if(somecondition && someothercondition)
            Print("somecondition && someothercondition hit");

            Comment


              #7
              Thanks for you help people heres a Strategy ive worked on.

              It works good on the ES 15min time frame with stop loss ticks at 15.
              Attached Files

              Comment


                #8
                Does anyone know how to code an ADX?

                How do you get an ADX >=18 or a rising ADX trending line? I need help coding an ADX into my indicator and need it to be rising or greater than 18 to move? Any suggestions?


                Much Appreciated

                Comment


                  #9
                  Hello cameo86,

                  Thank you for your post.

                  The following gives an example of plotting the ADX 20 period if it is rising or greater than or equal to 18:
                  Code:
                          protected override void Initialize()
                  		{
                  			Add(new Plot(Color.Blue, PlotStyle.Line, "ADX"));
                  			CalculateOnBarClose = true;
                  		}
                  
                         
                          protected override void OnBarUpdate()
                          {
                  			Value.Set(ADX(20)[0]);
                  			
                  			if(Rising(ADX(20)) || ADX(20)[0] >=18)
                  			{
                  				PlotColors[0][0] = Color.Blue;
                  			}
                  			else
                  				PlotColors[0][0] = Color.Transparent;
                  		}
                  Please let me know if you have any questions.

                  Comment


                    #10
                    Im having trouble placing the code in the code

                    I've got about 4 condition sets, ive added some photos, do you know where i went wrong?

                    /// This method is used to configure the strategy and is called once before any strategy method is called.
                    /// </summary>
                    protected override void Initialize()
                    {
                    Add(new Plot(Color.Blue, PlotStyle.Line, "ADX"));


                    ::::::::Then i put the other code below on bar update

                    protected override void OnBarUpdate()
                    {

                    Value.Set(ADX(20)[0]);

                    if(Rising(ADX(20)) || ADX(20)[0] >=18)
                    {
                    PlotColors[0][0] = Color.Blue;
                    }
                    else
                    PlotColors[0][0] = Color.Transparent;

                    // Condition set 1
                    Attached Files

                    Comment


                      #11
                      Hello cameo86,

                      Thank you for your response.

                      As you are using a strategy the method for plotting will be different. We have a reference sample that details how to create plots from your strategy. You can view this reference sample at the following link: http://www.ninjatrader.com/support/f...ead.php?t=6651

                      Please let me know if you have any questions.

                      Comment


                        #12
                        Thanks for your reply

                        Instead of the rising slope how do you get an ADX that is greater than lets say 15 and be able to toggle it? on the ninja-trader ADX


                        I also have another question, with the MACD im using how do i get 're-entry' because theirs some moves im seeing in my back test results that are missing the big moves and then it doesn't re enter the trade, that also why i think an ADX will be very important to sense the trend strength. Thanks for your help, ill also be posting this strategy for others to share as im receiving alot of help from people like your self.

                        Comment


                          #13
                          Regarding your sample zip. Ill be more than happy to test it tonight

                          Im just eating dinner with family. Im trying to perfect this strategy so i can test it this week in real time data (simulator)

                          Comment


                            #14
                            Hello cameo86,

                            Thank you for your response.

                            Originally posted by cameo86 View Post
                            Instead of the rising slope how do you get an ADX that is greater than lets say 15 and be able to toggle it? on the ninja-trader ADX
                            This would look similar to the following:
                            Code:
                            if(ADX(myPeriod)[0] > myInt)
                            {
                            //Do something
                            You will need to declare myPeriod and myInt as ints in the variable section of the code. For information on creating user defined variables please visit the following link: http://www.ninjatrader.com/support/f...ead.php?t=5782
                            Originally posted by cameo86 View Post
                            I also have another question, with the MACD im using how do i get 're-entry' because theirs some moves im seeing in my back test results that are missing the big moves and then it doesn't re enter the trade, that also why i think an ADX will be very important to sense the trend strength. Thanks for your help, ill also be posting this strategy for others to share as im receiving alot of help from people like your self.
                            Not sure what conditions this would need to be and what the 're-entry' would be either. Please provide additional information on what you are looking for here.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by AaronKoRn, Yesterday, 09:49 PM
                            1 response
                            18 views
                            0 likes
                            Last Post Rikazkhan007  
                            Started by ageeholdings, Today, 07:43 AM
                            0 responses
                            12 views
                            0 likes
                            Last Post ageeholdings  
                            Started by pibrew, Today, 06:37 AM
                            0 responses
                            4 views
                            0 likes
                            Last Post pibrew
                            by pibrew
                             
                            Started by rbeckmann05, Yesterday, 06:48 PM
                            1 response
                            14 views
                            0 likes
                            Last Post bltdavid  
                            Started by llanqui, Today, 03:53 AM
                            0 responses
                            9 views
                            0 likes
                            Last Post llanqui
                            by llanqui
                             
                            Working...
                            X