View Full Version : Inside bar order system
Marshmellowhead
11-08-2010, 08:13 AM
I'm trying to implement an automated order entry system in Sim mode for now. If an inside bar is created, send two orders based on the most recent bar, enter long with a stop limit at the high + 1 tick of the most recent bar (inside bar), and enter short stop limit at the low -1 tick of most recent bar (inside bar). When I apply this to a symbol, the long entry order works perfect, but the short entry order is not generated. Is Ninja limited to entering only one order based on a condition? How should I fix this? Below is the code for the conditions and order entry.
// Condition set 1
if (High[0] <= High[1]
&& Low[0] >= Low[1])
{
EnterLongStopLimit(DefaultQuantity, High[0] + 1 * TickSize, High[0] + 1 * TickSize, "");
EnterShortStopLimit(DefaultQuantity, Low[0] + -1 * TickSize, Low[0] + -1 * TickSize, "");
}
}
NinjaTrader_RyanM
11-08-2010, 08:22 AM
Hello Marshmellowhead,
Your results are expected with the managed order system. Please see our for our Internal Order Handling rules. (http://www.ninjatrader.com/support/helpGuides/nt7/managed_approach.htm) I have excerpted the rule you're running into.
Methods that generate orders to enter a position will be ignored if:
A position is open and an order submitted by an exit method (ExitLongLimit() for example) is active and the order is used to open a position in the opposite direction
A position is open and an order submitted by a set method (SetStopLoss() for example) is active and the order is used to open a position in the opposite direction
The entry signal name is not unique
The strategy position is flat and an order submitted by an enter method (EnterLongLimit() for example) is active and the order is used to open a position in the opposite direction
We introduced an unmanaged order entry system in version 7 that allows you to submit both long and short orders at the same time. Please see here for an introduction to unmanaged strategies.
http://www.ninjatrader.com/support/helpGuides/nt7/unmanaged_approach.htm (http://www.ninjatrader.com/support/helpGuides/nt7/unmanaged_approach.htm)
Marshmellowhead
11-08-2010, 08:39 AM
OK, I'll try simply putting the following in my code and see if that works.
Unmanaged = true;
Will the code change take immediate affect, or do I have to toggle the strategy on/off or anything?
In the control center view, I have the same strategy applied to 4 symbols. Under the "Strategy" tab, some are highlighted in Yellow, others green. What does this mean?
Marshmellowhead
11-08-2010, 08:50 AM
OK, well simply adding that line didn't work. How do I figure out why the strategy doesn't work on one chart which is highlighted in yellow in the control panel?
NinjaTrader_RyanM
11-08-2010, 08:56 AM
You'll have to do more than place Unmanaged = true in your Initialize method.
You can't combine managed order methods like EnterLong(), ExitShort() with unmanaged order methods. Unmanaged order methods use SubmitOrder() for all order submission.
There's also a lot going on under the hood with the managed order system that you'll be responsible for with an unmanaged system.
Please review the following page ->
Critical considerations when using Unmanaged order methods
http://www.ninjatrader.com/support/helpGuides/nt7/unmanaged_approach.htm (http://www.ninjatrader.com/support/helpGuides/nt7/unmanaged_approach.htm)
Will the code change take immediate affect, or do I have to toggle the strategy on/off or anything?
You must remove and reapply any instances of the strategy after making code changes.
In the control center view, I have the same strategy applied to 4 symbols. Under the "Strategy" tab, some are highlighted in Yellow, others green. What does this mean?
Green highlighted cells indicate a currently running strategy.
Yellow highlighted cells indicate the strategy is waiting until it reaches a flat position to be in sync with the account position before fully starting. (Please see the options Strategies Tab (http://www.ninjatrader.com/support/helpGuides/nt7/strategies_tab.htm) section for configuration options.)
Marshmellowhead
11-08-2010, 09:04 AM
Can "Submit Order()" be created using the Wizard, or do I have to do it manually within the code?
NinjaTrader_RyanM
11-08-2010, 09:10 AM
SubmitOrder and unmanaged systems require working with code directly. These methods are not available with the strategy wizard.
Marshmellowhead
11-08-2010, 02:56 PM
I have the following two lines for Order submission:
SubmitOrder(0, OrderAction.Buy, OrderType.StopLimit, 1, High[0] + 1 * TickSize, High[0] + 1 * TickSize, 0, "IB long");
SubmitOrder(0, OrderAction.SellShort, OrderType.StopLimit, 1, Low[0] + -1 * TickSize, Low[0] + -1 * TickSize, 0, "IB short");
I'm getting "invalid argument ' errors for these statements. Can I have arguments like High[0] +... or do I need to use a variable?
NinjaTrader_RyanM
11-08-2010, 03:03 PM
You can use values like High[0] in these methods.
You're seeing an issue for specifying a value of 0 for string field OCOid.
You can use an empty string here if you don't want any OCO handling between the orders.
SubmitOrder(0, OrderAction.Buy, OrderType.StopLimit, 1, High[0] + 1 * TickSize, High[0] + 1 * TickSize, "", "IB long");
SubmitOrder(0, OrderAction.SellShort, OrderType.StopLimit, 1, Low[0] + -1 * TickSize, Low[0] + -1 * TickSize, "", "IB short");
Marshmellowhead
11-08-2010, 03:34 PM
Is High[0] + 1 * tick size acceptable?
I'm basically bracketing the last bar with two orders and if one gets triggered, i want the other cancelled. How do I do that using the OCO id field?
NinjaTrader_RyanM
11-08-2010, 03:43 PM
Yes, High[0] + 1 * TickSize is acceptable.
To tie two orders together as OCO, use the same string in both.
SubmitOrder(0, OrderAction.Buy, OrderType.StopLimit, 1, High[0] + 1 * TickSize, High[0] + 1 * TickSize, "EntryOrders", "IB long");
SubmitOrder(0, OrderAction.SellShort, OrderType.StopLimit, 1, Low[0] + -1 * TickSize, Low[0] + -1 * TickSize, "EntryOrders", "IB short");
Marshmellowhead
11-08-2010, 05:24 PM
OK, thanks. It compiles now, but it doesn't generate any orders when it sees an inside bar.
NinjaTrader_Bertrand
11-09-2010, 03:52 AM
Are you sure the condition is triggering at all? Please add Prints or drawing objects for a quick visual check to confirm the condition would indeed work as you would expect.
Marshmellowhead
11-09-2010, 11:35 AM
Ok, well I redid the strategy, added a print statement, but now I can't enable it. It compiles. I've attached the script. Can someone advise me exactly where to put the "Unmanaged = True;" statement.
NinjaTrader_RyanM
11-09-2010, 11:39 AM
protected override void Initialize()
{
Unmanaged = (http://www.ninjatrader.com/support/helpGuides/nt7/unmanaged_approach.htm)true;
(http://www.ninjatrader.com/support/helpGuides/nt7/unmanaged_approach.htm)}
Marshmellowhead
11-09-2010, 12:15 PM
Thanks. Should I put the unmanaged statement after "protected override void OnBarUpdate()" also?
Adding the unmanaged statement as you said, I'm getting the long entry order generated after an inside bar, but not the short entry order.
Marshmellowhead
11-09-2010, 02:24 PM
Also, orders don't get generated outside of regular trading hours. How do I enable automatic execution 24/7?
NinjaTrader_RyanM
11-09-2010, 02:44 PM
You only need to declare Unmanaged = true; once, in the Initialize() method.
Use TraceOrders (http://www.ninjatrader.com/support/helpGuides/nt7/traceorders.htm) output and Print() statements to debug strategy submission issues.
You'll want to check the session template applied to the strategy for the times submitted.
Debugging your NinjaScript Code (http://www.ninjatrader.com/support/forum/showthread.php?t=3418)
Marshmellowhead
11-09-2010, 02:53 PM
How do I view the session template for the strategies? When I view my historical trades for the day, I can only see the regular trading hours as well on the 1 minute chart that shows the entry and exits. How do I change the session hours for this chart as well?
Thanks for all your help.
NinjaTrader_RyanM
11-09-2010, 03:40 PM
If the strategy is applied to a chart, then the session template is defined in the chart's Right Click > Data Series window.
You can see the definitions for session templates by clicking tools > Session manager
http://www.ninjatrader.com/support/helpGuides/nt7/session_manager.htm
Marshmellowhead
11-09-2010, 09:09 PM
OK, got the session time straightened out. Now, another issue, when I to the strategy tab in control center and right click to add a new strategy, first I get a blank window as seen in the attachment.(insidebar). I then choose a different strategy like "longentry" and all the contents of the window appear. I then switch back to "insidebarv2" at shown in the second attachment (insidebar2), but the label row under General stays at "longentry". Why is does this happen? I have to manually type in "insidebarv2" in order for the correct strategy to apply.
Furthermore, on debugging, I put in both the trace order statement, and a print statement but see no additional information in the output window. The strategy goes through a full cycle of entry/exit (long only for now, because the short orders won't generate), but no information is displayed in the output window.
Marshmellowhead
11-09-2010, 09:10 PM
Forgot attachments.
Marshmellowhead
11-09-2010, 09:17 PM
...and one more thing, :), in the output window I get a message, ( I can't cut and past it), "Failed to call Method 'Initialize' for strategy.......'SetTrailingStop method can't be called since 'Submitorder' was called before.
Can I not use SetTrailingStop now since I am using unmanaged orders?
NinjaTrader_Bertrand
11-10-2010, 02:15 AM
Correct, Set() methods can't be used in the unmanaged approach - on which NT7 release version are you now working?
Marshmellowhead
11-10-2010, 06:24 AM
Version 7.23
So then use something like submit order() to create profit targets and stop?
NinjaTrader_Bertrand
11-10-2010, 06:31 AM
Correct, you would use SubmitOrder then to send your Exit limit orders for the target and your ExitStop / StopLimit orders for the stops, you basically manage those yourself in this approach.
For the strategy accessing issues in the strategies tab - would you mind sending us Trace / Logs in via the Help > Mail to Support option?
Thanks
Marshmellowhead
11-10-2010, 07:19 AM
OK, eliminating the conflict of SubmitOrder() and Set() in unmanaged script eliminated a bunch of issues. Disregard posts 21 and 22. I now get an output that makes some sense, so I'm good there.
Can I just put submitOrder() in place (on the same line) as where I had th Set() code for profit/stop target? Does the code somehow automatically check to see if I'm in a position before executing this SubmitOrder? Is sounds like a need some logic like "if currentposition = 1" (EL text), then perform this SubmitOrder(). Any documentation I can reference as to how to code target/stop orders in unmanaged script?
Thanks!
NinjaTrader_RyanM
11-10-2010, 08:41 AM
Unfortunately you can't replace the Set lines with SubmitOrder lines.
There is always more than one way to do things and unmanaged strategy means there is nothing going on internally to manage your position.
You can still use market position checks with an unmanaged strategy so one option is to submit your target or stop loss orders when long or short. .
http://www.ninjatrader.com/support/helpGuides/nt7/marketposition.htm
Marshmellowhead
11-10-2010, 10:41 AM
OK. If I don't have any exit order script in my strategy, when I enable the strategy, it is highlighted yellow and never turns to green because it still thinks I should be in a position and is waiting for the position to be in sync, but it never will historically, because I didn't exit the order...historically. So, the strategy will never turn to green and start entering orders. Make sense? This is basically telling me I must have code in the strategy to exit the position entered by the strategy. Is this correct? If not, I have no problem exiting the positions manually by pointing and clicking on the chart and entering orders, I would just prefer the strategy to enter the orders automatically. Please clarify.
Thanks.
Secondly, I have a statement "Print("inside bar detected");" which appears in my output window as it should. is there a way to include in the output what symbol this message is referring to since I have the strategy applied to multiple symbols?
Marshmellowhead
11-10-2010, 11:02 AM
I'm now getting inside bars detected at the close of every bar. I think my ";" and/or "{}" may be in error. Can you check the following code and tell me if you think I've placed them correctly?
protected override void OnBarUpdate()
{
// Condition set 1
if (High[0] <= High[1]
&& Low[0] >= Low[1])
Print("inside bar detected");
{
SubmitOrder(0,OrderAction.Buy, OrderType.StopLimit, 1, High[0] + 1 * TickSize, High[0] + 1 * TickSize, "", "IB long");
SubmitOrder(0,OrderAction.SellShort, OrderType.StopLimit, 1, Low[0] + -1 * TickSize, Low[0] + -1 * TickSize, "", "IB short");
NinjaTrader_RyanM
11-10-2010, 11:03 AM
Yes, that sounds like an accurate description. With no exits there will not be a flat state in order to submit your entry orders. You'll need exit orders if you want to see any type of historical performance.
If you want to evaluate your entry orders in a real time scenario, you can write:
if (Historical) (http://www.ninjatrader.com/support/helpGuides/nt7/historical.htm)
return; (http://www.ninjatrader.com/support/helpGuides/nt7/historical.htm)
Place that in the first lines of OnBarUpdate() method and there won't be an historical strategy position when you start the strategy. It can then immediately evaluate your entry orders.
You can add the name of the instrument to your print statements.
Documentation for Instrument class here. (http://www.ninjatrader.com/support/helpGuides/nt7/instrument.htm)
Marshmellowhead
11-10-2010, 12:22 PM
Please disregard, figured it out. Had brackets in wrong place.
I'm now getting inside bars detected at the close of every bar. I think my ";" and/or "{}" may be in error. Can you check the following code and tell me if you think I've placed them correctly?
protected override void OnBarUpdate()
{
// Condition set 1
if (High[0] <= High[1]
&& Low[0] >= Low[1])
Print("inside bar detected");
{
SubmitOrder(0,OrderAction.Buy, OrderType.StopLimit, 1, High[0] + 1 * TickSize, High[0] + 1 * TickSize, "", "IB long");
SubmitOrder(0,OrderAction.SellShort, OrderType.StopLimit, 1, Low[0] + -1 * TickSize, Low[0] + -1 * TickSize, "", "IB short");
Marshmellowhead
11-24-2010, 09:26 AM
I'm applying the strategy to multiple symbols. If a stop limit order is generated and appears on each chart for 2 different symbols, then one of the orders gets filled for one symbol, the stop order on the other symbol gets cancelled. How can the conditions for one symbol be blocked from interfering with another symbol?
Marshmellowhead
11-24-2010, 09:41 AM
Also, if the strategy generates a stop limit order that doesn't get filled, and the conditions are no longer true which generated the order, how do I get the strategy to cancel the open limit order?
NinjaTrader_Brett
11-24-2010, 10:08 AM
Hello,
Thanks for your note.
By default there are no ties to each symbol. You just want to make sure not to share the same OCO ID which is a SubmitOrder() parameter.
Let me know if I can be of further assistance.
Marshmellowhead
11-24-2010, 10:44 AM
If I've coded the strategy to check if my current position is flat before entering the buy stop order, will the strategy check my realtime position, or the supposed position according to the strategy? Right now, it doesn't appear to be checking my realtime position.
NinjaTrader_Josh
11-24-2010, 11:27 AM
It will check your strategy position. Everything the strategy knows is in relation to the strategy and not your account.
Marshmellowhead
11-24-2010, 11:48 AM
It will check your strategy position. Everything the strategy knows is in relation to the strategy and not your account.
Ok, then if I'm using unmanaged orders and " if(Historical) return" so it doesn't wait to be insync with itself, how do I make the strategy factor in the realtime position and not the theoretical position? Basically, I want to automate my entries and manually enter my exits, but if the strategy want to create and entry order, and I'm already in a position I haven't manually exited, I want the strategy to do nothing.
NinjaTrader_Brett
11-24-2010, 12:39 PM
Hello,
In this case the only option availiable to you is the option under Control Center->Tools->Options->Strategies Tab->NinjaScript tab then set Wait Until flat before executing live.
Let me know if I can be of further assistance.
Marshmellowhead
11-24-2010, 08:36 PM
(Position.MarketPosition == MarketPosition.Flat)
For this coding, "marketposition" on either side of the == sign is looking at what, the actual real time position, or the position I should be in determined by the other coding in the strategy?
NinjaTrader_Bertrand
11-25-2010, 02:35 AM
Correct, this would look only at the strategy position, you need to ensure your real account position is in sync to this -
http://www.ninjatrader.com/support/forum/showthread.php?t=4033
Marshmellowhead
11-25-2010, 08:15 AM
OK, so "MarketPosition" is looking at the strategy position. Is there a different command to look at the Real time position (independent of the strategy position) ? Or some other way for the strategy to lookup the real time position?
NinjaTrader_Bertrand
11-25-2010, 08:19 AM
Unfortunately this is not possible programmatically - you would ensure both are in sync -
http://www.ninjatrader-support.com/HelpGuideV6/StrategyPositionVsAccountPosition.html
In out NT7 we added an option to AutoSync both if needed with a Market Orders issued by NinjaTrader (SyncAccountPosition property).