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

Quick question about if

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

    Quick question about if

    Am i correct in thinking that,

    if((Position.MarketPosition != MarketPosition.Flat)
    && (Time[0].Date.Month == 3)
    || (Time[0].Date.Month == 6)
    || (Time[0].Date.Month == 9)
    || (Time[0].Date.Month == 12)
    && (Time[0].Date.Day >= 5)
    && (Time[0].Date.Day <= 8)


    Means if I have a position and the current month is either March or June or Sep or Dec between the 5th and 8th of those months?

    I am just trying to get to the bottom of unexpected behaviour.

    #2
    Hi GKonheiser,

    Thank you for your post.

    Yes, this is correct in your assumption.

    What unexpected behavior are you trying to get the bottom of?
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Well the full code is below and my expected behaviour is that a long or short is entered at the beginning of the chart and exited between 5th and 8th of the expiry months Sep, Dec, Jun, Mar. It will not enter a new position then until after 8th of Sep, Dec, Jun, Mar.

      However the actual behaviour is is has a trade on 10th Dec which it exits on the next bar. Then it enter a position on the 13th Dec which it gets out of on 1st March NOT the 5th March as the code would indicate??

      #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 EMATRADE : Strategy
      {
      #region Variables

      private int eMAF = 10;
      private int eMAS = 20;

      #endregion


      protected override void Initialize()
      {
      EMA(EMAF).Plots[0].Pen.Color = Color.Red;
      EMA(EMAS).Plots[0].Pen.Color = Color.Black;

      Add(EMA(EMAF));
      Add(EMA(EMAS));

      CalculateOnBarClose = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {

      // Print(Instrument.Expiry.Month);
      Print(Time[0].Date.Month);
      Print(Time[0].Date.Day);
      //


      if((Position.MarketPosition != MarketPosition.Flat)
      && (Time[0].Date.Month == 3)
      || (Time[0].Date.Month == 6)
      || (Time[0].Date.Month == 9)
      || (Time[0].Date.Month == 12)
      && (Time[0].Date.Day >= 5)
      && (Time[0].Date.Day <= 8))

      ExitLong();
      ExitShort();

      if((Time[0].Date.Month == 3)
      || (Time[0].Date.Month == 6)
      || (Time[0].Date.Month == 9)
      || (Time[0].Date.Month == 12)
      && (Time[0].Date.Day >= 5)
      && (Time[0].Date.Day <= 8))
      return;

      if(Position.MarketPosition != MarketPosition.Flat)
      return;

      if(CrossAbove(EMA(eMAF), EMA(eMAS), 1))
      EnterLong();

      if(CrossBelow(EMA(eMAF), EMA(eMAS), 1))
      EnterShort();






      }
      #region Properties
      /// <summary>
      /// </summary>
      [Description("Period for fast MA")]
      [GridCategory("Parameters")]
      public int EMAF
      {
      get { return eMAF; }
      set { eMAF = Math.Max(1, value); }
      }

      /// <summary>
      /// </summary>
      [Description("Period for slow MA")]
      [GridCategory("Parameters")]
      public int EMAS
      {
      get { return eMAS; }
      set { eMAS = Math.Max(1, value); }
      }
      #endregion

      }
      }

      Comment


        #4
        GKonheiser,

        You will need to add the dates to each of the months.

        While you are on the right path, the logic is a little off.

        Here's what the if condition is saying -
        If im not flat and the month is 3 or 6 or 9 or 12 and the date is 5-8th of december....not necessarily the other months.

        The code is seeing the dates attached the month of december and not the other months.

        Let me know if I can be of further assistance.
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Ah.. i thought it was something like that. Thanks for your help.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by bortz, 11-06-2023, 08:04 AM
          47 responses
          1,605 views
          0 likes
          Last Post aligator  
          Started by jaybedreamin, Today, 05:56 PM
          0 responses
          8 views
          0 likes
          Last Post jaybedreamin  
          Started by DJ888, 04-16-2024, 06:09 PM
          6 responses
          18 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by Jon17, Today, 04:33 PM
          0 responses
          4 views
          0 likes
          Last Post Jon17
          by Jon17
           
          Started by Javierw.ok, Today, 04:12 PM
          0 responses
          13 views
          0 likes
          Last Post Javierw.ok  
          Working...
          X