Page 1 of 1

Best way to confirm buying power before issuing order??

Posted: Thu Jan 25, 2018 7:20 pm
by sectorbets
I am having a problem when reversing positions.

1) Let's say I am long and want to reverse to a short using an inverse ETF
2) I sell the long position, and if I immediately place an order to buy the inverse ETF, the inverse ETF order is usually rejected due to insufficient buying power available
3) So, the first thing I tried is wait_for_filled before placing the inverse ETF orderand it is better, but the problem still occurs sometimes -- it appears that IB isn't updating the buying power quickly enough so even though wait_for_filled has returned "1", the buying power hasn't been updated as quickly
4) And then I tried a timer after I get the fill notification, and it is better still, but once in awhile I still get rejected because the buying power hasn't been updated
5) I have exactly the same problem when reversing from short to long
6) I would like to just check that there is sufficient buying power before placing any order, but I don't see how to do that in the documentation.
7) You can see the messages in the log below, but what you can't see is that IB has plenty of buying power, but it just hasn't reported it back yet -- after the first sale of XIV in the first line from the log, plenty of BP was freed up, but IB hasn't reported it back before the order to buy VXX is placed

What is your preferred way to reverse a position ??

Thank you
Rick

here is an example from this morning:

20180125 10:00:00:081 [OUT] uid=103543 SELL XIV MKT 1348 DAY SMART lmt=0 aux=0 Order in Transit acc_? clientid=2 transmit=IB ref= nref= oca= filled=0 lastfill=0 fa group= fa profile=XIV Algo Alloc fa method= fa percentage= gat= gtd= outside rth=1 hidden=0 algo= algo-param= oid=99 permid=0 uidp=0 oidp=0 cid=50673 comment=
20180125 10:00:00:104; 5440;warning;API;;API-warning: order event warning:attribute 'outside regular trading hours' is ignored based on the order type and destination. placeorder is now being processed. code=2109 id=99
20180125 10:00:00:105; 2980;note;tcExpFunc;WAIT_FOR_FILLED;-103543 -2000
20180125 10:00:00:144; 5440;note;TWSSocket;position;postions of contract XIV_STK_USD___0.000000_=-200 conid=81488225 account=acc_2_master context=updatePortfolio avg=19.6114143 calls=2 publish_pos=1 publish_pl=1
20180125 10:00:00:146; 5440;note;TWSSocket;execDetails;execution: SLD shares=200 at 137.279900 oid=103543, contract=XIV_STK_USD___0.000000_SMART_ exec-id=00015e7e.5a69e011.01.01&20180125 10:00:00#
20180125 10:00:00:146 [IN] uid=103543 SELL XIV MKT 1348 DAY SMART lmt=0 aux=0 Filled Partially acc_? clientid=2 transmit=IB ref= nref= oca= filled=200 lastfill=137.2799 fa group= fa profile=XIV Algo Alloc fa method= fa percentage= gat= gtd= outside rth=1 hidden=0 algo= algo-param= oid=99 permid=0 uidp=0 oidp=0 cid=50673 comment=
20180125 10:00:00:147; 5440;note;SmartOrder;openOrderCallExpected;flag=0 uid=103543
20180125 10:00:00:147 [IN] uid=103543 SELL XIV MKT 1348 DAY SMART lmt=0 aux=0 ApiPending acc_? clientid=2 transmit=IB ref= nref=#~tc#;0=103543;1=20180125 10:00:00:080d;2=1348;> oca= filled=200 lastfill=137.2799 fa group= fa profile=XIV Algo Alloc fa method= fa percentage= gat= gtd= outside rth=0 hidden=0 algo= algo-param= oid=99 permid=490088303 uidp=0 oidp=0 cid=50673 comment=open order
20180125 10:00:00:172; 5440;note;TWSSocket;position;postions of contract XIV_STK_USD___0.000000_=-250 conid=81488225 account=acc_2_master context=updatePortfolio avg=23.6686069 calls=2 publish_pos=1 publish_pl=1
20180125 10:00:00:174; 5440;note;TWSSocket;execDetails;execution: SLD shares=50 at 137.270000 oid=103543, contract=XIV_STK_USD___0.000000_SMART_ exec-id=0000d6a5.5a69f029.01.01&20180125 10:00:00#
20180125 10:00:00:174 [IN] uid=103543 SELL XIV MKT 1348 DAY SMART lmt=0 aux=0 Filled Partially acc_? clientid=2 transmit=IB ref= nref=#~tc#;0=103543;1=20180125 10:00:00:080d;2=1348;> oca= filled=250 lastfill=137.27 fa group= fa profile=XIV Algo Alloc fa method= fa percentage= gat= gtd= outside rth=0 hidden=0 algo= algo-param= oid=99 permid=490088303 uidp=0 oidp=0 cid=50673 comment=
20180125 10:00:00:175; 5440;note;libwts_db;perf;perf(30-avg,SetStatus)=00:00:00:00:002:083
20180125 10:00:00:175; 5440;note;SmartOrder;openOrderCallExpected;flag=0 uid=103543
20180125 10:00:00:176 [IN] uid=103543 SELL XIV MKT 1348 DAY SMART lmt=0 aux=0 ApiPending acc_? clientid=2 transmit=IB ref= nref=#~tc#;0=103543;1=20180125 10:00:00:080d;2=1348;> oca= filled=250 lastfill=137.27 fa group= fa profile=XIV Algo Alloc fa method= fa percentage= gat= gtd= outside rth=0 hidden=0 algo= algo-param= oid=99 permid=490088303 uidp=0 oidp=0 cid=50673 comment=open order
20180125 10:00:00:178; 5440;note;TWSSocket;position;postions of contract XIV_STK_USD___0.000000_=-300 conid=81488225 account=acc_2_master context=updatePortfolio avg=27.4553200 calls=2 publish_pos=1 publish_pl=1
20180125 10:00:00:179; 5440;note;TWSSocket;execDetails;execution: SLD shares=50 at 137.270000 oid=103543, contract=XIV_STK_USD___0.000000_SMART_ exec-id=0000d6a5.5a69f02a.01.01&20180125 10:00:00#
20180125 10:00:00:180 [IN] uid=103543 SELL XIV MKT 1348 DAY SMART lmt=0 aux=0 Filled Partially acc_? clientid=2 transmit=IB ref= nref=#~tc#;0=103543;1=20180125 10:00:00:080d;2=1348;> oca= filled=300 lastfill=137.27 fa group= fa profile=XIV Algo Alloc fa method= fa percentage= gat= gtd= outside rth=0 hidden=0 algo= algo-param= oid=99 permid=490088303 uidp=0 oidp=0 cid=50673 comment=
20180125 10:00:00:182; 5440;note;SmartOrder;openOrderCallExpected;flag=0 uid=103543
20180125 10:00:00:182 [IN] uid=103543 SELL XIV MKT 1348 DAY SMART lmt=0 aux=0 ApiPending acc_? clientid=2 transmit=IB ref= nref=#~tc#;0=103543;1=20180125 10:00:00:080d;2=1348;> oca= filled=300 lastfill=137.27 fa group= fa profile=XIV Algo Alloc fa method= fa percentage= gat= gtd= outside rth=0 hidden=0 algo= algo-param= oid=99 permid=490088303 uidp=0 oidp=0 cid=50673 comment=open order
20180125 10:00:00:184; 5440;note;libwts_db;perf;perf(20-avg,update portfolio)=00:00:00:00:000:541
20180125 10:00:00:184; 5440;note;TWSSocket;position;postions of contract XIV_STK_USD___0.000000_=-1300 conid=81488225 account=acc_2_master context=updatePortfolio avg=71.3816320 calls=2 publish_pos=1 publish_pl=1
20180125 10:00:00:188; 5440;note;TWSSocket;execDetails;execution: SLD shares=1000 at 137.271100 oid=103543, contract=XIV_STK_USD___0.000000_SMART_ exec-id=0001506d.5a69dbc7.01.01&20180125 10:00:00#
20180125 10:00:00:188 [IN] uid=103543 SELL XIV MKT 1348 DAY SMART lmt=0 aux=0 Filled Partially acc_? clientid=2 transmit=IB ref= nref=#~tc#;0=103543;1=20180125 10:00:00:080d;2=1348;> oca= filled=1300 lastfill=137.2711 fa group= fa profile=XIV Algo Alloc fa method= fa percentage= gat= gtd= outside rth=0 hidden=0 algo= algo-param= oid=99 permid=490088303 uidp=0 oidp=0 cid=50673 comment=
20180125 10:00:00:189; 5440;note;SmartOrder;openOrderCallExpected;flag=0 uid=103543
20180125 10:00:00:189 [IN] uid=103543 SELL XIV MKT 1348 DAY SMART lmt=0 aux=0 ApiPending acc_? clientid=2 transmit=IB ref= nref=#~tc#;0=103543;1=20180125 10:00:00:080d;2=1348;> oca= filled=1300 lastfill=137.2711 fa group= fa profile=XIV Algo Alloc fa method= fa percentage= gat= gtd= outside rth=0 hidden=0 algo= algo-param= oid=99 permid=490088303 uidp=0 oidp=0 cid=50673 comment=open order
20180125 10:00:00:191; 5440;note;SmartOrder;openOrderCallExpected;flag=0 uid=103543
20180125 10:00:00:191 [IN] uid=103543 SELL XIV MKT 1348 DAY SMART lmt=0 aux=0 PreSubmitted acc_? clientid=2 transmit=IB ref= nref=#~tc#;0=103543;1=20180125 10:00:00:080d;2=1348;> oca= filled=1300 lastfill=137.2711 fa group= fa profile=XIV Algo Alloc fa method= fa percentage= gat= gtd= outside rth=0 hidden=0 algo= algo-param= oid=99 permid=490088303 uidp=0 oidp=0 cid=50673 comment=open order
20180125 10:00:00:192; 5440;note;SmartOrder;openOrderCallExpected;flag=0 uid=103543
20180125 10:00:00:221; 5440;note;SmartOrder;openOrderCallExpected;flag=0 uid=103543
20180125 10:00:00:243; 5440;note;SmartOrder;openOrderCallExpected;flag=0 uid=103543
20180125 10:00:00:244; 5440;note;SmartOrder;openOrderCallExpected;flag=0 uid=103543
20180125 10:00:01:154; 5440;note;TWSSocket;position;postions of contract XIV_STK_USD___0.000000_=-1348 conid=81488225 account=acc_2_master context=updatePortfolio avg=72.6152367 calls=2 publish_pos=1 publish_pl=1
20180125 10:00:01:157; 5440;note;libwts_db;perf;perf(20-avg,AddExecution)=00:00:00:00:001:164
20180125 10:00:01:157; 5440;note;TWSSocket;execDetails;execution: SLD shares=48 at 137.090000 oid=103543, contract=XIV_STK_USD___0.000000_SMART_ exec-id=0001a2f3.5a69de79.02.01&20180125 10:00:01#
20180125 10:00:01:157 [IN] uid=103543 SELL XIV MKT 1348 DAY SMART lmt=0 aux=0 Filled acc_? clientid=2 transmit=IB ref= nref=#~tc#;0=103543;1=20180125 10:00:00:080d;2=1348;> oca= filled=1348 lastfill=137.09 fa group= fa profile=XIV Algo Alloc fa method= fa percentage= gat= gtd= outside rth=0 hidden=0 algo= algo-param= oid=99 permid=490088303 uidp=0 oidp=0 cid=50673 comment=
20180125 10:00:01:158; 2980;note;tcExpFunc;WAIT_FOR_FILLED;ret=1
20180125 10:00:01:158; 2980;note;tcExpFunc;PLACE_ORDER;-50672 -0 -BUY -MKT -6741 -0.0000000000 -0.0000000000 -DAY --1 -0
20180125 10:00:01:159; 2980;note;tcExpFunc;PLACE_ORDER;ret=103544
20180125 10:00:01:159; 2980;note;tcExpFunc;SET_ORDER_VAL;-103544 -14 -0 --1
20180125 10:00:01:159; 2980;note;tcExpFunc;SET_ORDER_VAL;ret=1
20180125 10:00:01:159; 2980;note;tcExpFunc;SET_ORDER_VAL;-103544 -67 -Adaptive --1
20180125 10:00:01:160; 2980;note;tcExpFunc;SET_ORDER_VAL;ret=1
20180125 10:00:01:160; 2980;note;tcExpFunc;SET_ORDER_VAL;-103544 -68 -adaptivePriority=Normal --1
20180125 10:00:01:160; 2980;note;tcExpFunc;SET_ORDER_VAL;ret=1
20180125 10:00:01:160; 2980;note;tcExpFunc;SET_FA_MEMBERS;-103544 -acc_? - -XIV Algo Alloc - - 1
20180125 10:00:01:160; 2980;note;tcExpFunc;SET_FA_MEMBERS;ret=1
20180125 10:00:01:161; 5440;action;TWSSocket;FinishRequest;command [placeOrder] transmitted. Bytes sent=254
20180125 10:00:01:161; 5440;note;SmartOrder;openOrderCallExpected;flag=1 uid=103544
20180125 10:00:01:161 [OUT] uid=103544 BUY VXX MKT 6741 DAY SMART lmt=0 aux=0 Order in Transit acc_? clientid=2 transmit=IB ref= nref= oca= filled=0 lastfill=0 fa group= fa profile=XIV Algo Alloc fa method= fa percentage= gat= gtd= outside rth=0 hidden=0 algo=Adaptive algo-param=adaptivePriority=Normal oid=100 permid=0 uidp=0 oidp=0 cid=50672 comment=
20180125 10:00:01:172; 5440;note;libwts_db;perf;perf(60-avg,update acc)=00:00:00:00:000:420
20180125 10:00:01:174; 5440;note;TWSSocket;position;postions of contract XIV_STK_USD___0.000000_=-1016 conid=81488225 account=acc_2_master context=updatePortfolio avg=72.6152367 calls=2 publish_pos=1 publish_pl=1
20180125 10:00:01:176; 5440;note;TWSSocket;position;postions of contract XIV_STK_USD___0.000000_=0 conid=81488225 account=acc_43 context=updatePortfolio avg=0.0000000 calls=2 publish_pos=1 publish_pl=1
20180125 10:00:01:189; 5440;note;TWSSocket;execDetails;execution: SLD shares=332 at 137.265770 oid=103543, contract=XIV_STK_USD___0.000000_SMART_ exec-id=U+0000f9b8.5a69eaa6.01.01&20180125 10:00:01#
20180125 10:00:01:191; 5440;note;TWSSocket;position;postions of contract XIV_STK_USD___0.000000_=-627 conid=81488225 account=acc_2_master context=updatePortfolio avg=72.6152367 calls=2 publish_pos=1 publish_pl=1
20180125 10:00:01:194; 5440;note;TWSSocket;position;postions of contract XIV_STK_USD___0.000000_=0 conid=81488225 account=acc_34 context=updatePortfolio avg=0.0000000 calls=2 publish_pos=1 publish_pl=1
20180125 10:00:01:195; 5440;note;TWSSocket;execDetails;execution: SLD shares=389 at 137.265812 oid=103543, contract=XIV_STK_USD___0.000000_SMART_ exec-id=U+0000f9b8.5a69eaa8.01.01&20180125 10:00:01#
20180125 10:00:01:196; 5440;note;TWSSocket;position;postions of contract XIV_STK_USD___0.000000_=0 conid=81488225 account=acc_2_master context=updatePortfolio avg=72.6152367 calls=2 publish_pos=1 publish_pl=1
20180125 10:00:01:197; 5440;note;TWSSocket;position;postions of contract XIV_STK_USD___0.000000_=0 conid=81488225 account=acc_29 context=updatePortfolio avg=0.0000000 calls=2 publish_pos=1 publish_pl=1
20180125 10:00:01:198; 5440;note;TWSSocket;execDetails;execution: SLD shares=627 at 137.265970 oid=103543, contract=XIV_STK_USD___0.000000_SMART_ exec-id=U+0000f9b8.5a69eaaa.01.01&20180125 10:00:01#
20180125 10:00:01:208; 5440;note;TWSSocket;position;postions of contract VXX_STK_USD___0.000000_=26 conid=285777413 account=acc_30 context=updatePortfolio avg=4740.5603077 calls=4 publish_pos=1 publish_pl=1
20180125 10:00:01:288; 5440;note;SmartOrder;openOrderCallExpected;flag=0 uid=103544
20180125 10:00:01:288 [IN] uid=103544 BUY VXX MKT 6741 DAY SMART lmt=0 aux=0 Inactive acc_? clientid=2 transmit=IB ref= nref=#~tc#;0=103544;1=20180125 10:00:01:161d;2=6741;> oca= filled=0 lastfill=0 fa group= fa profile=XIV Algo Alloc fa method= fa percentage= gat= gtd= outside rth=0 hidden=0 algo=Adaptive algo-param=adaptivePriority=Normal oid=100 permid=490088304 uidp=0 oidp=0 cid=50672 comment=open order
20180125 10:00:01:323; 5440;error;API;;API-error: order rejected - reason:u1821349: cash available: 12255.64; cash needed for this order and other pending orders: 56728.18
<br>u2360031: cash available: 3459.09; cash needed for this order and other pending orders: 48307.82 code=201 id=100
20180125 10:00:01:323; 5440;note;SmartOrder;openOrderCallExpected;flag=0 uid=103544
20180125 10:00:01:325 [IN ERROR] uid=103544 BUY VXX MKT 6741 DAY SMART lmt=0 aux=0 Invalid acc_? clientid=2 transmit=IB ref= nref=#~tc#;0=103544;1=20180125 10:00:01:161d;2=6741;> oca= filled=0 lastfill=0 fa group= fa profile=XIV Algo Alloc fa method= fa percentage= gat= gtd= outside rth=0 hidden=0 algo=Adaptive algo-param=adaptivePriority=Normal oid=100 permid=490088304 uidp=0 oidp=0 cid=50672 comment=Order rejected - reason:U1821349: CASH AVAILABLE: 12255.64; CASH NEEDED FOR THIS ORDER AND OTHER PENDING ORDERS: 56728.18
<br>U2360031: CASH AVAILABLE: 3459.09; CASH NEEDED FOR THIS ORDER AND OTHER PENDING ORDERS: 48307.82

