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 junkone, Today, 11:37 AM
        0 responses
        1 view
        0 likes
        Last Post junkone
        by junkone
         
        Started by quantismo, 04-17-2024, 05:13 PM
        5 responses
        33 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by proptrade13, Today, 11:06 AM
        1 response
        5 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Started by love2code2trade, 04-17-2024, 01:45 PM
        4 responses
        34 views
        0 likes
        Last Post love2code2trade  
        Started by cls71, Today, 04:45 AM
        2 responses
        10 views
        0 likes
        Last Post eDanny
        by eDanny
         
        Working...
        X