Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multiple instruments

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

    Multiple instruments

    I am trying to learn to trade multiple instruments using NT but the stategy can color the screen but the orders do not fire.
    Here is the main code:
    protected override void Initialize()
    {

    CalculateOnBarClose = true;
    Add("MVV", PeriodType.Day, 1); // Makes MVV BarsInProgress = 1
    Add("DVY", PeriodType.Day, 1); // Makes DVY BarsInProgress = 2
    Add("MZZ", PeriodType.Day, 1); // Makes DVY BarsInProgress = 3
    Add("TLT", PeriodType.Day, 1); // Makes DVY BarsInProgress = 4
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (MACD(MyMACDFast1, MyMACDFast2, 1).Avg[0] > MACD(MyMACDFast1, MyMACDFast2, 1).Avg[DayBack]
    && MACD(MyMACDSlow1, MyMACDSlow2, 1).Avg[0] > 0)
    {
    BackColor = Color.LawnGreen;
    EnterLong(1, 1, "MVV Order");

    }

    // Condition set 2
    if (MACD(MyMACDFast1, MyMACDFast2, 1).Avg[0] < MACD(MyMACDFast1, MyMACDFast2, 1).Avg[DayBack]
    && MACD(MyMACDSlow1, MyMACDSlow2, 1).Avg[0] > 0)
    {
    BackColor = Color.PowderBlue;
    EnterLong(2, 1, "DVY Order");
    }

    // Condition set 3
    if (MACD(MyMACDFast1, MyMACDFast2, 1).Avg[0] < MACD(MyMACDFast1, MyMACDFast2, 1).Avg[DayBack]
    && MACD(MyMACDSlow1, MyMACDSlow2, 1).Avg[0] < 0)
    {
    BackColor = Color.Crimson;
    EnterLong(3, 1, "MZZ Order");
    }

    // Condition set 4
    if (MACD(MyMACDFast1, MyMACDFast2, 1).Avg[0] > MACD(MyMACDFast1, MyMACDFast2, 1).Avg[DayBack]
    && MACD(MyMACDSlow1, MyMACDSlow2, 1).Avg[0] < 0)
    {
    BackColor = Color.Goldenrod;
    EnterLong(4, 1, "TLT Order");
    }

    #2
    Hello cme4pif, which EntryHandling settings are you using with this strategy? Per default only 1 entry would 'come through' if you haven't change those - http://www.ninjatrader.com/support/h...=EntryHandling
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi;
      Well I don't have an exit, I was trying to only close the old position when a new position triggered.
      In other words as the conditions change this system is suppose to trade among 4 different instruments.
      The color backgrounds show the right position, but the trade logic does not work like the screen paint logic.
      So I added ExitLong() ahead of each trade, but that resulted in constant trading of the same instrument, in other words it would buy stock 1 over and over,

      Comment


        #4
        Hello,

        This is because your logic will always execute from top to bottom.

        You could change this in two different ways.

        The first would be to add bars in progress filtering, which is discussed here: http://www.ninjatrader.com/support/h...nstruments.htm

        The second would be to change your order handling to UniqueEntries from the strategy properties when running the strategy. A default for this can also be hard coded: http://www.ninjatrader.com/support/h...ryhandling.htm

        Let me know if I can be of further assistance.
        LanceNinjaTrader Customer Service

        Comment


          #5
          Still confused

          The graphic marked RIGHT comes from this code (see also wrong below):

          // Condition set 1
          if (MACD(MyMACDFast1, MyMACDFast2, 1).Avg[0] > MACD(MyMACDFast1, MyMACDFast2, 1).Avg[DayBack]
          && MACD(MyMACDSlow1, MyMACDSlow2, 1).Avg[0] > 0)
          {
          BackColor = Color.LawnGreen;
          }

          // Condition set 2
          if (MACD(MyMACDFast1, MyMACDFast2, 1).Avg[0] < MACD(MyMACDFast1, MyMACDFast2, 1).Avg[DayBack]
          && MACD(MyMACDSlow1, MyMACDSlow2, 1).Avg[0] > 0)
          {
          BackColor = Color.PowderBlue;
          }

          // Condition set 3
          if (MACD(MyMACDFast1, MyMACDFast2, 1).Avg[0] > MACD(MyMACDFast1, MyMACDFast2, 1).Avg[DayBack]
          && MACD(MyMACDSlow1, MyMACDSlow2, 1).Avg[0] < 0)
          {
          BackColor = Color.Goldenrod;
          }

          // Condition set 4
          if (MACD(MyMACDFast1, MyMACDFast2, 1).Avg[0] < MACD(MyMACDFast1, MyMACDFast2, 1).Avg[DayBack]
          && MACD(MyMACDSlow1, MyMACDSlow2, 1).Avg[0] < 0)
          {
          BackColor = Color.Firebrick;
          }

          The Graphic marked WRONG comes form this code:
          #region Variables
          // Wizard generated variables
          private int myMACDFast1 = 8; // Default setting for MyMACDFast1
          private int myMACDFast2 = 35; // Default setting for MyMACDFast2
          private int dayBack = 3; // Default setting for DayBack
          private int myMACDSlow1 = 110; // Default setting for MyMACDSlow1
          private int myMACDSlow2 = 210; // Default setting for MyMACDSlow2
          // private int myMACDSlow3 = 1; // Default setting for MyMACDSlow3
          private int phase = 1; // Default setting for Phase
          private int BIP = 0; // Default setting for BarInProgress(trade)
          // 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()
          {

          CalculateOnBarClose = true;
          Add("MVV", PeriodType.Day, 1); // Makes MVV BarsInProgress = 1
          Add("DVY", PeriodType.Day, 1); // Makes DVY BarsInProgress = 2
          Add("MZZ", PeriodType.Day, 1); // Makes DVY BarsInProgress = 3
          Add("TLT", PeriodType.Day, 1); // Makes DVY BarsInProgress = 4

          EntriesPerDirection = 1;
          EntryHandling = EntryHandling.UniqueEntries;
          }



          /// <summary>
          /// Called on each bar update event (incoming tick)
          /// </summary>
          protected override void OnBarUpdate()
          {
          // Make Sure we are only dealing with the Primary Bars
          // if (BarsInProgress !=0);
          // return;

          // Condition set 1
          if (MACD(MyMACDFast1, MyMACDFast2, 1).Avg[0] > MACD(MyMACDFast1, MyMACDFast2, 1).Avg[DayBack]
          && MACD(MyMACDSlow1, MyMACDSlow2, 1).Avg[0] > 0 && BIP != 1)
          {
          BackColor = Color.LawnGreen;
          ExitLong();
          BIP = 1; // "MVV Order
          EnterLong(BIP, 1, "MVV Order");
          }

          // Condition set 2
          if (MACD(MyMACDFast1, MyMACDFast2, 1).Avg[0] < MACD(MyMACDFast1, MyMACDFast2, 1).Avg[DayBack]
          && MACD(MyMACDSlow1, MyMACDSlow2, 1).Avg[0] > 0 && BIP != 2)
          {
          BackColor = Color.PowderBlue;
          ExitLong();
          BIP = 2; // "DVY Order
          EnterLong(BIP, 1, "DVY Order");
          }

          // Condition set 3
          if (MACD(MyMACDFast1, MyMACDFast2, 1).Avg[0] < MACD(MyMACDFast1, MyMACDFast2, 1).Avg[DayBack]
          && MACD(MyMACDSlow1, MyMACDSlow2, 1).Avg[0] < 0 && BIP != 3)
          {
          BackColor = Color.Crimson;
          ExitLong();
          BIP = 3; // "MZZ Order
          EnterLong(BIP, 1, "MZZ Order");
          }

          // Condition set 4
          if (MACD(MyMACDFast1, MyMACDFast2, 1).Avg[0] > MACD(MyMACDFast1, MyMACDFast2, 1).Avg[DayBack]
          && MACD(MyMACDSlow1, MyMACDSlow2, 1).Avg[0] < 0 && BIP != 5)
          {
          BackColor = Color.Goldenrod;
          ExitLong();
          BIP = 4; // "TLT Order
          EnterLong(BIP, 1, "TLT Order");
          }
          }



          The code is the same EXCEPT we are dealing with BarInProgress and clearly that is messing up the logic. Notice I tried the suggestion from help
          // Make Sure we are only dealing with the Primary Bars
          if (BarsInProgress !=0);
          return;
          But that made the color bars turn off completely. So I commented it out.
          Attached Files

          Comment


            #6
            What is going on here

            If you read my post below the MACD calcu appears to be using data off the traded instrument This system is to trade one of four securities based on two MACD based on the underlying chart of SPY. instead the MACD look like they are based on the traded instruments instead of the PrimaryBar or something.

            It all works fine (see right graph and code below) until we use the ADD function to give us the instruments to trade. (see wrong)
            I really don't even need the ADD function because my trade logic is only based on one instrument in one time frame. What I need is to specify the security in the EnterLong statement. But I don't think NT works that way,

            See post below.

            Comment


              #7
              Further along

              I also was able to get it to behave better by adding this code
              to the MACD Inputs[0],

              As in:
              if (MACD(Inputs[0], MyMACDFast1, MyMACDFast2, 1).Avg[0] > MACD(Inputs[0], MyMACDFast1, MyMACDFast2, 1).Avg[DayBack]
              && MACD(Inputs[0], MyMACDSlow1, MyMACDSlow2, 1).Avg[0] > 0 && BIP != 1)
              {

              But the chart still does not show any trade taking place.

              For rest of code see two post below

              Comment


                #8
                Next version

                Hi
                I have created another version see code below.
                It does trade and display correcly colored stripes where the trade should happen. but when I look at the trades in the strategy analyzer it repeatedly buys and sell the same security in a row, For example MVV three times in a row.

                Also the trades don't show on the chart even though it is set to.

                Source code:

                protected override void Initialize()
                {

                CalculateOnBarClose = true;
                Add("MVV", PeriodType.Day, 1); // Makes MVV BarsInProgress = 1
                Add("DVY", PeriodType.Day, 1); // Makes DVY BarsInProgress = 2
                Add("MZZ", PeriodType.Day, 1); // Makes MZZ BarsInProgress = 3
                Add("TLT", PeriodType.Day, 1); // Makes TLT BarsInProgress = 4

                EntriesPerDirection = 1;
                EntryHandling = EntryHandling.UniqueEntries;
                }



                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
                // Make Sure we are only dealing with the Primary Bars
                // if (BarsInProgress !=0);
                // return;

                // Condition set 1
                if (MACD(Inputs[0], MyMACDFast1, MyMACDFast2, 1).Avg[0] > MACD(Inputs[0], MyMACDFast1, MyMACDFast2, 1).Avg[DayBack]
                && MACD(Inputs[0], MyMACDSlow1, MyMACDSlow2, 1).Avg[0] > 0 && BIP != 1)
                {
                BackColor = Color.LawnGreen;
                // ExitLong();
                BIP = 1; // "MVV Order
                NewTrade = BIP;
                Security = "MVV";
                // EnterLong(BIP, 1, "MVV Order");
                }

                // Condition set 2
                if (MACD(Inputs[0], MyMACDFast1, MyMACDFast2, 1).Avg[0] < MACD(Inputs[0], MyMACDFast1, MyMACDFast2, 1).Avg[DayBack]
                && MACD(Inputs[0], MyMACDSlow1, MyMACDSlow2, 1).Avg[0] > 0 && BIP != 2)
                {
                BackColor = Color.PowderBlue;
                // ExitLong();
                BIP = 2; // "DVY Order
                NewTrade = BIP;
                Security = "DVY";
                // EnterLong(BIP, 1, "DVY Order");
                }

                // Condition set 3
                if (MACD(Inputs[0], MyMACDFast1, MyMACDFast2, 1).Avg[0] < MACD(Inputs[0], MyMACDFast1, MyMACDFast2, 1).Avg[DayBack]
                && MACD(Inputs[0], MyMACDSlow1, MyMACDSlow2, 1).Avg[0] < 0 && BIP != 3)
                {
                BackColor = Color.Crimson;
                // ExitLong();
                BIP = 3; // "MZZ Order
                NewTrade = BIP;
                Security = "MZZ";
                // EnterLong(BIP, 1, "MZZ Order");
                }

                // Condition set 4
                if (MACD(Inputs[0], MyMACDFast1, MyMACDFast2, 1).Avg[0] > MACD(Inputs[0], MyMACDFast1, MyMACDFast2, 1).Avg[DayBack]
                && MACD(Inputs[0], MyMACDSlow1, MyMACDSlow2, 1).Avg[0] < 0 && BIP != 4)
                {
                BackColor = Color.Goldenrod;
                // ExitLong();
                BIP = 4; // "TLT Order
                NewTrade = BIP;
                Security = "TLT";
                // EnterLong(BIP, 1, "TLT Order");
                }


                if (NewTrade != OldTrade);
                {
                OldTrade = NewTrade;
                ExitLong("", "");
                EnterLong(NewTrade, 1, Security);
                }

                Comment


                  #9
                  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


                  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 like me to test a specific aspect on my end please attach a complete cs file so that I may easily test. Located in (MY)Documents\NinjaTrader 7\bin\Custom\Strategy
                  LanceNinjaTrader Customer Service

                  Comment


                    #10
                    Ok Here is the source.

                    See enclosed .cs file

                    Note the section
                    if (NewTrade != OldTrade);

                    it should only fire once but it buys the same security many times.
                    Attached Files

                    Comment


                      #11
                      Hello,

                      You will need to add more distinction within your code to separate out the different Bars In Progress.

                      Currently you're having multiple conditions being checked within multiple bar series calls.

                      looking at your first condition for example:

                      When BarsInProgress == 0,1, 2, or 3 this condition is getting checked against different instruments 3 out of 4 times.

                      Please see the following for more information on how to properly structure multi-series scripts
                      LanceNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Stanfillirenfro, Yesterday, 09:19 AM
                      7 responses
                      51 views
                      0 likes
                      Last Post NinjaTrader_Gaby  
                      Started by TraderCro, 04-12-2024, 11:36 AM
                      4 responses
                      69 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Mindset, Yesterday, 02:04 AM
                      1 response
                      15 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by agclub, 04-21-2024, 08:57 PM
                      4 responses
                      18 views
                      0 likes
                      Last Post NinjaTrader_Gaby  
                      Started by Irukandji, Today, 04:58 AM
                      0 responses
                      6 views
                      0 likes
                      Last Post Irukandji  
                      Working...
                      X