Re: Best way to confirm buying power before issuing order??

Posted: Thu Jan 25, 2018 7:29 pm
by board_admin
Hello,
you could prior check the buying power with function GET_ACC_VALUE.
Please check the help for this:
file:///C:/Program%20Files%20(x86)/trade-commander/twslink2/Documentation/html/fct_GET_ACC_VAL.html
example:
GET_ACC_VALUE(43,"",myaccount,0)

Or try to place your order with what_if flag on. Such an order is a simulation "what happens if i place this order"..

SET_ORDER_VAL(uidorder,63,"1",transmission_flag)

Re: Best way to confirm buying power before issuing order??

Posted: Thu Jan 25, 2018 7:43 pm
by sectorbets
I looked at get_acc_val previously, but didn't completely understand how it would work. Will it work with the master account as well as IB's algos ?? Would I just stick it in a while....begin loop until IB updates it ??

And to make it more complicated, there are about 50 sub accounts attached to the master account, but we only trade a handful, so I assume that get_acc_val would pick up everything under the master rather than just our 4 - 5 accounts.

And same question about what if trades -- just stick it in a while...begin loop until I get a hypothetical fill ??



Thank you
Rick

Re: Best way to confirm buying power before issuing order??

Posted: Thu Jan 25, 2018 8:07 pm
by board_admin
- GET_ACC_VAL
You can provide a comma separated list of accounts and you get the sum.
Don't know if it works with master account, i guess not.
Please give it a try.
(Dont know how this should be related to algos)

- what_if
well, you get status callback along with commissions and margin information and an optional warning text.
Unfortunately, the information stucks in TWSLink and there is no interface (no GET_ORDER_VAL_attribute) to retrieve it. Simply not requested as feature so far...

Re: Best way to confirm buying power before issuing order??

Posted: Thu Jan 25, 2018 8:14 pm
by sectorbets
I will try the comma delimited list, although I had issues trying that in the past with another function -- I don't recall which one since it was over a year ago -- I had the same situation of wanting to deal only with positions in our short list of accounts, and the function swept through all 50 accounts. I'll let you know how things unfold.

Thank you
Rick

Re: Best way to confirm buying power before issuing order??

Posted: Thu Jan 25, 2018 8:17 pm
by board_admin
Sorry,need to revise my post about whatif.
You get the information back, so for instance the margin impact of the order.

Here is the sequence in the batchtester:

Code: Select all

REGISTER_CONTRACT("EUR","CASH","JPY","IDEALPRO","","","",0,"",0,0)
CONNECT("",7496,1,1000)
PLACE_ORDER(50005,0,"BUY","MKT",50000,0,0.0,"GTC",-1,0)
SET_ORDER_VAL(75,63,"1",1) // set the whatif flag
WAIT_FOR_ORDER_STATUS(75,7,5000,9) // wait up to 5 seconds for status presubmitted (should return very quickly as order is not placed in the real world)
GET_ORDER_VAL(75,72,"") // get the init margin impact for this order returned

Re: Best way to confirm buying power before issuing order??

Posted: Thu Jan 25, 2018 8:35 pm
by sectorbets
1) We wait_for_filled closing the original position
2) We place a whatif order reversing the position
3) We wait_for_filled on the whatif order
4) Once we get filled, we place the order a second time with the whatif flag off

Is that what you are suggesting ??

Re: Best way to confirm buying power before issuing order??

Posted: Thu Jan 25, 2018 8:50 pm
by board_admin
No.
Option a) Do the reverse in 2 steps. close order and open short order.
Option b) Use whatif order in order to determine if you can do the reverse in one step.

If using b), just take the sequence i posted, so no need to wait for filled status. A whatif order (in my test)
complete with presubmitted status.

Re: Best way to confirm buying power before issuing order??

Posted: Fri Jan 26, 2018 2:22 pm
by sectorbets
How does the presubmitted status work ??

Re: Best way to confirm buying power before issuing order??

Posted: Fri Jan 26, 2018 2:36 pm
by board_admin
It is just a status telling you the order is wating for getting submitted. For instance stop orders have presubmitted status.
When stop price is triggered, they get submitted as market and up to fill they have status submitted.

In our case this is not important. The status messages in only a feedback from server to carry out commission and margin
information. It could be any status. Only the information coming in with status message for whatif orders is important here.

(according my understanding)

Re: Best way to confirm buying power before issuing order??

Posted: Fri Feb 09, 2018 1:10 pm
by sectorbets
As I expected, listing just a few accounts that we want to check for buying power doesn't work. Instead of just picking up the two accounts in our test, it looked at every account attached to the master list. This is the same way that using an account list worked in the past -- it always used the entire list of accounts rather than just a short list.

Here is the code we tried:

BuyingPower = GET_ACC_VAL(43,"","DU216426,DU216425",0);
Print(BuyingPower," ",time);

And here is the log -- as you may see, it doesn't list the two accounts

20180209 08:02:54:448; 13612;note;tcExpFunc;GET_ACC_VAL;-43 - -acc_? -0
20180209 08:02:54:449; 13612;note;tcExpFunc;GET_ACC_VAL;ret=5511677.29

Thank you
Rick

Re: Best way to confirm buying power before issuing order??

Posted: Fri Feb 09, 2018 1:37 pm
by board_admin
Hello,
the log is confusing here. The ? for account is just for privacy.

I just made a check and the function works correctly. You get the sum of account value - here buying power - of listed
accounts returned.

Re: Best way to confirm buying power before issuing order??

Posted: Fri Feb 09, 2018 2:34 pm
by sectorbets
You are correct !! By asking for BP, we got 4X the cash value since the accounts are pattern day trading accounts. BP won't serve our purposes, so I will look at some other value.

Thank you
Rick

Re: Best way to confirm buying power before issuing order??

Posted: Fri Feb 09, 2018 2:52 pm
by board_admin
I understand. I think the buying power respects the account leverage.

Allow me a suggestion:

For my own modest trading, i use the margin level in % to calculate risk:
balance / margin * 100.

The margin is a sophisticated risk calculation by your broker.
So i think, it is pretty appropiate as base for risk calcuation.

After my opition margin level
>= 1000 is good
>= 2000 is comfortable
<= 500 is risky.

Re: Best way to confirm buying power before issuing order??

Posted: Mon Apr 09, 2018 4:18 pm
by sectorbets
We are ready to dig into this again -- a few things we have learned:

1) We have no problem in margin accounts since we use very little leverage, and the 4X buying power always works.
2) The problem as described before is only in cash accounts where is we are short by even a penny, the trade is rejected
3) We couldn't get the what-if trade to work -- our test accounts are all margin accounts, and some of our live accounts are cash accounts, and we certainly can't try things in the live accounts

So, what do you recommend that we can test in a margin account, and then go live in a cash account with the least risk of screwing up the live account. Or could you try a test of what-if or whatever else makes sense in a cash account, including the output so that we have more confidence before we consider moving the code to a cash account.

Thank you
Rick

