On Update() method and Windows Form - NinjaTrader Programming | futures io social day trading
futures io futures trading


On Update() method and Windows Form
Updated: Views / Replies:546 / 5
Created: by Speculari Attachments:0

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors – all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you don’t need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 
Thread Tools Search this Thread
 

On Update() method and Windows Form

  #1 (permalink)
Trading Apprentice
Cordoba Spain
 
Futures Experience: Beginner
Platform: NinjaTrader
Favorite Futures: Forex
 
Posts: 3 since Jul 2015
Thanks: 1 given, 0 received

On Update() method and Windows Form

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
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (permalink)
Elite Member
Austin TX
 
Futures Experience: Intermediate
Platform: SaintGobain Crystal Ball
Favorite Futures: 6E
 
Posts: 80 since Oct 2010
Thanks: 102 given, 129 received


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
The following 2 users say Thank You to Jaba for this post:
 
  #4 (permalink)
Trading Apprentice
Cordoba Spain
 
Futures Experience: Beginner
Platform: NinjaTrader
Favorite Futures: Forex
 
Posts: 3 since Jul 2015
Thanks: 1 given, 0 received

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)
Trading Apprentice
Cordoba Spain
 
Futures Experience: Beginner
Platform: NinjaTrader
Favorite Futures: Forex
 
Posts: 3 since Jul 2015
Thanks: 1 given, 0 received

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)
Elite Member
Austin TX
 
Futures Experience: Intermediate
Platform: SaintGobain Crystal Ball
Favorite Futures: 6E
 
Posts: 80 since Oct 2010
Thanks: 102 given, 129 received


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


Last edited by Jaba; August 6th, 2015 at 10:22 AM. Reason: typo
Reply With Quote
The following user says Thank You to Jaba for this post:

Reply



futures io > > > > > On Update() method and Windows Form

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Linda Bradford Raschke: Reading The Tape

Elite only

Adam Grimes: TBA

Elite only

NinjaTrader: TBA

January

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Benefiting form (Greece bailout ) lexxmd The Elite Circle 2 June 28th, 2015 01:21 AM
Greetings form the UK! conquest Reviews of Brokers and Data Feeds 3 January 21st, 2015 08:58 PM
windows 8 update issues with NT asiaexpat NinjaTrader 6 August 17th, 2013 12:54 PM
Own parameter form ejzj1 NinjaTrader Programming 9 February 1st, 2011 07:20 AM


All times are GMT -4. The time now is 08:27 AM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts
Page generated 2017-12-12 in 0.11 seconds with 19 queries on phoenix via your IP 54.90.207.75