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

Draw arrows on first condition only

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

    Draw arrows on first condition only

    Hi guys. I have a number of conditions and once satisfied I am drawing the candle green (see attached). No problem here.

    Now the problem is I am also trying to make an alert and insert an arrow at the first bar that satisfies the criteria. The problem is it is drawing arrows at every bar when I only want the first bar. I am able to do this with a crossover ok but not with other conditions (please see attached chart).

    What is the best way of doing this?

    Thanks
    DJ
    Attached Files

    #2
    DJ, sounds like you would need to include a boolean flag here in your conditions to draw - much like the user variables example we have for the wizard here :

    BertrandNinjaTrader Customer Service

    Comment


      #3
      Bool flag example

      Thanks Bertrand. I have tried to use these bool flags before without much success. I have simplified the example you had to make it more like I am doing without crossovers, please see below. This time the second part is ok but the first condition still has all the arrows(please see attached). So I guess the question is how do you tell the first one to work like the second? Thanks DJ

      {
      // Condition set 1
      if (SMA(Close,Fast)[0]> (SMA(Close,Normal)[0]))

      {
      Variable0 = 1;
      DrawArrowUp("arrow1"+CurrentBar, false, 0, Low[0] -1 * TickSize, Color.Lime);
      }


      // Condition set 3
      else if (Variable0 == 1
      &&(SMA(Close,Fast)[0]< (SMA(Close,Normal)[0])))
      {
      Variable0 = 0;
      DrawArrowDown("arrow1"+CurrentBar, false, 0, High[0] +1 * TickSize, Color.Magenta);
      }

      }
      Attached Files

      Comment


        #4
        Hello djkiwi,

        Is Variable0 what you are using to keep track of the condition? If so, it appears you check it on the second statement but didnt on the first. Try something like this:

        if ( ( SMA(Close,Fast)[0]> (SMA(Close,Normal)[0]) ) && Variable0 == 0)
        {
        Variable0 = 1;
        DrawArrowUp("arrow1"+CurrentBar, false, 0, Low[0] -1 * TickSize, Color.Lime);
        }

        This should make it so you only get one up arrow as well.
        DexterNinjaTrader Customer Service

        Comment


          #5
          Boolean flags with draw arrows and candles

          Thanks Dexter. That worked well but created another problem with coloring the candles. I am trying to get all candles in uptrend cyan and all in downtrend magenta but it now just draws the first one in the uptrend cyan and 1st one in the downtrend magenta.

          I tried to segregate the drawing candles into their own coding boxes but that didn't fix the issue ( please see attached chart and code below). So the question is how to I segregate the drawing of candles from the drawing of arrows?

          Thanks DJ


          #region Variables

          private int Variable0 = 0;

          protected override void OnBarUpdate()
          {
          {
          // Condition set 1
          if (SMA(Close,Fast)[0]> (SMA(Close,Normal)[0])
          && Variable0 == 0)

          {

          {
          BarColor = Color.Blue;
          CandleOutlineColor = Color.Lime;
          }

          {
          Variable0 = 1;
          DrawArrowUp("arrow1"+CurrentBar, false, 0, Low[0] -1 * TickSize, Color.Lime);
          }
          }

          // Condition set 2
          else if (Variable0 == 1
          &&(SMA(Close,Fast)[0]< (SMA(Close,Normal)[0])))

          {
          {
          BarColor = Color.Magenta;
          CandleOutlineColor = Color.Magenta;
          }

          {
          Variable0 = 0;
          DrawArrowDown("arrow1"+CurrentBar, false, 0, High[0] +1 * TickSize, Color.Magenta);
          }

          }
          Attached Files
          Last edited by djkiwi; 04-28-2011, 12:22 PM.

          Comment


            #6
            Hi djkiwi,

            To achieve this, you will need to just modify the if statement a little but and put a conditional if check on just the draw arrow call. Try this in your OnBarUpdate:

            // Condition set 1
            if (SMA(Close,Fast)[0]> SMA(Close,Normal)[0])
            {
            BarColor = Color.Blue;
            CandleOutlineColor = Color.Lime;

            if ( Variable0 == 0 )
            {
            DrawArrowUp("arrow1"+CurrentBar, false, 0, Low[0] -1 * TickSize, Color.Lime);
            Variable0 = 1;
            }
            }

            You would need to do the same thing with the down condition code as well.

            Let me know if I can assist you any further.
            DexterNinjaTrader Customer Service

            Comment


              #7
              Boll flag Issues with Arrows and Candle candles

              Hi Dexter, thanks I think I'm getting closer but when I put it into live use in my scenario I find something else that causes an issue. Please let me explain as follows using the simple scenario of 3 moving averages:

              Scenario1
              1. I have a number of conditions, if they are met it draws the arrows cyan, else if another set is met it draws them magenta then if neither are met then it draws them yellow (see attached first picture using the attached code). djboolean with your code commented out.

              2. Second scenario
              Now when I put the code you suggested it causes some of the candles that should be yellow to now be ignored. See the second picture and djboolean1 file which has all of your code suggestions so far.

              So the problem now seems to be the final else being introduced. I tried to do the same with the Variable0 on the "else" but nothing happened.

              Can you please provide some ideas on how to fix this last piece would be appreciated.

              Thanks
              DJ
              Attached Files

              Comment


                #8
                Hi djkiwi,

                It looks like your else statement is included in the else of for the SMA crosses which would only make it apply if the sma statements were already true. The logic is right, its just out of place.

                Try moving this this code:
                else
                {
                BarColor = Color.Yellow;
                CandleOutlineColor = Color.Yellow;
                }

                to the outside of your existing else if, like this:
                Code:
                // Condition set 2
                else if (SMA(Close,Fast)[0]< (SMA(Close,Normal)[0]) && (SMA(Close,Normal)[0]< (SMA(Close,Slow)[0])))
                {
                	BarColor = Color.Magenta;
                	CandleOutlineColor = Color.Magenta;
                	if (Variable0 ==1)		
                	{		
                		DrawArrowDown("arrow1"+CurrentBar, false, 0, High[0] +1 * TickSize, Color.Magenta);
                		Variable0 = 0;
                	}
                }
                else
                {
                    BarColor = Color.Yellow;
                    CandleOutlineColor = Color.Yellow;
                }
                Give that a shot and see if you are getting the right colors.
                DexterNinjaTrader Customer Service

                Comment


                  #9
                  Colors and Arrows

                  Thanks Dexter, nearly there I think, although the final "else" with the "yellow" is causing some problems with the arrow.

                  If I have only an "if" and "if else" the arrows work perfectly. The final else means that when it moves from yellow to either cyan or magenta it should insert a blue arrow as well. Sometimes it doesn't. In fact on my main indicator where I'm trying to include this there are many conditions but will never draw an arrow from a yellow bar to cyan or magenta.

                  You can see from the attached chart how it is missing drawing some arrows from yellow to cyan and yellow to magenta. Maybe something to do with this Variable 0 business.

                  Really appreciate the help on this.

                  Cheers
                  DJ
                  Attached Files

                  Comment


                    #10
                    Hello djkiwi,

                    Ok understood, in this case we have to modify the logic of checking Variable0. Since we want Yellow bars to reset our draw state, we will need to have it set Variable0 to a third value, and re-write the Cyan/Magenta states to check Variable0 and draw an additional blue arrow if Variable2 = 2 (our yellow state) as well.

                    Here is the code, let me know if this is what you were looking for:
                    Code:
                    // Condition set 1
                    if (SMA(Close,fast)[0]> (SMA(Close,normal)[0])
                        &&(SMA(Close,normal)[0]> (SMA(Close,slow)[0])))
                    {
                        BarColor = Color.Cyan;
                        CandleOutlineColor = Color.Cyan;		
                        if (Variable0 != 0)  // if we havent drawn an arrow for these cyan bars yet
                        {	
                    		if (Variable0 == 2) // if we are coming from yellow bars add a blue arrow
                        		DrawArrowUp("arrow2"+CurrentBar, false, 0, Low[0] -2 * TickSize, Color.Blue);
                    		
                    		DrawArrowUp("arrow1"+CurrentBar, false, 0, Low[0] -1 * TickSize, Color.Lime);
                    		
                        	Variable0 = 0;	
                        }	
                    }	
                    
                    // Condition set 2
                    else if
                    (SMA(Close, fast)[0] < (SMA(Close, normal)[0])
                    && (SMA(Close, normal)[0] < (SMA(Close, slow)[0])))
                    {
                        BarColor = Color.Magenta;
                        CandleOutlineColor = Color.Magenta;
                    
                        if (Variable0 != 1) // if we havent drawn an arrow for these magenta bars yet
                        {
                    		if(Variable0 == 2) // if we are coming from yellow bars add a blue arrow
                            	DrawArrowDown("arrow2" + CurrentBar, false, 0, High[0] + 2 * TickSize, Color.Blue);
                    		
                    		DrawArrowDown("arrow1" + CurrentBar, false, 0, High[0] + 1 * TickSize, Color.Magenta);
                    		
                            Variable0 = 1;
                        }
                    }
                    else
                    {
                        BarColor = Color.Yellow;
                        CandleOutlineColor = Color.Yellow;
                    	Variable0 = 2; // set Variable0 so we know which bars we are coming from
                    }
                    DexterNinjaTrader Customer Service

                    Comment


                      #11
                      Drawing Arrows Signals and Bar Colors

                      Thanks Dexter, great job. I've been trying to do this for quite awhile now so that has been quite a help plus helps others trying to do the same thing.

                      Cheers
                      DJ

                      Comment


                        #12
                        Hi,
                        I have similar problem. I need to check every bar after the cross above/below and display the arrow after the only when some condition met. For ex., if Stoch. D crosses above 20% then start checking every bar after the cross and draw arrow only if macd avg > 0, e.g. Rising. Not sure how to do this. If possible please help.
                        Thank you, Art.

                        Comment


                          #13
                          Hi Art,

                          This can done by coding sequences. Sequences can be coded using bool flags or user variables. User variables are shown a bit in this thread and also the following reference sample:
                          Ryan M.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi, I am relatively new to Ninja Trader. If I am using 3 charts (some tick, some time based) is it possible to put together a small script that shows an arrow and sounds an alert that 2 or 3 elements on each of those charts have lined up, thus resulting in a signal.

                            Comment


                              #15
                              Hi TrendTraderBH,

                              Welcome to the NinjaTrader forums. Yes, this is possible with multiseries NinjaScript. To get started can check out the built in sample under tools > edit NinjaScript > Strategy menu. More details for this are available here:
                              Ryan M.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Max238, Today, 01:28 AM
                              5 responses
                              40 views
                              0 likes
                              Last Post Max238
                              by Max238
                               
                              Started by giulyko00, Yesterday, 12:03 PM
                              3 responses
                              12 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by habeebft, Today, 07:27 AM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_ChristopherS  
                              Started by AveryFlynn, Today, 04:57 AM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Started by r68cervera, Today, 05:29 AM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X