IB data for order

Discuss, post questions here about MTIBBridge the MetaTrader-Interactive Brokers trade-copier

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
Guillaume
Posts: 21
Joined: Mon Sep 04, 2017 5:40 pm

IB data for order

Post by Guillaume »

This is my email conversation.
I have some other questions on the use of TC Bridge library (tc_bridge_utils.mqh)
  1. do the functions tc_ib_bid() and tc_ib_ask() use the true bid/ask value of a symbol from IB ? Can I use them to determine when to place order with this IB tick value ?
  2. When an order has been placed on IB, does the function PositionGetDouble(POSITION_PRICE_OPEN) return the true price of execution on IB ?
And your answer :
  1. yes
  2. yes (weighted average open price).
So, I use these functions to place orders.

But when I use the tc_ib_bid(_Symbol) or tc_ib_ask(_Symbol) functions to place orders :

Code: Select all

trade.Buy(Lots, _Symbol, tc_ib_ask(_Symbol), tc_ib_ask(_Symbol) - stopLoss, tc_ib_ask(_Symbol) + takeProfit);
I haven't any price returned. And so, the price is, for example :
2017.09.04 13:58:24.348 essaiStrategie (USDCHF,H1) CTrade::OrderSend: market buy 1.00 USDCHF sl: 17976931348623157081452742373170435679807056752584499659891747680315726078002853876058955863276687817154045895351438246423432132688946418276846754670353751698604991057655128207624549009038932894407586850845513

It's independent of the currency used.

What goes wrong with that ? Do you have any examples showing how to use these functions correctly ?
Furthermore, I have loaded the script tc_sc_bridge_events , and it shows very good price from IB in the chart.

Thanks

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

Re: IB data for order

Post by board_admin »

Hello,
in order to get IB data, you need a subsription for those data. Can you see market data in TWS ?
If not, get a subscription please and try again.

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

Re: IB data for order

Post by board_admin »

Sorry, forget to mention that quotes need to be enabled.
Can you check this please: In the bridge, first page, double click on the MetaTrader instance and make sure "Quote messages" is checked.

Guillaume
Posts: 21
Joined: Mon Sep 04, 2017 5:40 pm

Re: IB data for order

Post by Guillaume »

HI,
I have an IB accout, and MTIBBridge sees this account.
And in my chart I have the price information of IB :
Image
Attachments
Screen Shot 09-04-17 at 02.24 PM.PNG
Screen Shot 09-04-17 at 02.24 PM.PNG (13.15 KiB) Viewed 19746 times

Guillaume
Posts: 21
Joined: Mon Sep 04, 2017 5:40 pm

Re: IB data for order

Post by Guillaume »

board_admin wrote:
Mon Sep 04, 2017 6:23 pm
Sorry, forget to mention that quotes need to be enabled.
Can you check this please: In the bridge, first page, double click on the MetaTrader instance and make sure "Quote messages" is checked.
Yes, "Quote messages" is checked.

Do you have other things to check to know where the issue comes from?

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

Re: IB data for order

Post by board_admin »

Sorry, now i am confused: Obviously you get IB bid and ask because it is shown in the script.
Or do i misunderstand the problem. Please let me know.

Guillaume
Posts: 21
Joined: Mon Sep 04, 2017 5:40 pm

Re: IB data for order

Post by Guillaume »

OK, sorry.
I use the script provided by trade-commander to display info in chart. This is what you see in the picture.

Now, I want these information in my Expert Advisor script. And for that, I use the tc_ib_bid() and tc_ib_ask functions. But they return bad values as shown previously in the thread.

So, I don't understand what is missing in my script. And this is also why I ask an example of the use of this functions.

Thanks

Edit :
I see some calls like TCBC_SUBSCRIBE in init() function, to define a communication channel (?) . Is mandatory ?
Example :

Code: Select all

    /// create a message channel
    _ib_channel=TCBC_SUBSCRIBE();
    Print("subscriber id=",IntegerToString(_ib_channel));
    
  
    
    /// load current position into message queue for this expert
    TCBC_REFLECT_OBJECTS(_ib_channel,_TC_BDG_POSITION);
    TCBC_REFLECT_OBJECTS(_ib_channel,_TC_BDG_STATUS_MSG);

    /// request for IB quotes    
    TCBC_REQUEST_QUOTES(_ib_channel,Symbol(),1);
Do you have a kind of documentation on this functionality?

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

Re: IB data for order

Post by board_admin »

I just check the expert tc_ea_bridge_events in MT4. It's source is open.
May be you are using old code. What i found:

#include <tc_bridge_utils.mqh>

Get IBs bid for symbol:
TCBC_QUOTE(Symbol(),1);

Get IBs ask for symbol:
TCBC_QUOTE(Symbol(),2);

Get IBs last for symbol (not available for forex -> IB limitation):
TCBC_QUOTE(Symbol(),3);

Could you please check out these functions.

Guillaume
Posts: 21
Joined: Mon Sep 04, 2017 5:40 pm

Re: IB data for order

Post by Guillaume »

I use MT5
The function tc_ib_ask() is a proxy for a call to TCBC_QUOTE(Symbol(),2)
This is define in tcbridgecomm.mqh

Code: Select all

	#define		_TC_IB_QUOTE_ASK 2

and tc_bridge_utils.mqh

Code: Select all

double tc_ib_ask(const string& symbol)
{
    return TCBC_QUOTE(symbol,_TC_IB_QUOTE_ASK);
}
So, I think everything is OK on this side.

Since I integrate in my script the previous code defining subscription to the channel (and other part of the code that I don't understand, TCBC_REFLECT_OBJECTS, TCBC_REQUEST_QUOTES , ...) it seems I have no more issue. May be it's by chance ...

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

Re: IB data for order

Post by board_admin »

With MT5 i checked tc_sc_bridge_events and got immediately bid and ask.
Finally it is the same function called in the plugin DLL.
Could you please check out tc_sc_bridge_events ? It is a script.

Guillaume
Posts: 21
Joined: Mon Sep 04, 2017 5:40 pm

Re: IB data for order

Post by Guillaume »

Yes, I 've checked tc_sc_bridge_events. It is this script I use to display information in my charts. This is why I know something was wrong in my script.
In tc_sc_bridge_events we need to suscribe and doing some stuffs in the init() function.
Can you describe why we have all these functions ?
Do you have any information/documentation on the functions like TCBC_SUBSCRIBE, TCBC_REFLECT_OBJECTS, TCBC_REQUEST_QUOTES?

Thanks.

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

Re: IB data for order

Post by board_admin »

Unfortunately there is no documentation about.

TCBC_SUBSCRIBE: This is just get a channelid. You need to call it first and once.
TCBC_REFLECT_OBJECTS: Subscribe to specific objects, like position etc. What you can see in function IN_bridge_message as cases.
TCBC_REQUEST_QUOTES: Subscribe to quotes: bid, ask,last.The case in IN_bridge_message is _TC_BDG_IN_IB_QUOTE.
Can you please checkout a slightly updated version attached ? It is for MT5.
Attachments
tc_sc_bridge_events.zip
(4.13 KiB) Downloaded 483 times

Guillaume
Posts: 21
Joined: Mon Sep 04, 2017 5:40 pm

Re: IB data for order

Post by Guillaume »

Hi,
Thanks I'm checking that.
If I well understand the logical, I have to listen always messages coming from IB, and, for example, retrieving the quotes from these messages? So, I don't have to use anymore the

Code: Select all

void OnTick()
handler of MT5, because it is replaced by this code:

Code: Select all

        init();        
        while(IsStopped() == false)
        {
            IN_bridge_message();            
            Sleep(bridge_msg_refresh_rate_ms);
        }
Finally I'm confused, because I thought I could use the ticks from MT5 but placing order with prices from IB, with function tc_ib_ask()

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

Re: IB data for order

Post by board_admin »

Hello,
this is your choice.
You can do it in onTick but it is possible that there is no new IB tick in the meantime.
Or you make a loop where you wait for new IB events and you evaluate the event
in a case like demonstrated in the script.

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

Re: IB data for order

Post by board_admin »

Need something to clarify:

For market orders a price is only important when trading with MetaTrader broker, because you can define a maximum deviation.
For IB market orders, the price is ignored. You get the price which is made by market maker, regardless what slippage might results.
However, the bridge always records both: the MT price and the IB price for market orders. You most likely will find, that IBs prices
are better in average.

For limit orders, IB prices make no sense for MetaTrader, because the spread is widened. For IB orders it makes sense to operate with IB ask or IB bid.

You can get bid,ask whenever you like. If you query IB data during MetaTrader tick event, which means that there is just a MetaTrader broker data
tick - not an IB data tick - you get the latest stored tick of IB. So OnTick is just an occasion to query IB data.

Or you make a loop like in the script where you wait for IB events. If you there get a quote event, that means that IB data just had a tick and bid
or ask or last or bid size, or ask size or last size has been updated.

Guillaume
Posts: 21
Joined: Mon Sep 04, 2017 5:40 pm

Re: IB data for order

Post by Guillaume »

Thanks for your help.
I'm going to understand and to try all these ideas.

Post Reply