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!
See more
See less

Partner 728x90

Collapse

Single Instance Strategy/Indicator/Addon

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Single Instance Strategy/Indicator/Addon

    I need to be able to restrict a Strategy/Indicator in both NT7 and NT8 and an Addon in NT8 to run only a single instance.

    I have code for a thread-safe Singleton as follows:
    Code:
    [FONT="Courier New"]
    public sealed class Singleton
    {
    	private static volatile Singleton instance;
    	private static object syncRoot = new Object();
    	
    	private Singleton() {}
    	
    	public static Singleton Instance
    	{
    		get 
    		{
    			if (instance == null) 
    			{
    				lock (syncRoot) 
    				{
    				if (instance == null) 
    					instance = new Singleton();
    				}
    			}
    			
    			return instance;
    		}
    	}
    }
    [/FONT]
    which I use like this:
    Code:
    [FONT="Courier New"]
    Singleton single = Singleton.Instance;
    if (single == null) // ... Exit appropriately
    [/FONT]
    I also have code for a Mutex approach:
    Code:
    [FONT="Courier New"]
    public static Mutex mutex  = new Mutex(true,"MyDogHasFleas") ;
    public static bool SingleOut()
    {
    	if (mutex.WaitOne(TimeSpan.Zero,true))
    	{
    		mutex.ReleaseMutex() ;
    		return true ;
    	}
    	else
    	{
    		MessageBox.Show("Only one instance at a time!") ;
    		return false ;
    	}
    }
    [/FONT]
    which I use like this:
    Code:
    [FONT="Courier New"]
    bool solo = SingleOut();
    if (!solo) // ... Exit appropriately
    [/FONT]
    This all sits in the (Public or Partial) Strategy class.

    I cannot seem to make either approach work.In both approaches, I can run a strategy several times and each runs without detecting any previous instance.

    I am obviously doing something wrong and would appreciate advice on getting both approaches working in NT7 and NT8.

    Thanks.
    Multi-Dimensional Managed Trading
    jeronymite
    NinjaTrader Ecosystem Vendor - Mizpah Software

    #2
    Hello jeronymite,

    This custom code is beyond what our support is able to assist with, however, I do have an example of using a static property to share information between indicator / strategy instances.



    This thread will remain open for any community members that would like to assist.

    You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by samish18, Yesterday, 08:31 AM
    4 responses
    14 views
    0 likes
    Last Post elirion
    by elirion
     
    Started by funk10101, Yesterday, 09:43 PM
    1 response
    13 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by TheWhiteDragon, 01-21-2019, 12:44 PM
    5 responses
    551 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by rtwave, 04-12-2024, 09:30 AM
    5 responses
    37 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by funk10101, Today, 12:02 AM
    1 response
    11 views
    0 likes
    Last Post NinjaTrader_LuisH  
    Working...
    X