Need little help/ Unable to access a global variable in custom method - NinjaTrader | futures io social day trading
futures io futures trading


Need little help/ Unable to access a global variable in custom method
Updated: Views / Replies:1,798 / 9
Created: by nemeis45 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
 

Need little help/ Unable to access a global variable in custom method

  #1 (permalink)
Trading for Fun
delhi, delhi, india
 
Futures Experience: Intermediate
Platform: cqg ic
Favorite Futures: oil
 
Posts: 59 since Dec 2013
Thanks: 10 given, 13 received

Need little help/ Unable to access a global variable in custom method

Hi,
I have the following 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 System.Collections;
using System.Collections.Generic;
using System.Timers;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
    /// <summary>
    /// TestLinkedList
    /// </summary>
    [Description("TestLinkedList")]
    public class TestLinkedList : Strategy
    {
        #region Variables
        // Wizard generated variables
        private static int myInput = 1; // Default setting for MyInput0
		private static LinkedList<double> printlist = new LinkedList<double>();
		private static double lastprint =0;
		private static double someprint =0;		
		private static DateTime startuptime = DateTime.MinValue;		
		private static LinkedListNode<double> currentnode = new LinkedListNode<double>(0.00);
		private System.Timers.Timer secondtimer = new System.Timers.Timer(1000);		
				
        // User defined variables (add any user defined variables below)
        #endregion

        /// <summary>
        /// This method is used to configure the strategy and is called once before any strategy method is called.
        /// </summary>
        protected override void Initialize()
        {
            CalculateOnBarClose = false;
			Add(PeriodType.Second, 1);
			startuptime = DateTime.Now;
        }
		protected override void OnStartUp()
		{
			secondtimer.AutoReset = true;
			secondtimer.Elapsed+=  new ElapsedEventHandler((sender, e) => ProcessPerSecond(sender,e,currentnode));
			secondtimer.Enabled = true;				
			//GC.SuppressFinalize(secondtimer);
		}		

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {			
		
        }
		protected override void OnTermination()
		{
			// Cleans up our Timer object
			//secondtimer.Dispose();
		}
		protected void ProcessPerSecond(object sender, EventArgs e,LinkedListNode<double> somenode)
		{
			var instance = new TestLinkedList();
			if(Math.Round(somenode.Value,1)!= 0&&somenode!=null)
			{
				if(somenode.Value != printlist.Last.Value)
				{
					while(somenode!=null)
					{
						instance.Print("From LList");
						instance.Print("Last="+somenode.Value);
						somenode = somenode.Next;
					}
				}
			}
			instance.Print("here");										
			currentnode= printlist.Last;			
		}
		protected override void OnMarketData(MarketDataEventArgs e)
		{
			if(e.MarketDataType==MarketDataType.Last)
			{
				lock(printlist)
				{
				double nowprint = e.Price;
				if(Math.Round(nowprint,2)!=Math.Round(lastprint,2))
				{
					Print("Last Print is"+ nowprint );
					printlist.AddLast(nowprint);
					lastprint = nowprint;
				}
				}				
			}
			
		}
		
    #region Properties
        [Description("")]
        [GridCategory("Parameters")]        
		public int MyInput
        {
            get { return myInput; }           
        }
		public void SetMyInput(int somevalue)
		{
			 myInput = somevalue; 
		}
		[XmlIgnore()]
		public LinkedList<double> Printlist
		{
			get; set;
		}
		public double Lastprint
		{
			get;set;
		}
		public double Someprint
		{
			get;set;
		}		
		[XmlIgnore()]
		public LinkedListNode<double> Currentnode
		{
			get{return currentnode;}set{value = currentnode;}
		}
        #endregion
    }
}
What i am trying to do is take input from OnMarketData(), and then process the fresh data i have received every second.
I have used a timer for the purpose.

The problem i am getting is that the OnMarketData() is handling the global variables like lastprint just fine,
but i cannot seem to get global variables to work in my custom methods. in this case an event method ProcessPerSecond. When i try to access them in custom methods, they seem to be either returning null or a junk value.

Right now the only other thing i can think of is to print the data onto a file and then handle it in both methods separately, but that would significantly increase overhead, plus i want to know why the code isnt working.
How does NT differentiate between OnMarketData() and my custom method that i cannot seem to process global variables.

I tried changing the variable to static variable, using this.Variable to set the values etc, but nothing seems to be working.

Please help

Reply With Quote
The following user says Thank You to nemeis45 for this post:
 
  #2 (permalink)
Elite Member
Birmingham UK
 
Futures Experience: Intermediate
Platform: NinjaTrader
Broker/Data: IG/eSignal
Favorite Futures: Dax
 
ratfink's Avatar
 
Posts: 3,336 since Dec 2012
Thanks: 11,276 given, 7,090 received


nemeis45 View Post
I tried changing the variable to static variable, using this.Variable to set the values etc, but nothing seems to be working.

Please help

For reliable global variable use it is best to have the class loaded in an earlier opened workspace chart indicator. Then reference them anywhere using the class name prefix. You can just 'F5' to reload them before any edit/compilation cycles which will otherwise also delink. i.e. always load variables first, then reload distributed code. Also be aware of clearing them in any Initialise methods.

Travel Well
Reply With Quote
 
  #3 (permalink)
Trading for Fun
delhi, delhi, india
 
Futures Experience: Intermediate
Platform: cqg ic
Favorite Futures: oil
 
Posts: 59 since Dec 2013
Thanks: 10 given, 13 received


you mean like create a dummy class

class dummy: Indicator
{
private int variable1 =0;
private double varibale2 =0;

//Do nothing
// get; set for variables
}
save as an indiactor,
then

var instance = new Dummy();
and then access instance.variable1 and instance.variable2?

Is there any other way to do it, though it is marginally better than using a file to print, but in this case my variables will be in a different file, my code in another, again would be hard to debug if a bug comes up...

Just want to know though why the code isnt working. Is it because i made a mistake somewhere or is it due to Ninjatrader limitation?

The ideal scenario for me would be to use the dll which ninjatrader provides and work on a project using visual studio, but the dll which they provide have very limited functionality . i suspect its becuase they want people to use ninjascript only

Reply With Quote
 
  #4 (permalink)
Elite Member
Birmingham UK
 
Futures Experience: Intermediate
Platform: NinjaTrader
Broker/Data: IG/eSignal
Favorite Futures: Dax
 
ratfink's Avatar
 
Posts: 3,336 since Dec 2012
Thanks: 11,276 given, 7,090 received

It's not that hard, you can just declare the other class in the same file, always using 'public static', and only one declaration needed, it is made available to *all* code in your system then anyway, it's just the memory management and compilation re-linking that gets in the way when making changes.

 
Code
public static class nemVars
{
    public static double theAnswer = 0;
}

You can then reference 'nemVars.theAnswer' anywhere in all the Ninja codespace.

Travel Well

Last edited by ratfink; April 15th, 2014 at 07:34 AM. Reason: add code
Reply With Quote
The following 2 users say Thank You to ratfink for this post:
 
  #5 (permalink)
Trading for Fun
delhi, delhi, india
 
Futures Experience: Intermediate
Platform: cqg ic
Favorite Futures: oil
 
Posts: 59 since Dec 2013
Thanks: 10 given, 13 received

yes its not hard, will try it, not sure if will work 100%, cause the global variable is an object LinkedListNode<double> , there was earlier some problem with getter setter.
thanks!

is there any reason why the first code isnt working? also any other way to use global variables? why is this hard in ninjatrader? any difference in the way compiler sees code from visual studio.


Last edited by nemeis45; April 15th, 2014 at 08:03 AM.
Reply With Quote
 
  #6 (permalink)
Elite Member
Birmingham UK
 
Futures Experience: Intermediate
Platform: NinjaTrader
Broker/Data: IG/eSignal
Favorite Futures: Dax
 
ratfink's Avatar
 
Posts: 3,336 since Dec 2012
Thanks: 11,276 given, 7,090 received


nemeis45 View Post
is there any reason why the first code isnt working? also any other way to use global variables? why is this hard in ninjatrader? any difference in the way compiler sees code from visual studio.

I only glanced at it, applied my simple rule - if it looks too complicated for me then it probably is.

Stuff's not hard in NinjaTrader, I just don't think most people realise what an open environment these guys are providing and how enormous a risk that is, for them and for anybody who thinks programming is easy. Most people do not use simple debugging methods - e.g. Print output from the diagnostics, etc.

There's no difference in code, pretty much anything you can do in C#/.Net you can do under Ninja.

There is an almost infinite way to do things in any programming language, the key is to always look for examples, then learn to select and simplify and build your own bag of techniques and forget the rest.

E.g. if I were doing something like this I might use decalarations like:

 
Code
using System.Collections.Generic;

public class NemItem
{
    public int  value;
    public double ratio;
}


public static class Nem
{
    public static Dictionary<double, NemItem> MyItems = new Dictionary<double, NemItem>();
    public static int  count = 0;
}

(No relation to your application, just for illustration.)

But others might teach you to go the full get/set/object route, it's just choice and what works for you at the end of the day, but above all I say keep it simple and assume code is wrong unless you've seen a Print that says it's right. Cheers.

Travel Well
Reply With Quote
The following 2 users say Thank You to ratfink for this post:
 
  #7 (permalink)
Trading for Fun
delhi, delhi, india
 
Futures Experience: Intermediate
Platform: cqg ic
Favorite Futures: oil
 
Posts: 59 since Dec 2013
Thanks: 10 given, 13 received

 
Code
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using System.Collections;
using System.Collections.Generic;
using System.Timers;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
	public static class GlobalVariables
	{
		public static int myInput = 1; // Default setting for MyInput0
		public static LinkedList<double> printlist = new LinkedList<double>();
		public static double lastprint =0;
		public static double someprint =0;		
		public static LinkedListNode<double> currentnode = new LinkedListNode<double>(0.00);
	}
    /// <summary>
    /// TestLinkedList
    /// </summary>
    [Description("TestLinkedList")]
    public class TestLinkedList : Strategy
    {
        #region Variables
        // Wizard generated variables
		private static DateTime startuptime = DateTime.MinValue;        
		private System.Timers.Timer secondtimer = new System.Timers.Timer(1000);		
				
        // User defined variables (add any user defined variables below)
        #endregion

        /// <summary>
        /// This method is used to configure the strategy and is called once before any strategy method is called.
        /// </summary>
        protected override void Initialize()
        {
            CalculateOnBarClose = false;
			Add(PeriodType.Second, 1);
			startuptime = DateTime.Now;
        }
		protected override void OnStartUp()
		{
			secondtimer.AutoReset = true;
			secondtimer.Elapsed+=  new ElapsedEventHandler((sender, e) => ProcessPerSecond(sender,e,GlobalVariables.currentnode));
			secondtimer.Enabled = true;	
			
		}		

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {			
		
        }
		protected override void OnTermination()
		{
			// Cleans up our Timer object
			//secondtimer.Dispose();
		}
		protected void ProcessPerSecond(object sender, EventArgs e,LinkedListNode<double> somenode)
		{
			var instance = new TestLinkedList();
			if(Math.Round(somenode.Value,1)!= 0&&somenode!=null)
			{
				if(Math.Round(somenode.Value,1) != Math.Round(GlobalVariables.printlist.Last.Value,1))
				{
					while(somenode!=null)
					{
						instance.Print("From LList");
						instance.Print("Last="+somenode.Value);
						somenode = somenode.Next;
					}
				}
			}
			instance.Print("here");
			GlobalVariables.currentnode= GlobalVariables.printlist.Last;			
		}
		protected override void OnMarketData(MarketDataEventArgs e)
		{
			if(e.MarketDataType==MarketDataType.Last)
			{
				lock(GlobalVariables.printlist)
				{
				double nowprint = e.Price;
				if(Math.Round(nowprint,2)!=Math.Round(GlobalVariables.lastprint,2))
				{
					Print("Last Print is"+ nowprint );
					GlobalVariables.printlist.AddLast(nowprint);
					GlobalVariables.lastprint = nowprint;
				}
				}				
			}
			
		}
		
    #region Properties
        [Description("")]
        [GridCategory("Parameters")]     
		
		public double Someprint
		{
			get;set;
		}
		public DateTime Startuptime
		{
			get;set;
		}
        #endregion
    }
}
ok i did this. but the same problem is persisting. the timer() isnt working. ill try and debug .
it only times once.

