NexusFi: Find Your Edge


Home Menu

 





Creating global variables for Ninjatrader


Discussion in Traders Hideout

Updated
    1. trending_up 93 views
    2. thumb_up 6 thanks given
    3. group 3 followers
    1. forum 0 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,536 since Oct 2009
Thanks Given: 4,194
Thanks Received: 6,045

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




Last Updated on October 26, 2024


© 2024 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 - Privacy Policy - Sitemap - Downloads - Top
no new posts