NexusFi: Find Your Edge


Home Menu

 





On Update() method and Windows Form


Discussion in NinjaTrader

Updated
    1. trending_up 1,716 views
    2. thumb_up 3 thanks given
    3. group 3 followers
    1. forum 5 posts
    2. attach_file 0 attachments




 
Search this Thread

On Update() method and Windows Form

  #1 (permalink)
Speculari
Cordoba Spain
 
Posts: 3 since Jul 2015
Thanks Given: 1
Thanks Received: 0

Hello everybody!

I have written a indicator for controlling a single windows form so it has a TrackBar control that when is sliding, as well a horizontal line (DrawHorizontalLine method) is sliding. I need to use update() method because OnBarUpdate() method need to be called every time TrackBar.Value change, reglardless a new tick income.

This is my purpose but the code doesn´t work. I think it is because Update() method is not working.

Here is the Windows form code:

 
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestFormNT
{
    public partial class FormTrading : Form
    {
        public int ActualValue
        {
            get { return trackBar1.Value; }
        }

        public FormTrading()
        {
            InitializeComponent();
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            if (OnNewValue != null)
                OnNewValue(this, new EventArgs());
        }

        // Event to notify NinjTrader that SL range has been changed.
        public event EventHandler OnNewValue;
    }
}
And here is the indicator code:

 
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;
// Added to allow use of the Visual Studio Form dll.
using TestFormNT;
using System.Windows.Forms;
#endregion

// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
    /// <summary>
    /// Not description
    /// </summary>
    [Description("Not description")]
    public class MyTestFormTrading : Indicator
    {
        #region Variables
        // Wizard generated variables
            private int myInput0 = 1; // Default setting for MyInput0
        // User defined variables (add any user defined variables below)
		private bool firstTime = true;
		private double incremento = 0;
		public int aux = 0;
		FormTrading Trading = new FormTrading();
        #endregion

        /// <summary>
        /// This method is used to configure the indicator and is called once before any bar data is loaded.
        /// </summary>
        protected override void Initialize()
        {
            Overlay				= false;
        }
		
		private void OnNewValueHandler(object sender, EventArgs e)
		{
			Trading.Controls["textBox1"].Text = Convert.ToString(Trading.ActualValue);
			Update();
		}

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
			if (FirstTime)
			{
				Trading.Show();
				FirstTime = false;
				
				Trading.OnNewValue += new EventHandler(OnNewValueHandler);
			}
			
			incremento = Convert.ToDouble(Trading.ActualValue);
			
			DrawHorizontalLine("last", true, Close[0] + incremento * 0.0001, Color.Orange, DashStyle.Solid, 2);
        }

        #region Properties
		public bool FirstTime
		{
			get { return firstTime; }
			set { firstTime = value; }
		}
		
        [Description("")]
        [GridCategory("Parameters")]
        public int MyInput0
        {
            get { return myInput0; }
            set { myInput0 = Math.Max(1, value); }
        }
        #endregion
    }
}
How do I update OnBarUpdate() when I slide trackBar?

Thank you very much!

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Are there any eval firms that allow you to sink to your …
Traders Hideout
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Exit Strategy
NinjaTrader
Deepmoney LLM
Elite Quantitative GenAI/LLM
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
38 thanks
NexusFi site changelog and issues/problem reporting
26 thanks
GFIs1 1 DAX trade per day journal
19 thanks
The Program
18 thanks
  #3 (permalink)
 Jaba 
Austin TX
 
Experience: Intermediate
Platform: SaintGobain Crystal Ball
Trading: 6E
Posts: 81 since Oct 2010
Thanks Given: 103
Thanks Received: 134


I wouldn't call OnBarUpdate() just to update a horizontal line, it is supposed to be used by NT whenever a bar changes, or tick arrives, depending on the CalculateOnBarClose settings. I refactored your code a little I hope you don't mind.

 
Code
		#region Variables
		private int myInput0 = 1; // Default setting for MyInput0
		// User defined variables (add any user defined variables below)
		private bool firstTime = true;
		private double incremento = 0;
		public int aux = 0;
		FormTrading _tradingForm;
		IHorizontalLine _theLine;
		#endregion

		/// <summary>
		/// This method is used to configure the indicator and is called once before any bar data is loaded.
		/// </summary>
		protected override void Initialize()
		{
			Overlay = false;
		}
		protected override void OnStartUp()
		{
			_tradingForm = new FormTrading();
			_tradingForm.OnNewValue += new EventHandler(OnNewValueHandler);
			_tradingForm.Show();
			_theLine = DrawHorizontalLine("last", true, Close[0], Color.Orange, DashStyle.Solid, 2);

		}
		protected override void OnTermination()
		{
			if (_tradingForm != null)
			{
				_tradingForm.Close();
				_tradingForm.OnNewValue -= OnNewValueHandler;
				_tradingForm.Dispose();
			}
		}
		private void OnNewValueHandler(object sender, EventArgs e)
		{
			_tradingForm.Controls["textBox1"].Text = Convert.ToString(_tradingForm.ActualValue);// <<< this should be in the Form
			incremento = Convert.ToDouble(_tradingForm.ActualValue); // <<< this is probably not needed
			double lineValue = Close[0] + (_tradingForm.ActualValue * this.Instrument.MasterInstrument.TickSize);
			TriggerCustomEvent(new CustomEvent(MoveLineCustomEvt), lineValue);
		}
		private void MoveLineCustomEvt(object state)
		{
			_theLine.Y = Close[0] + (_tradingForm.ActualValue * this.Instrument.MasterInstrument.TickSize);
			ChartControl.Refresh();
		}

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

Reply With Quote
Thanked by:
  #4 (permalink)
Speculari
Cordoba Spain
 
Posts: 3 since Jul 2015
Thanks Given: 1
Thanks Received: 0

Thank you very much!! Your code is fine and it has saved my NinjaTrader programmer live! Sorry by delay.

I have read out on events and event handlers. I am interested on winform will be a .exe file and not a .dll file and so I have coded a static class for .dll (StaticMemberTest.Class1) which is call by ninjatrader indicator.

This static class has a event I want to be handled by ninjatrader indicator but this not working.

Here post the new codes:

For form (.exe):

 
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using StaticMemberTest;

namespace TestFormNT
{
    public partial class FormTrading : Form
    {
        public int ActualValue
        {
            get { return trackBar1.Value; }
        }

        public FormTrading()
        {
            InitializeComponent();
            Class1.OnNewValue += new EventHandler(OnNewValueHandler);
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            Class1.SetIncrementoPrevio();
            Class1.SetIncremento(trackBar1.Value);
            Class1.CheckChange();
        }

        private void OnNewValueHandler(object sender, EventArgs e)
        {
            textBox1.Text = Convert.ToString(Class1.GetIncremento());
        }
    }
}
For static class (.dll):

 
Code
using System;
using System.Collections.Generic;
using System.Text;

namespace StaticMemberTest
{
    public static class Class1
    {
        // Declaración e iniciación de atributos.
        private static int _incremento = 0;

        private static int _incrementoPrevio = 0;

        // Métodos
        public static int GetIncremento()
        {
            return _incremento;
        }

        public static void SetIncremento(int _inc)
        {
            _incremento = _inc;
        }

        public static void SetIncrementoPrevio()
        {
            _incrementoPrevio = _incremento;
        }

        public static void CheckChange()
        {
            if (_incremento != _incrementoPrevio)
            {
                if (OnNewValue != null)
                {
                    OnNewValue(null, EventArgs.Empty);
                }
            }
        }

        // Eventos.
        public static event EventHandler OnNewValue;
    }
}
And the NinjaTrader indicator code:

 
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;
// Added to allow use of the static class (dll).
using StaticMemberTest;
#endregion

// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
    /// <summary>
    /// 
    /// </summary>
    [Description("")]
    public class MyTestFormTradingJabaMember02 : Indicator
    {
        #region Variables
        // Wizard generated variables
            private int myInput0 = 1; // Default setting for MyInput0
        // User defined variables (add any user defined variables below)
		IHorizontalLine _theLine;
        #endregion

        /// <summary>
        /// This method is used to configure the indicator and is called once before any bar data is loaded.
        /// </summary>
        protected override void Initialize()
        {
            Overlay				= false;
        }
		
		protected override void OnStartUp()
		{
			Class1.OnNewValue += new EventHandler(OnNewValueHandler);
			_theLine = DrawHorizontalLine("last", true, 0, Color.Orange, DashStyle.Solid, 2);
		}
		
		public void OnNewValueHandler(object sender, EventArgs e)
		{
			int lineValue = 0;
			TriggerCustomEvent(new CustomEvent(MoveLineCustomEvt), lineValue);
		}
		
		public void MoveLineCustomEvt(object state)
		{
			_theLine.Y = Close[0] + (Class1.GetIncremento() * this.Instrument.MasterInstrument.TickSize);
			ChartControl.Refresh();
		}

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            // Use this method for calculating your indicator values. Assign a value to each
            // plot below by replacing 'Close[0]' with your own formula.
        }
All Visual Studio code is compiled in .NET 3.0, for compatibility issues with NinjaTrader.

I think the problem is on the method public void OnNewValueHandler(object sender, EventArgs e) but I don´t know which is the solution.

Thank you!

P.D.: Sorry by my english.

Reply With Quote
  #5 (permalink)
Speculari
Cordoba Spain
 
Posts: 3 since Jul 2015
Thanks Given: 1
Thanks Received: 0

In my windows form code, private void OnNewValueHandler(object sender, EventArgs e) is for testing event handler work fine. But when I implement it in my indicator code, the event handler is not called.

What is the problem?

Thank you!

Reply With Quote
  #6 (permalink)
 Jaba 
Austin TX
 
Experience: Intermediate
Platform: SaintGobain Crystal Ball
Trading: 6E
Posts: 81 since Oct 2010
Thanks Given: 103
Thanks Received: 134


Speculari View Post
In my windows form code, private void OnNewValueHandler(object sender, EventArgs e) is for testing event handler work fine. But when I implement it in my indicator code, the event handler is not called.

What is the problem?

Thank you!

The static class approach works only if you access the dll from multiple methods within the same Application Domain. The Ninjascript is running in NinjaTrader Application Domain, and your WinForms exe is running in its own Application Domain. If you want them to communicate, you need to implement some sort of Inter-Application Communication or IAC mechanism. A few ideas on technologies you could use:
- TCP/IP sockets (similar to NTDirect.dll)
- named pipes
- some shared database (MS SQL with ServiceBroker enabled for PUSH, for example)
- Message Queuing (MSMQ, NServiceBus, RabbitMQ etc)
- web services (WCF full-duplex)
- shared memory ( actually there was a dll for TradeStation sometime ago, I forgot the name)

The shared/static class will not work for this. There was a discussion on that here on futures.io (formerly BMT), I can't find the link though.
J

Reply With Quote
Thanked by:




Last Updated on August 5, 2015


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts