Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multible EMA Problem Long Only strategy

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

    Multible EMA Problem Long Only strategy

    Hi,
    I am trying to create a Long only strategy when these conditions are confirmed.
    1. Close cross Above EMA(Fast)
    2. EMA(Fast)> EMA(Medium)
    3.EMA(Medium)>EMA(Slow)

    Indeed, it is simple strategy but I cannot understand why it open position when these condition are not confirmed. When I am looking at the chart, it becomes clear that the strategy open and close position when the above condition and not confirmed. I have include below a part of the code. If you need the hole code let me know.

    Thanks

    if (EMA(Fast)[0] > EMA(Medium)[0])
    {
    Variable0=1;
    Variable1=0;
    }
    if(EMA(Medium)[0]>EMA(Slow)[0])
    {
    Variable2=1;
    Variable3=0;
    }
    if (CrossAbove(Close, EMA(Fast), 1))
    {
    Variable4 = 1;
    Variable5 = 0;
    }
    if(Variable0==1 && Variable2==1 && Variable4==1)
    {
    EnterLong(DefaultQuantity, "");
    }

    #2
    Hi Loukas,

    To debug this you'll want to print the states of all values, including user variables. You have 4 blocks there - A simple print statement that each one executed along with its CurrentBar value will go a long way in evaluating what your code should be doing.

    if (EMA(Fast)[0] > EMA(Medium)[0])
    {
    Variable0=1;
    Variable1=0;
    Print(CurrentBar + " First");
    }
    if(EMA(Medium)[0]>EMA(Slow)[0])
    {
    Variable2=1;
    Variable3=0;
    Print(CurrentBar + " Second");
    }
    if (CrossAbove(Close, EMA(Fast), 1))
    {
    Variable4 = 1;
    Variable5 = 0;
    Print(CurrentBar + " Third");
    }
    if(Variable0==1 && Variable2==1 && Variable4==1)
    {
    EnterLong(DefaultQuantity, "");
    Print(CurrentBar + " Fourth");
    }
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Hi,

      I didnt understand what exactly I have to do. I enter the print statments but I havent saw any difference, I do not know where I should look in order to evaluate what my code is doing.

      Thanks

      Comment


        #4
        Use tools > output window to check the print statements and review the page below for additional debugging help.

        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Hi,

          Sorry but I cannot understand the logic behind this . Have a look at the attached output file and at the code below.
          Could you explain me for this simple code how print command is working in order to understand it somewhere more complicated later on.

          Thanks

          #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 EMAsample : Strategy
          {
          #region Variables
          // Wizard generated variables
          private int period = 5; // Default setting for Period
          // 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()
          {
          SetProfitTarget("", CalculationMode.Ticks, 200);

          CalculateOnBarClose = true;
          }

          /// <summary>
          /// Called on each bar update event (incoming tick)
          /// </summary>
          protected override void OnBarUpdate()
          {
          // Condition set 1
          if (CrossAbove(EMA(5), EMA(13), 1))
          Print(CurrentBar + " Check for crossover");


          {
          EnterLong(DefaultQuantity, "");
          Print(CurrentBar + " Enter Long Trade ");
          }
          }

          #region Properties
          [Description("Period of EMA")]
          [GridCategory("Parameters")]
          public int Period
          {
          get { return period; }
          set { period = Math.Max(1, value); }
          }
          #endregion
          }
          }
          Attached Files

          Comment


            #6
            With your latest snippet, I would look closely at your brackets for structuring code flow. If you don't have the brackets immedietly after the if statement, then only the first line is executed. Your EnterLong() and Print - Enter Long Trade is not connected to the if block and is why you are seeing it printed on every bar. Try this instead:

            if (CrossAbove(EMA(5), EMA(13), 1))
            {
            Print(CurrentBar + " Check for crossover");
            EnterLong(DefaultQuantity, "");
            Print(CurrentBar + " Enter Long Trade ");
            }
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Hi,

              I understand how the output is working however I didnt figure out where is the problem and confirm conditions when they didnt meet the requirements.
              I have attached my strategy, I will realy appreciate if you could explain me where is the problem. There is no compile error. The problem is with the conditions
              Attached Files

              Comment


                #8
                Simplify and print the values of everything used in your conditions, including your user variables. If you're having trouble with a 4 part sequence, turn this into 2 parts until it works as you expect.
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  I do not know how can I simplifiy this simplicity. I modify the code as is shown below but nothing was change. It enter long trades always!!!

                  if (EMA(Fast)[0] > EMA(Medium)[0] && EMA(Medium)[0]>EMA(Slow)[0] && Close[0]>EMA(Fast)[0]&& Close[0]> Close[1] );

                  {
                  EnterLong(DefaultQuantity, "");
                  Print(CurrentBar + " Enter Long");
                  }

                  Regarding the variables on the previous code some times is working some times is not!!!! May the problem is cause by my code but from the system, is this possible? The is not compile error, sometime execute the condition correctly and sometimes not. It is too simple but it cannot execute it corret. The code with the possible error is below, I have also attached the file with the strategy

                  if (EMA(Fast)[0] > EMA(Medium)[0]);
                  {
                  Print(CurrentBar + " Fast MA > Medium MA");
                  Variable0=1;
                  Variable1=0;
                  }

                  if(EMA(Medium)[0]>EMA(Slow)[0]);
                  {
                  Print(CurrentBar + " Medium MA >Slow MA");
                  Variable2=1;
                  Variable3=0;
                  }

                  if (Close[0]>EMA(Fast)[0]);
                  {
                  Print(CurrentBar + " Close higher Fast MA");
                  Variable4 = 1;
                  Variable5 = 0;
                  }
                  if (Close[0]> Close[1]);
                  {
                  Print(CurrentBar + " Close0 > Close1");
                  Variable6 = 1;
                  Variable7 = 0;
                  }
                  if ( Variable0==1
                  && Variable2==1
                  && Variable4==1
                  && Variable6==1 );
                  {
                  EnterLong(DefaultQuantity, "");
                  Print(CurrentBar + " Enter Long");
                  }
                  Attached Files

                  Comment


                    #10
                    Simplify means to reduce the number of code blocks. You have 5 separate conditions and if it's not working the way you expect, then you reduce this until you can properly follow the code flow.

                    What was the result of printing your user variables? I don't see any place where you are resetting to a different value.This sequence happens once and then they all stay at 1 until you do something else with them.

                    Generally NinjaTrader does not provide debugging or code development services. We're happy to guide you in the right direction but if you want someone to go over your code and tell you why it does or doesn't do something, then consider hiring a NinjaScript consultant for this.
                    Ryan M.NinjaTrader Customer Service

                    Comment


                      #11
                      Your entry condition requires that the "VariableX" each be evaluated on every pass through the code. As they are declared as global values, they will retain their values on each run unless explicitly reset. Reset all the "VariableX" on each run, before the code blocks run.
                      Last edited by koganam; 06-17-2011, 02:26 PM.

                      Comment


                        #12
                        Thanks Koganam,
                        Could you tell me by whcih statement is resent the value of the "VariableX" of each run ? Maybe the problem is happend because the "Variables" are not update by each new bar and for this reason I have to enter a statement which tell to update the hole code by every new bar. I try make modification of the code but is not working, it is not about complexity and simplicity of the code!

                        Comment


                          #13
                          OnBarUpdate()
                          {
                          Variable0 = 0;
                          Variable1 = 0;
                          Variable2 = 0;
                          //etc
                          //your code processing blocks go here
                          }

                          Comment


                            #14
                            Hi Koganam, Thanks for your idea but unfortunately it doesnt working. It doesnt make any different for the previous code structure. It is going long at any time. I have attached a file with the code if you want to have a look
                            Attached Files

                            Comment


                              #15
                              Originally posted by Loukas View Post
                              Hi Koganam, Thanks for your idea but unfortunately it doesnt working. It doesnt make any different for the previous code structure. It is going long at any time. I have attached a file with the code if you want to have a look
                              Well, you have statements printing to output to show you the state of your variables. Can you point to one single candle on which your code is printing out something that is false? Just reading the code, I can see that by the definition of an uptrend, your conditions will be true on close to 90% of the bars if the market is trending up. So, with no limit on the number of entries, you will probably be generating a trade on a lot of successive candles.

                              If you cannot point to anywhere, where the output of your program is at variance with the reality of the chart, then the problem is not with NinjaTrader, or the computer: the problem is with the logic of your code. It just means the logic of your code does not match your intent. Bring them into alignment, and the code will behave as you expect.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              666 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              377 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              110 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              575 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              580 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X