PDA

View Full Version : LiveUntilCancelled switch issue


rperez
11-16-2009, 07:47 PM
Hi,

I'm developing a backtesting trading strategy with a programmed CancelOrder. I'm experiencing an issue with the following instruction:

entryOrder = EnterLongLimit(myTimeframe,true, DefaultQuantity,orderLevel, "Long:"+myPeriodTypeLegend);

When I turn the LiveUntilCancelled switch to true, my order entries do not appear in the graph screen. And when I turn the same switch to false, the orders appear normally.

In the attachment, the left side graph shows the entries with the switch turned to false and the right side graph does not show the order entries when the switch is turned to true.

How can I solve this issue?.

Thank you.

NinjaTrader_Bertrand
11-17-2009, 04:13 AM
rperez, please work with TraceOrders ( http://www.ninjatrader-support2.com/vb/showthread.php?t=3627 ) to debug why you see this behavior, with liveUntilCancelled orders you can easily run into the Internal Order Handling Rules, which would prevent entries from getting executed as one would expect -

http://www.ninjatrader-support.com/HelpGuideV6/Overview36.html (bottom section here)

rperez
11-17-2009, 07:29 AM
Bertrand,

This is a message from the output window with the liveUntilCancelled sset to false. The corresponding orders appear in the screen:

13/11/2009 10:00:00 a.m. Entered internal PlaceOrder() method at 13/11/2009 10:00:00 a.m.: Action=BuyToCover OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1088,50 SignalName='MiStop' FromEntrySignal='Corto:1Min'
13/11/2009 10:00:00 a.m. Entered internal PlaceOrder() method at 13/11/2009 10:00:00 a.m.: Action=BuyToCover OrderType=Limit Quantity=1 LimitPrice=1083,50 StopPrice=0 SignalName='MiTarget' FromEntrySignal='Corto:1Min'
13/11/2009 10:00:00 a.m. Cancelled pending exit order, since associated position is closed: Order='NT-00050/Sim101' Name='MiStop' State=Working Instrument='ES 12-09' Action=BuyToCover Limit price=0 Stop price=1088,42638568816 Quantity=1 Strategy='IntrabarBackTest4' Type=Stop Tif=Gtc Oco='' Filled=0 Fill price=0 Token='ec79754230d54817bacb3dd4d0b4c837' Gtd='01/12/2099 12:00:00 a.m.'


And now, the same message with the switch turned to true:

13/11/2009 10:00:00 a.m. Entered internal PlaceOrder() method at 13/11/2009 10:00:00 a.m.: Action=BuyToCover OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1088,50 SignalName='MiStop' FromEntrySignal='Corto:1Min'
13/11/2009 10:00:00 a.m. Entered internal PlaceOrder() method at 13/11/2009 10:00:00 a.m.: Action=BuyToCover OrderType=Limit Quantity=1 LimitPrice=1083,50 StopPrice=0 SignalName='MiTarget' FromEntrySignal='Corto:1Min'
13/11/2009 10:00:00 a.m. Cancelled pending exit order, since associated position is closed: Order='NT-00063/Sim101' Name='MiStop' State=Working Instrument='ES 12-09' Action=BuyToCover Limit price=0 Stop price=1088,42638568816 Quantity=1 Strategy='IntrabarBackTest4' Type=Stop Tif=Gtc Oco='' Filled=0 Fill price=0 Token='e4422b1f3b3647eb95ebcb6b061bc792' Gtd='01/12/2099 12:00:00 a.m.'

As you can see, they are basically the same, but the orders DO NOT appear in the screen.

Any idea?

NinjaTrader_Josh
11-17-2009, 09:44 AM
rperez,

These are not the traces for the order in the code snippet you provided earlier. Please find the trace output in relation to the entry order as opposed to the stop/target exit orders.

Suggest you also check the Control Center logs for any runtime errors.

rperez
11-17-2009, 10:23 AM
Thanks again Bertrand,

I think I found the cause of the problem because it appears to be working fine now.

I had forgotten to store the result of the following instruction in the entryOrder variable:

entryOrder = ExitShort(myTimeframe,DefaultQuantity,"CubreCorto", "Corto:"+ myPeriodValue);

I had it as:

ExitShort(myTimeframe,DefaultQuantity,"CubreCorto", "Corto:"+ myPeriodValue);


Does it make any sense?

NinjaTrader_Bertrand
11-17-2009, 10:40 AM
Sorry I don't really follow, but glad to hear you're back to working state.

rperez
11-17-2009, 06:32 PM
Bertrand,

Unfortunatelly, when backtesting my program, the lower time frame doesn't seem to be working, and neither does my liveUntilCancelled switch allow the viewing of any entries . I've assembled my program from the Sampleprograms found on this forum, so you will find everything is familiar. I've simplified it to leave only the assembled pieces from the sample programs.

Would you mind if I ask you to please take a fast look at the program to see if you find any obvious mistakes? I am not a very experienced programmer.

Thank you.

NinjaTrader_Bertrand
11-18-2009, 05:09 AM
Yours wouldn't even compile on my end, fixed two errors and ran it, displayed trades and placed orders as expected for me - please recheck the attached, thanks.

rperez
11-18-2009, 07:00 AM
Hi Bertrand,

Thank you very much for your kind support.

Did you try to run a backtest with the 1 min smaller time frame by setting myTimeFrame to 1? It doesn`t generate any orders.

And when the MyliveUntilCancelled switch is turned to true, no orders show up either.

Those are the two main problems the program seems to have.

NinjaTrader_Bertrand
11-18-2009, 07:23 AM
rperez, thanks for the trace order output I believe you cancel them to early, as you basically cancel them immediately after placing them.

Try to simplify things in the strategy, for example leaving the stops and targets / breakeven portion out and just ensure the entries are working as you would expect with the intrabar backtesting included, then move on from there.

rperez
11-18-2009, 07:35 AM
Good point Bertrand... I hadn't realized the orders were being cancelled immediately..... that's because of my inexperience as a programmer.... I just don't have feel for it yet.... as you obviously do! Thanks.

rperez
11-18-2009, 02:34 PM
Bertrand,

I got the program fixed and is working ok with minute bars. But, when trying to load my strategy or any other strategy, including the Sample Intrabar Strategy, with Range bars as the primary TimeFrame, the Strategy Analyzer generates an error.

My computer has 2 Gigs of memory, so it is not due to lack of computer resources.

Is there some specific and known issue regarding Ninja handling Range bars?

Thanks.

NinjaTrader_Josh
11-18-2009, 02:56 PM
rperez,

2 gigs of RAM is not that much RAM especially when running extended backtests on tick based intervals. Range charts are tick based. Please decrease your backtest time range.

rperez
11-18-2009, 04:38 PM
Thank you Josh....

Do you mean to say, instead of a 12R graph, use a 2R graph?, or,
Instead of backtesting data for 30 days back , use only 10 days back?

In any case, I've tried both a neither seems to work.

NinjaTrader_Austin
11-18-2009, 04:45 PM
Thank you Josh....

Do you mean to say, instead of a 12R graph, use a 2R graph?, or,
Instead of backtesting data for 30 days back , use only 10 days back?

In any case, I've tried both a neither seems to work.
He means instead of backtesting for 30 days back, just backtest for 10 days back.

Just like Josh said, range charts are built from ticks and backtesting many days worth of tick data is very computer intensive (especially if you're working with ES).

Can you get something like a 1 or 2 day backtest to work?

rperez
11-19-2009, 07:45 AM
Hi Austin,

I ran the test with a 1 day lookback period and it worked fine. What hardware configuration do you recommend in order to be able to do an extensive backtesting?

I need to backtest the system in batches of three months in order to be able to collect data for a minimum of 1 year.

Thank you.

NinjaTrader_Bertrand
11-19-2009, 07:55 AM
rperez, for backtesting on this much tickdata I would either recommend waiting on NinjaTrader 7 or testing this on 10-15 day chunks in NinjaTrader 6.5 on a performing PC (dual / quad with 4 GB RAM). Otherwise you easily run into Windows limitations for a 32 bit process (about 1.3 GB RAM usage).

rperez
11-19-2009, 09:19 AM
Hi Jason,

For some reason my break even is not working. I've spent hours trying to find out why. Could you please check if there is something wrong? I used your SampleOnBarUpdate code for this purpose.

Thank you.

NinjaTrader_Bertrand
11-19-2009, 10:09 AM
rperez, please add Prints to the section where you call your SetBreakEven() to see if the code makes it into this part as you would expect.

rperez
11-19-2009, 10:30 AM
Hi Bertrand.... I did that and the code does enter the SetBreakEven() method and evaluates the condition, but the stopOrder is allways null.

NinjaTrader_Bertrand
11-19-2009, 10:34 AM
Please also print this price portion of your breakeven, to see if the price you calculate is standing a chance to trigger the stop adjustment -


Position.AvgPrice + (bkEvenWhen*2-1 * (TickSize / 2)

rperez
11-19-2009, 10:44 AM
I tried both:

Position.AvgPrice + (bkEvenWhen*2-1 * (TickSize / 2)and:
Position.AvgPrice + bkEvenWhen * TickSizeand the condition should trigger the BkEven.

In this last instruction, if I change the value of the break even to a value smaller than the target, all the entries dissapear from the screen.

NinjaTrader_Bertrand
11-19-2009, 10:48 AM
rperez, you would really need to print out those values to be sure they calculate as you think they would. Try for example just to trigger the stops if Close[0] > Position.AvgPrice, does the logic after this then hit home?

rperez
11-19-2009, 11:55 AM
Bertrand, I inserted the Prints and when the setBreakEven() method is supposed to modify the BE it looses the track of the target value and whatever long position is open never gets closed, as shown in the screenshot below. It shows a long position filled on 11/6/2009 and never closes.

I also send another copy of the program for you to run.

NinjaTrader_Bertrand
11-19-2009, 12:43 PM
rperez, will look into and get back to you.

NinjaTrader_Bertrand
11-20-2009, 06:27 AM
rperez, first I would change the Prints to PrintWithTimeStamp, thus giving you more insight when the last modification was done / called, you can see it's getting 'stuck' in your SetBreakEven() call, for example when I test here on ES 1 min charts. Please also just call SetStopLoss again to change your stop losses set for the trail, you current attempt to submit fresh Exit() methods which bring you into trouble with our Internal Order Handling rules -

http://www.ninjatrader-support.com/HelpGuideV6/Overview36.html

rperez
11-20-2009, 07:53 AM
Bertrand....thanks again.... I'm sorry I didn't quite follow what you mean by:

"Please also just call SetStopLoss again to change your stop losses set for the trail,..."

Should I use the code that I commented out in the program that is below the SetBreakEven() call, instead of using the SetBreakEven()method?

SetStopLoss does not return any value, so how do I manage the stopOrder variable?

Sorry.

NinjaTrader_Bertrand
11-20-2009, 08:15 AM
rperez, please check into the link I posted in my previous post and scroll to the bottom section of it, it will explain the issues you run into when attempting to combine Set() and Exit() together - for example when working with the IOrders like you want in the code to go down the Advanced Order Handling route...please comment out the SetStopLoss / SetProfitTarget calls you have in the Initialize() and then debug order placement from this point forward.

rperez
11-20-2009, 10:42 AM
Hi Bertrand,

I replaced the SetBreakEven() code with the following one, and apparently it is working fine. Does it make sense to you? And please know that I am deeply thankful with you for your patience and time devoted to this matter.

// Resets the stop loss to the original value when all positions are closed
if (Position.MarketPosition == MarketPosition.Flat )
{
SetStopLoss("Short:"+myPeriodTypeLegend,CalculationMode.Ticks, stop, true);
SetStopLoss("Long:"+myPeriodTypeLegend,CalculationMode.Ticks, stop, true);

SetProfitTarget("Long:"+myPeriodTypeLegend, CalculationMode.Ticks, target);
SetProfitTarget("Short:"+myPeriodTypeLegend, CalculationMode.Ticks, target);

//PrintWithTimeStamp("Bars InP: "+BarsInProgress);
}

// If a long position is open, allow for stop loss modification to breakeven
else if (Position.MarketPosition == MarketPosition.Long )
{
// Once the price is greater than entry price+12 ticks, set stop loss to breakeven
//PrintWithTimeStamp("Close[0] >= Position.AvgPrice + bkEvenWhen * TickSize : "+(Close[0] >= Position.AvgPrice + bkEvenWhen * TickSize ));
if (Close[0] >= Position.AvgPrice + bkEvenWhen * TickSize )
{
SetStopLoss("Long:"+myPeriodTypeLegend,CalculationMode.Price, Position.AvgPrice + 1 * TickSize ,true );
//stopOrder = ExitLongStop(myTimeframe, true, stopOrder.Quantity, Position.AvgPrice+1*TickSize, "StopEnBE", "Long:"+myPeriodTypeLegend);
//PrintWithTimeStamp("stopOrder : "+stopOrder);
}
}

NinjaTrader_Bertrand
11-20-2009, 11:29 AM
rperez, you're welcome - yes it does, using this part instead of the mixup with Exit() and Set() provides 'clean' handling of your stops and targets as far as the Internal Order Handling Rules are concerned.