# Indicator Async Calc Result Push
- Python
- Proto
- C#
- Java
- C++
- JavaScript
on_recv_rsp(self, rsp_pb)
Description
Indicator async calculation result push callback. The result is paired with the original request submitted via request-indicator-calc by
calc_id. The client registers a handler (typically viaset_handler) to passively receive the push.Parameters
Parameter Type Description rsp_pb Qot_PushIndicatorCalc_pb2.Response This parameter does not need to be processed directly in the derived class. Return
Field Type Description ret RET_CODE API call result data dict When ret == RET_OK, returns the indicator calculation result str When ret != RET_OK, returns error description Result fields:
Field Type Description calc_id str Calculation task ID, matches the value returned by request_indicator_calc_asyncoutputs list Output line metadata, each item is an IndicatorOutputParam output_rows list Calculation result in time order, each row contains timeandvalues(one value per output)
Example
import time
from futu import *
class IndicatorCalcTest(IndicatorCalcHandlerBase):
def on_recv_rsp(self, rsp_pb):
ret_code, data = super(IndicatorCalcTest, self).on_recv_rsp(rsp_pb)
if ret_code != RET_OK:
print("IndicatorCalcTest: error, msg: %s" % data)
return RET_ERROR, data
print("IndicatorCalcTest ", data)
return RET_OK, data
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
handler = IndicatorCalcTest()
quote_ctx.set_handler(handler) # Register the push handler
# Trigger calculation; the result will arrive in the handler above
ret, kl_data, _ = quote_ctx.request_history_kline('US.AAPL', start='2024-01-01', end='2024-06-01', ktype=KLType.K_DAY)
if ret == RET_OK:
quote_ctx.request_indicator_calc_async('MACD', IndicatorLangType.MYLANG, 'US.AAPL', KLType.K_DAY, kl_data)
time.sleep(15)
quote_ctx.close()
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Qot_PushIndicatorCalc.proto
Description
Indicator async calculation result push. Paired with
Qot_RequestIndicatorCalc(3260) bycalcId. There is no request message; results are pushed actively by OpenD after the calculation completes.Return
// One row of calculation result, corresponding to a single candlestick moment.
// `values` is in the same order as `outputs`.
message IndicatorOutputRow
{
optional string time = 1; // Candlestick time (same as KLine.time in the request)
repeated double values = 2; // Value of each output line at this moment
}
message S2C
{
required string calcId = 1; // Matches the calcId returned by the original request
repeated Qot_Common.IndicatorOutputParam outputs = 2; // Output line metadata
repeated IndicatorOutputRow outputRows = 3; // Calculation result, in time order
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType, return result
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- Indicator output parameter: IndicatorOutputParam
- Request indicator calculation: Qot_RequestIndicatorCalc
- API call result structure: RetType
Protocol ID
3261
virtual void OnPush_PushIndicatorCalc(FTAPI_Conn client, QotPushIndicatorCalc.Response rsp);
Description
Indicator async calculation result push. Paired with
Qot_RequestIndicatorCalc(3260) bycalcId. The client registers an SPI callback to passively receive the push.Return
message IndicatorOutputRow
{
optional string time = 1;
repeated double values = 2;
}
message S2C
{
required string calcId = 1;
repeated Qot_Common.IndicatorOutputParam outputs = 2;
repeated IndicatorOutputRow outputRows = 3;
}
message Response
{
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- Indicator output parameter: IndicatorOutputParam
- Request indicator calculation: Qot_RequestIndicatorCalc
- API call result structure: RetType
Protocol ID
3261
void onPush_PushIndicatorCalc(FTAPI_Conn client, QotPushIndicatorCalc.Response rsp);
Description
Indicator async calculation result push. Paired with
Qot_RequestIndicatorCalc(3260) bycalcId. The client registers an SPI callback to passively receive the push.Return
message IndicatorOutputRow
{
optional string time = 1;
repeated double values = 2;
}
message S2C
{
required string calcId = 1;
repeated Qot_Common.IndicatorOutputParam outputs = 2;
repeated IndicatorOutputRow outputRows = 3;
}
message Response
{
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- Indicator output parameter: IndicatorOutputParam
- Request indicator calculation: Qot_RequestIndicatorCalc
- API call result structure: RetType
Protocol ID
3261
virtual void OnPush_PushIndicatorCalc(const Qot_PushIndicatorCalc::Response &stRsp) = 0;
Description
Indicator async calculation result push. Paired with
Qot_RequestIndicatorCalc(3260) bycalcId. There is no active call; the client registers the Qot SPI and passively receives the push (which is delivered after the originalRequestIndicatorCalcis accepted and the calculation completes).Return
message IndicatorOutputRow
{
optional string time = 1; // Candlestick time
repeated double values = 2; // Value of each output line at this moment
}
message S2C
{
required string calcId = 1; // Matches the calcId returned by the original request
repeated Qot_Common.IndicatorOutputParam outputs = 2; // Output line metadata
repeated IndicatorOutputRow outputRows = 3; // Calculation result, in time order
}
message Response
{
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- Indicator output parameter: IndicatorOutputParam
- Request indicator calculation: Qot_RequestIndicatorCalc
- API call result structure: RetType
Protocol ID
3261
Example
class Program : public FTSPI_Qot, public FTSPI_Conn
{
public:
Program() {
m_pQotApi = FTAPI::CreateQotApi();
m_pQotApi->RegisterQotSpi(this); // Register Qot SPI to passively receive pushes
m_pQotApi->RegisterConnSpi(this);
}
~Program() {
if (m_pQotApi != nullptr) {
m_pQotApi->UnregisterQotSpi();
m_pQotApi->UnregisterConnSpi();
FTAPI::ReleaseQotApi(m_pQotApi);
m_pQotApi = nullptr;
}
}
void Start() {
m_pQotApi->InitConnect("127.0.0.1", 11111, false);
}
virtual void OnInitConnect(FTAPI_Conn* pConn, Futu::i64_t nErrCode, const char* strDesc) {
// The push is registered by the server side when a RequestIndicatorCalc is sent.
// Here we just keep the SPI registered and wait for the push.
cout << "Waiting for OnPush_PushIndicatorCalc..." << endl;
}
// Passively receive indicator calc result push (3261)
virtual void OnPush_PushIndicatorCalc(const Qot_PushIndicatorCalc::Response &stRsp) {
string resp_str;
ProtoBufToBodyData(stRsp, resp_str);
cout << "OnPush_PushIndicatorCalc: " << UTF8ToLocal(resp_str) << endl;
}
protected:
FTAPI_Qot *m_pQotApi;
};
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
OnPush(cmd,res)
Description
Indicator async calculation result push. Paired with
Qot_RequestIndicatorCalc(3260) bycalcId. The client registers anonPushcallback to passively receive the push.Return
message IndicatorOutputRow
{
optional string time = 1;
repeated double values = 2;
}
message S2C
{
required string calcId = 1;
repeated Qot_Common.IndicatorOutputParam outputs = 2;
repeated IndicatorOutputRow outputRows = 3;
}
message Response
{
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- Indicator output parameter: IndicatorOutputParam
- Request indicator calculation: Qot_RequestIndicatorCalc
- API call result structure: RetType
Protocol ID
3261
Example
import ftWebsocket from "futu-api";
import { Common, Qot_Common } from "futu-api/proto";
import beautify from "js-beautify";
function QotPushIndicatorCalc(){
const { RetType } = Common
const { KLType, IndicatorLangType, QotMarket } = Qot_Common
let [addr, port, enable_ssl, key] = ["127.0.0.1", 33333, false, '7522027ccf5a06b1'];
let websocket = new ftWebsocket();
// 注册指标异步计算结果推送回调,与 RequestIndicatorCalc 返回的 calcId 配对
websocket.onPush_PushIndicatorCalc = (qotPushIndicatorCalc)=>{
let { errCode, retMsg, retType, s2c } = qotPushIndicatorCalc
console.log("PushIndicatorCalc: errCode %d, retMsg %s, retType %d", errCode, retMsg, retType);
if(retType == RetType.RetType_Succeed){
let data = beautify(JSON.stringify(s2c), { indent_size: 2, space_in_empty_paren: true });
console.log(data);
}
};
websocket.onlogin = (ret, msg)=>{
if (ret) {
// 发起一次 MACD 指标计算,结果由上面的回调推送回来
const req = {
c2s: {
shortName: "MACD",
langType: IndicatorLangType.IndicatorLangType_MyLang,
data: {
security: { market: QotMarket.QotMarket_HK_Security, code: "00700" },
klType: KLType.KLType_Day,
kLine: [
{ time: 1700000000, open: 310.0, close: 315.0, high: 318.0, low: 308.0, volume: 10000000, turnover: 3150000000 }
]
},
},
};
websocket.RequestIndicatorCalc(req)
.then((res)=>{ console.log("RequestIndicatorCalc calcId:", res.s2c && res.s2c.calcId); })
.catch((error)=>{ console.log("error:", error); });
} else {
console.log("start error", msg);
}
};
websocket.start(addr, port, enable_ssl, key);
setTimeout(()=>{ websocket.stop(); process.exit(); }, 5000);
}
QotPushIndicatorCalc()
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
- Output
RequestIndicatorCalc calcId: 1700000000000-0
PushIndicatorCalc: errCode 0, retMsg , retType 0
{
"calcId": "1700000000000-0",
"outputs": [
{ "index": 0, "name": "DIF", "type": 1 },
{ "index": 1, "name": "DEA", "type": 1 },
{ "index": 2, "name": "MACD", "type": 1 }
],
"outputRows": [
{ "time": "1700000000", "values": [0.5234, 0.4123, 0.2222] }
]
}
2
3
4
5
6
7
8
9
10
11
12
13
Tips
- This interface provides the function of continuously receiving pushed indicator calculation results. To trigger a calculation, call request-indicator-calc. Each push is paired with the original request by
calcId.
- Python
- Proto
- C#
- Java
- C++
- JavaScript
on_recv_rsp(self, rsp_pb)
Description
Indicator async calculation result push callback. The result is paired with the original request submitted via request-indicator-calc by
calc_id. The client registers a handler (typically viaset_handler) to passively receive the push.Parameters
Parameter Type Description rsp_pb Qot_PushIndicatorCalc_pb2.Response This parameter does not need to be processed directly in the derived class. Return
Field Type Description ret RET_CODE API call result data dict When ret == RET_OK, returns the indicator calculation result str When ret != RET_OK, returns error description Result fields:
Field Type Description calc_id str Calculation task ID, matches the value returned by request_indicator_calc_asyncoutputs list Output line metadata, each item is an IndicatorOutputParam output_rows list Calculation result in time order, each row contains timeandvalues(one value per output)
Example
import time
from moomoo import *
class IndicatorCalcTest(IndicatorCalcHandlerBase):
def on_recv_rsp(self, rsp_pb):
ret_code, data = super(IndicatorCalcTest, self).on_recv_rsp(rsp_pb)
if ret_code != RET_OK:
print("IndicatorCalcTest: error, msg: %s" % data)
return RET_ERROR, data
print("IndicatorCalcTest ", data)
return RET_OK, data
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
handler = IndicatorCalcTest()
quote_ctx.set_handler(handler)
ret, kl_data, _ = quote_ctx.request_history_kline('US.AAPL', start='2024-01-01', end='2024-06-01', ktype=KLType.K_DAY)
if ret == RET_OK:
quote_ctx.request_indicator_calc_async('MACD', IndicatorLangType.MYLANG, 'US.AAPL', KLType.K_DAY, kl_data)
time.sleep(15)
quote_ctx.close()
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Qot_PushIndicatorCalc.proto
Description
Indicator async calculation result push. Paired with
Qot_RequestIndicatorCalc(3260) bycalcId. There is no request message; results are pushed actively by OpenD after the calculation completes.Return
message IndicatorOutputRow
{
optional string time = 1;
repeated double values = 2;
}
message S2C
{
required string calcId = 1;
repeated Qot_Common.IndicatorOutputParam outputs = 2;
repeated IndicatorOutputRow outputRows = 3;
}
message Response
{
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- Indicator output parameter: IndicatorOutputParam
- Request indicator calculation: Qot_RequestIndicatorCalc
- API call result structure: RetType
Protocol ID
3261
virtual void OnPush_PushIndicatorCalc(MMAPI_Conn client, QotPushIndicatorCalc.Response rsp);
Description
Indicator async calculation result push. Paired with
Qot_RequestIndicatorCalc(3260) bycalcId. The client registers an SPI callback to passively receive the push.Return
message IndicatorOutputRow
{
optional string time = 1;
repeated double values = 2;
}
message S2C
{
required string calcId = 1;
repeated Qot_Common.IndicatorOutputParam outputs = 2;
repeated IndicatorOutputRow outputRows = 3;
}
message Response
{
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- Indicator output parameter: IndicatorOutputParam
- Request indicator calculation: Qot_RequestIndicatorCalc
- API call result structure: RetType
Protocol ID
3261
void onPush_PushIndicatorCalc(MMAPI_Conn client, QotPushIndicatorCalc.Response rsp);
Description
Indicator async calculation result push. Paired with
Qot_RequestIndicatorCalc(3260) bycalcId. The client registers an SPI callback to passively receive the push.Return
message IndicatorOutputRow
{
optional string time = 1;
repeated double values = 2;
}
message S2C
{
required string calcId = 1;
repeated Qot_Common.IndicatorOutputParam outputs = 2;
repeated IndicatorOutputRow outputRows = 3;
}
message Response
{
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- Indicator output parameter: IndicatorOutputParam
- Request indicator calculation: Qot_RequestIndicatorCalc
- API call result structure: RetType
Protocol ID
3261
virtual void OnPush_PushIndicatorCalc(const Qot_PushIndicatorCalc::Response &stRsp) = 0;
Description
Indicator async calculation result push. Paired with
Qot_RequestIndicatorCalc(3260) bycalcId. There is no active call; the client registers the Qot SPI and passively receives the push (which is delivered after the originalRequestIndicatorCalcis accepted and the calculation completes).Return
message IndicatorOutputRow
{
optional string time = 1;
repeated double values = 2;
}
message S2C
{
required string calcId = 1;
repeated Qot_Common.IndicatorOutputParam outputs = 2;
repeated IndicatorOutputRow outputRows = 3;
}
message Response
{
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- Indicator output parameter: IndicatorOutputParam
- Request indicator calculation: Qot_RequestIndicatorCalc
- API call result structure: RetType
Protocol ID
3261
Example
class Program : public MMSPI_Qot, public MMSPI_Conn
{
public:
Program() {
m_pQotApi = MMAPI::CreateQotApi();
m_pQotApi->RegisterQotSpi(this); // Register Qot SPI to passively receive pushes
m_pQotApi->RegisterConnSpi(this);
}
~Program() {
if (m_pQotApi != nullptr) {
m_pQotApi->UnregisterQotSpi();
m_pQotApi->UnregisterConnSpi();
MMAPI::ReleaseQotApi(m_pQotApi);
m_pQotApi = nullptr;
}
}
void Start() {
m_pQotApi->InitConnect("127.0.0.1", 11111, false);
}
virtual void OnInitConnect(MMAPI_Conn* pConn, Moomoo::i64_t nErrCode, const char* strDesc) {
// The push is registered by the server side when a RequestIndicatorCalc is sent.
// Here we just keep the SPI registered and wait for the push.
cout << "Waiting for OnPush_PushIndicatorCalc..." << endl;
}
// Passively receive indicator calc result push (3261)
virtual void OnPush_PushIndicatorCalc(const Qot_PushIndicatorCalc::Response &stRsp) {
string resp_str;
ProtoBufToBodyData(stRsp, resp_str);
cout << "OnPush_PushIndicatorCalc: " << UTF8ToLocal(resp_str) << endl;
}
protected:
MMAPI_Qot *m_pQotApi;
};
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
OnPush(cmd,res)
Description
Indicator async calculation result push. Paired with
Qot_RequestIndicatorCalc(3260) bycalcId. The client registers anonPushcallback to passively receive the push.Return
message IndicatorOutputRow
{
optional string time = 1;
repeated double values = 2;
}
message S2C
{
required string calcId = 1;
repeated Qot_Common.IndicatorOutputParam outputs = 2;
repeated IndicatorOutputRow outputRows = 3;
}
message Response
{
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- Indicator output parameter: IndicatorOutputParam
- Request indicator calculation: Qot_RequestIndicatorCalc
- API call result structure: RetType
Protocol ID
3261
Example
import mmWebsocket from "moomoo-api";
import { Common, Qot_Common } from "moomoo-api/proto";
import beautify from "js-beautify";
function QotPushIndicatorCalc(){
const { RetType } = Common
const { KLType, IndicatorLangType, QotMarket } = Qot_Common
let [addr, port, enable_ssl, key] = ["127.0.0.1", 33333, false, '7522027ccf5a06b1'];
let websocket = new mmWebsocket();
// 注册指标异步计算结果推送回调,与 RequestIndicatorCalc 返回的 calcId 配对
websocket.onPush_PushIndicatorCalc = (qotPushIndicatorCalc)=>{
let { errCode, retMsg, retType, s2c } = qotPushIndicatorCalc
console.log("PushIndicatorCalc: errCode %d, retMsg %s, retType %d", errCode, retMsg, retType);
if(retType == RetType.RetType_Succeed){
let data = beautify(JSON.stringify(s2c), { indent_size: 2, space_in_empty_paren: true });
console.log(data);
}
};
websocket.onlogin = (ret, msg)=>{
if (ret) {
// 发起一次 MACD 指标计算,结果由上面的回调推送回来
const req = {
c2s: {
shortName: "MACD",
langType: IndicatorLangType.IndicatorLangType_MyLang,
data: {
security: { market: QotMarket.QotMarket_HK_Security, code: "00700" },
klType: KLType.KLType_Day,
kLine: [
{ time: 1700000000, open: 310.0, close: 315.0, high: 318.0, low: 308.0, volume: 10000000, turnover: 3150000000 }
]
},
},
};
websocket.RequestIndicatorCalc(req)
.then((res)=>{ console.log("RequestIndicatorCalc calcId:", res.s2c && res.s2c.calcId); })
.catch((error)=>{ console.log("error:", error); });
} else {
console.log("start error", msg);
}
};
websocket.start(addr, port, enable_ssl, key);
setTimeout(()=>{ websocket.stop(); process.exit(); }, 5000);
}
QotPushIndicatorCalc()
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
- Output
RequestIndicatorCalc calcId: 1700000000000-0
PushIndicatorCalc: errCode 0, retMsg , retType 0
{
"calcId": "1700000000000-0",
"outputs": [
{ "index": 0, "name": "DIF", "type": 1 },
{ "index": 1, "name": "DEA", "type": 1 },
{ "index": 2, "name": "MACD", "type": 1 }
],
"outputRows": [
{ "time": "1700000000", "values": [0.5234, 0.4123, 0.2222] }
]
}
2
3
4
5
6
7
8
9
10
11
12
13
Tips
- This interface provides the function of continuously receiving pushed indicator calculation results. To trigger a calculation, call request-indicator-calc. Each push is paired with the original request by
calcId.