# 期权损益分析
- Python
- Proto
- C#
- Java
- C++
- JavaScript
get_option_strategy_analysis(combo_leg_list)
介绍
对自定义或多腿期权组合进行损益分析,返回盈亏曲线及相关分析数据。
参数
参数 类型 说明 combo_leg_list list 组合腿列表 元素为 OptionStrategyLeg,结构参见 get_option_strategy返回
参数 类型 说明 ret RET_CODE 接口调用结果 data pd.DataFrame 当 ret == RET_OK,返回期权损益分析结果 str 当 ret != RET_OK,返回错误描述 DataFrame 字段说明:
字段 类型 说明 code str 策略标识代码 name str 策略名称 option_strategy str 期权策略类型 bid1 float 组合买一价 ask1 float 组合卖一价 max_profit float 最大盈利 max_loss float 最大亏损 breakeven_points list 盈亏平衡点 prob_of_profit float 盈利概率 该字段为百分比字段,默认不展示 %,如 20 实际对应 20%delta float Delta theta float Theta
Example
from futu import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
ret, data = quote_ctx.get_option_strategy(code='HK.00700', option_strategy=OptionStrategyType.STRADDLE)
if ret == RET_OK:
index=0
print(data['legs'][index])
ret2,data2 = quote_ctx.get_option_strategy_analysis(data['legs'][index])
if ret2 == RET_OK:
print(data2)
else:
print("get_analysis,error:",data2)
else:
print('error:', data)
quote_ctx.close() # 结束后记得关闭当条连接,防止连接条数用尽
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- Output
[OptionStrategyLeg(code=HK.TCH260522P330000, action=BUY, quantity=1.0), OptionStrategyLeg(code=HK.TCH260522C330000, action=BUY, quantity=1.0)]
code name option_strategy bid1 ask1 max_profit max_loss breakeven_points prob_of_profit delta theta
0 TCH260522C/P330 腾讯 跨式策略 STRADDLE 0.0 130.44 1.000000e+15 -13044.0 [199.56, 460.44] 0.315492 0.974369 -0.785757
2
3
# Qot_OptionStrategyAnalysis.proto
介绍
期权组合损益分析
参数
message C2S
{
repeated OptionStrategyLeg comboLegList = 1; // 组合腿列表
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 组合腿结构参见 OptionStrategyLeg
- 返回
message S2C
{
// 损益分析结果,字段以协议定义为准
}
message Response
{
required int32 retType = 1 [default = -400]; // 返回结果,详见 Common.RetType
optional string retMsg = 2; // 返回结果描述
optional int32 errCode = 3; // 错误码
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
- 接口调用结果,结构参见 RetType
- 期权策略类型枚举参见 OptionStrategyType
uint OptionStrategyAnalysis(QotOptionStrategyAnalysis.Request req);
virtual void OnReply_OptionStrategyAnalysis(FTAPI_Conn client, uint nSerialNo, QotOptionStrategyAnalysis.Response rsp);
介绍
期权组合损益分析。请求时传入组合腿列表 multi_legs(可由 获取期权策略 取得),返回该组合的最大盈利、最大亏损、盈亏平衡点、盈利概率、组合 Delta/Theta 等指标。
参数
message C2S
{
repeated Qot_Common.ComboLeg multi_legs = 1;
optional Qot_Common.QotHeader header = 100;
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- multi_legs 可由 获取期权策略 返回的 multi_legs 取得
- 返回
message S2C
{
required string code = 1;
required string name = 2;
required int32 option_strategy = 3;
optional double bid1 = 4;
optional double ask1 = 5;
optional double max_profit = 6;
optional double max_loss = 7;
repeated double breakeven_points = 8;
optional double prob_of_profit = 9;
optional double delta = 10;
optional double theta = 11;
}
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
21
22
- 接口调用结果,结构参见 RetType
- 期权策略类型枚举参见 OptionStrategyType
- 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.ComboLeg leg1 = QotCommon.ComboLeg.CreateBuilder()
.SetSecurity(QotCommon.Security.CreateBuilder()
.SetMarket((int)QotCommon.QotMarket.QotMarket_HK_Security)
.SetCode("TCH260522P330000")
.Build())
.SetSide((int)TrdCommon.TrdSide.TrdSide_Buy)
.SetQtyRatio(1)
.Build();
QotCommon.ComboLeg leg2 = QotCommon.ComboLeg.CreateBuilder()
.SetSecurity(QotCommon.Security.CreateBuilder()
.SetMarket((int)QotCommon.QotMarket.QotMarket_HK_Security)
.SetCode("TCH260522C330000")
.Build())
.SetSide((int)TrdCommon.TrdSide.TrdSide_Buy)
.SetQtyRatio(1)
.Build();
QotOptionStrategyAnalysis.C2S c2s = QotOptionStrategyAnalysis.C2S.CreateBuilder()
.AddMultiLegs(leg1)
.AddMultiLegs(leg2)
.Build();
QotOptionStrategyAnalysis.Request req = QotOptionStrategyAnalysis.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.OptionStrategyAnalysis(req);
Console.Write("Send QotOptionStrategyAnalysis: {0}\n", seqNo);
}
public void OnDisconnect(FTAPI_Conn client, long errCode)
{
Console.Write("Qot onDisConnect: {0}\n", errCode);
}
public void OnReply_OptionStrategyAnalysis(FTAPI_Conn client, uint nSerialNo, QotOptionStrategyAnalysis.Response rsp)
{
Console.Write("Reply: QotOptionStrategyAnalysis: {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
58
59
60
61
62
63
64
65
66
67
68
69
- Output
Qot onInitConnect: ret=0 desc= connID=7459213669204006063
Send QotOptionStrategyAnalysis: 3
Reply: QotOptionStrategyAnalysis: 3 retType: 0 ...
2
3
int optionStrategyAnalysis(QotOptionStrategyAnalysis.Request req);
void onReply_OptionStrategyAnalysis(FTAPI_Conn client, int nSerialNo, QotOptionStrategyAnalysis.Response rsp);
介绍
期权组合损益分析。请求时传入组合腿列表 multi_legs(可由 获取期权策略 取得),返回该组合的最大盈利、最大亏损、盈亏平衡点、盈利概率、组合 Delta/Theta 等指标。
参数
message C2S
{
repeated Qot_Common.ComboLeg multi_legs = 1;
optional Qot_Common.QotHeader header = 100;
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- multi_legs 可由 获取期权策略 返回的 multi_legs 取得
- 返回
message S2C
{
required string code = 1;
required string name = 2;
required int32 option_strategy = 3;
optional double bid1 = 4;
optional double ask1 = 5;
optional double max_profit = 6;
optional double max_loss = 7;
repeated double breakeven_points = 8;
optional double prob_of_profit = 9;
optional double delta = 10;
optional double theta = 11;
}
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
21
22
- 接口调用结果,结构参见 RetType
- 期权策略类型枚举参见 OptionStrategyType
- 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.ComboLeg leg1 = QotCommon.ComboLeg.newBuilder()
.setSecurity(QotCommon.Security.newBuilder()
.setMarket(QotCommon.QotMarket.QotMarket_HK_Security_VALUE)
.setCode("TCH260522P330000")
.build())
.setSide(TrdCommon.TrdSide.TrdSide_Buy_VALUE)
.setQtyRatio(1)
.build();
QotCommon.ComboLeg leg2 = QotCommon.ComboLeg.newBuilder()
.setSecurity(QotCommon.Security.newBuilder()
.setMarket(QotCommon.QotMarket.QotMarket_HK_Security_VALUE)
.setCode("TCH260522C330000")
.build())
.setSide(TrdCommon.TrdSide.TrdSide_Buy_VALUE)
.setQtyRatio(1)
.build();
QotOptionStrategyAnalysis.C2S c2s = QotOptionStrategyAnalysis.C2S.newBuilder()
.addMultiLegs(leg1)
.addMultiLegs(leg2)
.build();
QotOptionStrategyAnalysis.Request req = QotOptionStrategyAnalysis.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.optionStrategyAnalysis(req);
System.out.printf("Send QotOptionStrategyAnalysis: %d\n", seqNo);
}
@Override
public void onDisconnect(FTAPI_Conn client, long errCode) {
System.out.printf("Qot onDisConnect: %d\n", errCode);
}
@Override
public void onReply_OptionStrategyAnalysis(FTAPI_Conn client, int nSerialNo, QotOptionStrategyAnalysis.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("QotOptionStrategyAnalysis failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive QotOptionStrategyAnalysis: %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
68
69
70
71
72
73
74
75
76
77
78
79
- Output
Qot onInitConnect: ret=0 desc= connID=7459213669204006063
Send QotOptionStrategyAnalysis: 2
Receive QotOptionStrategyAnalysis: {
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"code": "TCH260522C/P330",
"name": "腾讯 跨式策略",
"optionStrategy": 6,
"bid1": 25.6,
"ask1": 26.1,
"maxProfit": 9999999,
"maxLoss": -25.85,
"breakevenPoints": [304.15, 355.85],
"probOfProfit": 0.4231,
"delta": 0.012,
"theta": -0.045
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Futu::u32_t GetOptionStrategyAnalysis(const Qot_GetOptionStrategyAnalysis::Request &stReq);
virtual void OnReply_GetOptionStrategyAnalysis(Futu::u32_t nSerialNo, const Qot_GetOptionStrategyAnalysis::Response &stRsp) = 0;
介绍
期权组合损益分析。请求时传入组合腿列表 multi_legs(可由 获取期权策略 取得),返回该组合的最大盈利、最大亏损、盈亏平衡点、盈利概率、组合 Delta/Theta 等指标。
参数
message C2S
{
repeated Qot_Common.ComboLeg multi_legs = 1; //组合策略合约腿列表,至少传入 1 条(实际下单数量 = 外层 qty × 本腿 qty_ratio)
optional Qot_Common.QotHeader header = 100; //行情公共参数头
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- multi_legs 可由 获取期权策略 返回的 multi_legs 取得
- 返回
message S2C
{
required string code = 1; //组合策略代码
required string name = 2; //组合策略名称
required int32 option_strategy = 3; //OptionStrategyType,组合策略类型
optional double bid1 = 4; //买一价
optional double ask1 = 5; //卖一价
optional double max_profit = 6; //最大盈利,无上限时为 9999999
optional double max_loss = 7; //最大亏损,无下限时为 9999999
repeated double breakeven_points = 8; //盈亏平衡点列表,可能有多个
optional double prob_of_profit = 9; //盈利概率
optional double delta = 10; //组合 Delta
optional double theta = 11; //组合 Theta(每日时间价值损耗)
}
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
13
14
15
16
17
18
19
20
21
22
- 接口调用结果,结构参见 RetType
- 期权策略类型枚举参见 OptionStrategyType
- 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;
// 组包
Qot_GetOptionStrategyAnalysis::Request req;
Qot_GetOptionStrategyAnalysis::C2S *c2s = req.mutable_c2s();
// 组合腿列表,实际场景下通常由 GetOptionStrategy 返回的 multi_legs 取得
Qot_Common::ComboLeg *leg1 = c2s->add_multi_legs();
Qot_Common::Security *sec1 = leg1->mutable_security();
sec1->set_market(Qot_Common::QotMarket::QotMarket_HK_Security);
sec1->set_code("TCH260522P330000");
leg1->set_side(Trd_Common::TrdSide::TrdSide_Buy);
leg1->set_qtyratio(1);
Qot_Common::ComboLeg *leg2 = c2s->add_multi_legs();
Qot_Common::Security *sec2 = leg2->mutable_security();
sec2->set_market(Qot_Common::QotMarket::QotMarket_HK_Security);
sec2->set_code("TCH260522C330000");
leg2->set_side(Trd_Common::TrdSide::TrdSide_Buy);
leg2->set_qtyratio(1);
m_GetOptionStrategyAnalysisSerialNo = m_pQotApi->GetOptionStrategyAnalysis(req);
cout << "Request GetOptionStrategyAnalysis SerialNo: " << m_GetOptionStrategyAnalysisSerialNo << endl;
}
virtual void OnReply_GetOptionStrategyAnalysis(Futu::u32_t nSerialNo, const Qot_GetOptionStrategyAnalysis::Response &stRsp) {
if (nSerialNo != m_GetOptionStrategyAnalysisSerialNo) return;
cout << "OnReply_GetOptionStrategyAnalysis SerialNo: " << nSerialNo << endl;
// 解析内部结构打印出来
// ProtoBufToBodyData 和 UTF8ToLocal 函数的定义参见 Sample 中的 tool.h 文件
string resp_str;
ProtoBufToBodyData(stRsp, resp_str);
cout << UTF8ToLocal(resp_str) << endl;
}
protected:
FTAPI_Qot *m_pQotApi;
Futu::u32_t m_GetOptionStrategyAnalysisSerialNo = 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
72
73
74
75
76
77
78
79
80
81
- Output
connect
Request GetOptionStrategyAnalysis SerialNo: 3
OnReply_GetOptionStrategyAnalysis SerialNo: 3
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"code": "TCH260522C/P330",
"name": "腾讯 跨式策略",
"optionStrategy": 6,
"bid1": 25.6,
"ask1": 26.1,
"maxProfit": 9999999,
"maxLoss": -25.85,
"breakevenPoints": [304.15, 355.85],
"probOfProfit": 0.4231,
"delta": 0.012,
"theta": -0.045
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
GetOptionStrategyAnalysis(req);
介绍
期权组合损益分析
参数
message C2S
{
repeated Qot_Common.ComboLeg multi_legs = 1; //组合策略合约腿列表,至少传入 1 条
optional Qot_Common.QotHeader header = 100; //行情公共参数头
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- 组合腿 multiLegs 可由 GetOptionStrategy 返回的 multiLegs 获得
- 返回
message S2C
{
required string code = 1; //组合策略代码
required string name = 2; //组合策略名称
required int32 option_strategy = 3; //OptionStrategyType,组合策略类型
optional double bid1 = 4; //买一价
optional double ask1 = 5; //卖一价
optional double max_profit = 6; //最大盈利,无上限时为极大值
optional double max_loss = 7; //最大亏损,无下限时为极小值
repeated double breakeven_points = 8;//盈亏平衡点列表,可能有多个
optional double prob_of_profit = 9; //盈利概率
optional double delta = 10; //组合 Delta
optional double theta = 11; //组合 Theta(每日时间价值损耗)
}
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
13
14
15
16
17
18
19
20
21
22
- 接口调用结果,结构参见 RetType
- 期权策略类型枚举参见 OptionStrategyType
- 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 QotGetOptionStrategyAnalysis(){
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) { // 登录成功
// 先获取期权策略,取其中一组组合腿
websocket.GetOptionStrategy({
c2s: {
owner: { market: QotMarket.QotMarket_HK_Security, code: "00700" },
optionStrategy: OptionStrategyType.OptionStrategyType_Straddle,
},
})
.then((res) => {
let { retType, s2c: { strategyList } } = res
if(retType == RetType.RetType_Succeed && strategyList.length > 0){
const req = {
c2s: {
multiLegs: strategyList[0].multiLegs,
},
};
websocket.GetOptionStrategyAnalysis(req)
.then((res) => {
let { errCode, retMsg, retType, s2c } = res
console.log("GetOptionStrategyAnalysis: 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);
});
}
})
.catch((error) => {
console.log("GetOptionStrategy 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
- Output
GetOptionStrategyAnalysis: errCode 0, retMsg , retType 0
{
"code": "TCH260522C/P330",
"name": "腾讯 跨式策略",
"optionStrategy": 6,
"bid1": 0,
"ask1": 130.44,
"maxProfit": 1000000000000000,
"maxLoss": -13044,
"breakevenPoints": [199.56, 460.44],
"probOfProfit": 0.315492,
"delta": 0.974369,
"theta": -0.785757
}
stop
2
3
4
5
6
7
8
9
10
11
12
13
14
15
接口限制
- 不占用期权订阅额度。
- 每 30 秒内最多请求 30 次。
- Python
- Proto
- C#
- Java
- C++
- JavaScript
get_option_strategy_analysis(combo_leg_list)
介绍
对自定义或多腿期权组合进行损益分析,返回盈亏曲线及相关分析数据。
参数
参数 类型 说明 combo_leg_list list 组合腿列表 元素为 OptionStrategyLeg,结构参见 get_option_strategy返回
参数 类型 说明 ret RET_CODE 接口调用结果 data pd.DataFrame 当 ret == RET_OK,返回期权损益分析结果 str 当 ret != RET_OK,返回错误描述 DataFrame 字段说明:
字段 类型 说明 code str 策略标识代码 name str 策略名称 option_strategy str 期权策略类型 bid1 float 组合买一价 ask1 float 组合卖一价 max_profit float 最大盈利 max_loss float 最大亏损 breakeven_points list 盈亏平衡点 prob_of_profit float 盈利概率 该字段为百分比字段,默认不展示 %,如 20 实际对应 20%delta float Delta theta float Theta
Example
from moomoo import *
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
ret, data = quote_ctx.get_option_strategy(code='HK.00700', option_strategy=OptionStrategyType.STRADDLE)
if ret == RET_OK:
index=0
print(data['legs'][index])
ret2,data2 = quote_ctx.get_option_strategy_analysis(data['legs'][index])
if ret2 == RET_OK:
print(data2)
else:
print("get_analysis,error:",data2)
else:
print('error:', data)
quote_ctx.close() # 结束后记得关闭当条连接,防止连接条数用尽
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- Output
[OptionStrategyLeg(code=HK.TCH260522P330000, action=BUY, quantity=1.0), OptionStrategyLeg(code=HK.TCH260522C330000, action=BUY, quantity=1.0)]
code name option_strategy bid1 ask1 max_profit max_loss breakeven_points prob_of_profit delta theta
0 TCH260522C/P330 腾讯 跨式策略 STRADDLE 0.0 130.44 1.000000e+15 -13044.0 [199.56, 460.44] 0.315492 0.974369 -0.785757
2
3
# Qot_OptionStrategyAnalysis.proto
介绍
期权组合损益分析
参数
message C2S
{
repeated OptionStrategyLeg comboLegList = 1; // 组合腿列表
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
- 组合腿结构参见 OptionStrategyLeg
- 返回
message S2C
{
// 损益分析结果,字段以协议定义为准
}
message Response
{
required int32 retType = 1 [default = -400]; // 返回结果,详见 Common.RetType
optional string retMsg = 2; // 返回结果描述
optional int32 errCode = 3; // 错误码
optional S2C s2c = 4;
}
2
3
4
5
6
7
8
9
10
11
12
- 接口调用结果,结构参见 RetType
- 期权策略类型枚举参见 OptionStrategyType
uint OptionStrategyAnalysis(QotOptionStrategyAnalysis.Request req);
virtual void OnReply_OptionStrategyAnalysis(MMAPI_Conn client, uint nSerialNo, QotOptionStrategyAnalysis.Response rsp);
介绍
期权组合损益分析。请求时传入组合腿列表 multi_legs(可由 获取期权策略 取得),返回该组合的最大盈利、最大亏损、盈亏平衡点、盈利概率、组合 Delta/Theta 等指标。
参数
message C2S
{
repeated Qot_Common.ComboLeg multi_legs = 1;
optional Qot_Common.QotHeader header = 100;
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- multi_legs 可由 获取期权策略 返回的 multi_legs 取得
- 返回
message S2C
{
required string code = 1;
required string name = 2;
required int32 option_strategy = 3;
optional double bid1 = 4;
optional double ask1 = 5;
optional double max_profit = 6;
optional double max_loss = 7;
repeated double breakeven_points = 8;
optional double prob_of_profit = 9;
optional double delta = 10;
optional double theta = 11;
}
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
21
22
- 接口调用结果,结构参见 RetType
- 期权策略类型枚举参见 OptionStrategyType
- 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.ComboLeg leg1 = QotCommon.ComboLeg.CreateBuilder()
.SetSecurity(QotCommon.Security.CreateBuilder()
.SetMarket((int)QotCommon.QotMarket.QotMarket_HK_Security)
.SetCode("TCH260522P330000")
.Build())
.SetSide((int)TrdCommon.TrdSide.TrdSide_Buy)
.SetQtyRatio(1)
.Build();
QotCommon.ComboLeg leg2 = QotCommon.ComboLeg.CreateBuilder()
.SetSecurity(QotCommon.Security.CreateBuilder()
.SetMarket((int)QotCommon.QotMarket.QotMarket_HK_Security)
.SetCode("TCH260522C330000")
.Build())
.SetSide((int)TrdCommon.TrdSide.TrdSide_Buy)
.SetQtyRatio(1)
.Build();
QotOptionStrategyAnalysis.C2S c2s = QotOptionStrategyAnalysis.C2S.CreateBuilder()
.AddMultiLegs(leg1)
.AddMultiLegs(leg2)
.Build();
QotOptionStrategyAnalysis.Request req = QotOptionStrategyAnalysis.Request.CreateBuilder().SetC2S(c2s).Build();
uint seqNo = qot.OptionStrategyAnalysis(req);
Console.Write("Send QotOptionStrategyAnalysis: {0}\n", seqNo);
}
public void OnDisconnect(MMAPI_Conn client, long errCode)
{
Console.Write("Qot onDisConnect: {0}\n", errCode);
}
public void OnReply_OptionStrategyAnalysis(MMAPI_Conn client, uint nSerialNo, QotOptionStrategyAnalysis.Response rsp)
{
Console.Write("Reply: QotOptionStrategyAnalysis: {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
58
59
60
61
62
63
64
65
66
67
68
69
- Output
Qot onInitConnect: ret=0 desc= connID=7459213669204006063
Send QotOptionStrategyAnalysis: 3
Reply: QotOptionStrategyAnalysis: 3 retType: 0 ...
2
3
int optionStrategyAnalysis(QotOptionStrategyAnalysis.Request req);
void onReply_OptionStrategyAnalysis(MMAPI_Conn client, int nSerialNo, QotOptionStrategyAnalysis.Response rsp);
介绍
期权组合损益分析。请求时传入组合腿列表 multi_legs(可由 获取期权策略 取得),返回该组合的最大盈利、最大亏损、盈亏平衡点、盈利概率、组合 Delta/Theta 等指标。
参数
message C2S
{
repeated Qot_Common.ComboLeg multi_legs = 1;
optional Qot_Common.QotHeader header = 100;
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- multi_legs 可由 获取期权策略 返回的 multi_legs 取得
- 返回
message S2C
{
required string code = 1;
required string name = 2;
required int32 option_strategy = 3;
optional double bid1 = 4;
optional double ask1 = 5;
optional double max_profit = 6;
optional double max_loss = 7;
repeated double breakeven_points = 8;
optional double prob_of_profit = 9;
optional double delta = 10;
optional double theta = 11;
}
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
21
22
- 接口调用结果,结构参见 RetType
- 期权策略类型枚举参见 OptionStrategyType
- 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.ComboLeg leg1 = QotCommon.ComboLeg.newBuilder()
.setSecurity(QotCommon.Security.newBuilder()
.setMarket(QotCommon.QotMarket.QotMarket_HK_Security_VALUE)
.setCode("TCH260522P330000")
.build())
.setSide(TrdCommon.TrdSide.TrdSide_Buy_VALUE)
.setQtyRatio(1)
.build();
QotCommon.ComboLeg leg2 = QotCommon.ComboLeg.newBuilder()
.setSecurity(QotCommon.Security.newBuilder()
.setMarket(QotCommon.QotMarket.QotMarket_HK_Security_VALUE)
.setCode("TCH260522C330000")
.build())
.setSide(TrdCommon.TrdSide.TrdSide_Buy_VALUE)
.setQtyRatio(1)
.build();
QotOptionStrategyAnalysis.C2S c2s = QotOptionStrategyAnalysis.C2S.newBuilder()
.addMultiLegs(leg1)
.addMultiLegs(leg2)
.build();
QotOptionStrategyAnalysis.Request req = QotOptionStrategyAnalysis.Request.newBuilder().setC2S(c2s).build();
int seqNo = qot.optionStrategyAnalysis(req);
System.out.printf("Send QotOptionStrategyAnalysis: %d\n", seqNo);
}
@Override
public void onDisconnect(MMAPI_Conn client, long errCode) {
System.out.printf("Qot onDisConnect: %d\n", errCode);
}
@Override
public void onReply_OptionStrategyAnalysis(MMAPI_Conn client, int nSerialNo, QotOptionStrategyAnalysis.Response rsp) {
if (rsp.getRetType() != 0) {
System.out.printf("QotOptionStrategyAnalysis failed: %s\n", rsp.getRetMsg());
}
else {
try {
String json = JsonFormat.printer().print(rsp);
System.out.printf("Receive QotOptionStrategyAnalysis: %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
68
69
70
71
72
73
74
75
76
77
78
79
- Output
Qot onInitConnect: ret=0 desc= connID=7459213669204006063
Send QotOptionStrategyAnalysis: 2
Receive QotOptionStrategyAnalysis: {
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"code": "TCH260522C/P330",
"name": "腾讯 跨式策略",
"optionStrategy": 6,
"bid1": 25.6,
"ask1": 26.1,
"maxProfit": 9999999,
"maxLoss": -25.85,
"breakevenPoints": [304.15, 355.85],
"probOfProfit": 0.4231,
"delta": 0.012,
"theta": -0.045
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
moomoo::u32_t GetOptionStrategyAnalysis(const Qot_GetOptionStrategyAnalysis::Request &stReq);
virtual void OnReply_GetOptionStrategyAnalysis(moomoo::u32_t nSerialNo, const Qot_GetOptionStrategyAnalysis::Response &stRsp) = 0;
介绍
期权组合损益分析。请求时传入组合腿列表 multi_legs(可由 获取期权策略 取得),返回该组合的最大盈利、最大亏损、盈亏平衡点、盈利概率、组合 Delta/Theta 等指标。
参数
message C2S
{
repeated Qot_Common.ComboLeg multi_legs = 1; //组合策略合约腿列表,至少传入 1 条(实际下单数量 = 外层 qty × 本腿 qty_ratio)
optional Qot_Common.QotHeader header = 100; //行情公共参数头
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- multi_legs 可由 获取期权策略 返回的 multi_legs 取得
- 返回
message S2C
{
required string code = 1; //组合策略代码
required string name = 2; //组合策略名称
required int32 option_strategy = 3; //OptionStrategyType,组合策略类型
optional double bid1 = 4; //买一价
optional double ask1 = 5; //卖一价
optional double max_profit = 6; //最大盈利,无上限时为 9999999
optional double max_loss = 7; //最大亏损,无下限时为 9999999
repeated double breakeven_points = 8; //盈亏平衡点列表,可能有多个
optional double prob_of_profit = 9; //盈利概率
optional double delta = 10; //组合 Delta
optional double theta = 11; //组合 Theta(每日时间价值损耗)
}
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
13
14
15
16
17
18
19
20
21
22
- 接口调用结果,结构参见 RetType
- 期权策略类型枚举参见 OptionStrategyType
- 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;
// 组包
Qot_GetOptionStrategyAnalysis::Request req;
Qot_GetOptionStrategyAnalysis::C2S *c2s = req.mutable_c2s();
// 组合腿列表,实际场景下通常由 GetOptionStrategy 返回的 multi_legs 取得
Qot_Common::ComboLeg *leg1 = c2s->add_multi_legs();
Qot_Common::Security *sec1 = leg1->mutable_security();
sec1->set_market(Qot_Common::QotMarket::QotMarket_HK_Security);
sec1->set_code("TCH260522P330000");
leg1->set_side(Trd_Common::TrdSide::TrdSide_Buy);
leg1->set_qtyratio(1);
Qot_Common::ComboLeg *leg2 = c2s->add_multi_legs();
Qot_Common::Security *sec2 = leg2->mutable_security();
sec2->set_market(Qot_Common::QotMarket::QotMarket_HK_Security);
sec2->set_code("TCH260522C330000");
leg2->set_side(Trd_Common::TrdSide::TrdSide_Buy);
leg2->set_qtyratio(1);
m_GetOptionStrategyAnalysisSerialNo = m_pQotApi->GetOptionStrategyAnalysis(req);
cout << "Request GetOptionStrategyAnalysis SerialNo: " << m_GetOptionStrategyAnalysisSerialNo << endl;
}
virtual void OnReply_GetOptionStrategyAnalysis(moomoo::u32_t nSerialNo, const Qot_GetOptionStrategyAnalysis::Response &stRsp) {
if (nSerialNo != m_GetOptionStrategyAnalysisSerialNo) return;
cout << "OnReply_GetOptionStrategyAnalysis SerialNo: " << nSerialNo << endl;
// 解析内部结构打印出来
// ProtoBufToBodyData 和 UTF8ToLocal 函数的定义参见 Sample 中的 tool.h 文件
string resp_str;
ProtoBufToBodyData(stRsp, resp_str);
cout << UTF8ToLocal(resp_str) << endl;
}
protected:
MMAPI_Qot *m_pQotApi;
moomoo::u32_t m_GetOptionStrategyAnalysisSerialNo = 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
72
73
74
75
76
77
78
79
80
81
- Output
connect
Request GetOptionStrategyAnalysis SerialNo: 3
OnReply_GetOptionStrategyAnalysis SerialNo: 3
{
"retType": 0,
"retMsg": "",
"errCode": 0,
"s2c": {
"code": "TCH260522C/P330",
"name": "腾讯 跨式策略",
"optionStrategy": 6,
"bid1": 25.6,
"ask1": 26.1,
"maxProfit": 9999999,
"maxLoss": -25.85,
"breakevenPoints": [304.15, 355.85],
"probOfProfit": 0.4231,
"delta": 0.012,
"theta": -0.045
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
GetOptionStrategyAnalysis(req);
介绍
期权组合损益分析
参数
message C2S
{
repeated Qot_Common.ComboLeg multi_legs = 1; //组合策略合约腿列表,至少传入 1 条
optional Qot_Common.QotHeader header = 100; //行情公共参数头
}
message Request
{
required C2S c2s = 1;
}
2
3
4
5
6
7
8
9
10
- 组合腿 multiLegs 可由 GetOptionStrategy 返回的 multiLegs 获得
- 返回
message S2C
{
required string code = 1; //组合策略代码
required string name = 2; //组合策略名称
required int32 option_strategy = 3; //OptionStrategyType,组合策略类型
optional double bid1 = 4; //买一价
optional double ask1 = 5; //卖一价
optional double max_profit = 6; //最大盈利,无上限时为极大值
optional double max_loss = 7; //最大亏损,无下限时为极小值
repeated double breakeven_points = 8;//盈亏平衡点列表,可能有多个
optional double prob_of_profit = 9; //盈利概率
optional double delta = 10; //组合 Delta
optional double theta = 11; //组合 Theta(每日时间价值损耗)
}
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
13
14
15
16
17
18
19
20
21
22
- 接口调用结果,结构参见 RetType
- 期权策略类型枚举参见 OptionStrategyType
- 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 QotGetOptionStrategyAnalysis(){
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) { // 登录成功
// 先获取期权策略,取其中一组组合腿
websocket.GetOptionStrategy({
c2s: {
owner: { market: QotMarket.QotMarket_HK_Security, code: "00700" },
optionStrategy: OptionStrategyType.OptionStrategyType_Straddle,
},
})
.then((res) => {
let { retType, s2c: { strategyList } } = res
if(retType == RetType.RetType_Succeed && strategyList.length > 0){
const req = {
c2s: {
multiLegs: strategyList[0].multiLegs,
},
};
websocket.GetOptionStrategyAnalysis(req)
.then((res) => {
let { errCode, retMsg, retType, s2c } = res
console.log("GetOptionStrategyAnalysis: 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);
});
}
})
.catch((error) => {
console.log("GetOptionStrategy 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
- Output
GetOptionStrategyAnalysis: errCode 0, retMsg , retType 0
{
"code": "TCH260522C/P330",
"name": "腾讯 跨式策略",
"optionStrategy": 6,
"bid1": 0,
"ask1": 130.44,
"maxProfit": 1000000000000000,
"maxLoss": -13044,
"breakevenPoints": [199.56, 460.44],
"probOfProfit": 0.315492,
"delta": 0.974369,
"theta": -0.785757
}
stop
2
3
4
5
6
7
8
9
10
11
12
13
14
15
接口限制
- 不占用期权订阅额度。
- 每 30 秒内最多请求 30 次。