Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
MarketAnalyzer Columns use data from text file
Collapse
X
-
MarketAnalyzer Columns use data from text file
My MarketAnalyzer column imports a small text file that contains some values needed for its' calculation. The problem is, when I load a MarketAnalyzer window with the 500 stocks in the S&P, each row does a fetch at the same time which results in some file in use errors. Is there any way to program my column in a way that it could import this file once and the value could be shared with each row in the MarketAnalyzer window?Tags: None
-
Hi swcooke thanks for your note.
Unfortunately, loading files outside of NT does go out of the scope of support I can provide, so I would not be able to find a direct answer to this problem. I want to say you could do a using statement on the file like in this stack overflow answer:
I'm writing a program in C# that needs to repeatedly access 1 image file. Most of the time it works, but if my computer's running fast, it will try to access the file before it's been saved back to...
If the columns all have the same data, you could load it in just once into a static class. See this post I made on the subject (post #2 on this page):
Please let me know if you have any further questions.Chris L.NinjaTrader Customer Service
-
Hi Chris,
Thanks for your reply. I am not experienced with Addons but I just gave the documentation a quick scan. I'm looking at these sample code blocks that would allow me to do something at startup and at shutdown of NT:
Code:protected override void OnWindowCreated(Window window) { if(window is ControlCenter){ DeactivateLicenseAsync(); } }
Code:protected override void OnWindowDestroyed(Window window) { if(window is ControlCenter){ DeactivateLicenseAsync(); } }
I'm just trying to learn if this is hypothetically possible before I start digging into it. If so, I could just put a timer in the Addon that would fetch the file if it's over an hour old or something. The Addon could just serve data when called by the MarketAnalyzer column. Possible?
Comment
-
Hello swooke,
An AddOn could be built which starts operations in OnWindowCreated when the Control Center is created to start a task on start up. It is also possible to have other NinjaScripts access public properties of an AddOn. I have linked an example below. (This example demonstrates indicators and strategies, but the same concepts can be applied to other NinjaScripts.)
What you are describing would be a plausible approach. The AddOn could read the file periodically, and your Market Analyzer Column can then request that information from the AddOn. You could also consider using try/catches to see if a a StreamReader has failed reading a file if it is already in use. If it fails, you can trap that occurence in the catch, and notify the script that you would need to attempt reading the file again. Our SampleStreamReader example demonstrates using a try/catch with a StreamReader.
SampleStreamReader - https://ninjatrader.com/support/help...o_read_fro.htm
Please let us know if you have any additional questions.JimNinjaTrader Customer Service
Comment
-
Hi Jim,
Here is a quick AddOn that holds the value of a dictionary and returns it to a caller:
Code:namespace NinjaTrader.NinjaScript.AddOns { public class sjSharesAddOn : NinjaTrader.NinjaScript.AddOnBase { public static class Shares{ public static Dictionary<string,double> Dict = new Dictionary<string,double>(GetShares()); public static Dictionary<string, double> GetShares(){ Dictionary<string,double> result = new Dictionary<string,double>(); result.Add("MSFT",200); return result; } } } }
Last edited by swcooke; 01-27-2020, 08:44 AM.
Comment
-
Hello swcooke,
What you are looking at is a standard C# structure, a class with a static property. To learn more about why this works or how it works you can search online for C# static examples or other information related to using static in C#. This type of information won't be in any NinjaScript documentation because this is not a NinjaScript concept but is just C#.
MSDN provides a wealth of knowledge on this topic however it may be pretty dry to read, you can also search google for "C# static examples" for some other ideas. https://docs.microsoft.com/en-us/dot...-class-members
The static class and property will be created at runtime so you won't need to instantiate the class or do anything else, you just need to use its static member one time for that to happen automatically. The property would be destroyed when you do that yourself or when NinjaScript is reloaded during a compile. Otherwise it would just remain in memory once accessed.
Keep in mind using static in other areas like for an indicators property or variable would never be suggested, static is very powerful and should only be used in very specific scenarios like this with a specific intention.
I look forward to being of further assistance.JesseNinjaTrader Customer Service
- Likes 1
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by mly916, Today, 09:48 PM
|
0 responses
6 views
0 likes
|
Last Post
by mly916
Today, 09:48 PM
|
||
Started by Mikey_, Today, 09:21 PM
|
0 responses
10 views
0 likes
|
Last Post
by Mikey_
Today, 09:21 PM
|
||
Started by cre8able, Today, 05:53 PM
|
3 responses
13 views
0 likes
|
Last Post
by cre8able
Today, 09:20 PM
|
||
Started by Aryan Kumar, Today, 09:04 PM
|
0 responses
4 views
0 likes
|
Last Post
by Aryan Kumar
Today, 09:04 PM
|
||
Started by yertle, Yesterday, 05:49 PM
|
3 responses
16 views
0 likes
|
Last Post
by Mindset
Today, 08:38 PM
|
Comment