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

Cannot implicitly convert type 'void' to Ninjatrader.Cbi.IOrder error.

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

    Cannot implicitly convert type 'void' to Ninjatrader.Cbi.IOrder error.

    I can't figure out what's wrong with my code for a strategy that reverses at a trailing stop. The errors come up on the lines containing settrailstop

    Code:
             #region Variables
            // Wizard generated variables
            // User defined variables (add any user defined variables below)
            #endregion
                      private IOrder longentry = null;
    		  private IOrder shortentry = null;
    		  private IOrder longstop = null;
    		  private IOrder shortstop = null;
    		  
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
         
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Condition set 1
                if (SMA(14)[0] < Close[0]
                    && ToTime(Time[0]) == ToTime(6, 0, 0))
                {
                    longentry = EnterLong(1, "long");
    				
    				
                
                }
    
                // Condition set 2
                if (SMA(14)[0] < Close[0]
                    && ToTime(Time[0]) == ToTime(6, 0, 0))
                {
                    shortentry = EnterShort(1, "short");
    				
                    
                }
            }
             protected override void OnOrderUpdate(IOrder order)
            {
    			// Handle entry orders here. The entryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
    			if (longentry != null && longentry == order)
    			{	
    				// Reset the entryOrder object to null if order was cancelled without any fill
    				if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
    				{
    					longentry = null;
    					
    				}
    			}
    			if (shortentry != null && shortentry == order)
    			{	
    				// Reset the entryOrder object to null if order was cancelled without any fill
    				if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
    				{
    					shortentry = null;
    				}
    			}
            }
    		 protected override void OnExecution(IExecution execution)
            {
    			/* We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
    			which ensures your strategy has received the execution which is used for internal signal tracking. */
    			if (longentry != null && longentry == execution.Order)
    			{
    				if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    				{
    					// Stop-Loss order 4 ticks below our entry price
    				longstop = SetTrailStop("long", CalculationMode.Ticks, 40, false);
    					
    					// Target order 8 ticks above our entry price
    					 SetProfitTarget("long", CalculationMode.Ticks, 50);
    					
    					
    					if (execution.Order.OrderState != OrderState.PartFilled)
    					{
    						longentry	= null;
    					}
    				}
    			}
    		
    			// Reset our stop order and target orders' IOrder objects after our position is closed.
    			if ((longstop != null && longstop == execution.Order))
    			{
    				if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
    				{
    					shortentry = EnterShort(1, "short");
    				    SetTrailStop("short", CalculationMode.Ticks, 40, false);
    				    SetProfitTarget("short", CalculationMode.Ticks, 50);
    				}
    			}
    			/* We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
    			which ensures your strategy has received the execution which is used for internal signal tracking. */
    			if (shortentry != null && shortentry == execution.Order)
    			{
    				if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    				{
    					// Stop-Loss order 4 ticks below our entry price
    					shortstop = SetTrailStop("short", CalculationMode.Ticks, 40, false);
    					
    					// Target order 8 ticks above our entry price
    					 SetProfitTarget("short", CalculationMode.Ticks, 50);
    					
    					// Resets the entryOrder object to null after the order has been filled
    					if (execution.Order.OrderState != OrderState.PartFilled)
    					{
    						 shortentry	= null;
    					}
    				}
    			}
    		
    			// Reset our stop order and target orders' IOrder objects after our position is closed.
    			if ((shortstop != null && shortstop == execution.Order))
    			{
    				if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
    				{
    					longentry = EnterLong(1, "long");
    					SetTrailStop("long", CalculationMode.Ticks, 40, false);
    				    SetProfitTarget("long", CalculationMode.Ticks, 50);
    				}
    			}
    		}

    #2
    Hello,

    Thank you for the question.

    The error would be because the Set methods are void methods. This means that they do not return an IOrder object. in C# void is a term for a method that does not need to return a value to where it had been called from.


    using only the following would work:

    Code:
    SetTrailStop("long", CalculationMode.Ticks, 40, false);
    If you are trying to find the order for the trailing stop, it depends are you trying to find when this order was filled or before that? You would need to utilize either the OnOrderUpdate or the OnExecution methods to find this order specifically by its name.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks Jesse,
      I'm trying to find when the trailstop gets filled so I can enter a position reversing order with the same conditions and name as the first entry, just backwards because of the flip of position, and then if that trailing stop gets hit reverse the positon with the same named order as the first, so it will continue to reverse until a target is hit, similar to the ATM's reverse at stop.

      Comment


        #4
        Hello,

        Thank you for the reply.

        You already almost have what you need to find the order when it is filled in what you had provided. You would simply need to add another condition to look for that order.

        First, here is a simple example of displaying All filled orders to see what each order object contains for assistance in seeing what is available:

        Code:
        protected override void OnExecution(IExecution execution)
        {
        	if(execution.Order != null && execution.Order.OrderState == OrderState.Filled)
        	{
        		Print(execution.Order.ToString());
        	}
        }
        You will see the following output:

        Code:
        Order='NT-01777/Sim101' [B]Name='Trail stop' [/B]State=Filled Instrument='ES 06-16' Action=Sell Limit price=0 Stop price=2034.75 Quantity=1 Strategy='TrendDown' Type=Stop Tif=Gtc Oco='NT-01182-32' Filled=1 Fill price=2034.75 Token='782bb48a0bf34cca916a1e2a25d738b4' Gtd='12/1/2099 12:00:00 AM'
        There is a Name property on the Order object, so that would mean a condition could be something like:

        Code:
        protected override void OnExecution(IExecution execution)
        {
        	if(execution.Order != null && execution.Order.OrderState == OrderState.Filled && execution.Order.Name == "Trail stop")
        	{
        		Print(execution.Order.ToString());
        	}
        }

        I look forward to being of further assistance.
        Last edited by NinjaTrader_Jesse; 03-23-2016, 08:24 AM. Reason: modified to Trail stop
        JesseNinjaTrader Customer Service

        Comment


          #5
          Ok that works. Is there another condition I can enter along with the trail stop name that will specify whether it was the trail stop from a long position or a short position so I can enter a reversing order?

          Comment


            #6
            Hello,

            You could either use the FromEntrySignal if you are using Signal names:

            Code:
            execution.Order.FromEntrySignal == "SomeSignalName"
            or the type of the order:

            Code:
            execution.Order.OrderAction == OrderAction.Sell
            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Thanks. I was able to get the strategy running. However, when backtesting, for some reason on many days no trade is being submitted. My conditions for the first order are either a long or short trade if , ToTime(Time[0]) == ToTime(5, 59, 59), and if SMA(14)[0] < Close[0] for short trade or SMA(14)[0] > Close[0] for long trade. A backtested from 2/16 to 3/16, and trades were placed only 7 of the days. I'm using a 4 or 1 tick range for the data series.
              Last edited by sampras010; 03-24-2016, 09:32 AM.

              Comment


                #8
                Hello,

                It would be hard to say just based on the settings mentioned on what may be happening.

                Have you tried printing the values of the conditions outside of the condition to see why they are not becoming true when you think it should ?

                For example, if you are using
                ToTime(Time[0]) == ToTime(5, 59, 59), and if SMA(14)[0] < Close[0

                You could do something simple in OnBarUpdate like:

                Code:
                Print(ToTime(Time[0]) + " == " + ToTime(5, 59, 59));
                Print(SMA(14)[0] + " < " + Close[0]);
                This would give you the Values of each to compare in the output window to see why on that Time, it was not true when you think it should be. From there you could determine what the root cause is that the values are not the same.

                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Hey How can I do this for NT8?

                  Comment


                    #10
                    Hello rickyblah12,

                    What part are you referring to? The original post is about an error, are you referring to saving orders as variables or some other part of this post?

                    I look forward to being of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Perr0Grande, Today, 08:16 PM
                    0 responses
                    2 views
                    0 likes
                    Last Post Perr0Grande  
                    Started by elderan, Today, 08:03 PM
                    0 responses
                    4 views
                    0 likes
                    Last Post elderan
                    by elderan
                     
                    Started by algospoke, Today, 06:40 PM
                    0 responses
                    10 views
                    0 likes
                    Last Post algospoke  
                    Started by maybeimnotrader, Today, 05:46 PM
                    0 responses
                    9 views
                    0 likes
                    Last Post maybeimnotrader  
                    Started by quantismo, Today, 05:13 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post quantismo  
                    Working...
                    X