View Full Version : Order Pending Logic
I have been thoroughly testing today. Everything is going reasonable well.
I have encountered the following problem:
An order is submitted from inside OnBarUpdate.
It can take a second to go from accepted->working->filled.
In that time, a new TICK arrived. OnBarUpdate is retriggered and a new order is submitted. Like I said this happens in the space between accepted and filled.
I have been looking in the reference for a function/method that gives something along the lines of: OrderState that returns Working/Accepted etc... does this exist somewhere?
Also: I am seeing a lot of State=PendingChange. What causes this?
04-02-2007, 01:12 AM
Would you mind eloborating on your initial concern? I can't make out what behaviour you are seeing that you are not expecting.
There are not methods in the NinjaScript layer that return order status. In concept, this should not be required since the NinjaScript layer takes care of managing order submission, re-submission and cancellation.
PendingChange is caused when an order is changed. The following code sample would do this if called on the close of each bar.
sorry for the lack of clarity. I'll try again:
When an order is submiited from inside OnBarUpdate (EnterLongLimit say), what happens next? Is the script somehow halted at that point until the order filled, or does the next line of code execute immediately?
04-02-2007, 02:07 AM
Processing is not halted, the next line of code is executed. The only time code is not executed is when an exception is trapped at which time the strategy stops running and an error is logged in the Control Center log tab.
OK - good.
So then When the EnterLongLimit line is execute, an order is sent. This order takes time to go from PendingSubmit->Accepted->Working->Filled, right? This isn't at all instantaneous is it?
04-02-2007, 02:14 AM
Correct, it is not instantaneous. There is always a delay with live account orders and in the Sim101 account.
OK - great.
So this is where there is a problem.
Order is Pending.
OnBarUpdate method finishes = waiting for next TICK.
Order is Accepted.
New TICK arrives.
Position.MarketPosition == MarketPosition.Flat
Order is Working.
EnterLongLimit executes a second time.
Order is filled just before new order is pending.
I now have TWO, instead of ONE, orders off the same trade signal.
I MUST have a logic switch that says something like:
Position.MarketPosition == MarketPosition.PENDING
PENDING is not Flat.
This is the problem...
here is a screen shot of two exact same order executing within a half second of one another:
04-02-2007, 02:29 AM
When run your strategy,
Set your "Entries per direction" to a value of 1 if you only ever want to have one of the orders executed.
thanks for the suggestion but it is not a solution. A good stratgey never simply runs long then short then long then short. In theory my strategies can go LONG an infinite number of time without ever being short.
What does that then mean - my Strat only trades once? This is limited and not workable unfortunately.
04-02-2007, 02:40 AM
Doyou want to golong multiple times on the same trade signal?
If no, then provide a unique signal name for each trade signal in the long direction and set the "Entries per direction" to a value of 1, set "Entry handling" to Unique Entries.
If yes, but you only want to enter while an order is NOT pending, then you have to get creative and add some logic to check for pending conditions.
I'll have a closer look at the handling = unique side of the coin. That soundspromising.I do not want to enter multiple trades off of one signal. But I do want a long to be able to follow a long...
"add some logic to check for pending conditions."
I have looked for a method that will access the State=[Pending*, Accepted, Working, Filled] but can't see any...and I think you said below that there are none.
04-02-2007, 03:13 AM
Check for pending means adding your own variables to track when an order is submitted, cancelled, filled by checking the market position etc... That's what I meant by creative.