Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Difference between Open Close

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

    Difference between Open Close

    How do you state in a strategy that if the open is 30 pips greater than the close, sell........??

    #2
    Hello,

    You first would need to declare a variable to store the calculation of the difference between the open and the close.

    Code:
    double difference = Open[0] - Close[0];
    Next, you would then need to create a condition to check when this was more than 30 pips
    Code:
    if (difference > 30)
                {
                // enter your short position
                }
    MatthewNinjaTrader Product Management

    Comment


      #3
      Thanks Matthew!

      Comment


        #4
        Matthew....I am getting an error message from this.....any idea why?????

        #region Variables
        // Wizard generated variables
        private int myInput0 = 1; // Default setting for MyInput0
        // User defined variables (add any user defined variables below)
        double difference = Open[0]-Close[0];
        #endregion


        protected override void Initialize()
        {
        SetProfitTarget("", CalculationMode.Ticks, 10);
        TraceOrders = true;
        CalculateOnBarClose = true;
        ExitOnClose = false;
        }

        protected override void OnBarUpdate()
        {

        // Condition set 1
        if (Position.MarketPosition == MarketPosition.Flat
        && (difference >= 10)
        && (Close[0] > Open[0]))
        {
        EnterLong(10000, "");
        }

        // Condition set 2
        if (Position.MarketPosition == MarketPosition.Flat
        && (difference >= 10)
        && (Close[0] < Open[0]))
        {
        EnterShort(10000, "");
        }

        Comment


          #5
          Hi edgeliner,

          You can't access those bar objects there in variables region. Best practice here is declaring in Variables region, and then assign in OnBarUpdate() as example below.

          Code:
          #region Variables
          // Wizard generated variables
          private int myInput0 = 1; // Default setting for MyInput0
          // User defined variables (add any user defined variables below)
          private double difference;
          #endregion
          
          protected override void OnBarUpdate()
          {
          difference = Open[0]-Close[0];
          //rest of code here. 
          
          }
          Last edited by NinjaTrader_RyanM1; 10-12-2011, 09:14 AM.
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            Oh, that's right! I should have known that!! Thanks a bunch Ryan!!!!

            Comment


              #7
              OK.....all set with regard to compiling.....but for some reason, it never opens a position?????

              protected override void OnBarUpdate()
              {

              {
              //Only run on real-time data. This will prevent your strategy from executing on historical data and basically will now run on real-time data only!
              if (Historical)
              return;
              }
              difference = Open[0]-Close[0];

              // Condition set 1
              if (Position.MarketPosition == MarketPosition.Flat
              && (difference >= 10)
              && (Close[0] > Open[0]))
              {
              EnterLong(10000, "");
              }

              // Condition set 2
              if (Position.MarketPosition == MarketPosition.Flat
              && (difference <= -10)
              && (Close[0] < Open[0]))
              {
              EnterShort(10000, "");
              }

              Comment


                #8
                No entries usually means your condition is never true.

                If you're talking about pips and a difference of 10, this may explain it. You would never see a difference of 10 from Open[0] - Close[0] when looking at EURUSD, for example.

                To check difference of 10 pips, make this change:
                difference >= 10 * TickSize

                It's always good to print all values and verify through Tools > Output Window. This post can help with this:
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  You are awesome Ryan! Why didn't I think of that????

                  Comment


                    #10
                    This is crazy Ryan! You were right about the EURUSD.....and I change it, but it still did not open any positions????

                    difference = Open[0]-Close[0];

                    // Condition set 1
                    if (Position.MarketPosition == MarketPosition.Flat
                    && (difference >= 10 * TickSize)
                    && (Close[0] > Open[0]))
                    {
                    EnterLong(10000, "");
                    }

                    // Condition set 2
                    if (Position.MarketPosition == MarketPosition.Flat
                    && (difference <= -10 * TickSize)
                    && (Close[0] < Open[0]))
                    {
                    EnterShort(10000, "");
                    }

                    Comment


                      #11
                      It's the same item - the condition never evaluates true. You will need to learn to check values through print statements so you can verify this on your end.

                      Print(difference); and check using Tools > Output Window.

                      These two conditions will never be true together.
                      difference >= 10 * TickSize
                      Close[0] > Open[0]

                      Break this down with some hypothetical values and you can see why it might never be true.

                      Open = 100
                      Close = 112

                      110 - 112 = -12

                      Basically -- for every bar where Close > Open, the difference between Open - Close on these bars will always be a negative value.
                      Last edited by NinjaTrader_RyanM1; 10-13-2011, 11:14 AM.
                      Ryan M.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

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