PDA

View Full Version : Batch OCA orders on session close


abfc123
11-16-2010, 07:36 AM
Hi,

I have a 1min BarOnClose=false strategy that I have been running without problems on a realtime basis.

I would like to change the functionality to use it simply as batch uploader of bracket-orders to Interactive Brokers.
I would like to run it after the close as this is what I add/subtract from to derive the respective StopIn Long/Short levels.

I suppose the best way to run it (please offer alternatives) is as a backtest on a folder of the Tradeable instruments.
It would then fire the OCA groups acrros to IB after each instrument test.

Which order functions would I best use to place contingent OCA's?
Which feature/flafg do I use to allow the orders to be delivered as Inactive so they can be checked in the IB Trrader Workstation before manually being activated by a trader?

protected override void Initialize()
{
Add(PeriodType.Day, 1);

CalculateOnBarClose = false;
}

protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;

if (Bars.FirstBarOfSession)
{
firstDailyOpen = Open[0];
lastDailyClose = Closes[1][0];
}

Because I am putting the orders in before the next open in the case of Equities, and after in the case of the 24hr FX market, I want to check if the last bar read was the end of the final daily session or start of the next incomplete session

Can you please suggest which functions to look at.

Thanks

NinjaTrader_RyanM
11-16-2010, 08:14 AM
Hello abfc123,

Unfortunately there's no way to place contingent or inacitve orders. Once you route an order to Interactive Brokers, they handle it and orders are typically in a working or accepted state.

Any end of session identification must be custom coded, using Time functions or BarsSinceSession(). There is not a built in method or property to identify last bar of session.

abfc123
11-16-2010, 09:27 AM
So there is no way to replicate Bracket orders in NT?

Also, to perform an action at the end of the backtest(ie after last bar), is there a way to test for this condition?


if (CurrentBar == Count-1) gives me multiple lines.

Thks

NinjaTrader_RyanM
11-16-2010, 09:45 AM
There's no way to submit inactive orders or orders awaiting approval in TWS. If this fits with your understanding of bracket, then it's not possible with NinjaTrader.

You can do some actions at the end of a strategy run in OnTermination()
http://www.ninjatrader.com/support/helpGuides/nt7/ontermination.htm

You can also work with Historical property. This returns true on Historical bars and false on real time bars.
http://www.ninjatrader.com/support/helpGuides/nt7/historical.htm

abfc123
11-16-2010, 10:28 AM
Regarding using OnTermination(), I am backtesting on a list of instruments. I want to know when it has completed the backtest on each separate instrument in the list.
Is it correct that there would be one strategy run, and therefore a call to the OnTermination() method for each instrument?

Thks

abfc123
11-16-2010, 10:37 AM
Regarding using OnTermination(), I am backtesting on a list of instruments. I want to know when it has completed the backtest on each separate instrument in the list.
Is it correct that there would be one strategy run, and therefore a call to the OnTermination() method for each instrument?

Thks

NinjaTrader_RyanM
11-16-2010, 10:42 AM
Correct - OnTermination() will be processed for each instrument in the list.

abfc123
11-16-2010, 11:13 AM
Thanks Ryan.

So after each run , I want to check if the current date is within 5mins of the date of the last session close.
If so, and also flat, I want to try emulating Bracket setup

LongTarget2
LongTarget1
LongStopIn
LongStopLoss

as resting orders

Is this viable?

Thks

NinjaTrader_RyanM
11-16-2010, 11:31 AM
Sorry, I don't follow here. A backtest is only run on historical data, so you won't be able to use it to submit live working orders.

OnTermination is run once the strategy has completed, so you can't use this method to submit orders.

You may be looking for an ATM strategy rather than a NinjaScript strategy. These are targets and stop losses that are automatically submitted once the entry order has been filled. You define your target and stop losses before the entry order is submitted.
http://www.ninjatrader.com/support/helpGuides/nt7/advanced_trade_management_atm.htm

If you want to use a NinjaScript strategy to submit this particular combination of orders, you would need to use our unmanaged order system.
http://www.ninjatrader.com/support/helpGuides/nt7/unmanaged_approach.htm

You can make checks with the current date / time compared to the time stamp of a bar.