📄 eclientsocket.java
字号:
? Double.MAX_VALUE : order.m_stockRangeLower; double upper = (m_serverVersion == 26 && order.m_orderType.equals("VOL")) ? Double.MAX_VALUE : order.m_stockRangeUpper; sendMax( lower); sendMax( upper); } if (m_serverVersion >= 22) { send( order.m_overridePercentageConstraints); } if (m_serverVersion >= 26) { // Volatility orders sendMax( order.m_volatility); sendMax( order.m_volatilityType); if (m_serverVersion < 28) { send( order.m_deltaNeutralOrderType.equalsIgnoreCase("MKT")); } else { send( order.m_deltaNeutralOrderType); sendMax( order.m_deltaNeutralAuxPrice); } send( order.m_continuousUpdate); if (m_serverVersion == 26) { // Volatility orders had specific watermark price attribs in server version 26 double lower = order.m_orderType.equals("VOL") ? order.m_stockRangeLower : Double.MAX_VALUE; double upper = order.m_orderType.equals("VOL") ? order.m_stockRangeUpper : Double.MAX_VALUE; sendMax( lower); sendMax( upper); } sendMax( order.m_referencePriceType); } if (m_serverVersion >= 30) { // TRAIL_STOP_LIMIT stop price sendMax( order.m_trailStopPrice); } if (m_serverVersion >= MIN_SERVER_VER_SCALE_ORDERS) { sendMax (order.m_scaleNumComponents); sendMax (order.m_scaleComponentSize); sendMax (order.m_scalePriceIncrement); } if (m_serverVersion >= MIN_SERVER_VER_PTA_ORDERS) { send (order.m_clearingAccount); send (order.m_clearingIntent); } if (m_serverVersion >= MIN_SERVER_VER_WHAT_IF_ORDERS) { send (order.m_whatIf); } } catch( Exception e) { error( id, EClientErrors.FAIL_SEND_ORDER, "" + e); close(); } } public synchronized void reqAccountUpdates(boolean subscribe, String acctCode) { // not connected? if( !m_connected) { error( EClientErrors.NO_VALID_ID, EClientErrors.NOT_CONNECTED, ""); return; } final int VERSION = 2; // send cancel order msg try { send( REQ_ACCOUNT_DATA ); send( VERSION); send( subscribe); // Send the account code. This will only be used for FA clients if ( m_serverVersion >= 9 ) { send( acctCode); } } catch( Exception e) { error( EClientErrors.NO_VALID_ID, EClientErrors.FAIL_SEND_ACCT, "" + e); close(); } } public synchronized void reqExecutions(ExecutionFilter filter) { // not connected? if( !m_connected) { error( EClientErrors.NO_VALID_ID, EClientErrors.NOT_CONNECTED, ""); return; } final int VERSION = 2; // send cancel order msg try { send( REQ_EXECUTIONS); send( VERSION); // Send the execution rpt filter data if ( m_serverVersion >= 9 ) { send( filter.m_clientId); send( filter.m_acctCode); // Note that the valid format for m_time is "yyyymmdd-hh:mm:ss" send( filter.m_time); send( filter.m_symbol); send( filter.m_secType); send( filter.m_exchange); send( filter.m_side); } } catch( Exception e) { error( EClientErrors.NO_VALID_ID, EClientErrors.FAIL_SEND_EXEC, "" + e); close(); } } public synchronized void cancelOrder( int id) { // not connected? if( !m_connected) { error( EClientErrors.NO_VALID_ID, EClientErrors.NOT_CONNECTED, ""); return; } final int VERSION = 1; // send cancel order msg try { send( CANCEL_ORDER); send( VERSION); send( id); } catch( Exception e) { error( id, EClientErrors.FAIL_SEND_CORDER, "" + e); close(); } } public synchronized void reqOpenOrders() { // not connected? if( !m_connected) { error( EClientErrors.NO_VALID_ID, EClientErrors.NOT_CONNECTED, ""); return; } final int VERSION = 1; // send cancel order msg try { send( REQ_OPEN_ORDERS); send( VERSION); } catch( Exception e) { error(EClientErrors.NO_VALID_ID, EClientErrors.FAIL_SEND_OORDER, "" + e); close(); } } public synchronized void reqIds( int numIds) { // not connected? if( !m_connected) { error( EClientErrors.NO_VALID_ID, EClientErrors.NOT_CONNECTED, ""); return; } final int VERSION = 1; try { send( REQ_IDS); send( VERSION); send( numIds); } catch( Exception e) { error( EClientErrors.NO_VALID_ID, EClientErrors.FAIL_SEND_CORDER, "" + e); close(); } } public synchronized void reqNewsBulletins( boolean allMsgs) { // not connected? if( !m_connected) { error( EClientErrors.NO_VALID_ID, EClientErrors.NOT_CONNECTED, ""); return; } final int VERSION = 1; try { send( REQ_NEWS_BULLETINS); send( VERSION); send( allMsgs); } catch( Exception e) { error( EClientErrors.NO_VALID_ID, EClientErrors.FAIL_SEND_CORDER, "" + e); close(); } } public synchronized void cancelNewsBulletins() { // not connected? if( !m_connected) { error( EClientErrors.NO_VALID_ID, EClientErrors.NOT_CONNECTED, ""); return; } final int VERSION = 1; // send cancel order msg try { send( CANCEL_NEWS_BULLETINS); send( VERSION); } catch( Exception e) { error( EClientErrors.NO_VALID_ID, EClientErrors.FAIL_SEND_CORDER, "" + e); close(); } } public synchronized void setServerLogLevel(int logLevel) { // not connected? if( !m_connected) { error( EClientErrors.NO_VALID_ID, EClientErrors.NOT_CONNECTED, ""); return; } final int VERSION = 1; // send the set server logging level message try { send( SET_SERVER_LOGLEVEL); send( VERSION); send( logLevel); } catch( Exception e) { error( EClientErrors.NO_VALID_ID, EClientErrors.FAIL_SEND_SERVER_LOG_LEVEL, "" + e); close(); } } public synchronized void reqAutoOpenOrders(boolean bAutoBind) { // not connected? if( !m_connected) { error( EClientErrors.NO_VALID_ID, EClientErrors.NOT_CONNECTED, ""); return; } final int VERSION = 1; // send req open orders msg try { send( REQ_AUTO_OPEN_ORDERS); send( VERSION); send( bAutoBind); } catch( Exception e) { error(EClientErrors.NO_VALID_ID, EClientErrors.FAIL_SEND_OORDER, "" + e); close(); } } public synchronized void reqAllOpenOrders() { // not connected? if( !m_connected) { error( EClientErrors.NO_VALID_ID, EClientErrors.NOT_CONNECTED, ""); return; } final int VERSION = 1; // send req all open orders msg try { send( REQ_ALL_OPEN_ORDERS); send( VERSION); } catch( Exception e) { error(EClientErrors.NO_VALID_ID, EClientErrors.FAIL_SEND_OORDER, "" + e); close(); } } public synchronized void reqManagedAccts() { // not connected? if( !m_connected) { error( EClientErrors.NO_VALID_ID, EClientErrors.NOT_CONNECTED, ""); return; } final int VERSION = 1; // send req FA managed accounts msg try { send( REQ_MANAGED_ACCTS); send( VERSION); } catch( Exception e) { error(EClientErrors.NO_VALID_ID, EClientErrors.FAIL_SEND_OORDER, "" + e); close(); } } public synchronized void requestFA( int faDataType ) { // not connected? if( !m_connected) { error( EClientErrors.NO_VALID_ID, EClientErrors.NOT_CONNECTED, ""); return; } // This feature is only available for versions of TWS >= 13 if( m_serverVersion < 13) { error(EClientErrors.NO_VALID_ID, EClientErrors.UPDATE_TWS.code(), EClientErrors.UPDATE_TWS.msg()); return; } final int VERSION = 1; try { send( REQ_FA ); send( VERSION); send( faDataType); } catch( Exception e) { error( faDataType, EClientErrors.FAIL_SEND_FA_REQUEST, "" + e); close(); } } public synchronized void replaceFA( int faDataType, String xml ) { // not connected? if( !m_connected) { error( EClientErrors.NO_VALID_ID, EClientErrors.NOT_CONNECTED, ""); return; } // This feature is only available for versions of TWS >= 13 if( m_serverVersion < 13) { error(EClientErrors.NO_VALID_ID, EClientErrors.UPDATE_TWS.code(), EClientErrors.UPDATE_TWS.msg()); return; } final int VERSION = 1; try { send( REPLACE_FA ); send( VERSION); send( faDataType); send( xml); } catch( Exception e) { error( faDataType, EClientErrors.FAIL_SEND_FA_REPLACE, "" + e); close(); } } public synchronized void reqCurrentTime() { // not connected? if( !m_connected) { error( EClientErrors.NO_VALID_ID, EClientErrors.NOT_CONNECTED, ""); return; } // This feature is only available for versions of TWS >= 33 if( m_serverVersion < 33) { error(EClientErrors.NO_VALID_ID, EClientErrors.UPDATE_TWS, " It does not support current time requests."); return; } final int VERSION = 1; try { send( REQ_CURRENT_TIME ); send( VERSION); } catch( Exception e) { error( EClientErrors.NO_VALID_ID, EClientErrors.FAIL_SEND_REQCURRTIME, "" + e); close(); } } protected synchronized void error( String err) { m_anyWrapper.error( err); } protected synchronized void error( int id, int errorCode, String errorMsg) { m_anyWrapper.error( id, errorCode, errorMsg); } protected void close() { eDisconnect(); m_anyWrapper.connectionClosed(); } private static boolean is( String str) { // return true if the string is not empty return str != null && str.length() > 0; } private static boolean isNull( String str) { // return true if the string is null or empty return !is( str); } private void error(int id, EClientErrors.CodeMsgPair pair, String tail) { error(id, pair.code(), pair.msg() + tail); } protected void send( String str) throws IOException { // write string to data buffer; writer thread will // write it to socket if( !IsEmpty(str)) { m_dos.write( str.getBytes() ); } sendEOL(); } private void sendEOL() throws IOException { m_dos.write( EOL); } protected void send( int val) throws IOException { send( String.valueOf( val) ); } protected void send( char val) throws IOException { m_dos.write( val); sendEOL(); } protected void send( double val) throws IOException { send( String.valueOf( val) ); } protected void send( long val) throws IOException { send( String.valueOf( val) ); } private void sendMax( double val) throws IOException { if (val == Double.MAX_VALUE) { sendEOL(); } else { send(String.valueOf(val)); } } private void sendMax( int val) throws IOException { if (val == Integer.MAX_VALUE) { sendEOL(); } else { send(String.valueOf(val)); } } protected void send( boolean val) throws IOException { send( val ? 1 : 0); } private static boolean IsEmpty(String str) { return str == null || str.length() == 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -