Page 1 of 1
Managing multiple positions - is there a "comment" field in position?
Posted: Thu Jan 14, 2021 2:09 pm
by hkenrique
Hello,
I am trying to manage multiple positions in IB, where ibgCREATE_POSITION_SNAPSHOT seems to present a nice realization.
Now, I need to be able to differentiate the positions created by different strategies, they can be of the same symbol or different symbols.
In MT5 I normally use magic number but I do not see it in the IB system.
I am thinking about using "comment" in the OrderSend function which will write to Order Reference in IB system.
But is there a way I can retrieve this "comment" from the position functions (through ibgPOSITION_INFO_XXX functions)? Or there is another way to realize this? like __conid or sth else?
Thank you.
Re: Managing multiple positions - is there a "comment" field in position?
Posted: Thu Jan 14, 2021 3:08 pm
by board_admin
A position is an accumulation of executions.
There is always only one position per Symbol.
ibgORDER_SEND_STR
has a field where you can set a comment. This comment is pegged to order (and execution).
The atribute identfier is 49 -> see help of IBPlugin.
But you can not peg a comment to a position.
Re: Managing multiple positions - is there a "comment" field in position?
Posted: Thu Jan 14, 2021 3:14 pm
by hkenrique
Hi,
Thanks that has answered my question.
Re: Managing multiple positions - is there a "comment" field in position?
Posted: Thu Jan 14, 2021 4:01 pm
by hkenrique
Hi,
Continuing our discussion about the comment field.
So I sent an order with some comment but it seems I cannot get the comment with
string comment=ibgORDER_INFO_STR(i,49); //comment of order
long pid=ibgORDER_INFO_INT64(i,55); //pid of order
PrintFormat("Order Info: comment:%s, pid:%d",comment,pid);
the output is:
Order Info: comment:, pid:-703489356
Comment is empty. But I was able to get the comment in previous version before the 2020 Sep upgrade.
I can see my comment written into Order Ref in IB TWS. It's within the 9~ field (the value of my comment is: 10005,1610646493) :
Order Ref: |0~348364093889204|1~0|2~12087792|3~100000|4~ibg|6~|9~10005,1610646493
Please let me know if anything is not correct here.
Thanks.
Re: Managing multiple positions - is there a "comment" field in position?
Posted: Thu Jan 14, 2021 4:13 pm
by board_admin
This function makes only sense, when prior called:
Have you done this ?
Re: Managing multiple positions - is there a "comment" field in position?
Posted: Thu Jan 14, 2021 4:21 pm
by hkenrique
Hi,
Yes I have done this but cannot retrieve the comment.
full code:
Code: Select all
int orderNum=ibgCREATE_PENDING_ORDER_SNAPSHOT(IB_PLUGIN_DEF_CONNID,__conid,1);
for(int i=0;i<orderNum;++i)
{
string comment=ibgORDER_INFO_STR(i,49); //comment of order
unsigned long pid=ibgORDER_INFO_INT64(i,55); //pid of order
PrintFormat("Order Info: comment:%s, pid:%d",comment,pid);
}
Re: Managing multiple positions - is there a "comment" field in position?
Posted: Thu Jan 14, 2021 4:27 pm
by board_admin
Are you sure there are strict pending orders for that conid ?
Strict means, you can see them in TWS with one of the pending status colors (blue,green)
Re: Managing multiple positions - is there a "comment" field in position?
Posted: Thu Jan 14, 2021 4:36 pm
by hkenrique
Yes you can see my loop is able to retrieve the parent order id of the orders (490385839). but comment is void:
CS 0 00:34:41.005 HFTLiteClassicIB (EURUSD,M1) CONNECTIONID: 1
MF 0 00:34:41.006 HFTLiteClassicIB (EURUSD,M1) Order Info: comment:, pid:490385839
NR 0 00:34:41.006 HFTLiteClassicIB (EURUSD,M1)
CF 0 00:34:41.006 HFTLiteClassicIB (EURUSD,M1) Order Info: comment:, pid:490385839
DS 0 00:34:41.006 HFTLiteClassicIB (EURUSD,M1)
Re: Managing multiple positions - is there a "comment" field in position?
Posted: Thu Jan 14, 2021 5:11 pm
by hkenrique
Hi,
I also tested the code:
comment = ibgORDER_ATTRIBUTE(IB_PLUGIN_DEF_CONNID,orderId,10);
this code works in the old version to retrieve the comment but seems not working in the new version any more.
I suspect there is some defect in the dll.
It's an important function for me so please take a look.
Re: Managing multiple positions - is there a "comment" field in position?
Posted: Thu Jan 14, 2021 9:25 pm
by board_admin
There are in fact some changes to version of last year.
Please use the help. it was thoroughly revised.
For order attributes, the attribute table is what matters.
I could not reproduce the problem. I get the order comment returned correctly with attribute id = 49.
This function call
Code: Select all
string comment=ibgORDER_INFO_STR(i,49);
should return order comment, always presumed it is still pending.
Seems you are using MetaTrader. Please mind, the uid is long (64 bit signed integer) not unsigned long.
This attribute string for ibgORDER_SEND_STR sets comment to "my_comment"
Code: Select all
"#1=50000#53=BUY#6=LMT#49=my_comment#2=1.17"
This, makes no sense for your purpose
Code: Select all
comment = ibgORDER_ATTRIBUTE(IB_PLUGIN_DEF_CONNID,orderId,10);
Because 10 is block order flag. Just pass 49,as above to get comment.
Mind the orderid. In your code you query orderid by
Code: Select all
long pid=ibgORDER_INFO_INT64(i,55); //p
which is wrong because this is the uid of the
parent order.
What you should pass is the
1011, the code for uid.
>>Please apologize the mess in the ids. <<
Each function that refers somehow to an order attribute, uses the same order attribute table.
Re: Managing multiple positions - is there a "comment" field in position?
Posted: Fri Jan 15, 2021 3:01 am
by hkenrique
thank you for your detailed explanation.
maybe it's something with my order send function that caused the comment cannot be retrieved?
my code that sends the order:
Code: Select all
long uid= __ib.order_send(__conid_EURUSD
,ibOrderType // defined in file include/trade-commander.com/ibplugin_types.mqh
,size
,0
,targetPrice
,stopLossPrice
,takeProfitPrice
,comment
,0
,0);
order_send definition:
Code: Select all
long order_send(int uid_symbol
,ENUM_ORDER_TYPE cmd
,int shares
,double lmt_price=0.0
,double stp_price=0.0
,double stop_loss=0.0
,double take_profit=0.0
,string comment=""
,int id_allocation=0
,color arrow_color=clrNONE)
{
if(IsTesting == false)
{
if(uid_symbol < 0)
uid_symbol=_uidc;
string action="";
string otype="";
MT_OTYPE_TO_IB(cmd,action,otype);
long ticket= ibgORDER_SEND (_connectionid, uid_symbol,action,otype,shares,lmt_price,stp_price,stop_loss,take_profit, comment,id_allocation);
if(arrow_color != clrNONE)
{
}
return ticket;
}
else
return -1;
}
Re: Managing multiple positions - is there a "comment" field in position?
Posted: Fri Jan 15, 2021 1:11 pm
by board_admin
What is not working with your code ?
Re: Managing multiple positions - is there a "comment" field in position?
Posted: Fri Jan 15, 2021 1:29 pm
by hkenrique
I'm just trying to find out why my comment cannot be retrieved.
If you don't see a problem of the above order send function then probably it's fine.
Re: Managing multiple positions - is there a "comment" field in position?
Posted: Fri Jan 15, 2021 2:02 pm
by board_admin
I checked it always outside MetaTrader and got the comment with both function calls.
But when testing in MetaTrader, i also get no comment returned.
Sorry for this. Need to investigate.
Re: Managing multiple positions - is there a "comment" field in position?
Posted: Fri Jan 15, 2021 2:30 pm
by hkenrique
thanks. I will stick with the old version for now.
Re: Managing multiple positions - is there a "comment" field in position?
Posted: Fri Jan 15, 2021 2:41 pm
by board_admin
Dont do this please. It works differently.
The problem seems only to be in function ibgORDER_SEND in MetaTrader.
Just use
instead or wait for fix.
MetaTrader has a convenient string implementation
which allows to easily create the attribute string for this function
using StringFormat function or just the
+ operator;
Help for this function with link for table of attributes:
file:///C:/Program%20Files/trade-commander.com/IBPlugin/documentation/fct_ibgORDER_SEND_STR.html
-----
Just stick with the ibplugin_demo, where i skipped the wrapper
and use direct DLL calls. This is most transparent.
In the attribute string, the comment is introduced by:
This way, you should be able to get the comment returned without
using old version.
Re: Managing multiple positions - is there a "comment" field in position?
Posted: Fri Jan 15, 2021 3:06 pm
by board_admin
New version uploaded:
https://trade-commander.com/de/ibpluginsetup/
Just over install, make sure MetaTrader closed before installation.
Please checkout, if you get comment and let me know. Thank you.
Re: Managing multiple positions - is there a "comment" field in position?
Posted: Mon Jan 18, 2021 11:52 am
by hkenrique
Hi,
It works now. Thanks a lot for the help.