softwarespartan / IB4m

Interactive Brokers API for Matlab
GNU General Public License v2.0
62 stars 21 forks source link

how to incorporate with FX #6

Closed justinshekhk closed 7 years ago

justinshekhk commented 7 years ago

Hi Abel,

thanks for sharing this code, I have just started an IB account and I would like to know how to handle FX using your code.

Thanks in advance.

Justin

softwarespartan commented 7 years ago

Hi Justin

Thanks for reaching out.

You should just be able to configure a contract any way you like and request data or place orders etc.

Create a contract object with

contract = com.ib.client.Contract()

and then have a look at the fields of the object

fields(contract)

ans =

18×1 cell array

'm_conId'
'm_symbol'
'm_secType'
'm_expiry'
'm_strike'
'm_right'
'm_multiplier'
'm_exchange'
'm_currency'
'm_localSymbol'
'm_tradingClass'
'm_primaryExch'
'm_includeExpired'
'm_secIdType'
'm_secId'
'm_comboLegsDescrip'
'm_comboLegs'
'm_underComp'

For example see the IB API examples for FX here:

https://www.interactivebrokers.com/en/software/api/apiguide/java/how_to_determine_a_futures_contract.htm <https://www.interactivebrokers.com/en/software/api/apiguide/java/how_to_determine_a_futures_contract.htm>

Don’t hesitate to reply if you need anything additional or have more questions.

Cheers -abel

On May 14, 2017, at 1:18 PM, justinshekhk notifications@github.com wrote:

Hi Abel,

thanks for sharing this code, I have just started an IB account and I would like to know how to handle FX using your code.

Thanks in advance.

Justin

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/6, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1GXju33OwGENbTO4rvS5TCucSUUIks5r5zdygaJpZM4NacD_.

justinshekhk commented 7 years ago

Hi Abel,

Thanks for your prompt reply, it works now for me to submit orders, while I have another question for requesting historical market data.

Let me refer to your HistoricalDataExample.m file, I just simply replace the contract using the following:

contract = com.ib.client.Contract(); contract.m_symbol = 'USD'; contract.m_secType = 'CASH'; contract.m_exchange = 'IDEALPRO'; contract.m_primaryExch = 'IDEALPRO'; contract.m_currency = 'JPY'; contract.m_includeExpired = 0; contract.m_strike = 0;

and I get the error message when it runs to % Retreive the historical data event from the buffer hde = buf.get();:

the message shown:

-1 501 Already connected.

-1 501 Already connected.

1 162 Historical Market Data Service error message:No historical market data for USD/CASH@FXSUBPIP Last 1800

1 162 Historical Market Data Service error message:No historical market data for USD/CASH@FXSUBPIP Last 1800

Error using HistoricalDataExample (line 40) Java exception occurred: org.apache.commons.collections.BufferUnderflowException: The buffer is already empty at org.apache.commons.collections.buffer.BoundedFifoBuffer.get(BoundedFifoBuffer.java:260)

Please bear with me, and thanks again for sharing the code which helps me a lot!

Best, Justin

On 15 May 2017, at 02:47, Abel Brown notifications@github.com wrote:

Hi Justin

Thanks for reaching out.

You should just be able to configure a contract any way you like and request data or place orders etc.

Create a contract object with

contract = com.ib.client.Contract()

and then have a look at the fields of the object

fields(contract)

ans =

18×1 cell array

'm_conId' 'm_symbol' 'm_secType' 'm_expiry' 'm_strike' 'm_right' 'm_multiplier' 'm_exchange' 'm_currency' 'm_localSymbol' 'm_tradingClass' 'm_primaryExch' 'm_includeExpired' 'm_secIdType' 'm_secId' 'm_comboLegsDescrip' 'm_comboLegs' 'm_underComp'

For example see the IB API examples for FX here:

https://www.interactivebrokers.com/en/software/api/apiguide/java/how_to_determine_a_futures_contract.htm https://www.interactivebrokers.com/en/software/api/apiguide/java/how_to_determine_a_futures_contract.htm

Don’t hesitate to reply if you need anything additional or have more questions.

Cheers -abel

On May 14, 2017, at 1:18 PM, justinshekhk notifications@github.com wrote:

Hi Abel,

thanks for sharing this code, I have just started an IB account and I would like to know how to handle FX using your code.

Thanks in advance.

Justin

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/6, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1GXju33OwGENbTO4rvS5TCucSUUIks5r5zdygaJpZM4NacD_.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/6#issuecomment-301331732, or mute the thread https://github.com/notifications/unsubscribe-auth/AY1iWw_xJePLONCKoJqM4-VO267c5XsRks5r50wpgaJpZM4NacD_.

softwarespartan commented 7 years ago

Hi Justin

Since the errors are all event based it can make it a little difficult to figure out what’s going on.

First, the simple issue is calling the “get()” method on an empty queue will throw an exception. Nothing to worry about and can be ignored. Admittedly it is not very idiomatic of Matlab but since we’re using java objects the behavior changes a bit.

You can prevent this error by simply using an if statement like:

if buf.size() > 0: hde = buf.get(); end

The reason the buffer is empty is because there is no historical data for USD/CASH@FXSUBPIP.

That 162 error is generated by TWS and sent over to you. There is an automatic callback in session that prints errors to the screen. Also session has an error buffer which keeps track of error events if you need to retrieve them.

In general, you’ll get error 162 often when trying to setup new contract etc. TWS can be a little fickle sometimes when it comes to contract properties.

Cheers -abel

On May 15, 2017, at 5:52 PM, justinshekhk notifications@github.com wrote:

Hi Abel,

Thanks for your prompt reply, it works now for me to submit orders, while I have another question for requesting historical market data.

Let me refer to your HistoricalDataExample.m file, I just simply replace the contract using the following:

contract = com.ib.client.Contract(); contract.m_symbol = 'USD'; contract.m_secType = 'CASH'; contract.m_exchange = 'IDEALPRO'; contract.m_primaryExch = 'IDEALPRO'; contract.m_currency = 'JPY'; contract.m_includeExpired = 0; contract.m_strike = 0;

and I get the error message when it runs to % Retreive the historical data event from the buffer hde = buf.get();:

the message shown:

-1 501 Already connected.

-1 501 Already connected.

1 162 Historical Market Data Service error message:No historical market data for USD/CASH@FXSUBPIP Last 1800

1 162 Historical Market Data Service error message:No historical market data for USD/CASH@FXSUBPIP Last 1800

Error using HistoricalDataExample (line 40) Java exception occurred: org.apache.commons.collections.BufferUnderflowException: The buffer is already empty at org.apache.commons.collections.buffer.BoundedFifoBuffer.get(BoundedFifoBuffer.java:260)

Please bear with me, and thanks again for sharing the code which helps me a lot!

Best, Justin

On 15 May 2017, at 02:47, Abel Brown notifications@github.com wrote:

Hi Justin

Thanks for reaching out.

You should just be able to configure a contract any way you like and request data or place orders etc.

Create a contract object with

contract = com.ib.client.Contract()

and then have a look at the fields of the object

fields(contract)

ans =

18×1 cell array

'm_conId' 'm_symbol' 'm_secType' 'm_expiry' 'm_strike' 'm_right' 'm_multiplier' 'm_exchange' 'm_currency' 'm_localSymbol' 'm_tradingClass' 'm_primaryExch' 'm_includeExpired' 'm_secIdType' 'm_secId' 'm_comboLegsDescrip' 'm_comboLegs' 'm_underComp'

For example see the IB API examples for FX here:

https://www.interactivebrokers.com/en/software/api/apiguide/java/how_to_determine_a_futures_contract.htm https://www.interactivebrokers.com/en/software/api/apiguide/java/how_to_determine_a_futures_contract.htm

Don’t hesitate to reply if you need anything additional or have more questions.

Cheers -abel

On May 14, 2017, at 1:18 PM, justinshekhk notifications@github.com wrote:

Hi Abel,

thanks for sharing this code, I have just started an IB account and I would like to know how to handle FX using your code.

Thanks in advance.

Justin

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/6, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1GXju33OwGENbTO4rvS5TCucSUUIks5r5zdygaJpZM4NacD_.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/6#issuecomment-301331732, or mute the thread https://github.com/notifications/unsubscribe-auth/AY1iWw_xJePLONCKoJqM4-VO267c5XsRks5r50wpgaJpZM4NacD_.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/6#issuecomment-301518188, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1NzFCwDvl-FqzEHyNoEbu3juOPCrks5r6HTQgaJpZM4NacD_.

justinshekhk commented 7 years ago

Hi Abel,

it's working now, thanks for your generous help. Cheers!

Justin

softwarespartan commented 7 years ago

Great to hear!

Don’t hesitate to reach out again if you need anything additional on this or have other questions.

Cheers -abel

On May 16, 2017, at 6:18 PM, justinshekhk notifications@github.com wrote:

Hi Abel,

it's working now, thanks for your generous help. Cheers!

Justin

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/6#issuecomment-301834858, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1CojXgv-cyuuILRSilQgf9QmcVdAks5r6cxEgaJpZM4NacD_.

madeinquant commented 5 years ago

Hi Abel, Thanks for your library "IB4m", I would download historical data but it is not as expected, please comment what I have don't incorrectly.

Regards, Nelson.

addpath('IB4m') addpath('IB4m/docs') javaaddpath(fullfile(pwd, 'IB4m/Jar', 'TWS973.jar'))

% initialize session with TWS session = TWS.Session.getInstance();

% create local buffer for market data events with size 10,000 (default size is 32) [databuf,datalh] = TWS.initBufferForEvent(TWS.Events.MARKETDATA,10000);

% create local buffer for market metadata events with size 1000 [metabuf,metalh] = TWS.initBufferForEvent(TWS.Events.MARKETMETADATA,1000);

% connect to TWS session.eClientSocket.eConnect('127.0.0.1',7496,0);

% create contract object contract = com.ib.client.Contract(); contract.symbol('USD'); contract.secType('CASH'); contract.exchange('IDEALPRO'); contract.primaryExch('IDEALPRO'); contract.currency('JPY');

queryTime = datestr(datetime('now') - calmonths(6), 'yyyymmdd HH:MM:SS');

session.eClientSocket.reqHistoricalData(4001, contract, queryTime, '1 M', '1 min', 'MIDPOINT', 1, 1, false, []); pause(0.5);

if buf.size() > 0 hde = buf.get(); end

softwarespartan commented 5 years ago

You likely need to use ContractDetails in order to configure the correct contract for the request.

See also: https://interactivebrokers.github.io/tws-api/basic_contracts.html https://interactivebrokers.github.io/tws-api/basic_contracts.html

Are you able to access any FX or just this particular example gives you issue?

You also need to make sure that you have your IB account configured to access these data (i.e. data subscriptions etc)

In general, it’s helpful to provide the error messages your receiving which will help debug your situation.

Happy Holidays

Cheers -abel

On Dec 24, 2018, at 10:15 AM, madeinquant notifications@github.com wrote:

Hi Abel, Thanks for you IB4m, I would download historical data but it is not as expected, would you comment what I have don't incorrectly.

Regards, Nelson.

addpath('IB4m') addpath('IB4m/docs') javaaddpath(fullfile(pwd, 'IB4m/Jar', 'TWS973.jar'))

% initialize session with TWS session = TWS.Session.getInstance();

% create local buffer for market data events with size 10,000 (default size is 32) [databuf,datalh] = TWS.initBufferForEvent(TWS.Events.MARKETDATA,10000);

% create local buffer for market metadata events with size 1000 [metabuf,metalh] = TWS.initBufferForEvent(TWS.Events.MARKETMETADATA,1000);

% connect to TWS session.eClientSocket.eConnect('127.0.0.1',7496,0);

% create contract object contract = com.ib.client.Contract(); contract.symbol('USD'); contract.secType('CASH'); contract.exchange('IDEALPRO'); contract.primaryExch('IDEALPRO'); contract.currency('JPY');

queryTime = datestr(datetime('now') - calmonths(6), 'yyyymmdd HH:MM:SS');

session.eClientSocket.reqHistoricalData(4001, contract, queryTime, '1 M', '1 min', 'MIDPOINT', 1, 1, false, []); pause(0.5);

if buf.size() > 0 hde = buf.get(); end

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/6#issuecomment-449745204, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1AJkZYNvyagHCQVGf7FRPpqm5GeAks5u8O93gaJpZM4NacD_.

madeinquant commented 5 years ago

Hi Abel, I got this error message. I have already subscribed to access forex data.

[buf,lh] = TWS.initBufferForEvent(TWS.Events.MARKETDATA,10000); hde = buf.get(); Java exception occurred: org.apache.commons.collections.BufferUnderflowException: The buffer is already empty at org.apache.commons.collections.buffer.BoundedFifoBuffer.get(BoundedFifoBuffer.java:261)

Happy Christmas.

Regards, Nelson.

softwarespartan commented 5 years ago

Thanks Nelson. Merry Christmas to you as well!

That means that no data was returned for your query. You can just call

if buf.size() < 0; e = buf.get(); end

to avoid the error message.

Typically, this happens when the contract specification is incorrectly configured. Additionally, please confirm that you have the associated data subscriptions for the data you are requesting

Sent from my iPhone

On Dec 24, 2018, at 8:01 PM, madeinquant notifications@github.com wrote:

Hi Abel, I got this error message.

[buf,lh] = TWS.initBufferForEvent(TWS.Events.MARKETDATA,10000); hde = buf.get(); Java exception occurred: org.apache.commons.collections.BufferUnderflowException: The buffer is already empty at org.apache.commons.collections.buffer.BoundedFifoBuffer.get(BoundedFifoBuffer.java:261)

Happy Christmas.

Regards, Nelson.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub, or mute the thread.

madeinquant commented 5 years ago

HI Abel, I checked that I have already subscribed to access FX data USD.JPY, I can retrieve realtime data in trader workstation.

Global IDEAL FX - Trader Workstation Fee Waived IDEAL FX - Trader Workstation Fee Waived