IB Java API:提取多个合约的报价数据(实时条) [英] IB Java API: Extracting ticker data (real time bars) for multiple contracts

查看:231
本文介绍了IB Java API:提取多个合约的报价数据(实时条)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用算法交易和IB API进行一些自学和实验.我决定使用Java,但是我愿意切换到C ++.我浏览了一个在线教程,其中带您看了下面显示的代码,但想将其扩展到仅一支股票.我想浏览所有SP500的股票并检查股票行情数据以据此做出决定.

I'm doing some self-learning and experimentation with algorithmic trading and the IB API. I decided to use Java but I'm open to switching to C++. I went through an online tutorial that walks you through the code shown below but was wondering about extending it past just one stock. I want to go through all SP500 stocks and check ticker data to make decisions based on that.

下面的代码将为Microsoft创建合同并获取数据,但我想获取所有500只股票的数据.在EWrapper界面中定义的所有其他方法都被忽略,以提高可读性.

The code below will create a contract for and get data for Microsoft but I'd like to get data for all 500 stocks. All of the other methods defined in the EWrapper interface were left out of the post for more ease of readability.

我认为我需要将股票代码存储在文件中,进行解析,然后将每个合约一一添加到向量中.但是,我不确定此后如何监视数据.

I'm thinking that I need to store the ticker symbols in a file, parse this, and add each contract one by one to a vector. However, I'm not sure about how to monitor the data after that. It would be nice if I could just sequentially loop through each ticker and make a request for data but I believe the stream is processed on an asynchronous thread (correct me if wrong.)

那我该如何浏览所有500只股票并检查其股票行情数据?

代码段和解释将不胜感激.谢谢!

Code snippets and explanations would be appreciated. Thanks!

// Import Java utilities and Interactive Brokers API                                            
import java.util.Vector;
import com.ib.client.Contract;
import com.ib.client.ContractDetails;
import com.ib.client.EClientSocket;
import com.ib.client.EWrapper;
import com.ib.client.Execution;
import com.ib.client.Order;
import com.ib.client.OrderState;
import com.ib.client.TagValue;
import com.ib.client.CommissionReport;
import com.ib.client.UnderComp;

// RealTimeBars Class is an implementation of the                                               
// IB API EWrapper class                                                                        
public class RealTimeBars implements EWrapper
{
    // Keep track of the next ID                                                                
    private int nextOrderID = 0;
    // The IB API Client Socket object                                                          
    private EClientSocket client = null;

    public RealTimeBars ()
    {
        // Create a new EClientSocket object                                                    
        client = new EClientSocket (this);
        // Connect to the TWS or IB Gateway application                                         
        // Leave null for localhost                                                             
    // Port Number (should match TWS/IB Gateway configuration                               
        client.eConnect (null, 7496, 0);

        // Pause here for connection to complete                                                
    try
            {
                // Thread.sleep (1000);                                                         
                while (! (client.isConnected()));
            } catch (Exception e) {
            e.printStackTrace ();

        };
        // Create a new contract                                                                
        Contract contract = new Contract ();
        contract.m_symbol = "MSFT";
        contract.m_exchange = "SMART";
        contract.m_secType = "STK";
    contract.m_primaryExch = "NASDAQ";
        contract.m_currency = "USD";
        // Create a TagValue list                                                               
        Vector<TagValue> realTimeBarsOptions = new Vector<TagValue>();
        // Make a call to start off data retrieval                                              
        client.reqRealTimeBars(0, contract,
                               5,            // Bar Size 5 seconds                              
                               "TRADES",     // whatToShow                                      
                               false,         // useRTH                                         
                               realTimeBarsOptions);
        // At this point our call is done and any market data events                            
        // will be returned via the realtimeBar method                                          

    } 

public static void main (String args[])
{
    try
        {
            // Create an instance                                                           
            // At this time a connection will be made                                       
    // and the request for market data will happen                                  
            RealTimeBars myData = new RealTimeBars();
        }
    catch (Exception e)
        {
            e.printStackTrace ();
        }
}    

}

推荐答案

我不知道这对所有500个都有效,但是您可以尝试.数据来自 https://raw .githubusercontent.com/datasets/s-and-p-500-companies/master/data/constituents.csv SP

I don't know how this will work for all 500, but you can try. The data is from https://raw.githubusercontent.com/datasets/s-and-p-500-companies/master/data/constituents.csv SP

package sp;

import com.ib.client.Contract;
import com.ib.client.EClientSocket;
import com.ib.client.EWrapper;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class SP {
    //just a sample, like this so you can just use Files.lines instead.
    private static List<String> lines = Arrays.asList(new String[]{
        "Symbol,Name,Sector",
        "MMM,3M Company,Industrials",
        "ABT,Abbott Laboratories,Health Care",
        "ABBV,AbbVie,Health Care",
        "ACN,Accenture plc,Information Technology",
        "ATVI,Activision Blizzard,Information Technology",
        "AYI,Acuity Brands Inc,Industrials",
        "ADBE,Adobe Systems Inc,Information Technology",
        "AAP,Advance Auto Parts,Consumer Discretionary",
        "AES,AES Corp,Utilities",
        "AET,Aetna Inc,Health Care",
        "AMG,Affiliated Managers Group Inc,Financials",
        "AFL,AFLAC Inc,Financials",
        "A,Agilent Technologies Inc,Health Care",
        "APD,Air Products & Chemicals Inc,Materials",
        "AKAM,Akamai Technologies Inc,Information Technology",
    });


    public static void main(String[] args) throws InterruptedException{
        EWrapper wrapper = new  Wrapper();
        EClientSocket socket = new EClientSocket(wrapper);
        socket.eConnect("", 4001, 123);
        //supposedly gives frozen last recorded value, not working!
        socket.reqMarketDataType(2);

        AtomicInteger tickerId = new AtomicInteger(0);
        lines.stream().skip(1).forEach(line -> {
            //new cont for every request
            Contract cont = new Contract();
            cont.m_currency = "usd";
            cont.m_exchange = "smart";
            cont.m_secType = "stk";
            cont.m_symbol = line.split(",")[0];
            Data data = new Data(cont, socket);
        });

        //need you own logic for when to end program
        //Thread.sleep(5000);//this thread, Socket starts a reader thread
        //socket.eDisconnect();
    }
}

包装器

package sp;

import com.ib.client.CommissionReport;
import com.ib.client.Contract;
import com.ib.client.ContractDetails;
import com.ib.client.EWrapper;
import com.ib.client.Execution;
import com.ib.client.Order;
import com.ib.client.OrderState;
import com.ib.client.TickType;
import com.ib.client.UnderComp;
import java.util.HashMap;
import java.util.Map;

public class Wrapper  implements EWrapper{
    public Map<Integer, Data> dataMap = new HashMap<>();
    public Map<Integer, Strat> orderMap = new HashMap<>();

    //reqMktData snapshots are received here
    @Override
    public void tickPrice(int tickerId, int field, double price, int canAutoExecute) {
        if (field == TickType.LAST) {
            //if you just want the last price
            dataMap.get(tickerId).dataRecd(price);
        }
    } 

    @Override
    public void execDetails(int reqId, Contract contract, Execution execution) {
        orderMap.get(execution.m_orderId).exec(execution);
    }
//snip
}

数据

package sp;

import com.ib.client.Contract;
import com.ib.client.EClientSocket;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class Data {
            final Contract cont;
    private final EClientSocket socket;
    private final Strat strat;

    private static int nextId = 1; //auto increment for each request
    private final int myId;

    List<Double> prices = new ArrayList<>();
    double lastPrice = -1;

    public Data(Contract cont, EClientSocket socket) {
        this.cont = cont;
        this.socket = socket;
        strat = new Strat(this, socket);
        myId = nextId++;
        ((Wrapper) socket.wrapper()).dataMap.put(myId, this);
        reqData();
//        //call every 10 min
//        Timer timer = new Timer();
//        timer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                reqData();
//            }
//        }, 10 * 60 * 1000);
    }

    private void reqData(){
        socket.reqMktData(myId, cont, "", false /* true */, null);
    }

    public void dataRecd(double last){
        lastPrice = last;
        prices.add(last);
        strat.check();
    }
}

策略

package sp;

import com.ib.client.EClientSocket;
import com.ib.client.Execution;

public class Strat {
    public static final int NULL=0, LOOK=1<<0, LONG=1<<1, SHORT=1<<2, WAIT_FILL=1<<3, WAIT_CANCEL=1<<4;
    public int sysState = NULL;
    private final Data data;
    private final EClientSocket socket;

    private static int nextOrderId = 1;

    Strat(Data data, EClientSocket socket) {
        this.data = data;
        this.socket = socket;
        sysState = LOOK;
    }

    void check() {
        System.out.println("should I buy? "+ data.cont.m_symbol + " @ " + data.lastPrice);
        /*if (false && sysState & LOOK == LOOK) {
            ((Wrapper) socket.wrapper()).orderMap.put(nextOrderId, this);
            socket.placeOrder(nextOrderId++, data.cont, new Order());
            sysState = WAIT_FILL;
            nextOrderId++;
        }*/
    }

    public void exec(Execution exec){
        //will be called by wrapper after an exec.
        //sysState = LONG; //or whatever
    }
}

这篇关于IB Java API:提取多个合约的报价数据(实时条)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