Re: Best way to confirm buying power before issuing order??

Posted: Tue Apr 10, 2018 2:43 pm
by board_admin
The problem is that i am not able to test this, because i have no cash accounts and also the public demo is always
a margin account.

With a cash account, isnt it much easier to make all calculations on your own before placing
an oder?

But, if short trades get rejected even you have the balance to do this, enable the API log in TWS and set log details of TWS to detailed.
Then check the logs of TWS. If you get no anwers, please ask IB support.

Also, may there are some risk settings (precauctions) in TWS which makes short orders to get rejected.

Re: Best way to confirm buying power before issuing order??

Posted: Tue Apr 10, 2018 3:11 pm
by sectorbets
All reversals in cash accounts -- not just shorts -- get rejected if IB hasn't reported back the available cash even though it has reported back that the order was filled. -- I will turn on the IB log to see what I can learn -- thank you.

Re: Best way to confirm buying power before issuing order??

Posted: Tue Apr 10, 2018 3:25 pm
by board_admin
If you say reversals, so you like to turn a position in one step, you may split this into 2 steps: One close, one open.
But perhaps i have no exact picture of the problem.

If you say "IB hasn't reported back available cash". Normally this should happen very quickly after each execution.

Re: Best way to confirm buying power before issuing order??

Posted: Tue Apr 10, 2018 3:57 pm
by sectorbets
We do a two step reversal -- we have been using your WAIT_FOR_FILLED function, but most of the time it reports back "1" before IB reports back the cash available to trade which isn't a problem in a margin account, but we have a few IRA cash accounts and the second step (trade) gets rejected more often not because IB hasn't reported the available cash yet.

We have stretched out the wait time to 20000, and we still get some trades rejected -- at 2000 almost all are rejected

So neither of us have a cash simulation account, and we don't want to use one of our live accounts until we are 99.9% sure that our test won't blow things up.

So, what would you suggest using to see if IB has reported back the available cash before we enter the second order in our reversal -- also, we are using IB's FA structure which complicates things a little. Since we are using the FA structure, TOTAL cash or total BP get reported back for example. Let's say that we are trading $100,000 position sizes, and that the total equity of three accounts is $120,000, $150,000, $500,000 so the first two accounts would have available cash of $20,000 and $50,000 while waiting for the available cash to report back. But let's say the third account has $400,000 while it is waiting for the total cash available to be reported. IN this case the total cash available is $470,000 even if IB hasn't reported back yet, so our code thinks there is plenty of cash to take the second trade, and the first two accounts reject the trade since they individually don't have enough cash.

So that is why we have been using WAIT_FOR_FILL which works some of the time and sometimes not -- depending on whether or not there is enough cash by the time the trade is reported "filled"

Thank you