Managing multiple positions - is there a "comment" field in position?

IBPlugin is further development of TWSLink.It is focused for the useage with MetaTrader. But as it is a DLL, you can use it everywhere on Windows which supports DLL, e.g. in Trade Station or C++ applications or make wrapper for Perl, Python etc.

Users who just registered, please write an email to
forum@trade-commander.de
with your username mentioned.
So we can distinguish you from bots.
Thank you
Post Reply
hkenrique
Posts: 28
Joined: Tue Jul 28, 2020 3:23 pm

Managing multiple positions - is there a "comment" field in position?

Post 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.

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: Managing multiple positions - is there a "comment" field in position?

Post 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.

hkenrique
Posts: 28
Joined: Tue Jul 28, 2020 3:23 pm

Re: Managing multiple positions - is there a "comment" field in position?

Post by hkenrique »

Hi,

Thanks that has answered my question.

hkenrique
Posts: 28
Joined: Tue Jul 28, 2020 3:23 pm

Re: Managing multiple positions - is there a "comment" field in position?

Post 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.

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: Managing multiple positions - is there a "comment" field in position?

Post by board_admin »

This function makes only sense, when prior called:

Code: Select all

ibgCREATE_PENDING_ORDER_SNAPSHOT
Have you done this ?

hkenrique
Posts: 28
Joined: Tue Jul 28, 2020 3:23 pm

Re: Managing multiple positions - is there a "comment" field in position?

Post 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);
      }
           

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: Managing multiple positions - is there a "comment" field in position?

Post 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)

hkenrique
Posts: 28
Joined: Tue Jul 28, 2020 3:23 pm

Re: Managing multiple positions - is there a "comment" field in position?

Post 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)

hkenrique
Posts: 28
Joined: Tue Jul 28, 2020 3:23 pm

Re: Managing multiple positions - is there a "comment" field in position?

Post 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.

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: Managing multiple positions - is there a "comment" field in position?

Post 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.

hkenrique
Posts: 28
Joined: Tue Jul 28, 2020 3:23 pm

Re: Managing multiple positions - is there a "comment" field in position?

Post 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;
   }
   
Last edited by hkenrique on Fri Jan 15, 2021 1:29 pm, edited 1 time in total.

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: Managing multiple positions - is there a "comment" field in position?

Post by board_admin »

What is not working with your code ?

hkenrique
Posts: 28
Joined: Tue Jul 28, 2020 3:23 pm

Re: Managing multiple positions - is there a "comment" field in position?

Post 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.

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: Managing multiple positions - is there a "comment" field in position?

Post 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.

hkenrique
Posts: 28
Joined: Tue Jul 28, 2020 3:23 pm

Re: Managing multiple positions - is there a "comment" field in position?

Post by hkenrique »

thanks. I will stick with the old version for now.

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: Managing multiple positions - is there a "comment" field in position?

Post by board_admin »

Dont do this please. It works differently.
The problem seems only to be in function ibgORDER_SEND in MetaTrader.

Just use

Code: Select all

ibgORDER_SEND_STR 
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:

Code: Select all

#49=My Comment
This way, you should be able to get the comment returned without
using old version.

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: Managing multiple positions - is there a "comment" field in position?

Post 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.

hkenrique
Posts: 28
Joined: Tue Jul 28, 2020 3:23 pm

Re: Managing multiple positions - is there a "comment" field in position?

Post by hkenrique »

Hi,

It works now. Thanks a lot for the help.

Post Reply