java interactive brokers api 历史数据和报价

java interactive brokers api historical data and tick

brian 能够在 9.72 上得到一个这样的工作示例,稍作修改它可能可以在 9.73 中 运行。问题很简单:查看当前价格并与前一天的高点和低点进行比较。如果价格高于它打印出买入,如果低于前一交易日的历史低点它会打印出卖出。起初,由于我对 Java 的了解不足,我很挣扎,但布赖恩在他的最佳答案中展示了如何使用当前价格和历史价格进行比较。非常简陋,但很重要。

这是当前代码,应该采用当前价格并将其与前一交易日进行比较,如果高于高价则给出买入打印,如果低于低价则给出卖出打印。在 9.72 API 中,这段代码什么也没做,因为它没有正确连接数据类型。

    package apidemo;

    import java.io.IOException;
    import java.util.Set;
    import ib.client.TickType;
    import ib.client.CommissionReport;
    import ib.client.Contract;
    import ib.client.ContractDetails;
    import ib.client.DeltaNeutralContract;
    import ib.client.EClientSocket;
    import ib.client.EJavaSignal;
    import ib.client.EReader;
    import ib.client.EWrapper;
    import ib.client.Execution;
    import ib.client.Order;
    import ib.client.OrderState;
    import ib.client.SoftDollarTier;
    import ib.client.TagValue;
    import java.util.Vector;
    import com.ib.client.*;
    import com.ib.contracts.StkContract;
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Set;

  public class xxx implements EWrapper {
    EJavaSignal m_signal = new EJavaSignal();
    EClientSocket m_s = new EClientSocket(this, m_signal);
    // Keep track of the next ID
    private int nextOrderID = 0;
    // The IB API Client Socket object
    private EClientSocket client = null;
  private double high = Double.MAX_VALUE;
    private double low = -Double.MAX_VALUE;
    public static void main(String[] args) {
        new xxx().run();

    }






    private void run() {
        m_s.eConnect("localhost", 7497, 0);

        final EReader reader = new EReader(m_s, m_signal);

        reader.start();

        new Thread() {
            public void run() {
                while (m_s.isConnected()) {
                    m_signal.waitForSignal();
                    try {
                        javax.swing.SwingUtilities
                                .invokeAndWait(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            reader.processMsgs();
                                        } catch (IOException e) {
                                            error(e);
                                        }
                                    }
                                });
                    } catch (Exception e) {
                        error(e);
                    }
                }
            }
        }.start();
        Contract contract = new Contract ();
                 Order order = new Order();
                  m_s.reqMarketDataType(3);
                  //contract.m_localSymbol = "ESM7";

    }


    @Override public void nextValidId(int orderId) {
             System.out.println("id "+orderId);
        nextOrderID = orderId;
        Contract c = new Contract ();
          //contract.m_localSymbol = "ESM7";
        c.m_symbol = "EUR";
        c.m_exchange = "IDEALPRO";

        c.m_secType = "CASH";
        c.m_currency = "USD";
        m_s.reqHistoricalData(1, c, 
                LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE)+ " 16:00:00",
                "2 D", "1 day", "MIDPOINT", 1, 1, null);
        m_s.reqMktData(1, c, "", false, null);

    }
   public void historicalData(int reqId, String date, double open, double high, double low, double close, int volume, int count, double WAP, boolean hasGaps) {
      System.out.println("date xxx"+date);
        this.high = high;
        this.low = low;
        System.out.println(high);
         System.out.println(low);
          System.out.println("xxx");
        }
 public void tickPrice(int orderId, int field, double price,
            int canAutoExecute)
    {
        System.out.println(high);
         System.out.println(low);
         System.out.println("idx: "+orderId + " " + TickType.getField(field) + " pricex: "+price);
       if (field == TickType.LAST.index()){
            if (price > high) {
                System.out.println("buy");
            }
            if (price < low){
                System.out.println("sell");
            }
        }   
    }
        @Override public void connectionClosed() {
        }


    }

此代码目前输出如下:

id 1836254
date xxx20170328
1.08727
1.079905
xxx
date xxx20170329
1.08267
1.073995
xxx
date xxxfinished-20170327  16:00:00-20170329  16:00:00
-1.0
-1.0
xxx
-1.0
-1.0
idx: 1 bidPrice pricex: 1.07658
-1.0
-1.0
idx: 1 askPrice pricex: 1.07659
-1.0
-1.0

请注意,每日最高点和最低点显示了两个时段,这真是太神奇了。但是当试图在 tickprice 中使用那些高点和低点时,它不会被记住并且 returns -1.

我从你的代码中删除了一堆东西并添加了需要的部分。连接后等待 nextValidId 开始请求东西。获取历史并设置高点和低点。获取 mktData 并检查最后价格,buy/sell 根据需要。

import com.ib.client.*;
import com.ib.contracts.StkContract;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Set;

public class Test implements EWrapper {
    EJavaSignal m_signal = new EJavaSignal();
    EClientSocket m_client = new EClientSocket(this, m_signal);

    private int nextOrderID = 0;
    private double high = Double.MAX_VALUE;
    private double low = -Double.MAX_VALUE;

    public static void main(String[] args) {
        new Test.run();
    }

    private void run() {
        m_client.eConnect("localhost", 7497, 123);
        final EReader reader = new EReader(m_client, m_signal);
        reader.start();
        new Thread() {
            @Override
            public void run() {
                while (m_client.isConnected()) {
                    m_signal.waitForSignal();
                    try {
                        reader.processMsgs();
                    } catch (Exception e) {
                        System.out.println("Exception: " + e.getMessage());
                    }
                }
            }
        }.start();
    }

    @Override
    public void nextValidId(int orderId) {
        System.out.println("id "+orderId);
        nextOrderID = orderId;
        Contract c = new StkContract("IBKR");
        m_client.reqHistoricalData(1, c, 
                LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE)+ " 16:00:00",
                "2 D", "1 day", "TRADES", 1, 1, null);
        m_client.reqMktData(1, c, "", false, null);
    }

    @Override
    public void error(int id, int errorCode, String errorMsg) {
        System.out.println(id + " " + errorCode + " " + errorMsg);
    }

    @Override
    public void historicalData(int reqId, String date, double open, double high, double low, double close, int volume, int count, double WAP, boolean hasGaps) {
        //if being run on the next calendar day, this works
        if (LocalDate.now().minusDays(1).format(DateTimeFormatter.BASIC_ISO_DATE).equals(date)){
            this.high = high;
            this.low = low;
            System.out.println(date + " h: " + high + " l: " +low);
        }
    }

    @Override
        public void tickPrice(int tickerId, int field, double price, int canAutoExecute) {

        System.out.println("id: "+tickerId + " " + TickType.getField(field) + " price: "+price);
        if (field == TickType.LAST.index()){
            if (price > high) {
                System.out.println("buy");
            }
            if (price < low){
                System.out.println("sell");
            }
        }

    }
 //impl rest of EWrapper
}

输出

id 130002
-1 2104 Market data farm connection is OK:usfuture
-1 2104 Market data farm connection is OK:cashfarm
-1 2104 Market data farm connection is OK:cafarm
-1 2104 Market data farm connection is OK:usfarm
-1 2106 HMDS data farm connection is OK:ushmds
date 20170328
date 20170329
date finished-20170327  16:00:00-20170329  16:00:00
id: 1 bidPrice price: 34.74
id: 1 askPrice price: 34.82
id: 1 lastPrice price: 34.73
buy
id: 1 high price: 34.87
id: 1 low price: 34.56