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

Cancelling each other out

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

    Cancelling each other out

    My indicator is acting up because I realized that I have two pieces of code that are cancelling each other out - but I do not know how to fix it.

    Here is the code:

    Code:
            protected override void OnBarUpdate()
            {
    			if(CurrentBar < smaPeriod) return; 
    			if(CurrentBar < maEnvPeriod) return;
    			if(CurrentBar < ichimokuPeriod) return;
    			
    			AroonUp = Aroon(14).Up[0];
    			AroonDown = Aroon(14).Down[0];
    			
                // Bullish Trend
    			
    			if (Close[0] > SMA(smaPeriod)[0]
    				&& Close[0] > ParabolicSAR(.02,.2,.02)[0]
    				&& Close[0] > Open[0]
    				&& AroonUp == 100
    				&& Close[0] > MAEnvelopes(maEnvPerc,3,maEnvPeriod).Upper[0]
    				&& Close[0] > Ichimoku(9,26,52)[0]
    				&& !barpainted)
    			{
    				BarColorSeries[0] = Color.Blue;
    				CandleOutlineColor = Color.Black;
    				barpainted = true;
    			}
    			
    			if (Close[0] < Ichimoku(9,26,52).KijunSen[0])
    			{
    				barpainted = false;
    			}
    			
    			// Bearish Trend
    			if (Close[0] < SMA(smaPeriod)[0]
    				&& Close[0] < ParabolicSAR(.02,.2,.02)[0]
    				&& Close[0] < Open[0]
    				&& AroonDown == 100
    				&& Close[0] < MAEnvelopes(maEnvPerc,3,maEnvPeriod).Upper[0]
    				&& Close[0] < Ichimoku(9,26,52).KijunSen[0]
    				&& !barpainted)
    			{
    				BarColorSeries[0] = Color.HotPink;
    				CandleOutlineColor = Color.Black;
    				barpainted = true;
    			}
    			
    			if (Close[0] > Ichimoku(9,26,52).KijunSen[0])
    			{
    				barpainted = false;
    			}
    			
    
            }
    The idea is to color the bar when all of the parameters are met. I only want the first bar to be colored and as soon as it crosses below the KijunSen (bullish) then it will reset.

    This works great as long as I do not have the Bearish section there. As soon as I tell the code to do the same thing for the bearish, it then causes every instance of the bullish criteria to be active.

    I need to know how I can get the same indicator to paint both bullish and bearish bars, without cancelling each other out. Is this possible?

    Thank you for your help.

    #2
    Originally posted by jg123 View Post
    My indicator is acting up because I realized that I have two pieces of code that are cancelling each other out - but I do not know how to fix it.

    Here is the code:

    Code:
            protected override void OnBarUpdate()
            {
                if(CurrentBar < smaPeriod) return; 
                if(CurrentBar < maEnvPeriod) return;
                if(CurrentBar < ichimokuPeriod) return;
                
                AroonUp = Aroon(14).Up[0];
                AroonDown = Aroon(14).Down[0];
                
                // Bullish Trend
                
                if (Close[0] > SMA(smaPeriod)[0]
                    && Close[0] > ParabolicSAR(.02,.2,.02)[0]
                    && Close[0] > Open[0]
                    && AroonUp == 100
                    && Close[0] > MAEnvelopes(maEnvPerc,3,maEnvPeriod).Upper[0]
                    && Close[0] > Ichimoku(9,26,52)[0]
                    && !barpainted)
                {
                    BarColorSeries[0] = Color.Blue;
                    CandleOutlineColor = Color.Black;
                    barpainted = true;
                }
                
    [COLOR=Red]            if (Close[0] < Ichimoku(9,26,52).KijunSen[0])
                {
                    barpainted = false;
                }[/COLOR]
                
                // Bearish Trend
                if (Close[0] < SMA(smaPeriod)[0]
                    && Close[0] < ParabolicSAR(.02,.2,.02)[0]
                    && Close[0] < Open[0]
                    && AroonDown == 100
                    && Close[0] < MAEnvelopes(maEnvPerc,3,maEnvPeriod).Upper[0]
                    && Close[0] < Ichimoku(9,26,52).KijunSen[0]
                    && !barpainted)
                {
                    BarColorSeries[0] = Color.HotPink;
                    CandleOutlineColor = Color.Black;
                    barpainted = true;
                }
                
    [COLOR=Red]            if (Close[0] > Ichimoku(9,26,52).KijunSen[0])
                {
                    barpainted = false;
                }[/COLOR]
                
    
            }
    The idea is to color the bar when all of the parameters are met. I only want the first bar to be colored and as soon as it crosses below the KijunSen (bullish) then it will reset.

    This works great as long as I do not have the Bearish section there. As soon as I tell the code to do the same thing for the bearish, it then causes every instance of the bullish criteria to be active.

    I need to know how I can get the same indicator to paint both bullish and bearish bars, without cancelling each other out. Is this possible?

    Thank you for your help.
    Use different bools for your barPainted. You are using the same one, so it is being set as false on every tick. The parts that I have emphasized in red show that whatever the state of the Close[0], you are setting the barPainted to false, as the 2 conditions together are the universal boolean set.
    Last edited by koganam; 03-06-2014, 08:44 AM. Reason: Corrected grammar and spelling.

    Comment


      #3
      Hello jg123,

      Thank you for your post.

      You would want to follow Koganam's post example and use a different bool for your bear and bull statements
      Cal H.NinjaTrader Customer Service

      Comment


        #4
        That worked perfectly. Thank you both

        Comment


          #5
          How to get current Bar color in Indicator? Red or Green?

          I want to find out the current bar color which is getting completed.
          Kindly reply.

          Comment


            #6
            Originally posted by theitpro View Post
            I want to find out the current bar color which is getting completed.
            Kindly reply.
            I don't quite understand your question.

            Could you possibly rephrase it?

            Comment


              #7
              How to get current bar color ??

              I want to know color of current bar which has closed recently in Indicator so that I can decide last bar was Up or Down depending on bar color.
              Last edited by theitpro; 03-07-2014, 06:34 AM.

              Comment


                #8
                I want to know color of current bar which has closed recently in Indicator so that I can decide last bar was Up or Down depending on bar color.
                Last edited by theitpro; 03-07-2014, 06:34 AM.

                Comment


                  #9
                  Okay, am I understanding you correctly that you would like to know if a candle closed up based on its candle color? or closed down based on its candle color?

                  Obviously, that can be done just simply through looking at the chart - in general if the candle color is blue or green (most commonly) then the candle closed up. In general if it is red or black it closed down.

                  That being said, if you want to ninjascript to do something based on a bullish candle or bearish candle then you can just say something like:

                  if (Close[0] > Open[0])
                  {
                  }

                  (This would tell the computer, "if the most recent candle is a bullish candle, then do something"

                  or you can say

                  if (Close[0] < Open[0])
                  {
                  }

                  This would say, "If the most recent candle was bearish, then do something"

                  Maybe there is a reason to tell the computer to pay attention to the candle color, but telling it to pay attention to the Close vs. Open price seems like it would make more logical sense in this case.

                  Please help me out if I am not understanding you correctly and I will try to help as I can.

                  Comment


                    #10
                    Thank you very much .
                    I want to check that last closed bar is down or up on start of new bar.(Candle). I want to check it in NinjaScript indicator

                    Comment


                      #11
                      Hi,

                      Actually , I have written belows code OnBarUpdate() Event in NinjaTrader Indicator for find out one bar has completed and other has started

                      if (FirstTickOfBar)
                      {
                      }


                      but it is not working properly in my code.I also add below line in Initialise()
                      CalculateOnBarClose = false;

                      I want to know if current bar completed and new started .

                      Comment


                        #12
                        Hello theitpro,

                        The code in jg123's post #9 does demonstrate how to find if the last closed bar if the script is running with Calculate on bar close set to true.

                        If Calculate on bar close is set to false, you will need to use 1 as the bars ago value.
                        For example:
                        If (Close[1] > Open[1])
                        {
                        // execute code
                        }

                        This is because when Calculate on bar close is false the bar currently being built will be bars ago 0 and the most completed bar will be bars ago 1.

                        Below is a link to the help guide on Multi-Time Frame & Instruments. Bars ago with Calculate on bar close is visually explained in the section 'How Bar Data is Referenced'.
                        http://www.ninjatrader.com/support/h...nstruments.htm


                        Regarding the FirstTickOfBar. How do you know this is not working correctly? Have you added a print to your script to see when the print appears in the output window?

                        Also, FirstTickOfBar will not work with historical data. This works in real time as historical data will always be run with Calculate on bar close true until realtime data is being received.

                        FirstTickOfBar - http://www.ninjatrader.com/support/h...ttickofbar.htm
                        CalculateOnBarClose - http://www.ninjatrader.com/support/h...onbarclose.htm
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          I want to just know if 30 bar generates in 2 seconds, then will it execute bellow condition 30 times and print 30 message?

                          protected override void OnBarUpdate()
                          {
                          if (FirstTickOfBar)
                          {
                          Print("Message");
                          }
                          }

                          Comment


                            #14
                            Hi theitpro,

                            I want to clarify your question first.

                            Are you asking if 30 bars are formed in 2 seconds if there will be 30 prints?
                            Yes, if there are 30 bars that form in 2 seconds on the chart in real time (this would probably have to be 1 tick data) and CalculateOnBarClose is set to false, then you will get 30 prints in the output window.

                            Or are you asking if a 30 tick bar is formed in 2 seconds if there will be 30 prints?
                            If you are talking about one 30 tick bar that forms in 2 seconds then there will be one print because there is one bar.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              I am converting string to integer using
                              Convert.ToInt32(string);
                              But it is not working in NinjaScript Indicator
                              Why?????

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by usazencort, Today, 01:16 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post usazencort  
                              Started by kaywai, 09-01-2023, 08:44 PM
                              5 responses
                              603 views
                              0 likes
                              Last Post NinjaTrader_Jason  
                              Started by xiinteractive, 04-09-2024, 08:08 AM
                              6 responses
                              22 views
                              0 likes
                              Last Post xiinteractive  
                              Started by Pattontje, Yesterday, 02:10 PM
                              2 responses
                              21 views
                              0 likes
                              Last Post Pattontje  
                              Started by flybuzz, 04-21-2024, 04:07 PM
                              17 responses
                              230 views
                              0 likes
                              Last Post TradingLoss  
                              Working...
                              X