Profile Picture

timing of OrderFilled method during backtest

Posted By uaruss 5 Years Ago
Message
Posted Wednesday January 11 2012
I posted this on the Yahoo Group as well - not certain if one place is better than another... Anyway, any help is much appreciated!

I understand that the "OrderFilled" method is called when an order is filled.
However, for a backtest that is run EOD with daily data, when exactly does the
order get filled and thus the "OrderFilled" method get called (i.e. after any
lines with Open/Close actions are executed, after the "NewBar" procedure
finishes, etc?)?

I have code that is working fairly well, but there are multiple orders being
unexpectedly cancelled and I'm worried that this could present problems for the
future. I've written out various flags to a CSV file to attempt to understand
better, but still am not certain.

Thanks in advance for any help!
Posted Wednesday January 11 2012
RightEdge processes prices data first in the paper broker, then sends it to your system. This means that when NewBar() is called, the prices for that bar have already been processed in the paper broker. Any orders you submit from NewBar will be sent to the broker, but won't be filled until the next price the paper broker processes.

So if you submit an order from the NewBar() event for Bar 0, it will (potentially) be filled during Bar 1. You would get the OrderFilled or OrderCancelled calls for Bar 1, and then you'd get the NewBar call for Bar 1.

Does that help?

Thanks,
Daniel
Posted Thursday January 12 2012
That does help. One of the reasons I really like RE is that it absolutely prevents my systems from trading on future data. Also, I'm not sending the order to the broker (IB) at this time, but rather just placing the orders manually at night (based on the system output, and then they fill at the open). However, I do have one additional question.

My system is currently generating multiple buy/sell signals for the same ticker for a day. Does RE simply queue these in the order they are generated by the code (I use "at market" orders only, which fill on the open in RE) and then execute the first one in / last one out? For a little more background, I'm only trading 1 symbol with this model and generally trade 100% of the account with each trade. I know trading 100% was advised against in a previous post, but if my system generates an "Open Position" call, it checks for existing positions going the other way and, if it finds any, executes a "CloseAtMarket", before calling the "Open Position" code. An example of the situation is below...

System generates a "BUY" signal
The code checks for any open SHORT position and executes a "CloseAtMarket" for all SHORT positions found (should be at max 1)
The code then calls "OpenPosition" at market
then later in another section of the code...
System generates a "SELL" signal
The code checks for any open LONG position and executes a "CloseAtMarket" for all LONG positions found (should be at max 1)
The code then calls "OpenPosition" at market

In this case, should the buy signal get effectively cancelled, or the sell signal? What if there was a third signal later in the code that went back to a "BUY"? I know this is not a good situation, but the system works over a couple of decades of data - and so far I've been unable to fully isolate the buy/sells and get similar results.

Again, thanks for your help!

Thanks!
phg
Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)
Posted Thursday January 12 2012
RE is not going to queue those orders nor will it cross-check with outstanding orders. You can unravel things in your buy or sell code though, with this:
foreach(Position pos in OpenPositions) . . .
and monitoring the filled and cancelled events.

See the members of the Position class. You can check the status of filled positons this way. I haven't done so, but I think you can even check for pending orders. Baiscally of pos.Orders.Count is greater than zero then there is something previous to deal with.

-Pete

See also Yahoo group about applying RE.

Posted Sunday January 15 2012
The RightEdge paper broker does generally process orders in the same order (sequence may be a better term here) that they were submitted.

The problem with trading 100% doesn't have to do with existing positions, it has to do with the fact that you don't know what the next day's open price will be, you don't know what the fill price will be and hence you don't know what position size to use to trade 100% of your portfolio. The price could change and suddenly the position could be for 101% of the portfolio and the broker would reject it.

In this case, should the buy signal get effectively cancelled, or the sell signal?


It depends on how you are checking for open long or short positions. A position isn't considered "Open" until it gets its first fill. So when you call OpenPositon for a BUY signal, it gives you a position, but that position isn't yet in the OpenPositions list. It is in the PendingPositions list. So when the SELL signal comes around, you probably want to check both the PendingPositions and OpenPositions lists, and call CloseAtMarket() for any positions in the lists. For a pending position this will simply cause the open order to be cancelled before it is ever filled.

So if you do this, then the last signal generated should be the one that is actually processed. If you don't, then the BUY and SELL orders would both be submitted, and they'd probably both get filled, leaving you with no position.

Thanks,
Daniel
phg
Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)
Posted Sunday January 15 2012
A related observation: neither OpenPositions nor PendingPositions appear in the RE API reference guide. Is that just a documentation glitch?

-Pete

See also Yahoo group about applying RE.

Posted Sunday January 15 2012
They are properties on the SymbolScriptBase class.

PendingPositions
OpenPositions

Daniel
Posted Sunday January 15 2012
Thanks guys for the clarification. I'll check out the "PendingPositions". In the interim, I've just written all the various open/close attempts out to a CSV file to better understand things... Interesting indeed!

Thanks -
Russell
phg
Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)Supreme Being (48,533 reputation)
Posted Monday January 16 2012
My slow. I was looking under 'Index'. But they can be found with 'Search'.

-Pete

See also Yahoo group about applying RE.



Similar Topics


Reading This Topic


2005-2017 © RightEdge Systems