Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

using inst[1] as a trading condition

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

    using inst[1] as a trading condition

    Is this the right way to trade instrument[0] using crossing on SMA (40) of the instrument [1]?



    if (CrossBelow(Medians[1][0], SMA (BarsArray[1],40) ,1))
    {

    EnterLong(0,positionTaken,"Long");

    }

    #2
    Hello dafonseca,

    Yes, that is correct as it follows the following overload method.

    EnterLong(int barsInProgressIndex, int quantity, string signalName)

    So it will enter you in a Long Position on the Instrument that your strategy is set to.
    JCNinjaTrader Customer Service

    Comment


      #3
      The problem is :when I open a chart with intrument[1] using the same timeframe used on trategy through Add(), and, on this chart I insert the indicators used above ->SMA(40).
      My strategy was supposed to generate much more orders than it is generating..

      I think my target : buy and sell inst[0] when Median of inst[1] cross (A or B) it SMA (x) is not working here..
      Whe I debbug using :

      Print (Medians[1][0]> SMA (BarsArray[1],SMACycle)[0]);
      I get the correct True False, So I want to go long when True turn to False (CrossBelow, on the code below).
      But something is going wrong and the strategy is not generating the correct orders, as expected..
      Does my code bellow executes the same logic as the Print showed above? What can be going wrong here?
      if (CrossBelow(Medians[1][0], SMA (BarsArray[1],SMACycle) ,1))

      Comment


        #4
        I insert this part on my code:
        1-Print (Time[0]);
        2-Print (Medians[1][0]> SMA (BarsArray[1],SMACycle)[0]);
        3-Print (CrossBelow (Medians[1][0], SMA (BarsArray[1],SMACycle) ,1));

        to check if crossbelow(3) turns to True when (2) goes from T to F. ut as you can se below (from the output window) at 14.54.00 when True turns to False ,, Crossbelow (3) does not turns to True and my strategy does not generates an long order as I expect..
        True
        False
        8/2/2013 14:53:00
        True
        False
        8/2/2013 14:53:00
        True
        False
        8/2/2013 14:54:00
        False
        False
        8/2/2013 14:54:00
        False
        False
        8/2/2013 14:55:00
        False
        False

        Comment


          #5
          Still,
          Print (Time[0]);
          Print (Medians[1][0]> SMA (BarsArray[1],SMACycle)[0]);
          Print(Medians[1][0]);
          Print(SMA (BarsArray[1],SMACycle)[0]);
          Print (CrossBelow(Medians[1][0], SMA (BarsArray[1],SMACycle) ,1));

          see below:

          8/2/2013 14:53:00
          True
          22.785<---
          22.783<---
          False
          8/2/2013 14:54:00
          False
          22.73<=====
          22.783<====
          False <<-it must be TRUE since medians [1][0] has crossed below SMA (BarsArray[1],SMACycle) ,, Am I wrong????
          8/2/2013 14:54:00
          False
          22.73
          22.783
          False

          Comment


            #6
            Dafonseca,

            The overload method for the CrossBelow

            (Dataseries, Dataseris, BarsAgo)

            This means that the code checks for the cross from one bar ago when set to 1 from your code.

            Your check would return false for the same bar since it is checking one bar ago.
            Cal H.NinjaTrader Customer Service

            Comment


              #7
              But in this case the system should send TRUE in the next bar, Am I wrong?
              And it is not sending,, What can be done? any clue!

              8/2/2013 14:53:00
              True
              22.785
              22.783
              False
              8/2/2013 14:54:00
              False
              22.73
              22.783
              False
              8/2/2013 14:54:00
              False
              22.73
              22.783
              False <===============
              8/2/2013 14:55:00
              False
              22.725
              22.78275
              False <==============

              Comment


                #8
                Dafonseca,

                Are you running the strategy with Calculate on Bar Close set to true or false?
                Cal H.NinjaTrader Customer Service

                Comment


                  #9
                  What is interesting is: the IF condition below print parameters even when True == True when it can only print -> True False, or False and True,
                  if ((Medians[1][0]> SMA (BarsArray[1],SMACycle)[0]) != (Medians[1][1]> SMA (BarsArray[1],SMACycle)[1]));
                  {
                  Print (Time[0]);
                  Print (Medians[1][0]> SMA (BarsArray[1],SMACycle)[0]);
                  Print (Medians[1][1]> SMA (BarsArray[1],SMACycle)[1]);
                  Print (CrossBelow(Medians[1][0], SMA (BarsArray[1],SMACycle) ,0));
                  Print (CrossAbove(Medians[1][0], SMA (BarsArray[1],SMACycle) ,0));
                  }

                  8/2/2013 14:54:00
                  False
                  True
                  False
                  False
                  8/2/2013 14:55:00
                  False
                  False
                  False
                  False
                  8/2/2013 14:55:00
                  False
                  False
                  False
                  False
                  8/2/2013 14:56:00
                  False
                  False
                  False
                  False
                  8/2/2013 14:56:00
                  False
                  False
                  False
                  False
                  8/2/2013 14:57:00
                  False
                  False
                  False
                  False

                  Comment


                    #10
                    Dafonseca,

                    You need to have a minimum look back period of 1 for the CrosssAbove and Below otherwise they will return false all the time.

                    This forum post below might help in explaining how the Cross method works.
                    http://www.ninjatrader.com/support/f...37&postcount=2
                    Cal H.NinjaTrader Customer Service

                    Comment


                      #11
                      You are right,,But that is not the problem here..
                      the probelm is this: If says when != but the system is printing when it is ==.

                      if ((Medians[1][0]> SMA (BarsArray[1],SMACycle)[0]) != (Medians[1][1]> SMA (BarsArray[1],SMACycle)[1]));
                      {
                      Print (Time[0]);
                      Print (Medians[1][0]> SMA (BarsArray[1],SMACycle)[0]); <<===
                      Print (Medians[1][1]> SMA (BarsArray[1],SMACycle)[1]); <<==
                      8/2/2013 14:55:00
                      False <<<==
                      False <<<==
                      False
                      False
                      8/2/2013 14:55:00
                      False <<==
                      False <<==
                      False
                      False

                      Comment


                        #12
                        Dafonseca,

                        If your code has no compile errors, it is more than likely that your code contains logical issues.

                        Your NinjaScript / C# Code will always be logically processed and evaluate according to your set logic – this can of course lead to unexpected results at times, thus we would suggest to simplify and debug your code to better understand the event sequence it would go through - unfortunately we cannot offer such debug or code modification services here, but please see the provided resources below to help you proceed productively :

                        First of all you would want to use Print() statements to verify values are what you expect - Debugging your NinjaScript code.

                        For strategies add TraceOrders = true to your Initialize() method and you can then view valuable output related to strategy submitted orders through Tools > Output window - TraceOrders

                        It may also help to add drawing objects to your chart for signal and condition confirmation - Drawing Objects.

                        If you would prefer the debug assist of a professional NinjaScript consultant, please check into the following listings - Click here for a list of certified NinjaScript Consultants
                        Cal H.NinjaTrader Customer Service

                        Comment


                          #13
                          That is what I did (see my code below, It has just 7 lines of debbuging code) I need to understand (to find the bug on my strategy) is:
                          Why : if ((Medians[1][0]> SMA (BarsArray[1],smaCycle)[0]) != (Medians[1][1]> SMA (BarsArray[1],smaCycle)[1]));

                          Prints on otuput window when the two conditions are equal (if the condition above says IF is !=)

                          namespace NinjaTrader.Strategy
                          {
                          public class testesmaaapl2 : Strategy
                          {
                          #region Variables
                          private int smaCycle = 40;
                          #endregion

                          protected override void Initialize()
                          {
                          ExitOnClose = true;
                          EntriesPerDirection = 1;
                          EntryHandling = EntryHandling.AllEntries;
                          Add("^VXAPL", PeriodType.Minute,1);
                          CalculateOnBarClose = true;
                          }

                          protected override void OnBarUpdate()
                          {
                          //DEBBUGING;
                          if ((Medians[1][0]> SMA (BarsArray[1],smaCycle)[0]) != (Medians[1][1]> SMA (BarsArray[1],smaCycle)[1]));
                          {
                          Print (Time[0]);
                          Print (Medians[1][0]> SMA (BarsArray[1],smaCycle)[0]);
                          Print (Medians[1][1]> SMA (BarsArray[1],smaCycle)[1]);
                          //Print(Medians[1][0]);
                          //Print(SMA (BarsArray[1],SMACycle)[0]);
                          Print (CrossBelow(Medians[1][0], SMA (BarsArray[1],smaCycle) ,1));
                          Print (CrossAbove(Medians[1][0], SMA (BarsArray[1],smaCycle) ,1));
                          }

                          }
                          #region Properties

                          #endregion
                          }
                          }

                          Comment


                            #14
                            Dafonseca,

                            There are a couple areas of the code that we should look over.

                            The first is the IF statement
                            Code:
                            if ((Medians[1][0]> SMA (BarsArray[1],smaCycle)[0]) != (Medians[1][1]> SMA (BarsArray[1],smaCycle)[1]));
                            Your checking if a logic statement does not equal another logic statement. Not sure if that is what you are looking for.

                            Additionally, there is a Semi-Colon after your IF statement which will effectively end that line and why you are having the print statements continuously print.
                            Cal H.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

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