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

Multi-threading and SubmitOrder

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

    Multi-threading and SubmitOrder

    Hello,
    I want to submit an entry order on a secundary thread, but does not work and Ninjatrader is frozen.
    Is there any internal lock inside SubmitOrder() ?
    If I replace the function "SubmitOrder" by "Print" works well. So I suspect there is some internal lock. Could you confirm this point?.
    Thanks very much.

    Code:
    using System.Threading;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    
    namespace NinjaTrader.Strategy
    {
        public class Zs7Borrar01 : Strategy
        {
    	private bool done = false;
    
            protected override void Initialize()
            {
    		Unmanaged = true;
               	CalculateOnBarClose = true;
            }
    
            protected override void OnBarUpdate()
            {
    		if ( !Historical && !done )
    		{
    			Thread ts = new Thread(threadSubmitEntryOrder);
    			ts.Start();
    			ts.Join();
    			done = true;
    		}
            }
    		
    	private void threadSubmitEntryOrder()
    	{
    		IOrder o = SubmitOrder( 0, OrderAction.Buy, OrderType.Market, 1, 0.0, 0.0, string.Empty, "Compra" );
    	}
        }
    }

    #2
    Hello,

    To work in new thread you need to send current "engine" to that. Example code:

    protected override void OnBarUpdate()
    {
    if ( !Historical && !done )
    {
    Thread ts = new Thread(threadSubmitEntryOrder);
    ts.Start(this);
    done = true;
    }
    }

    private void threadSubmitEntryOrder(object data)
    {
    StrategyBase strategy = data as StrategyBase;


    IOrder o = stategy.SubmitOrder( 0, OrderAction.Buy, OrderType.Market, 1, 0.0, 0.0, string.Empty, "Compra" );
    }

    Comment


      #3
      Thanks Mikha, but still not working. NinjaTrader is frozen.

      With StrategyBase.Print() works correctly, but not with StrategyBase.SubmitOrder(). I think there is some internal lock.

      Comment


        #4
        Hello cls71,

        While running in real-time data, NinjaTrader will have the incoming data on a single thread and it will process the NinjaScript/Order submissions on another thread. Unfortunately, submitting an order an a different thread other than the one intended would not be supported.
        JCNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by bortz, 11-06-2023, 08:04 AM
        47 responses
        1,611 views
        0 likes
        Last Post aligator  
        Started by jaybedreamin, Today, 05:56 PM
        0 responses
        9 views
        0 likes
        Last Post jaybedreamin  
        Started by DJ888, 04-16-2024, 06:09 PM
        6 responses
        19 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by Jon17, Today, 04:33 PM
        0 responses
        6 views
        0 likes
        Last Post Jon17
        by Jon17
         
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        22 views
        0 likes
        Last Post Javierw.ok  
        Working...
        X