#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); } } }
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!
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
NinjaTrader
Cannot implicitly convert type 'void' to Ninjatrader.Cbi.IOrder error.
Collapse
X
-
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:Tags: None
-
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);
I look forward to being of further assistance.JesseNinjaTrader Customer Service
-
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
-
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()); } }
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'
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.JesseNinjaTrader Customer Service
Comment
-
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
-
Hello,
You could either use the FromEntrySignal if you are using Signal names:
Code:execution.Order.FromEntrySignal == "SomeSignalName"
Code:execution.Order.OrderAction == OrderAction.Sell
JesseNinjaTrader Customer Service
Comment
-
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
-
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]);
I look forward to being of further assistance.JesseNinjaTrader Customer Service
Comment
-
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by SantoshXX, Today, 03:09 AM
|
0 responses
6 views
0 likes
|
Last Post
by SantoshXX
Today, 03:09 AM
|
||
Started by DanielTynera, Today, 01:14 AM
|
0 responses
2 views
0 likes
|
Last Post
by DanielTynera
Today, 01:14 AM
|
||
Started by yertle, 04-18-2024, 08:38 AM
|
9 responses
41 views
0 likes
|
Last Post
by yertle
Today, 01:00 AM
|
||
Started by techgetgame, Yesterday, 11:42 PM
|
0 responses
12 views
0 likes
|
Last Post
by techgetgame
Yesterday, 11:42 PM
|
||
Started by sephichapdson, Yesterday, 11:36 PM
|
0 responses
2 views
0 likes
|
Last Post Yesterday, 11:36 PM |
Comment