You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.1 KiB
86 lines
2.1 KiB
-- QUIK Ticker Information System |
|
|
|
json = require "JSON" |
|
require "string" |
|
local zmq = require "zeromq" |
|
local quik = require "quik_api" |
|
|
|
running = true |
|
|
|
function get_data(class, code) |
|
local lot_size = tonumber(quik.getParamEx(class, code, "LOTSIZE").param_value) |
|
local tick_size = tonumber(quik.getParamEx(class, code, "SEC_PRICE_STEP").param_value) |
|
local tick_value = tonumber(quik.getParamEx(class, code, "STEPPRICET").param_value) |
|
local long_name = quik.getParamEx(class, code, "LONGNAME").param_image |
|
|
|
return { ticker = class .. "#" .. code, |
|
lot_size = lot_size, |
|
tick_size = tick_size, |
|
tick_value = tick_value, |
|
long_name = long_name } |
|
end |
|
|
|
function handle_message(msg) |
|
quik.message("Incoming message") |
|
local rq = json:decode(msg) |
|
local class, code = string.match(rq.ticker, "(%w+)#(%w+)") |
|
quik.message("Requested: " .. rq.ticker) |
|
|
|
local data = get_data(class, code) |
|
if data == nil then |
|
quik.message("Error") |
|
return "ERROR: can't get data: (" .. class .. "/" .. code .. ")", "" |
|
end |
|
|
|
quik.message("Returning data") |
|
return "OK", json:encode(data) |
|
end |
|
|
|
Ctx = nil |
|
Sock = nil |
|
|
|
function OnStop() |
|
if Sock then |
|
Sock:close() |
|
Sock = nil |
|
end |
|
if Ctx then |
|
Ctx:term() |
|
Ctx = nil |
|
end |
|
end |
|
|
|
function main() |
|
local status, err = pcall(pmain) |
|
if not status then |
|
quik.message("Error: " .. err) |
|
end |
|
OnStop() |
|
end |
|
|
|
function pmain() |
|
local err |
|
Ctx = zmq.new_ctx() |
|
Sock = Ctx:socket(zmq.REP) |
|
Sock:setsockopt_int(zmq.SOCKOPT_LINGER, 0) |
|
_, err = Sock:bind("tcp://*:5523") |
|
if err then |
|
quik.message("Bind error: " .. tostring(err)) |
|
return |
|
end |
|
|
|
local poller = Ctx:poller() |
|
poller:add(Sock, zmq.POLLIN) |
|
while running do |
|
local events, msg |
|
events, err = poller:poll(60000) |
|
if #events > 0 then |
|
quik.message("Incoming") |
|
msg, err = Sock:recv() |
|
if not err then |
|
local outmsg_header, outmsg_data = handle_message(msg) |
|
Sock:send({outmsg_header, outmsg_data}) |
|
end |
|
end |
|
end |
|
end
|
|
|