Skip to content

Commit d1fc1c0

Browse files
committed
完善合约代码解析
1 parent b005a8f commit d1fc1c0

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

src/index.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ class Strategy implements hft.IStrategy, hft.ITickReceiver {
5858

5959
console.log("Trading Day", this.engine.getTradingDay());
6060

61+
this.engine.queryInstrument(this.symbol, {
62+
onInstrument: (instrument) => {
63+
if (!instrument) {
64+
console.error("Symbol", this.symbol, "error");
65+
exit(1);
66+
}
67+
68+
console.log("Instrument", instrument);
69+
},
70+
});
71+
6172
this.engine.queryCommissionRate(this.symbol, {
6273
onCommissionRate: (rate) => {
6374
console.log("Commission Rate", rate);

src/market.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export class Market extends CTPProvider implements IMarketProvider {
252252
const instrumentIds = new Set<string>();
253253

254254
symbols.forEach((symbol) => {
255-
const instrumentId = this._symbolToInstrumentId(symbol);
255+
const [instrumentId] = this._parseSymbol(symbol);
256256

257257
if (this.recordings.has(instrumentId)) {
258258
return;
@@ -300,7 +300,7 @@ export class Market extends CTPProvider implements IMarketProvider {
300300
const instrumentIds = new Set<string>();
301301

302302
symbols.forEach((symbol) => {
303-
const instrumentId = this._symbolToInstrumentId(symbol);
303+
const [instrumentId] = this._parseSymbol(symbol);
304304
const receivers = this.subscribers.get(instrumentId);
305305

306306
if (receivers) {
@@ -328,7 +328,7 @@ export class Market extends CTPProvider implements IMarketProvider {
328328
const instrumentIds = new Set<string>();
329329

330330
symbols.forEach((symbol) => {
331-
const instrumentId = this._symbolToInstrumentId(symbol);
331+
const [instrumentId] = this._parseSymbol(symbol);
332332
const receivers = this.subscribers.get(instrumentId);
333333

334334
if (!receivers) {

src/provider.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ export class CTPProvider {
5959
}
6060
}
6161

62-
protected _symbolToInstrumentId(symbol: string) {
63-
return symbol.split(".")[0];
62+
protected _parseSymbol(symbol: string): [string, string] {
63+
const [instrumentId, exchangeId] = symbol.split(".");
64+
return [instrumentId, exchangeId];
6465
}
6566

6667
protected _isErrorResp(

src/trader.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ export class Trader extends CTPProvider implements ITraderProvider {
627627
}
628628

629629
queryCommissionRate(symbol: string, receiver: ICommissionRateReceiver) {
630-
const instrumentId = this._symbolToInstrumentId(symbol);
630+
const [instrumentId] = this._parseSymbol(symbol);
631631
const commRate = this.commRates.get(instrumentId);
632632

633633
if (commRate) {
@@ -648,7 +648,7 @@ export class Trader extends CTPProvider implements ITraderProvider {
648648
}
649649

650650
queryMarginRate(symbol: string, receiver: IMarginRateReceiver) {
651-
const instrumentId = this._symbolToInstrumentId(symbol);
651+
const [instrumentId] = this._parseSymbol(symbol);
652652
const marginRate = this.marginRates.get(instrumentId);
653653

654654
if (marginRate) {
@@ -670,10 +670,12 @@ export class Trader extends CTPProvider implements ITraderProvider {
670670
}
671671

672672
queryInstrument(symbol: string, receiver: IInstrumentReceiver) {
673-
const instrumentId = this._symbolToInstrumentId(symbol);
673+
const [instrumentId, exchangeId] = this._parseSymbol(symbol);
674674

675675
const instrument = this.instruments.find(
676-
(instrument) => instrument.InstrumentID === instrumentId,
676+
(instrument) =>
677+
instrument.InstrumentID === instrumentId &&
678+
instrument.ExchangeID === exchangeId,
677679
);
678680

679681
receiver.onInstrument(
@@ -689,7 +691,7 @@ export class Trader extends CTPProvider implements ITraderProvider {
689691
return;
690692
}
691693

692-
const instrumentId = this._symbolToInstrumentId(symbol);
694+
const [instrumentId] = this._parseSymbol(symbol);
693695

694696
if (!this.symbols.has(instrumentId)) {
695697
receiver.onPosition(undefined);
@@ -820,7 +822,7 @@ export class Trader extends CTPProvider implements ITraderProvider {
820822
return;
821823
}
822824

823-
const instrumentId = this._symbolToInstrumentId(symbol);
825+
const [instrumentId] = this._parseSymbol(symbol);
824826

825827
const instrument = this.instruments.find(
826828
(instrument) => instrument.InstrumentID === instrumentId,
@@ -1469,7 +1471,7 @@ export class Trader extends CTPProvider implements ITraderProvider {
14691471
while (!this.marginRatesQueue.isEmpty()) {
14701472
const nextQuery = this.marginRatesQueue.peekFront()!;
14711473

1472-
const instrumentId = this._symbolToInstrumentId(nextQuery.symbol);
1474+
const [instrumentId] = this._parseSymbol(nextQuery.symbol);
14731475
const marginRate = this.marginRates.get(instrumentId);
14741476

14751477
if (marginRate) {
@@ -1494,7 +1496,7 @@ export class Trader extends CTPProvider implements ITraderProvider {
14941496
while (!this.commRatesQueue.isEmpty()) {
14951497
const nextQuery = this.commRatesQueue.peekFront()!;
14961498

1497-
const instrumentId = this._symbolToInstrumentId(nextQuery.symbol);
1499+
const [instrumentId] = this._parseSymbol(nextQuery.symbol);
14981500
const commRate = this.commRates.get(instrumentId);
14991501

15001502
if (commRate) {

0 commit comments

Comments
 (0)