Title: Re[2]: [amibroker] GetStatus error, is it code or a timing error?
It is very educational to log states at each refresh and/or display transient order status. Your log will tell you how often there are problems, and what they are. They may be different for different environments. I use variations of this:
if( ShowTransientStatus )
{
TransientOrderStatus =
"TRANSIENT ORDERID STATUS:\n"+
"Pending OrderIDs = "+ ibc.GetPendingList( 0, "")+"\n"+
"NotYetTransmitted = "+ ibc.GetPendingList( 0, "NotYetTransmitted")+"\n"+
"PreSubMitted = "+ ibc.GetPendingList( 0, "PreSubMitted")+"\n"+
"SubMitted = "+ ibc.GetPendingList( 0, "SubMitted")+"\n"+
"Pending = "+ ibc.GetPendingList( 0, "Pending")+"\n"+
"Cancelled = "+ ibc.GetPendingList( 0, "Cancelled")+"\n"+
"Filled = "+ ibc.GetPendingList( 0, "Filled")+"\n"+
"Error = "+ ibc.GetPendingList( 0, "Error")+"\n"+
"InActive = "+ ibc.GetPendingList( 0, "InActive")+"\n\n";
}
else TransientOrderStatus = "";
Title = .... your own stuff + TransientOrderStatus;
Next you might want to add a function to monitor and report the IBPosSize and its changing at all times. For example along the lines of the code below (this is NOT complete code, just to give you the idea!).
function MonitorPosSize( Ticker )
{
global ibc, SharesTraded, Commission;
PrevPosSize = Nz(StaticVarGet("IBPosSize"));
IBPosSize = ibc.GetPositionSize( Ticker );
StaticVarSet("IBPosSize",IBPosSize);
PosChange = IBPosSize - PrevPosSize;
if( PosChange )
{
if( abs(IBPosSize) > SharesTraded )
{
xSay("Position Error, Closing Down System");
CloseAllPositions("");
StaticVarSet("AutoTrading",0);
}
else if( PrevPosSize == 0 )
{
if( IBPosSize > PrevPosSize )
{
xSay(" AT, New Long Position ");
_TRACE("# New Position: "+NumToStr(IBPosSize,1.0));
}
else if( IBPosSize < PrevPosSize )
{
xSay("New Short Position ");
_TRACE(" AT, New Position: "+NumToStr(IBPosSize,1.0));
}
}
else if( PosChange < 0 AND IBPosSize == 0 )
{
xSay("Sold, Gone cash");
BuyEntryPrice= Nz(StaticVarGet("BuyEntryPrice"));
SellExitPrice= Nz(StaticVarGet("SellExitPrice"));
LongProfit = Nz(StaticVarGet("LongProfit"));
Profit = SellExitPrice - BuyEntryPrice - Commission;
Longprofit = LongProfit + ( Profit * SharesTraded );
StaticVarSet("LongProfit",LongProfit);
_TRACE(" AT, Position Sold, TradeProfit, "+ NumToStr(Profit,1.2)+
", BuyEntryPrice, "+NumToStr(BuyEntryPrice,1.2)+
", SellExitPrice, "+NumToStr(SellExitPrice,1.2)+
",, LongProfit, "+NumToStr(LongProfit,1.2)+", "+
WriteIf( Profit>0, ">>> WINNER <<<",">>> LOSER <<<"));
}
else if( PosChange > 0 AND IBPosSize == 0 )
{
xSay("Covered, Gone cash");
ShortEntryPrice = Nz(StaticVarGet("ShortEntryPrice"));
CoverExitPrice = Nz(StaticVarGet("CoverExitPrice"));
ShortProfit = Nz(StaticVarGet("ShortProfit"));
Profit = ShortEntryPrice - CoverExitPrice - Commission;
Shortprofit = ShortProfit + ( Profit * SharesTraded );
StaticVarSet("ShortProfit",ShortProfit);
_TRACE(" AT, Position Covered, TradeProfit, "+NumToStr(Profit,1.2)+
", ShortEntryPrice, "+NumToStr(ShortEntryPrice,1.2)+
", CoverExitPrice, "+NumToStr(CoverExitPrice,1.2)+
",, ShortProfit, "+NumToStr(ShortProfit,1.2)+", "+
WriteIf(Profit>0,">>> WINNER <<<",">>> LOSER <<<"));
}
}
return IBPosSize;
}
Best regards,
herman
Sunday, May 10, 2009, 8:15:59 AM, you wrote:
> Hi Barry,
> I've found similar inconsistancies. I is probably due to IB messaging and
> order processing.
> So here is what I do: I save previous states of all 3 orders before in each
> refresh (1 sec) I ask for the new states. I also ask for position size.
> I compare previous and current states. If there is a difference I report
> what the code suspect (target, stop loss or entry order fills.)
> and start a delay of 5 sec. After this delay I my code take the right
> action. By this time position size is usually correct.
> If anyone have a better solution it is welcome.
> Regards it helps.
> Y
> --------------------------------------------------
> From: "Barry Scarborough" <razzbarry@xxxxxxxxxxxx>
> Sent: Thursday, May 07, 2009 7:51 PM
> To: <amibroker@xxxxxxxxxxxxxxx>
> Subject: [amibroker] GetStatus error, is it code or a timing error?
>> I am submitting an OCA group of three orders, a MKT order with a GAT set
>> to
>> three bars in the future. The second order is a MIT set to the profit
>> target and
>> the third is a STP set to the loss target.
>> When I send a order I turn a GetStatus flag on and every scan through the
>> program it gets the status of the orders. Once in a while GetStatus
>> reports the
>> status of the order incorrectly. In the last case the MIT was filled but
>> the
>> status was reported as pre-submitted and the MKT and STP were reported as
>> pre-Submitted. The IBC message log shows the first and third order
>> canceled and
>> the MIT filled. Get status does not report a filled status.
>> In the trace, before the order was canceled due to the MIT, the GetStatus
>> returned the following:
>> [580] #, StatusInc, OrdState 1a1, Status A, B and C=PreSubmitted,
>> PreSubmitted,
>> PreSubmitted
>> The program captures the error codes that come back and that shows the
>> order was
>> canceled and the reason was blank meaning the cancel was due to the unused
>> OCA
>> members being canceled.
>> Right after that I ask for status of the three orders and this is what I
>> get:
>> [580] #, StatusInc, OrdState 1a1, Status1, A, B and C=Canceled,
>> PreSubmitted,
>> PreSubmitted According to this the order was not filled.
>> The above does not agree with the IBC trade log. Is there a timing problem
>> here?
>> This is the part of the status processing code:
>> LastError = "";
>> {
>> LastError = ibc.GetLastError(0);
>> if(LastError != "")
>> {
>> LastErrorID = fProcessError(LastError);
>> if(DebugOn) _TRACE("#, Status processing @@@#@@@ LastError=" +
>> LastError);
>> }
>> }
>> if(DebugOn) _TRACE("#, Status processing @#$$$#@ LastError=" + LastError);
>> tempStatus = "None";
>> if(GetStatus)
>> {
>> tempStatus = tempStatusA = tempStatusB = "";
>> tempStatus = ibc.GetStatus( ordID, True ); // get order status
>> tempStatusA = ibc.GetStatus( ordIDa, True ); // get order status of OCA
>> group
>> member
>> tempStatusB = ibc.GetStatus( ordIDb, True ); // get order status of OCA
>> group
>> member
>> if(DebugOn) _TRACE("#, StatusInc, OrdState 1a1, Status A, B and C=" +
>> tempStatus + ", " + tempStatusA + ", " + tempStatusB);
>> if(tempStatus == "Filled" OR tempStatusA == "Filled" OR tempStatusB ==
>> "Filled"
>> ) // check for a filled on each order
>> ...
>> Since InclFilled is set to True statuses of orders listed on "Executions"
>> list
>> are reported and this means that you can get "Canceled", "Filled",
>> "Error",
>> "Inactive" consistently.
>> It seems they are not reported consistently and I don't know why. 95% of
>> the
>> time this works flawlessly. But the other 5% are screwing my system up.
>> Any idea
>> why GetStatus is not processing the order status correctly???
>> Will getting LastError change the results of the GetStatus? Most of the
>> time it
>> does not. It seems status and error reporting are separately or
>> independently.
>> Thanks in advance,
>> Barry
>> ------------------------------------
>> **** IMPORTANT PLEASE READ ****
>> This group is for the discussion between users only.
>> This is *NOT* technical support channel.
>> TO GET TECHNICAL SUPPORT send an e-mail directly to
>> SUPPORT {at} amibroker.com
>> TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
>> http://www.amibroker.com/feedback/
>> (submissions sent via other channels won't be considered)
>> For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
>> http://www.amibroker.com/devlog/
>> Yahoo! Groups Links
> ------------------------------------
> **** IMPORTANT PLEASE READ ****
> This group is for the discussion between users only.
> This is *NOT* technical support channel.
> TO GET TECHNICAL SUPPORT send an e-mail directly to
> SUPPORT {at} amibroker.com
> TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
> http://www.amibroker.com/feedback/
> (submissions sent via other channels won't be considered)
> For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
> http://www.amibroker.com/devlog/
> Yahoo! Groups Links
> <*> To visit your group on the web, go to:
> http://groups.yahoo.com/group/amibroker/
> <*> Your email settings:
> Individual Email | Traditional
> <*> To change settings online go to:
> http://groups.yahoo.com/group/amibroker/join
> (Yahoo! ID required)
> <*> To change settings via email:
> mailto:amibroker-digest@xxxxxxxxxxxxxxx
> mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx
> <*> To unsubscribe from this group, send an email to:
> amibroker-unsubscribe@xxxxxxxxxxxxxxx
> <*> Your use of Yahoo! Groups is subject to:
> http://docs.yahoo.com/info/terms/
__._,_.___
**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.
TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com
TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
__,_._,___
|