• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Fibonacci Fan Code

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

    Fibonacci Fan Code

    I don't know why NT doesn't have a Fibonacci Fan tool, but it should. Until it does, I would like to try and create my own one. The easiest way to do this would be to take the existing code for the Fib Extension Tool and just modify it. Is there any way to access this code?

    Thanks, Roy

    #2
    Hello Roy, thanks for the post and feedback - a fan tool in on our list of potential enhancements to the platform, I've added your request for it in here as well.

    The source code of the drawing studies could unfortunately not be accessed in NinjaScript.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thanks for your response. According to this thread, its been under consideration for quite a while:

      http://www.ninjatrader.com/support/f...=fibonacci+fan

      I'm not sure I really want to wait another three years

      I could pretty much code it myself as an indicator, except for handling the part that selects the two anchor points. Is there any generic code you could provide that I could use for this purpose, and I will do the rest? Thanks!

      Comment


        #4
        I could unfortunately not point you to a supported / documented framework for achieving that, but you can check into for example this custom data box study published on http://www.volumedigger.com/NinjaTra...dsDataBox.aspx

        This would give you access to the code needed for mouse selecting the bar.

        Also, if I'm not mistaken a partner company offers a Fib Fan indicator as custom addon (http://www.syete.com/)
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Ok, thanks, I'll take a look!

          Comment


            #6
            I finally had a chance to return to this. The code from volume digger works nicely and allows me to choose an anchor bar by mouseclick, but there is one thing I can't figure out. How do I prevent the OnBarUpdate method from beginning to process all the data until I can first click on an anchor bar? Somehow I need to "pause" OnBarUpdate until there is a proper value for the anchor bar.
            Last edited by Srgtroy; 01-20-2013, 04:06 AM.

            Comment


              #7
              I think here you can look into simply returning out of the OnBarUpdate() until the desired value is found / located. OnBarUpdate() would still receive a call, but would not calculate anything as you put control back to the start until the value is there.

              if (myAnchor != desiredValue) return;
              BertrandNinjaTrader Customer Service

              Comment


                #8
                Fib fan tool

                You can see a tool like this in this video.

                http://youtu.be/FyHCuj1DCrM
                Attached Files
                Last edited by eDanny; 01-21-2013, 10:03 AM. Reason: added picture
                eDanny
                NinjaTrader Ecosystem Vendor - Integrity Traders

                Comment


                  #9
                  Originally posted by NinjaTrader_Bertrand View Post
                  I think here you can look into simply returning out of the OnBarUpdate() until the desired value is found / located. OnBarUpdate() would still receive a call, but would not calculate anything as you put control back to the start until the value is there.

                  if (myAnchor != desiredValue) return;
                  The problem with this solution is that each bar will still be processed (even though no code will) before I ever have a chance of choosing the anchor bar with a mouseclick. By the time, I do that, all the bars will already be processed and essentially nothing will be drawn/happen.

                  Rather,

                  I think I need to say something like:

                  if (CurrentBar == 0)
                  {
                  if (myAnchor == null)
                  {
                  Some code here that will make it wait for anchor click
                  }
                  }

                  Comment


                    #10
                    Originally posted by Srgtroy View Post
                    The problem with this solution is that each bar will still be processed (even though no code will) before I ever have a chance of choosing the anchor bar with a mouseclick. By the time, I do that, all the bars will already be processed and essentially nothing will be drawn/happen.

                    Rather,

                    I think I need to say something like:

                    if (CurrentBar == 0)
                    {
                    if (myAnchor == null)
                    {
                    Some code here that will make it wait for anchor click
                    }
                    }
                    I am not really understanding your issue, but it seems to me that maybe there is a lack of understanding about event-driven programming. You need to focus on the event that you want to generate/capture and how you want to handle it.

                    Event-driven programs run in a loop, trapping events and producing output. There is nothing to pause while you click: you instead write an event handler for your click event. The messaging loop will get the event and send it to the scheduler, which will produce a return, which you handle.

                    Comment


                      #11
                      Originally posted by koganam View Post
                      I am not really understanding your issue, but it seems to me that maybe there is a lack of understanding about event-driven programming. You need to focus on the event that you want to generate/capture and how you want to handle it.

                      Event-driven programs run in a loop, trapping events and producing output. There is nothing to pause while you click: you instead write an event handler for your click event. The messaging loop will get the event and send it to the scheduler, which will produce a return, which you handle.
                      You are correct that I don't really understand Event Handling. I will add Windows Forms to that category as well. I've managed to teach myself NinjaScript but not much more although I am trying now to get a handle (no pun intended) on Event Handling. That was the original purpose of this thread. I was directed to code from www.volumedigger.com which provided an on the fly databox. I've slightly altered that code to simply display the bar number of any bar I click on, a prerequisite for selecting an anchor bar. Here is the code I'm working with that does work:

                      Code:
                      #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.Gui.Chart;
                      
                      using System.Windows.Forms;
                      #endregion
                      
                      
                      namespace NinjaTrader.Indicator
                      {
                          
                          [Description("Identifies Bar Number")]
                          public class BarNum : Indicator
                          {
                              #region Variables
                      		
                      		ToolStripLabel tsLabel = null;
                      		ToolStripSeparator tsSeparator = null;
                      		
                              #endregion
                      
                              protected override void Initialize()
                              {
                      		         
                                  CalculateOnBarClose	= true;
                                  Overlay				= true;
                      						
                              }
                      
                      		
                      		protected override void OnStartUp()
                      		{
                      			//add the mouse events
                      			this.ChartControl.ChartPanel.MouseClick += new MouseEventHandler(myMouseEvents);
                      			
                      			//add the controls
                      			if (ChartControl != null)
                      			{
                      				ToolStrip toolstrip = (ToolStrip)ChartControl.Controls["tsrTool"];
                      				
                      				if (toolstrip !=null)
                      				{
                      					//add separator
                      					tsSeparator = new ToolStripSeparator();
                      					tsSeparator.Name = "tsSeparator";
                      					toolstrip.Items.Add(tsSeparator);
                      				
                      					//add label
                      					tsLabel = new ToolStripLabel();
                      					tsLabel.Name = "tsLabel";
                      					tsLabel.Text = "www.volumedigger.com";
                      					toolstrip.Items.Add(tsLabel);
                      				}
                      			}
                      		}
                      		
                      		
                             
                              protected override void OnBarUpdate()
                              {
                                  
                      			
                      		}
                      		
                      		
                      			
                      		
                      		private void myMouseEvents(object sender, MouseEventArgs e)
                      		{
                      			try
                      			{
                      				TriggerCustomEvent(new CustomEvent(myCustomEvent),e );
                      			}
                      			catch (Exception ex)
                      			{
                      				Print("Error " + ex.Message);
                      			}
                      		}
                      		
                      		private void myCustomEvent(object state)
                      		{
                      			try
                      			{
                      				MouseEventArgs m = (MouseEventArgs)state;
                      					
                      				int xpos = m.X;
                      				
                      				if (xpos >= ChartControl.CanvasRight) return;	//make sure clicked on chart area
                      				
                      				int barclick = (int)(xpos/ChartControl.BarSpace);	//gets the bar, from the painted ones where the mouse is clicked
                      			
                      					
                      				int idx = ChartControl.BarsPainted - barclick ;
                      				idx = Bars.Count - 2 - ChartControl.LastBarPainted + idx;
                      				
                      				if (idx > Bars.Count - 2)	return;	//firstbar
                      				if (idx < -1) return; //last bar
                      				
                      				int bn = Bars.Count - 2 - idx;
                      				
                      				tsLabel.Text = "BarNumber:" + bn.ToString();
                      				//DrawText("text", idx.ToString() + " " + Bars.Count.ToString(),idx,Low[idx] - TickSize * 2,Color.Blue);
                      			
                      			}
                      			catch (Exception ex)
                      			{
                      				Print("Error " + ex.Message);
                      			}
                      		}
                      		
                      		
                      		protected override void OnTermination()
                      		{
                      			if (this.ChartControl != null)
                      			{
                      				//remove the mouse click event
                      				this.ChartControl.ChartPanel.MouseMove -=myMouseEvents;
                      				
                      				//remove the controls
                      				ToolStrip toolstrip = (ToolStrip)ChartControl.Controls["tsrTool"];
                      				if (toolstrip != null)
                      				{
                      					if (tsSeparator != null) toolstrip.Items.Remove(tsSeparator);
                      					if (tsLabel != null) toolstrip.Items.Remove(tsLabel);
                      				}
                      			}
                      		}
                      		
                      
                              #region Properties
                              
                      		
                              #endregion
                          }
                      }
                      
                      
                      #region NinjaScript generated code. Neither change nor remove.
                      #endregion
                      Now, the trick is to use this code to choose an anchor. Let's forget about fancy fib fan code and let's just say I want to click on a bar and as a result have a red dot drawn at its close. What i tried to do with no success was the following:

                      1. In Variables Region, made bn variable global.
                      2. Added code in OnBarUpdate method to draw dot.
                      3. In EventHandling code, removed int declaration from bn variable.

                      I suspect the problem is that the handling code isn't meant to return the value globally but I'm not sure. Anyway, this is what the code now looks like with changes in red:


                      Code:
                      #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.Gui.Chart;
                      
                      using System.Windows.Forms;
                      #endregion
                      
                      
                      namespace NinjaTrader.Indicator
                      {
                          
                          [Description("Identifies Bar Number")]
                          public class BarNum : Indicator
                          {
                              #region Variables
                      		
                      		
                      		private int bn;
                      		
                      		ToolStripLabel tsLabel = null;
                      		ToolStripSeparator tsSeparator = null;
                      		
                              #endregion
                      
                              protected override void Initialize()
                              {
                      		         
                                  CalculateOnBarClose	= true;
                                  Overlay				= true;
                      						
                              }
                      
                      		
                      		protected override void OnStartUp()
                      		{
                      			//add the mouse events
                      			this.ChartControl.ChartPanel.MouseClick += new MouseEventHandler(myMouseEvents);
                      			
                      			//add the controls
                      			if (ChartControl != null)
                      			{
                      				ToolStrip toolstrip = (ToolStrip)ChartControl.Controls["tsrTool"];
                      				
                      				if (toolstrip !=null)
                      				{
                      					//add separator
                      					tsSeparator = new ToolStripSeparator();
                      					tsSeparator.Name = "tsSeparator";
                      					toolstrip.Items.Add(tsSeparator);
                      				
                      					//add label
                      					tsLabel = new ToolStripLabel();
                      					tsLabel.Name = "tsLabel";
                      					tsLabel.Text = "www.volumedigger.com";
                      					toolstrip.Items.Add(tsLabel);
                      				}
                      			}
                      		}
                      		
                      		
                             
                              protected override void OnBarUpdate()
                              {
                                  if (CurrentBar == bn)
                      			{
                      				DrawDot("z" + CurrentBar, true, 0, Close[0], Color.Red);
                      			}
                      			
                      		}
                      		
                      		
                      			
                      		
                      		private void myMouseEvents(object sender, MouseEventArgs e)
                      		{
                      			try
                      			{
                      				TriggerCustomEvent(new CustomEvent(myCustomEvent),e );
                      			}
                      			catch (Exception ex)
                      			{
                      				Print("Error " + ex.Message);
                      			}
                      		}
                      		
                      		private void myCustomEvent(object state)
                      		{
                      			try
                      			{
                      				MouseEventArgs m = (MouseEventArgs)state;
                      					
                      				int xpos = m.X;
                      				
                      				if (xpos >= ChartControl.CanvasRight) return;	//make sure clicked on chart area
                      				
                      				int barclick = (int)(xpos/ChartControl.BarSpace);	//gets the bar, from the painted ones where the mouse is clicked
                      			
                      					
                      				int idx = ChartControl.BarsPainted - barclick ;
                      				idx = Bars.Count - 2 - ChartControl.LastBarPainted + idx;
                      				
                      				if (idx > Bars.Count - 2)	return;	//firstbar
                      				if (idx < -1) return; //last bar
                      				
                      				bn = Bars.Count - 2 - idx;
                      				
                      				tsLabel.Text = "BarNumber:" + bn.ToString();
                      				//DrawText("text", idx.ToString() + " " + Bars.Count.ToString(),idx,Low[idx] - TickSize * 2,Color.Blue);
                      			
                      			}
                      			catch (Exception ex)
                      			{
                      				Print("Error " + ex.Message);
                      			}
                      		}
                      		
                      		
                      		protected override void OnTermination()
                      		{
                      			if (this.ChartControl != null)
                      			{
                      				//remove the mouse click event
                      				this.ChartControl.ChartPanel.MouseMove -=myMouseEvents;
                      				
                      				//remove the controls
                      				ToolStrip toolstrip = (ToolStrip)ChartControl.Controls["tsrTool"];
                      				if (toolstrip != null)
                      				{
                      					if (tsSeparator != null) toolstrip.Items.Remove(tsSeparator);
                      					if (tsLabel != null) toolstrip.Items.Remove(tsLabel);
                      				}
                      			}
                      		}
                      		
                      
                              #region Properties
                              
                      		
                              #endregion
                          }
                      }
                      
                      #region NinjaScript generated code. Neither change nor remove.
                      #endregion
                      Despite it all, the red dot is always drawn on the first bar.
                      Last edited by Srgtroy; 01-26-2013, 05:01 PM.

                      Comment


                        #12
                        Another possible starting point

                        I used a program I found in the Ninja 7 user indicators in the forums called dDrawABC.
                        http://www.ninjatrader.com/support/f...DrawABC&desc=1

                        It has way more than you want, but it might be a good starting point for you. You would strip out most of the code and insert your drawing of lines within the Event Handler. He also has a lot of other routines which I did not need, that are related to his desired outcome for drawing lines. I am probably just ahead of you in the learning dept, but I am willing to Help. Kogonam is great and talented and always ready to help. He really understand this stuff, relative to C# and Ninja script capabilities. Also, I have Visual Studio and I use the debugging capabilities it has to let me step through code and watch variables to see what really happens, vs what I thought I coded. PM me your Skype ID if you have one and would like to chat. I am in US EST.

                        John

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by reedstickets, Today, 07:53 PM
                        0 responses
                        4 views
                        0 likes
                        Last Post reedstickets  
                        Started by Atilla, Today, 04:05 PM
                        0 responses
                        16 views
                        0 likes
                        Last Post Atilla
                        by Atilla
                         
                        Started by Displacer, Today, 02:57 PM
                        2 responses
                        13 views
                        0 likes
                        Last Post Displacer  
                        Started by SAILIKITY, 05-22-2019, 05:41 PM
                        1 response
                        9 views
                        0 likes
                        Last Post NinjaTrader_PaulH  
                        Started by rmillar, 01-09-2019, 06:02 PM
                        6 responses
                        75 views
                        1 like
                        Last Post eDanny
                        by eDanny
                         
                        Working...
                        X