NexusFi: Find Your Edge


Home Menu

 





Creating global variables for Ninjatrader


Discussion in Traders Hideout

Updated
    1. trending_up 621 views
    2. thumb_up 9 thanks given
    3. group 4 followers
    1. forum 1 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
 
trendisyourfriend's Avatar
 trendisyourfriend 
Quebec Canada
Legendary Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: AMP/CQG
Trading: ES, NQ, YM
Frequency: Daily
Duration: Minutes
Posts: 4,553 since Oct 2009
Thanks Given: 4,218
Thanks Received: 6,123

Hi fio coding members,

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:

 
Code
public class My_Shared_Variables
    {

        #region VARIABLES_to_SHARE
        //    define the variables you want to share globally
            private static double _Count;

        ///    To write/read this variable you call these methods:
        //    My_Shared_Variables.GlobalInstance.AddToCount(5);
        //    My_Shared_Variables.GlobalInstance.GetCount();

        #endregion

        private static readonly My_Shared_Variables _GlobalInstance = new My_Shared_Variables();

        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static My_Shared_Variables()
        {

        }

        private My_Shared_Variables()
        {

        }

        public static My_Shared_Variables GlobalInstance
        {
            get
            {
                return _GlobalInstance;
            }
        }

        public void AddToCount(double value)
        {
            _Count += value;
        }

        public double GetCount()
        {
            return _Count;
        }
    }
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:
 
Code
My_Shared_Variables.GlobalInstance.AddToCount(5);
double count = My_Shared_Variables.GlobalInstance.GetCount();
Thread Safety:

This pattern is thread-safe as it initializes _GlobalInstance statically. This ensures that initialization happens only once, even in a multithreaded environment.


Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Experience with this Trading Indicator/System from www.p …
Traders Hideout
For DOM based trader, market markers. (ES/NQ)
Traders Hideout
The Safety Trade System
Platforms and Indicators
GANN THEORY DAVID BOWDEN SAFETY IN THE MARKETS
Traders Hideout
Small Indicator to visually see Nth Bar Close - NT8
Traders Hideout
 
  #2 (permalink)
 wavey 
Germany / Italy
 
Experience: Advanced
Platform: NT8, TS, TV
Trading: Index Futures, FX
Posts: 87 since Nov 2009
Thanks Given: 52
Thanks Received: 37


trendisyourfriend View Post
Hi fio coding members,

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:

 
Code
public class My_Shared_Variables
    {

        #region VARIABLES_to_SHARE
        //    define the variables you want to share globally
            private static double _Count;

        ///    To write/read this variable you call these methods:
        //    My_Shared_Variables.GlobalInstance.AddToCount(5);
        //    My_Shared_Variables.GlobalInstance.GetCount();

        #endregion

        private static readonly My_Shared_Variables _GlobalInstance = new My_Shared_Variables();

        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static My_Shared_Variables()
        {

        }

        private My_Shared_Variables()
        {

        }

        public static My_Shared_Variables GlobalInstance
        {
            get
            {
                return _GlobalInstance;
            }
        }

        public void AddToCount(double value)
        {
            _Count += value;
        }

        public double GetCount()
        {
            return _Count;
        }
    }
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:
 
Code
My_Shared_Variables.GlobalInstance.AddToCount(5);
double count = My_Shared_Variables.GlobalInstance.GetCount();
Thread Safety:

This pattern is thread-safe as it initializes _GlobalInstance statically. This ensures that initialization happens only once, even in a multithreaded environment.

Thanks for sharing this pattern, trendisyourfriend

Trying to get a once in a session reload of historical data stabilized, right now as per docs you should really only trigger if the strategy has seen a connection loss event. Seeing a lot of data inconsistencies unfortunately looking to just have a from server reload forced in before the actual hours of trading for the bots start.

So looking for a smart way to store data had been reloaded for a strategy instance for the given session. Could probably also write to some txt file to persist states.


Reply With Quote




Last Updated on March 1, 2025


© 2025 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
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.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts