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)
I'd like to share a script I've been using when there's a need to share variables across strategies or indicators.
---
Description
This Singleton design pattern should work well in NinjaTrader, especially if you want to store values that should remain consistent across different indicators or strategies running simultaneously.
You must define this class at the top of your script:
This code creates a single instance of the My_Shared_Variables class, making it a global storage accessible across different parts of your strategy or indicator.
Quick breakdown of how this pattern works
Singleton Implementation:
The line private static readonly My_Shared_Variables _GlobalInstance = new My_Shared_Variables(); creates a single instance (_GlobalInstance) of My_Shared_Variables.
The constructor private My_Shared_Variables() is private, so no other instances of My_Shared_Variables can be created, ensuring only one instance throughout the application.
Accessing the Singleton Instance:
Access the Singleton instance via My_Shared_Variables.GlobalInstance. For example:
Thread Safety:
This pattern is thread-safe as it initializes _GlobalInstance statically. This ensures that initialization happens only once, even in a multithreaded environment.