⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ereader.java

📁 TWS Source code for IB interface
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * EReader.java * */package com.ib.client;import java.io.DataInputStream;import java.io.IOException;public class EReader extends Thread {    // incoming msg id's    static final int TICK_PRICE		= 1;    static final int TICK_SIZE		= 2;    static final int ORDER_STATUS	= 3;    static final int ERR_MSG		= 4;    static final int OPEN_ORDER         = 5;    static final int ACCT_VALUE         = 6;    static final int PORTFOLIO_VALUE    = 7;    static final int ACCT_UPDATE_TIME   = 8;    static final int NEXT_VALID_ID      = 9;    static final int CONTRACT_DATA      = 10;    static final int EXECUTION_DATA     = 11;    static final int MARKET_DEPTH     	= 12;    static final int MARKET_DEPTH_L2    = 13;    static final int NEWS_BULLETINS    	= 14;    static final int MANAGED_ACCTS    	= 15;    static final int RECEIVE_FA    	    = 16;    static final int HISTORICAL_DATA    = 17;    static final int BOND_CONTRACT_DATA = 18;    static final int SCANNER_PARAMETERS = 19;    static final int SCANNER_DATA       = 20;    static final int TICK_OPTION_COMPUTATION = 21;    static final int TICK_GENERIC = 45;    static final int TICK_STRING = 46;    static final int TICK_EFP = 47;    static final int CURRENT_TIME = 49;    static final int REAL_TIME_BARS = 50;    private EClientSocket 	m_parent;    private DataInputStream m_dis;    protected EClientSocket parent()    { return m_parent; }    private EWrapper eWrapper()         { return (EWrapper)parent().wrapper(); }        public EReader( EClientSocket parent, DataInputStream dis) {        this("EReader", parent, dis);    }        protected EReader( String name, EClientSocket parent, DataInputStream dis) {        setName( name);        m_parent = parent;        m_dis = dis;    }    public void run() {        try {            // loop until thread is terminated            while( !isInterrupted() && processMsg(readInt()));        }        catch ( Exception ex ) {            parent().wrapper().error( ex);            //parent().wrapper().connectionClosed();            // closed connection would be reported anyway by the line below        }        m_parent.close();    }    /** Overridden in subclass. */    protected boolean processMsg(int msgId) throws IOException{        if( msgId == -1) return false;                switch( msgId) {            case TICK_PRICE: {                int version = readInt();                int tickerId = readInt();                int tickType = readInt();                double price = readDouble();                int size = 0;                if( version >= 2) {                    size = readInt();                }                int canAutoExecute = 0;                if (version >= 3) {                    canAutoExecute = readInt();                }                eWrapper().tickPrice( tickerId, tickType, price, canAutoExecute);                if( version >= 2) {                    int sizeTickType = -1 ; // not a tick                    switch (tickType) {                        case 1: // BID                            sizeTickType = 0 ; // BID_SIZE                            break ;                        case 2: // ASK                            sizeTickType = 3 ; // ASK_SIZE                            break ;                        case 4: // LAST                            sizeTickType = 5 ; // LAST_SIZE                            break ;                    }                    if (sizeTickType != -1) {                        eWrapper().tickSize( tickerId, sizeTickType, size);                    }                }                break;            }            case TICK_SIZE: {                int version = readInt();                int tickerId = readInt();                int tickType = readInt();                int size = readInt();                eWrapper().tickSize( tickerId, tickType, size);                break;            }            case TICK_OPTION_COMPUTATION: {                int version = readInt();                int tickerId = readInt();                int tickType = readInt();                double impliedVol = readDouble();            	if (impliedVol < 0) { // -1 is the "not yet computed" indicator            		impliedVol = Double.MAX_VALUE;            	}                double delta = readDouble();            	if (Math.abs(delta) > 1) { // -2 is the "not yet computed" indicator            		delta = Double.MAX_VALUE;            	}            	double modelPrice, pvDividend;            	if (tickType == TickType.MODEL_OPTION) { // introduced in version == 5            		modelPrice = readDouble();            		pvDividend = readDouble();            	} else {            		modelPrice = pvDividend = Double.MAX_VALUE;            	}                eWrapper().tickOptionComputation( tickerId, tickType, impliedVol, delta, modelPrice, pvDividend);            	break;            }            	            case TICK_GENERIC: {                int version = readInt();                int tickerId = readInt();                int tickType = readInt();                double value = readDouble();                eWrapper().tickGeneric( tickerId, tickType, value);                break;            }            case TICK_STRING: {                int version = readInt();                int tickerId = readInt();                int tickType = readInt();                String value = readStr();                eWrapper().tickString( tickerId, tickType, value);                break;            }            case TICK_EFP: {                int version = readInt();                int tickerId = readInt();                int tickType = readInt();                double basisPoints = readDouble();                String formattedBasisPoints = readStr();                double impliedFuturesPrice = readDouble();                int holdDays = readInt();                String futureExpiry = readStr();                double dividendImpact = readDouble();                double dividendsToExpiry = readDouble();                eWrapper().tickEFP( tickerId, tickType, basisPoints, formattedBasisPoints,                					impliedFuturesPrice, holdDays, futureExpiry, dividendImpact, dividendsToExpiry);                break;            }            	            case ORDER_STATUS: {                int version = readInt();                int id = readInt();                String status = readStr();                int filled = readInt();                int remaining = readInt();                double avgFillPrice = readDouble();                int permId = 0;                if( version >= 2) {                    permId = readInt();                }                int parentId = 0;                if( version >= 3) {                    parentId = readInt();                }                double lastFillPrice = 0;                if( version >= 4) {                    lastFillPrice = readDouble();                }                int clientId = 0;                if( version >= 5) {                    clientId = readInt();                }                                String whyHeld = null;                if( version >= 6) {                	whyHeld = readStr();                }                eWrapper().orderStatus( id, status, filled, remaining, avgFillPrice,                                permId, parentId, lastFillPrice, clientId, whyHeld);                break;            }            case ACCT_VALUE: {                int version = readInt();                String key = readStr();                String val  = readStr();                String cur = readStr();                String accountName = null ;                if( version >= 2) {                    accountName = readStr();                }                eWrapper().updateAccountValue(key, val, cur, accountName);                break;            }            case PORTFOLIO_VALUE: {                int version = readInt();                Contract contract = new Contract();                if (version >= 6) {                	contract.m_conId = readInt();                }                contract.m_symbol  = readStr();                contract.m_secType = readStr();                contract.m_expiry  = readStr();                contract.m_strike  = readDouble();                contract.m_right   = readStr();                contract.m_currency = readStr();                if ( version >= 2 ) {                    contract.m_localSymbol = readStr();                }                int position  = readInt();                double marketPrice = readDouble();                double marketValue = readDouble();                double  averageCost = 0.0;                double  unrealizedPNL = 0.0;                double  realizedPNL = 0.0;                if (version >=3 ) {                    averageCost = readDouble();                    unrealizedPNL = readDouble();                    realizedPNL = readDouble();                }                String accountName = null ;                if( version >= 4) {                    accountName = readStr();                }                eWrapper().updatePortfolio(contract, position, marketPrice, marketValue,                                averageCost, unrealizedPNL, realizedPNL, accountName);                break;            }            case ACCT_UPDATE_TIME: {                int version = readInt();                String timeStamp = readStr();                eWrapper().updateAccountTime(timeStamp);                break;            }            case ERR_MSG: {                int version = readInt();                if(version < 2) {                    String msg = readStr();                    m_parent.error( msg);                } else {                    int id = readInt();                    int errorCode    = readInt();                    String errorMsg = readStr();                    m_parent.error(id, errorCode, errorMsg);                }                break;            }            case OPEN_ORDER: {                // read version                int version = readInt();                // read order id                Order order = new Order();                order.m_orderId = readInt();                // read contract fields                Contract contract = new Contract();                if (version >= 17) {                	contract.m_conId = readInt();                }                contract.m_symbol = readStr();                contract.m_secType = readStr();                contract.m_expiry = readStr();                contract.m_strike = readDouble();                contract.m_right = readStr();                contract.m_exchange = readStr();                contract.m_currency = readStr();                if ( version >= 2 ) {                    contract.m_localSymbol = readStr();                }                // read order fields                order.m_action = readStr();                order.m_totalQuantity = readInt();                order.m_orderType = readStr();                order.m_lmtPrice = readDouble();                order.m_auxPrice = readDouble();                order.m_tif = readStr();                order.m_ocaGroup = readStr();                order.m_account = readStr();                order.m_openClose = readStr();                order.m_origin = readInt();                order.m_orderRef = readStr();                if(version >= 3) {                    order.m_clientId = readInt();                }                if( version >= 4 ) {                    order.m_permId = readInt();                    if ( version < 18) {                        // will never happen                    	/* order.m_ignoreRth = */ readBoolFromInt();                    }                    else {                    	order.m_outsideRth = readBoolFromInt();                    }                    order.m_hidden = readInt() == 1;                    order.m_discretionaryAmt = readDouble();                }                if ( version >= 5 ) {                    order.m_goodAfterTime = readStr();                }                if ( version >= 6 ) {                	// skip deprecated sharesAllocation field                    readStr();                }                if ( version >= 7 ) {                    order.m_faGroup = readStr();                    order.m_faMethod = readStr();                    order.m_faPercentage = readStr();                    order.m_faProfile = readStr();                }                if ( version >= 8 ) {                    order.m_goodTillDate = readStr();                }                if ( version >= 9) {                    order.m_rule80A = readStr();                    order.m_percentOffset = readDouble();                    order.m_settlingFirm = readStr();                    order.m_shortSaleSlot = readInt();                    order.m_designatedLocation = readStr();                    order.m_auctionStrategy = readInt();                    order.m_startingPrice = readDouble();                    order.m_stockRefPrice = readDouble();                    order.m_delta = readDouble();                    order.m_stockRangeLower = readDouble();                    order.m_stockRangeUpper = readDouble();                    order.m_displaySize = readInt();                    if ( version < 18) {                        // will never happen                    	/* order.m_rthOnly = */ readBoolFromInt();                    }                    order.m_blockOrder = readBoolFromInt();                    order.m_sweepToFill = readBoolFromInt();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -