class OptionEventHandlerBase(RspHandlerBase)
Description
Receive option event push notifications. When a configured event alert is triggered, the server will actively push the event information. You need to first set alert conditions via set_option_event_alert and register a callback via handler. Users inherit OptionEventHandlerBase and override on_recv_rsp to receive push notifications.
Parameters
on_recv_rsp callback returns (ret_code, content), where content is a dict:
| Parameter | Type | Description |
| owner_code | str | Underlying code (e.g. 'US.TSLA') |
| option_code | str | Option contract code (e.g. 'US.TSLA250620C250') |
| message | str | Push message text |
Example
from moomoo import *
class OptionEventHandler(OptionEventHandlerBase):
def on_recv_rsp(self, rsp_pb):
ret_code, content = super(OptionEventHandler, self).on_recv_rsp(rsp_pb)
if ret_code != RET_OK:
print("OptionEvent error:", content)
return RET_ERROR, content
print("Received option event push:")
print(" Underlying:", content['owner_code'])
print(" Option:", content['option_code'])
print(" Message:", content['message'])
return RET_OK, content
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
quote_ctx.set_handler(OptionEventHandler())
item = OptionEventAlertItem(
option_market=OptionMarket.US_SECURITY,
option_type=OptionType.CALL,
order_type_list=[AlertOrderType.SWEEP],
size_range_min=100,
)
ret, data = quote_ctx.set_option_event_alert(AlertOpType.ADD, item)
if ret == RET_OK:
print('Alert set successfully, waiting for push...')
else:
print('Failed to set alert:', data)
import time
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
pass
quote_ctx.close()
1
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
Received option event push:
Underlying: US.TSLA
Option: US.TSLA250620C250
Message: TSLA $250 Call 06/20 Large sweep 500 contracts at $12.50
1
2
3
4
Qot_UpdateOptionEvent.proto
message S2C
{
optional Qot_Common.Security owner = 1;
optional Qot_Common.Security option = 2;
optional string message = 3;
}
message Response
{
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
virtual void OnPush_UpdateOptionEvent(MMAPI_Conn client, uint nSerialNo, Qot_UpdateOptionEvent.Response rsp);
message S2C
{
optional Qot_Common.Security owner = 1;
optional Qot_Common.Security option = 2;
optional string message = 3;
}
message Response
{
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
onPush_UpdateOptionEvent(MMAPI_Conn client, int nSerialNo, Qot_UpdateOptionEvent.Response rsp)
message S2C
{
optional Qot_Common.Security owner = 1;
optional Qot_Common.Security option = 2;
optional string message = 3;
}
message Response
{
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
virtual void OnPush_UpdateOptionEvent(const Qot_UpdateOptionEvent::Response &stRsp) = 0;
message S2C
{
optional Qot_Common.Security owner = 1;
optional Qot_Common.Security option = 2;
optional string message = 3;
}
message Response
{
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
message S2C
{
optional Qot_Common.Security owner = 1;
optional Qot_Common.Security option = 2;
optional string message = 3;
}
message Response
{
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
onPush_UpdateOptionEvent(qotUpdateOptionEvent)
message S2C
{
optional Qot_Common.Security owner = 1;
optional Qot_Common.Security option = 2;
optional string message = 3;
}
message Response
{
required int32 retType = 1 [default = -400];
optional string retMsg = 2;
optional int32 errCode = 3;
optional S2C s2c = 4;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import mmWebsocket from "moomoo-api";
import { mmCmdID } from "moomoo-api";
import { Common } from "moomoo-api/proto";
import beautify from "js-beautify";
function QotUpdateOptionEvent(){
const { RetType } = Common
let [addr, port, enable_ssl, key] = ["127.0.0.1", 11112, false, ''];
let websocket = new mmWebsocket();
websocket.onPush = (cmd, res)=>{
if(mmCmdID.QotUpdateOptionEvent.cmd == cmd){
let { retType, s2c } = res
if(retType == RetType.RetType_Succeed){
let data = beautify(JSON.stringify(s2c), {
indent_size: 2,
space_in_empty_paren: true,
});
console.log("UpdateOptionEvent:");
console.log(data);
} else {
console.log("UpdateOptionEvent: error")
}
}
};
websocket.start(addr, port, enable_ssl, key);
setTimeout(()=>{
websocket.stop();
console.log("stop");
}, 3600 * 1000);
}
QotUpdateOptionEvent()
1
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
UpdateOptionEvent:
{
"owner": { "market": 1, "code": "TSLA" },
"option": { "market": 1, "code": "TSLA250620C250" },
"message": "TSLA $250 Call 06/20 大额扫单 500张 成交价$12.50"
}
stop
1
2
3
4
5
6
7