• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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:
    
    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;
    		}
    	}
    }
    
    which I use like this:
    Code:
    
    Singleton single = Singleton.Instance;
    if (single == null) // ... Exit appropriately
    
    I also have code for a Mutex approach:
    Code:
    
    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 ;
    	}
    }
    
    which I use like this:
    Code:
    
    bool solo = SingleOut();
    if (!solo) // ... Exit appropriately
    
    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.

    #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.

    https://ninjatrader.com/support/foru...980#post442980

    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 Atilla, Today, 04:05 PM
    0 responses
    4 views
    0 likes
    Last Post Atilla
    by Atilla
     
    Started by Displacer, Today, 02:57 PM
    2 responses
    13 views
    0 likes
    Last Post Displacer  
    Started by SAILIKITY, 05-22-2019, 05:41 PM
    1 response
    8 views
    0 likes
    Last Post NinjaTrader_PaulH  
    Started by rmillar, 01-09-2019, 06:02 PM
    6 responses
    70 views
    1 like
    Last Post eDanny
    by eDanny
     
    Started by mlarocco, Today, 11:39 AM
    4 responses
    22 views
    0 likes
    Last Post mlarocco  
    Working...
    X