Skip to content

Commit 3b413a2

Browse files
适配 BrokerApi 更新将 Security 改名为 SecurityInfo
1 parent 189a219 commit 3b413a2

File tree

5 files changed

+19
-16
lines changed

5 files changed

+19
-16
lines changed

lib/src/main/kotlin/org/rationalityfrontline/ktrader/broker/ctp/CtpBrokerApi.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ class CtpBrokerApi(config: Map<String, Any>, kEvent: KEvent) : BrokerApi(config,
7676
return runWithRetry({ tdApi.queryLastTick(code, useCache, extras) })
7777
}
7878

79-
override suspend fun querySecurity(code: String, useCache: Boolean, extras: Map<String, Any>?): Security? {
79+
override suspend fun querySecurity(code: String, useCache: Boolean, extras: Map<String, Any>?): SecurityInfo? {
8080
return runWithRetry({ tdApi.queryInstrument(code, useCache, extras) })
8181
}
8282

83-
override suspend fun queryAllSecurities(useCache: Boolean, extras: Map<String, Any>?): List<Security> {
83+
override suspend fun queryAllSecurities(useCache: Boolean, extras: Map<String, Any>?): List<SecurityInfo> {
8484
return runWithRetry({ tdApi.queryAllInstruments(useCache, extras) })
8585
}
8686

lib/src/main/kotlin/org/rationalityfrontline/ktrader/broker/ctp/CtpBrokerInfo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package org.rationalityfrontline.ktrader.broker.ctp
33
import org.rationalityfrontline.jctp.CThostFtdcTraderApi
44

55
object CtpBrokerInfo {
6-
val name: String = "CTP"
6+
const val name: String = "CTP"
77
val version: String = CThostFtdcTraderApi.GetApiVersion()
88
val configKeys: List<Pair<String, String>> = listOf(
99
Pair("mdFronts", "List<String> 行情前置"),

lib/src/main/kotlin/org/rationalityfrontline/ktrader/broker/ctp/CtpTdApi.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ internal class CtpTdApi(val config: CtpConfig, val kEvent: KEvent, val sourceId:
8484
/**
8585
* 缓存的合约信息,key 为合约 code
8686
*/
87-
val instruments: MutableMap<String, Security> = mutableMapOf()
87+
val instruments: MutableMap<String, SecurityInfo> = mutableMapOf()
8888
/**
8989
* 品种代码表,key 为合约 code,value 为品种代码(productId)。用于从 code 快速映射到 [productStatusMap]
9090
*/
@@ -438,7 +438,7 @@ internal class CtpTdApi(val config: CtpConfig, val kEvent: KEvent, val sourceId:
438438
/**
439439
* 查询某一特定合约的信息。[extras.queryFee: Boolean = false]【是否查询保证金率及手续费率,如果之前没查过,可能会耗时。当 useCache 为 false 时无效】
440440
*/
441-
suspend fun queryInstrument(code: String, useCache: Boolean = true, extras: Map<String, Any>? = null): Security? {
441+
suspend fun queryInstrument(code: String, useCache: Boolean = true, extras: Map<String, Any>? = null): SecurityInfo? {
442442
if (useCache) {
443443
val cachedInstrument = instruments[code]
444444
if (cachedInstrument != null) {
@@ -464,13 +464,13 @@ internal class CtpTdApi(val config: CtpConfig, val kEvent: KEvent, val sourceId:
464464
/**
465465
* 查询全市场合约的信息
466466
*/
467-
suspend fun queryAllInstruments(useCache: Boolean = true, extras: Map<String, Any>? = null): List<Security> {
467+
suspend fun queryAllInstruments(useCache: Boolean = true, extras: Map<String, Any>? = null): List<SecurityInfo> {
468468
if (useCache && instruments.isNotEmpty()) return instruments.values.toList()
469469
val qryField = CThostFtdcQryInstrumentField()
470470
val requestId = nextRequestId()
471471
return runWithResultCheck({ tdApi.ReqQryInstrument(qryField, requestId) }, {
472472
suspendCoroutineWithTimeout(TIMEOUT_MILLS * 2) { continuation ->
473-
requestMap[requestId] = RequestContinuation(requestId, continuation, data = mutableListOf<Security>())
473+
requestMap[requestId] = RequestContinuation(requestId, continuation, data = mutableListOf<SecurityInfo>())
474474
}
475475
})
476476
}
@@ -867,7 +867,7 @@ internal class CtpTdApi(val config: CtpConfig, val kEvent: KEvent, val sourceId:
867867
/**
868868
* 获取缓存的期货/期权手续费率,如果没有,则查询后再获取
869869
*/
870-
private fun getOrQueryCommissionRate(instrument: Security): CommissionRate? {
870+
private fun getOrQueryCommissionRate(instrument: SecurityInfo): CommissionRate? {
871871
if (config.disableFeeCalculation) return null
872872
if (instrument.commissionRate == null) {
873873
runBlocking { prepareFeeCalculation(instrument.code, false) }
@@ -878,7 +878,7 @@ internal class CtpTdApi(val config: CtpConfig, val kEvent: KEvent, val sourceId:
878878
/**
879879
* 获取缓存的期货保证金率,如果没有,则查询后再获取
880880
*/
881-
private fun getOrQueryMarginRate(instrument: Security): MarginRate? {
881+
private fun getOrQueryMarginRate(instrument: SecurityInfo): MarginRate? {
882882
if (config.disableFeeCalculation) return null
883883
if (instrument.marginRate == null) {
884884
runBlocking { prepareFeeCalculation(instrument.code, false) }
@@ -991,7 +991,7 @@ internal class CtpTdApi(val config: CtpConfig, val kEvent: KEvent, val sourceId:
991991
/**
992992
* 计算期货保证金
993993
*/
994-
private fun calculateFuturesMargin(instrument: Security, direction: Direction, yesterdayVolume: Int, todayVolume: Int, avgOpenPrice: Double, fallback: Double): Double {
994+
private fun calculateFuturesMargin(instrument: SecurityInfo, direction: Direction, yesterdayVolume: Int, todayVolume: Int, avgOpenPrice: Double, fallback: Double): Double {
995995
if (yesterdayVolume + todayVolume == 0) return 0.0
996996
val marginRate = getOrQueryMarginRate(instrument) ?: return fallback
997997
val (tick, isLatestTick) = getOrQueryTick(instrument.code)
@@ -1032,7 +1032,7 @@ internal class CtpTdApi(val config: CtpConfig, val kEvent: KEvent, val sourceId:
10321032
/**
10331033
* 计算期权保证金
10341034
*/
1035-
private fun calculateOptionsMargin(instrument: Security, direction: Direction, volume: Int, avgOpenPrice: Double, fallback: Double, isOpen: Boolean): Double {
1035+
private fun calculateOptionsMargin(instrument: SecurityInfo, direction: Direction, volume: Int, avgOpenPrice: Double, fallback: Double, isOpen: Boolean): Double {
10361036
if (volume == 0) return 0.0
10371037
when (direction) {
10381038
Direction.LONG -> { // 买方
@@ -1986,7 +1986,7 @@ internal class CtpTdApi(val config: CtpConfig, val kEvent: KEvent, val sourceId:
19861986
checkRspInfo(pRspInfo, {
19871987
// 如果是查询单个合约
19881988
if (reqData is String) {
1989-
val con = request.continuation as Continuation<Security?>
1989+
val con = request.continuation as Continuation<SecurityInfo?>
19901990
if (instrument == null) {
19911991
con.resume(null)
19921992
requestMap.remove(nRequestID)
@@ -2002,10 +2002,10 @@ internal class CtpTdApi(val config: CtpConfig, val kEvent: KEvent, val sourceId:
20022002
}
20032003
}
20042004
} else { // 如果是查询多个合约
2005-
val insList = request.data as MutableList<Security>
2005+
val insList = request.data as MutableList<SecurityInfo>
20062006
if (instrument != null) insList.add(instrument)
20072007
if (bIsLast) {
2008-
(request.continuation as Continuation<List<Security>>).resume(insList)
2008+
(request.continuation as Continuation<List<SecurityInfo>>).resume(insList)
20092009
requestMap.remove(nRequestID)
20102010
}
20112011
}

lib/src/main/kotlin/org/rationalityfrontline/ktrader/broker/ctp/Translator.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import java.time.format.DateTimeFormatter
1010
/**
1111
* 翻译器,用于将本地的 CTP 信息翻译为标准的 BrokerApi 信息
1212
*/
13+
@Suppress("MemberVisibilityCanBePrivate")
1314
internal object Translator {
1415

1516
private val THOST_FTDC_OF_Open_S = jctpConstants.THOST_FTDC_OF_Open.toString()
@@ -110,6 +111,7 @@ internal object Translator {
110111
/**
111112
* 行情推送的 [Tick] 中很多字段可能是无效值,CTP 内用 [Double.MAX_VALUE] 表示,在此需要统一为 0.0
112113
*/
114+
@Suppress("NOTHING_TO_INLINE")
113115
private inline fun formatDouble(input: Double): Double {
114116
return if (input == Double.MAX_VALUE) 0.0 else input
115117
}
@@ -125,7 +127,7 @@ internal object Translator {
125127
}
126128
}
127129

128-
fun securityC2A(insField: CThostFtdcInstrumentField, onTimeParseError: (Exception) -> Unit): Security? {
130+
fun securityC2A(insField: CThostFtdcInstrumentField, onTimeParseError: (Exception) -> Unit): SecurityInfo? {
129131
return try {
130132
val type = when (insField.productClass) {
131133
jctpConstants.THOST_FTDC_PC_Futures -> SecurityType.FUTURES
@@ -134,7 +136,7 @@ internal object Translator {
134136
else -> SecurityType.UNKNOWN
135137
}
136138
if (type == SecurityType.UNKNOWN) null else {
137-
Security(
139+
SecurityInfo(
138140
code = "${insField.exchangeID}.${insField.instrumentID}",
139141
type = type,
140142
productId = insField.productID,

lib/src/main/kotlin/org/rationalityfrontline/ktrader/broker/ctp/utils.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ internal data class BiPosition(
200200
/**
201201
* 交易所 ID
202202
*/
203+
@Suppress("unused")
203204
object ExchangeID {
204205
const val SHFE = "SHFE"
205206
const val INE = "INE"

0 commit comments

Comments
 (0)