softwarespartan / IB4m

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

Wrong Results Sorting by data #123

Closed Aerospace19 closed 3 years ago

Aerospace19 commented 3 years ago

Hello, I'm trying to get historical data. No problems or errors but I noticed that sorting by data always produces different data.

I mean there is no alignment between underlying price and index ( at least not always).

Look for example at this example run for 2 times.

It produces 2 different sorted data and following the wrong plot.

This is the code that I'm trying to execute:

`List of Top 100,50,25,10 Companies in the S&P500 name_Top100 = ["AAPL" "MSFT" "AMZN" "FB" "GOOGL" "GOOG" "TSLA" "JPM" "JNJ" "V" "NVDA" "UNH" "DIS" "HD" "PG" "MA" "PYPL" "BAC" "INTC" "CMCSA" "NFLX" "ADBE" "VZ" "XOM" "CSCO" "T" "ABT" "KO" "CRM" "PFE" "CVX" "AVGO" "PEP" "WMT" "MRK" "ABBV" "TMO" "ACN" "TXN" "MCD" "NKE" "WFC" "MDT" "COST" "QCOM" "HON" "NEE" "C" "LIN" "UNP" "DHR" "LLY" "AMGN" "LOW" "BA" "ORCL" "PM" "BMY" "SBUX" "AMAT" "CAT" "UPS" "IBM" "MS" "GE" "RTX" "DE" "GS" "BLK" "MMM" "INTU" "AMT" "MU" "NOW" "TGT" "AMD" "SCHW" "BKNG" "CVS" "AXP" "MO" "LRCX" "LMT" "FIS" "ISRG" "SPGI" "CHTR" "ANTM" "CI" "MDLZ" "TJX" "SYK" "GILD" "ADP" "PLD" "TFC" "USB" "GM" "ZTS"]; name_Top50 = ["AAPL" "MSFT" "AMZN" "FB" "GOOGL" "GOOG" "TSLA" "JPM" "JNJ" "V" "NVDA" "UNH" "DIS" "HD" "PG" "MA" "PYPL" "BAC" "INTC" "CMCSA" "NFLX" "ADBE" "VZ" "XOM" "CSCO" "T" "ABT" "KO" "CRM" "PFE" "CVX" "AVGO" "PEP" "WMT" "MRK" "ABBV" "TMO" "ACN" "TXN" "MCD" "NKE" "WFC" "MDT" "COST" "QCOM" "HON" "NEE" "C" "LIN"]; name_Top25 = ["AAPL" "MSFT" "AMZN" "FB" "GOOGL" "GOOG" "TSLA" "JPM" "JNJ" "V" "NVDA" "UNH" "DIS" "HD" "PG" "MA" "PYPL" "BAC" "INTC" "CMCSA" "NFLX" "ADBE" "VZ" "XOM"]; name_Top10 = ["AAPL" "MSFT" "AMZN" "FB" "GOOGL" "GOOG" "TSLA" "JPM" "JNJ"]; %Select the list ListSelected=name_Top10; Initialize session with Trader Workstation % initialize session with TWS session = TWS.Session.getInstance();

buf=cell(numel(ListSelected),1); lh=cell(numel(ListSelected),1);

hde=(cell(numel(ListSelected),1)); bars=(cell(numel(ListSelected),1));

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

Requesting historical data from IB/TWS and Process historical data event % create an empty stock contract for i=1:1:numel(ListSelected) [buf(i,1),lh(i,1)] = TWS.initBufferForEvent(TWS.Events.HISTORICALDATA); contract(i)=com.tws.ContractFactory.GenericStockContract(ListSelected(i)); % request historical data for our contract for a year w/ 1-day bars contract(i).secType("STK"); session.eClientSocket.reqHistoricalData(1000001,contract(i),'20210414 22:30:01','3 D','1 day','TRADES',1,1,false,[]); while(buf{i}.size()==0); pause(0.1); end hde{i} = buf{i}.get(); bars{i} = collection2cell(hde{i}.data); open(:,i) = cellfun(@(b)b.open,bars{i}); close(:,i) = cellfun(@(b)b.close,bars{i}); high(:,i) = cellfun(@(b)b.high,bars{i}); low(:,i) = cellfun(@(b)b.low,bars{i}); vol(:,i) = cellfun(@(b)b.volume,bars{i}); dt = datenum(cellfun(@(b)char(b.dtstr),bars{i},'UniformOutput',false),'yyyymmdd HH:MM:SS'); [~,indx] = sort(dt); end NumberDateSorted=sort(dt); DateSorted=datetime(NumberDateSorted,'ConvertFrom','datenum'); Organize Stocks Data [Open;High;Low;Close] to a Struct for i=1:1:numel(ListSelected) my_field = ListSelected(i); STOCKSDATA.(my_field) = [open(:,i) high(:,i) low(:,i) close(:,i)]; end`

Any idea?

Thanks in advance

1 2

Aerospace19 commented 3 years ago

the problem was that on each request there's a different sorting