# Get Valid Spread
- Python
- Proto
- C#
- Java
- C++
- JavaScript
get_option_strategy_spread(code, option_strategy, expire_time, far_expire_time=None, index_option_type=IndexOptionType.NORMAL)
Description
Get the list of valid spreads for the specified option strategy under the current underlying and expiration date.
Parameters
Parameter Type Description code str Underlying stock code 如 US.AAPL、HK.00700option_strategy OptionStrategyType Option strategy type expire_time str Expiration date Format: yyyy-MM-dd, in market timezonefar_expire_time str Far expiration date Required for DiagonalSpread and similar strategies; format: yyyy-MM-ddindex_option_type IndexOptionType Index option type Only effective for HK index option filtering- option_strategy only supports Spread, Strangle, Collar, Butterfly, Condor, IronButterfly, IronCondor, and DiagonalSpread.
Return
Parameter Type Description ret RET_CODE API call result data pd.DataFrame When ret == RET_OK, returns valid spread list str When ret != RET_OK, returns error description DataFrame fields:
Field Type Description spread float Valid spread
Example
from futu import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
ret,data = quote_ctx.get_option_strategy_spread(code='HK.00700', option_strategy=OptionStrategyType.STRANGLE)
if ret == RET_OK:
print(data)
else:
print('error:', data)
quote_ctx.close() # Remember to close the connection to avoid exhausting connection quota
2
3
4
5
6
7
8
9
- Output
spread
0 10.0
1 20.0
2 30.0
3 40.0
4 50.0
5 60.0
6 70.0
7 80.0
8 90.0
9 100.0
10 110.0
11 120.0
12 130.0
13 140.0
14 150.0
15 160.0
16 170.0
17 180.0
18 190.0
19 200.0
20 210.0
21 220.0
22 230.0
23 240.0
24 250.0
25 260.0
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
# Qot_GetOptionStrategySpread.proto
Description
Get valid spread list for option strategy
Parameters
message C2S
{
required Qot_Common.Security security = 1; // Underlying stock
required Qot_Common.OptionStrategyType optionStrategy = 2; // Option strategy type
required string expireTime = 3; // Expiration date,格式 yyyy-MM-dd
optional string farExpireTime = 4; // Far expiration date,格式 yyyy-MM-dd
optional Qot_Common.IndexOptionType indexOptionType = 5; // Index option type
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Security structure: Security
- OptionStrategyType enum: OptionStrategyType
- IndexOptionType enum: IndexOptionType
- Return
message S2C
{
repeated double spreadList = 1; // Valid spread list
}
message Response
{
required int32 retType = 1 [default = -400]; // Return result,详见 Common.RetType
optional string retMsg = 2; // Return result description
optional int32 errCode = 3; // Error code
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
- API call result structure: RetType
uint GetOptionStrategySpread(QotGetOptionStrategySpread.Request req);
virtual void OnReply_GetOptionStrategySpread(FTAPI_Conn client, uint nSerialNo, QotGetOptionStrategySpread.Response rsp);
Description
Get the valid spread list for a given option strategy type. Only supports Spread, Strangle, Collar, Butterfly, Condor, IronButterfly, IronCondor, and DiagonalSpread. The returned spread values can be used as the spread parameter of GetOptionStrategy.
Parameters
message C2S
{
required Qot_Common.Security owner = 1;
required int32 option_strategy = 2;
optional string expire_time = 3;
optional string far_expire_time = 4;
optional int32 index_option_type = 5;
optional Qot_Common.QotHeader header = 100;
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
- Underlying stock; HK/US stocks and major indices only
- OptionStrategyType
- Only Spread, Strangle, Collar, Butterfly, Condor, IronButterfly, IronCondor, DiagonalSpread, etc. are supported for spread queries
- Return
message S2C
{
repeated double spreadList = 1;
}
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
- API call result structure: RetType
- Example
public class Program : FTSPI_Qot, FTSPI_Conn
{
FTAPI_Qot qot = new FTAPI_Qot();
public Program()
{
qot.SetClientInfo("csharp", 1);
qot.SetConnCallback(this);
qot.SetQotCallback(this);
}
public void Start()
{
qot.InitConnect("127.0.0.1", (ushort)11111, false);
}
public void OnInitConnect(FTAPI_Conn client, long errCode, String desc)
{
Console.Write("Qot onInitConnect: ret={0} desc={1} connID={2}\n", errCode, desc, client.GetConnectID());
if (errCode != 0)
return;
QotCommon.Security owner = QotCommon.Security.CreateBuilder()
.SetMarket((int)QotCommon.QotMarket.QotMarket_HK_Security)
.SetCode("00700")
.Build();
QotGetOptionStrategySpread.C2S c2s = QotGetOptionStrategySpread.C2S.CreateBuilder()
.SetOwner(owner)
.SetOptionStrategy((int)QotCommon.OptionStrategyType.OptionStrategyType_Spread)
.Build();
QotGetOptionStrategySpread.Request req = QotGetOptionStrategySpread.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.GetOptionStrategySpread(req);
Console.Write("Send QotGetOptionStrategySpread: {0}\n", seqNo);
}
public void OnDisconnect(FTAPI_Conn client, long errCode)
{
Console.Write("Qot onDisConnect: {0}\n", errCode);
}
public void OnReply_GetOptionStrategySpread(FTAPI_Conn client, uint nSerialNo, QotGetOptionStrategySpread.Response rsp)
{
Console.Write("Reply: QotGetOptionStrategySpread: {0} {1}\n", nSerialNo, rsp.ToString());
}
public static void Main(String[] args)
{
FTAPI.Init();
Program qot = new Program();
qot.Start();
while (true)
Thread.Sleep(1000 * 600);
}
}
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
50
51
52
53
54
55
56
57
- Output
Qot onInitConnect: ret=0 desc= connID=7459213669204006063
Send QotGetOptionStrategySpread: 3
Reply: QotGetOptionStrategySpread: 3 retType: 0 ...
2
3
int getOptionStrategySpread(QotGetOptionStrategySpread.Request req);
void onReply_GetOptionStrategySpread(FTAPI_Conn client, int nSerialNo, QotGetOptionStrategySpread.Response rsp);
Description
Get the valid spread list for a given option strategy type. Only supports Spread, Strangle, Collar, Butterfly, Condor, IronButterfly, IronCondor, and DiagonalSpread. The returned spread values can be used as the spread parameter of GetOptionStrategy.
Parameters
message C2S
{
required Qot_Common.Security owner = 1;
required int32 option_strategy = 2;
optional string expire_time = 3;
optional string far_expire_time = 4;
optional int32 index_option_type = 5;
optional Qot_Common.QotHeader header = 100;
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
- Underlying stock; HK/US stocks and major indices only
- OptionStrategyType
- Only Spread, Strangle, Collar, Butterfly, Condor, IronButterfly, IronCondor, DiagonalSpread, etc. are supported for spread queries
- Return
message S2C
{
repeated double spreadList = 1;
}
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
- API call result structure: RetType
- Example
public class QotDemo implements FTSPI_Qot, FTSPI_Conn {
FTAPI_Conn_Qot qot = new FTAPI_Conn_Qot();
public QotDemo() {
qot.setClientInfo("javaclient", 1);
qot.setConnSpi(this);
qot.setQotSpi(this);
}
public void start() {
qot.initConnect("127.0.0.1", (short)11111, false);
}
@Override
public void onInitConnect(FTAPI_Conn client, long errCode, String desc)
{
System.out.printf("Qot onInitConnect: ret=%b desc=%s connID=%d\n", errCode, desc, client.getConnectID());
if (errCode != 0)
return;
QotCommon.Security owner = QotCommon.Security.newBuilder()
.setMarket(QotCommon.QotMarket.QotMarket_HK_Security_VALUE)
.setCode("00700")
.build();
QotGetOptionStrategySpread.C2S c2s = QotGetOptionStrategySpread.C2S.newBuilder()
.setOwner(owner)
.setOptionStrategy(QotCommon.OptionStrategyType.OptionStrategyType_Spread_VALUE)
.build();
QotGetOptionStrategySpread.Request req = QotGetOptionStrategySpread.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.getOptionStrategySpread(req);
System.out.printf("Send QotGetOptionStrategySpread: %d\n", seqNo);
}
@Override
public void onDisconnect(FTAPI_Conn client, long errCode) {
System.out.printf("Qot onDisConnect: %d\n", errCode);
}
@Override
public void onReply_GetOptionStrategySpread(FTAPI_Conn client, int nSerialNo, QotGetOptionStrategySpread.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("QotGetOptionStrategySpread failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive QotGetOptionStrategySpread: %s\n", json);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
FTAPI.init()
QotDemo qot = new QotDemo();
qot.start();
while (true) {
try {
Thread.sleep(1000 * 600);
} catch (InterruptedException exc) {
}
}
}
}
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
- Output
Qot onInitConnect: ret=0 desc= connID=7459213669204006063
Send QotGetOptionStrategySpread: 2
Receive QotGetOptionStrategySpread: {
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"spreadList": [5, 10, 20, 40]
}
}
2
3
4
5
6
7
8
9
10
Futu::u32_t GetOptionStrategySpread(const Qot_GetOptionStrategySpread::Request &stReq);
virtual void OnReply_GetOptionStrategySpread(Futu::u32_t nSerialNo, const Qot_GetOptionStrategySpread::Response &stRsp) = 0;
Description
Get valid spread list for option strategy
Parameters
message C2S
{
required Qot_Common.Security security = 1; // Underlying stock
required Qot_Common.OptionStrategyType optionStrategy = 2; // Option strategy type
required string expireTime = 3; // Expiration date,格式 yyyy-MM-dd
optional string farExpireTime = 4; // Far expiration date,格式 yyyy-MM-dd
optional Qot_Common.IndexOptionType indexOptionType = 5; // Index option type
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Security structure: Security
- OptionStrategyType enum: OptionStrategyType
- IndexOptionType enum: IndexOptionType
- Return
message S2C
{
repeated double spreadList = 1; // Valid spread list
}
message Response
{
required int32 retType = 1 [default = -400]; // Return result,详见 Common.RetType
optional string retMsg = 2; // Return result description
optional int32 errCode = 3; // Error code
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
- API call result structure: RetType
- Example
class Program : public FTSPI_Qot, public FTSPI_Trd, public FTSPI_Conn
{
public:
Program() {
m_pQotApi = FTAPI::CreateQotApi();
m_pQotApi->RegisterQotSpi(this);
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) {
cout << "connect" << endl;
// Build request
Qot_GetOptionStrategySpread::Request req;
Qot_GetOptionStrategySpread::C2S *c2s = req.mutable_c2s();
Qot_Common::Security *owner = c2s->mutable_owner();
owner->set_market(Qot_Common::QotMarket::QotMarket_HK_Security);
owner->set_code("00700");
c2s->set_option_strategy(Qot_Common::OptionStrategyType_Spread); // Vertical spread strategy
m_GetOptionStrategySpreadSerialNo = m_pQotApi->GetOptionStrategySpread(req);
cout << "Request GetOptionStrategySpread SerialNo: " << m_GetOptionStrategySpreadSerialNo << endl;
}
virtual void OnReply_GetOptionStrategySpread(Futu::u32_t nSerialNo, const Qot_GetOptionStrategySpread::Response &stRsp) {
if (nSerialNo != m_GetOptionStrategySpreadSerialNo) return;
cout << "OnReply_GetOptionStrategySpread SerialNo: " << nSerialNo << endl;
// Parse the internal structure and print
// ProtoBufToBodyData and UTF8ToLocal are defined in tool.h under Sample
string resp_str;
ProtoBufToBodyData(stRsp, resp_str);
cout << UTF8ToLocal(resp_str) << endl;
}
protected:
FTAPI_Qot *m_pQotApi;
Futu::u32_t m_GetOptionStrategySpreadSerialNo = 0;
};
int32_t main(int32_t argc, char** argv)
{
FTAPI::Init();
{
Program program;
program.Start();
getchar();
}
protobuf::ShutdownProtobufLibrary();
FTAPI::UnInit();
return 0;
}
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
- Output
connect
Request GetOptionStrategySpread SerialNo: 3
OnReply_GetOptionStrategySpread SerialNo: 3
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"spreadList": [5, 10, 20, 40]
}
}
2
3
4
5
6
7
8
9
10
11
GetOptionStrategySpread(req);
Description
Get valid spread list for option strategy
Parameters
message C2S
{
required Qot_Common.Security owner = 1; //Underlying stock; only HK/US stocks and HSI/HSCEI supported
required int32 option_strategy = 2; //OptionStrategyType, option strategy type
optional string expire_time = 3; //Expiry (near), yyyy-MM-dd; empty takes the first
optional string far_expire_time = 4; //Far expiry, required for DiagonalSpread
optional int32 index_option_type = 5; //IndexOptionType; only for HSI/HSCEI
optional Qot_Common.QotHeader header = 100; //Quote common header
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
- Security structure: Security
- OptionStrategyType enum: OptionStrategyType
- IndexOptionType enum: IndexOptionType
- Return
message S2C
{
repeated double spreadList = 1; //Valid spread list, e.g. [0.5, 1.0, 2.0]
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
- API call result structure: RetType
- Example
import ftWebsocket from "futu-api";
import { ftCmdID } from "futu-api";
import { Common, Qot_Common } from "futu-api/proto";
import beautify from "js-beautify";
function QotGetOptionStrategySpread(){
const { RetType } = Common
const { QotMarket, OptionStrategyType } = Qot_Common
let [addr, port, enable_ssl, key] = ["127.0.0.1", 33333, false, '7522027ccf5a06b1'];
let websocket = new ftWebsocket();
websocket.onlogin = (ret, msg)=>{
if (ret) { // 登录成功
const req = {
c2s: {
owner: { market: QotMarket.QotMarket_HK_Security, code: "00700" },
optionStrategy: OptionStrategyType.OptionStrategyType_Strangle, // 宽跨式策略
},
};
websocket.GetOptionStrategySpread(req)
.then((res) => {
let { errCode, retMsg, retType, s2c } = res
console.log("GetOptionStrategySpread: 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);
}
})
.catch((error) => {
console.log("error:", error);
});
} else {
console.log("start error", msg);
}
};
websocket.start(addr, port, enable_ssl, key);
//关闭行情连接,连接不再使用之后,要关闭,否则占用不必要资源
//同时OpenD也限制了最多128条连接
//也可以一个页面或者一个项目维护一条连接,这里范例请求一次创建一条连接
setTimeout(()=>{
websocket.stop();
console.log("stop");
}, 5000); // 5秒后断开
}
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
50
- Output
GetOptionStrategySpread: errCode 0, retMsg , retType 0
{
"spreadList": [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260]
}
stop
2
3
4
5
API Restrictions
- Maximum 30 requests per 30 seconds.
- Python
- Proto
- C#
- Java
- C++
- JavaScript
get_option_strategy_spread(code, option_strategy, expire_time, far_expire_time=None, index_option_type=IndexOptionType.NORMAL)
Description
Get the list of valid spreads for the specified option strategy under the current underlying and expiration date.
Parameters
Parameter Type Description code str Underlying stock code 如 US.AAPL、HK.00700option_strategy OptionStrategyType Option strategy type expire_time str Expiration date Format: yyyy-MM-dd, in market timezonefar_expire_time str Far expiration date Required for DiagonalSpread and similar strategies; format: yyyy-MM-ddindex_option_type IndexOptionType Index option type Only effective for HK index option filtering- option_strategy only supports Spread, Strangle, Collar, Butterfly, Condor, IronButterfly, IronCondor, and DiagonalSpread.
Return
Parameter Type Description ret RET_CODE API call result data pd.DataFrame When ret == RET_OK, returns valid spread list str When ret != RET_OK, returns error description DataFrame fields:
Field Type Description spread float Valid spread
Example
from moomoo import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
ret,data = quote_ctx.get_option_strategy_spread(code='HK.00700', option_strategy=OptionStrategyType.STRANGLE)
if ret == RET_OK:
print(data)
else:
print('error:', data)
quote_ctx.close() # Remember to close the connection to avoid exhausting connection quota
2
3
4
5
6
7
8
9
- Output
spread
0 10.0
1 20.0
2 30.0
3 40.0
4 50.0
5 60.0
6 70.0
7 80.0
8 90.0
9 100.0
10 110.0
11 120.0
12 130.0
13 140.0
14 150.0
15 160.0
16 170.0
17 180.0
18 190.0
19 200.0
20 210.0
21 220.0
22 230.0
23 240.0
24 250.0
25 260.0
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
# Qot_GetOptionStrategySpread.proto
Description
Get valid spread list for option strategy
Parameters
message C2S
{
required Qot_Common.Security security = 1; // Underlying stock
required Qot_Common.OptionStrategyType optionStrategy = 2; // Option strategy type
required string expireTime = 3; // Expiration date,格式 yyyy-MM-dd
optional string farExpireTime = 4; // Far expiration date,格式 yyyy-MM-dd
optional Qot_Common.IndexOptionType indexOptionType = 5; // Index option type
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Security structure: Security
- OptionStrategyType enum: OptionStrategyType
- IndexOptionType enum: IndexOptionType
- Return
message S2C
{
repeated double spreadList = 1; // Valid spread list
}
message Response
{
required int32 retType = 1 [default = -400]; // Return result,详见 Common.RetType
optional string retMsg = 2; // Return result description
optional int32 errCode = 3; // Error code
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
- API call result structure: RetType
uint GetOptionStrategySpread(QotGetOptionStrategySpread.Request req);
virtual void OnReply_GetOptionStrategySpread(MMAPI_Conn client, uint nSerialNo, QotGetOptionStrategySpread.Response rsp);
Description
Get the valid spread list for a given option strategy type. Only supports Spread, Strangle, Collar, Butterfly, Condor, IronButterfly, IronCondor, and DiagonalSpread. The returned spread values can be used as the spread parameter of GetOptionStrategy.
Parameters
message C2S
{
required Qot_Common.Security owner = 1;
required int32 option_strategy = 2;
optional string expire_time = 3;
optional string far_expire_time = 4;
optional int32 index_option_type = 5;
optional Qot_Common.QotHeader header = 100;
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
- Underlying stock; HK/US stocks and major indices only
- OptionStrategyType
- Only Spread, Strangle, Collar, Butterfly, Condor, IronButterfly, IronCondor, DiagonalSpread, etc. are supported for spread queries
- Return
message S2C
{
repeated double spreadList = 1;
}
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
- API call result structure: RetType
- Example
public class Program : MMSPI_Qot, MMSPI_Conn
{
MMAPI_Qot qot = new MMAPI_Qot();
public Program()
{
qot.SetClientInfo("csharp", 1);
qot.SetConnCallback(this);
qot.SetQotCallback(this);
}
public void Start()
{
qot.InitConnect("127.0.0.1", (ushort)11111, false);
}
public void OnInitConnect(MMAPI_Conn client, long errCode, String desc)
{
Console.Write("Qot onInitConnect: ret={0} desc={1} connID={2}\n", errCode, desc, client.GetConnectID());
if (errCode != 0)
return;
QotCommon.Security owner = QotCommon.Security.CreateBuilder()
.SetMarket((int)QotCommon.QotMarket.QotMarket_HK_Security)
.SetCode("00700")
.Build();
QotGetOptionStrategySpread.C2S c2s = QotGetOptionStrategySpread.C2S.CreateBuilder()
.SetOwner(owner)
.SetOptionStrategy((int)QotCommon.OptionStrategyType.OptionStrategyType_Spread)
.Build();
QotGetOptionStrategySpread.Request req = QotGetOptionStrategySpread.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.GetOptionStrategySpread(req);
Console.Write("Send QotGetOptionStrategySpread: {0}\n", seqNo);
}
public void OnDisconnect(MMAPI_Conn client, long errCode)
{
Console.Write("Qot onDisConnect: {0}\n", errCode);
}
public void OnReply_GetOptionStrategySpread(MMAPI_Conn client, uint nSerialNo, QotGetOptionStrategySpread.Response rsp)
{
Console.Write("Reply: QotGetOptionStrategySpread: {0} {1}\n", nSerialNo, rsp.ToString());
}
public static void Main(String[] args)
{
MMAPI.Init();
Program qot = new Program();
qot.Start();
while (true)
Thread.Sleep(1000 * 600);
}
}
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
50
51
52
53
54
55
56
57
- Output
Qot onInitConnect: ret=0 desc= connID=7459213669204006063
Send QotGetOptionStrategySpread: 3
Reply: QotGetOptionStrategySpread: 3 retType: 0 ...
2
3
int getOptionStrategySpread(QotGetOptionStrategySpread.Request req);
void onReply_GetOptionStrategySpread(MMAPI_Conn client, int nSerialNo, QotGetOptionStrategySpread.Response rsp);
Description
Get the valid spread list for a given option strategy type. Only supports Spread, Strangle, Collar, Butterfly, Condor, IronButterfly, IronCondor, and DiagonalSpread. The returned spread values can be used as the spread parameter of GetOptionStrategy.
Parameters
message C2S
{
required Qot_Common.Security owner = 1;
required int32 option_strategy = 2;
optional string expire_time = 3;
optional string far_expire_time = 4;
optional int32 index_option_type = 5;
optional Qot_Common.QotHeader header = 100;
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
- Underlying stock; HK/US stocks and major indices only
- OptionStrategyType
- Only Spread, Strangle, Collar, Butterfly, Condor, IronButterfly, IronCondor, DiagonalSpread, etc. are supported for spread queries
- Return
message S2C
{
repeated double spreadList = 1;
}
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
- API call result structure: RetType
- Example
public class QotDemo implements MMSPI_Qot, MMSPI_Conn {
MMAPI_Conn_Qot qot = new MMAPI_Conn_Qot();
public QotDemo() {
qot.setClientInfo("javaclient", 1);
qot.setConnSpi(this);
qot.setQotSpi(this);
}
public void start() {
qot.initConnect("127.0.0.1", (short)11111, false);
}
@Override
public void onInitConnect(MMAPI_Conn client, long errCode, String desc)
{
System.out.printf("Qot onInitConnect: ret=%b desc=%s connID=%d\n", errCode, desc, client.getConnectID());
if (errCode != 0)
return;
QotCommon.Security owner = QotCommon.Security.newBuilder()
.setMarket(QotCommon.QotMarket.QotMarket_HK_Security_VALUE)
.setCode("00700")
.build();
QotGetOptionStrategySpread.C2S c2s = QotGetOptionStrategySpread.C2S.newBuilder()
.setOwner(owner)
.setOptionStrategy(QotCommon.OptionStrategyType.OptionStrategyType_Spread_VALUE)
.build();
QotGetOptionStrategySpread.Request req = QotGetOptionStrategySpread.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.getOptionStrategySpread(req);
System.out.printf("Send QotGetOptionStrategySpread: %d\n", seqNo);
}
@Override
public void onDisconnect(MMAPI_Conn client, long errCode) {
System.out.printf("Qot onDisConnect: %d\n", errCode);
}
@Override
public void onReply_GetOptionStrategySpread(MMAPI_Conn client, int nSerialNo, QotGetOptionStrategySpread.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("QotGetOptionStrategySpread failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive QotGetOptionStrategySpread: %s\n", json);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
MMAPI.init()
QotDemo qot = new QotDemo();
qot.start();
while (true) {
try {
Thread.sleep(1000 * 600);
} catch (InterruptedException exc) {
}
}
}
}
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
- Output
Qot onInitConnect: ret=0 desc= connID=7459213669204006063
Send QotGetOptionStrategySpread: 2
Receive QotGetOptionStrategySpread: {
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"spreadList": [5, 10, 20, 40]
}
}
2
3
4
5
6
7
8
9
10
moomoo::u32_t GetOptionStrategySpread(const Qot_GetOptionStrategySpread::Request &stReq);
virtual void OnReply_GetOptionStrategySpread(moomoo::u32_t nSerialNo, const Qot_GetOptionStrategySpread::Response &stRsp) = 0;
Description
Get valid spread list for option strategy
Parameters
message C2S
{
required Qot_Common.Security security = 1; // Underlying stock
required Qot_Common.OptionStrategyType optionStrategy = 2; // Option strategy type
required string expireTime = 3; // Expiration date,格式 yyyy-MM-dd
optional string farExpireTime = 4; // Far expiration date,格式 yyyy-MM-dd
optional Qot_Common.IndexOptionType indexOptionType = 5; // Index option type
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
- Security structure: Security
- OptionStrategyType enum: OptionStrategyType
- IndexOptionType enum: IndexOptionType
- Return
message S2C
{
repeated double spreadList = 1; // Valid spread list
}
message Response
{
required int32 retType = 1 [default = -400]; // Return result,详见 Common.RetType
optional string retMsg = 2; // Return result description
optional int32 errCode = 3; // Error code
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
- API call result structure: RetType
- Example
class Program : public MMSPI_Qot, public MMSPI_Trd, public MMSPI_Conn
{
public:
Program() {
m_pQotApi = MMAPI::CreateQotApi();
m_pQotApi->RegisterQotSpi(this);
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) {
cout << "connect" << endl;
// Build request
Qot_GetOptionStrategySpread::Request req;
Qot_GetOptionStrategySpread::C2S *c2s = req.mutable_c2s();
Qot_Common::Security *owner = c2s->mutable_owner();
owner->set_market(Qot_Common::QotMarket::QotMarket_HK_Security);
owner->set_code("00700");
c2s->set_option_strategy(Qot_Common::OptionStrategyType_Spread); // Vertical spread strategy
m_GetOptionStrategySpreadSerialNo = m_pQotApi->GetOptionStrategySpread(req);
cout << "Request GetOptionStrategySpread SerialNo: " << m_GetOptionStrategySpreadSerialNo << endl;
}
virtual void OnReply_GetOptionStrategySpread(moomoo::u32_t nSerialNo, const Qot_GetOptionStrategySpread::Response &stRsp) {
if (nSerialNo != m_GetOptionStrategySpreadSerialNo) return;
cout << "OnReply_GetOptionStrategySpread SerialNo: " << nSerialNo << endl;
// Parse the internal structure and print
// ProtoBufToBodyData and UTF8ToLocal are defined in tool.h under Sample
string resp_str;
ProtoBufToBodyData(stRsp, resp_str);
cout << UTF8ToLocal(resp_str) << endl;
}
protected:
MMAPI_Qot *m_pQotApi;
moomoo::u32_t m_GetOptionStrategySpreadSerialNo = 0;
};
int32_t main(int32_t argc, char** argv)
{
MMAPI::Init();
{
Program program;
program.Start();
getchar();
}
protobuf::ShutdownProtobufLibrary();
MMAPI::UnInit();
return 0;
}
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
- Output
connect
Request GetOptionStrategySpread SerialNo: 3
OnReply_GetOptionStrategySpread SerialNo: 3
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"spreadList": [5, 10, 20, 40]
}
}
2
3
4
5
6
7
8
9
10
11
GetOptionStrategySpread(req);
Description
Get valid spread list for option strategy
Parameters
message C2S
{
required Qot_Common.Security owner = 1; //Underlying stock; only HK/US stocks and HSI/HSCEI supported
required int32 option_strategy = 2; //OptionStrategyType, option strategy type
optional string expire_time = 3; //Expiry (near), yyyy-MM-dd; empty takes the first
optional string far_expire_time = 4; //Far expiry, required for DiagonalSpread
optional int32 index_option_type = 5; //IndexOptionType; only for HSI/HSCEI
optional Qot_Common.QotHeader header = 100; //Quote common header
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
- Security structure: Security
- OptionStrategyType enum: OptionStrategyType
- IndexOptionType enum: IndexOptionType
- Return
message S2C
{
repeated double spreadList = 1; //Valid spread list, e.g. [0.5, 1.0, 2.0]
}
message Response
{
required int32 retType = 1 [default = -400]; //RetType
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
- API call result structure: RetType
- Example
import mmWebsocket from "moomoo-api";
import { mmCmdID } from "moomoo-api";
import { Common, Qot_Common } from "moomoo-api/proto";
import beautify from "js-beautify";
function QotGetOptionStrategySpread(){
const { RetType } = Common
const { QotMarket, OptionStrategyType } = Qot_Common
let [addr, port, enable_ssl, key] = ["127.0.0.1", 33333, false, '7522027ccf5a06b1'];
let websocket = new mmWebsocket();
websocket.onlogin = (ret, msg)=>{
if (ret) { // 登录成功
const req = {
c2s: {
owner: { market: QotMarket.QotMarket_HK_Security, code: "00700" },
optionStrategy: OptionStrategyType.OptionStrategyType_Strangle, // 宽跨式策略
},
};
websocket.GetOptionStrategySpread(req)
.then((res) => {
let { errCode, retMsg, retType, s2c } = res
console.log("GetOptionStrategySpread: 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);
}
})
.catch((error) => {
console.log("error:", error);
});
} else {
console.log("start error", msg);
}
};
websocket.start(addr, port, enable_ssl, key);
//关闭行情连接,连接不再使用之后,要关闭,否则占用不必要资源
//同时OpenD也限制了最多128条连接
//也可以一个页面或者一个项目维护一条连接,这里范例请求一次创建一条连接
setTimeout(()=>{
websocket.stop();
console.log("stop");
}, 5000); // 5秒后断开
}
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
50
- Output
GetOptionStrategySpread: errCode 0, retMsg , retType 0
{
"spreadList": [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260]
}
stop
2
3
4
5
API Restrictions
- Maximum 30 requests per 30 seconds.