Edit: Nope cant get the timer to work. there is no reason for timer object to be public.
there is nothing complicated about this code. Just takes data from OnMarketData, puts it in a linkedlist and then tries to process fresh data from LinkedList when event from timer is called.

I am getting the data from OnMarketData all right, its printing the o/p allright , problem is processing it from the timer, or any other custom method for that matter.
Also used GC.KeepAlive(), i am pretty sure now its not a timer problem.
I have tested all the individual code snippets in visual studio, the logic works fine, please do help have been struck at same point for almost a week


Last edited by nemeis45; April 15th, 2014 at 09:11 AM.
Reply With Quote
 
  #8 (permalink)
Elite Member
Florida
 
Futures Experience: Advanced
Platform: NinjaTrader 8
Favorite Futures: YM, ES, NQ, CL, ZB
 
trendwaves's Avatar
 
Posts: 742 since Dec 2012
Thanks: 2,822 given, 2,433 received

Try putting the declaration outside the namespace like this...


#region Using declarations
using System;
.......
#endregion

#region GlobalVariableDeclarations
public static class GlobalVariables
{
public static int myInput = 1; // Default setting for MyInput0
public static LinkedList<double> printlist = new LinkedList<double>();
public static double lastprint =0;
public static double someprint =0;
public static LinkedListNode<double> currentnode = new LinkedListNode<double>(0.00);
}
#endregion

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// Summary...
}

Be Patient and Trade Smart
Reply With Quote
The following 3 users say Thank You to trendwaves for this post:
 
  #9 (permalink)
Trading for Fun
delhi, delhi, india
 
Futures Experience: Intermediate
Platform: cqg ic
Favorite Futures: oil
 
Posts: 59 since Dec 2013
Thanks: 10 given, 13 received

Thanks!, did that now at least the timer is firing

Last Print is103.54
Last Print is103.55
here
here

Its getting fired twice though, i set the timer interval to 10 seconds just to be sure,
its supposed to print "here" only once.

still i think i am dealing with garbage values...

Also in OnTermination()
if i use
GlobalVariables.secondtimer.Dispose();

and i try to apply strategy now, it gives an error in output,
**NT** Error on calling 'OnStartUp' method for strategy 'TestLinkedList/6dce577bd2a643f5b55160c750207055': Cannot access a disposed object.
Object name: 'Timer'.

any idea as to this? getting junk output. how else to dispose off timer

public static class GlobalVariables
{
public static int myInput = 1; // Default setting for MyInput0
public static LinkedList<double> printlist = new LinkedList<double>();
public static double lastprint =0;
public static double someprint =0;
public static LinkedListNode<double> currentnode = new LinkedListNode<double>(0.00);
public static System.Timers.Timer secondtimer = new System.Timers.Timer(10000);
}

Reply With Quote
 
  #10 (permalink)
Trading for Fun
delhi, delhi, india
 
Futures Experience: Intermediate
Platform: cqg ic
Favorite Futures: oil
 
Posts: 59 since Dec 2013
Thanks: 10 given, 13 received


Got it working nvm, please ignore

Reply With Quote

Reply



futures io > > > > Need little help/ Unable to access a global variable in custom method

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
Looking to pay someone to create many Custom Alerts & Custom Scans on ThinkOrSwim jasonbanash ThinkOrSwim Programming 1 January 30th, 2014 07:26 AM
Possible to Access Trades Collection from Inside Optimization Method? (Monte Carlo) kbeary33 NinjaTrader Programming 3 August 13th, 2013 07:45 PM
Using Global Variable string with Excel is causing problem arjfca MultiCharts 0 July 11th, 2013 07:02 PM
Is there a 64bit Global Variable replacement? Tarkus11 NinjaTrader Programming 4 March 16th, 2011 08:47 AM
NT7 new account access method marotom NinjaTrader Programming 3 January 18th, 2010 10:49 AM


All times are GMT -4. The time now is 06:45 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-13 in 0.15 seconds with 19 queries on phoenix via your IP 54.196.182.102