Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Need little help/ Unable to access a global variable in custom method
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.
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.
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
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.
You can then reference 'nemVars.theAnswer' anywhere in all the Ninja codespace.
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.
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:
(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.
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
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...
}
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);
}