项目作者: santif

项目描述 :
Elixir implementation of FIX Session Protocol FIXT.1.1. Currently only supports FIX initiator (buy side)
高级语言: Elixir
项目地址: git://github.com/santif/ex_fix.git
创建时间: 2017-07-30T02:12:21Z
项目社区:https://github.com/santif/ex_fix

开源协议:Other

下载


ExFIX

Coverage Status
Tokei
Hex.pm Version
License

Elixir implementation of FIX Session Protocol FIXT.1.1.
Currently only supports FIX session initiator (buy side).

Installation

Add ex_fix to your list of dependencies in mix.exs:

  1. def deps do
  2. [{:ex_fix, "~> 0.2.5"}]
  3. end

Features

  • FIXT.1.1 session protocol implementation (session level messages)
  • Message validation: sequence number, length, checksum
  • Auto reconnection (with ResetSeqNumFlag=Y)
  • SSL compatible
  • Session registry
  • Two-phase parse of FIX messages

Usage

  1. defmodule MySessionHandler do
  2. @behaviour ExFix.SessionHandler
  3. require Logger
  4. alias ExFix.InMessage
  5. alias ExFix.OutMessage
  6. alias ExFix.Parser
  7. @msg_new_order_single "D"
  8. @field_account "1"
  9. @field_cl_ord_id "11"
  10. @field_order_qty "38"
  11. @field_ord_type "40"
  12. @field_price "44"
  13. @field_side "54"
  14. @field_symbol "55"
  15. @field_transact_time "60"
  16. @value_side_buy "1"
  17. @value_ord_type_limit "2"
  18. def on_logon(session_name, _env) do
  19. spawn fn() ->
  20. ## Buy 10 shares of SYM1 for $1.23 per share
  21. @msg_new_order_single
  22. |> OutMessage.new()
  23. |> OutMessage.set_field(@field_account, 1234)
  24. |> OutMessage.set_field(@field_cl_ord_id, "cod12345")
  25. |> OutMessage.set_field(@field_order_qty, 10)
  26. |> OutMessage.set_field(@field_ord_type, @value_ord_type_limit)
  27. |> OutMessage.set_field(@field_price, 1.23)
  28. |> OutMessage.set_field(@field_side, @value_side_buy)
  29. |> OutMessage.set_field(@field_symbol, "SYM1")
  30. |> OutMessage.set_field(@field_transact_time, DateTime.utc_now())
  31. |> ExFix.send_message!(session_name)
  32. end
  33. end
  34. def on_app_message(_session_name, _msg_type, %InMessage{} = msg, _env) do
  35. Logger.info "App msg received: #{inspect Parser.parse2(msg)}"
  36. end
  37. def on_session_message(_session_name, _msg_type, %InMessage{} = msg, _env) do
  38. Logger.info "Session msg received: #{inspect Parser.parse2(msg)}"
  39. end
  40. def on_logout(_session_id, _env), do: :ok
  41. end
  42. ExFix.start_session_initiator("simulator", "BUY", "SELL", MySessionHandler,
  43. hostname: "localhost", port: 9876, username: "user1", password: "pwd1",
  44. transport_mod: :ssl)

Output

  1. [debug] Starting FIX session: [simulator]
  2. [debug] [simulator] Trying to connect to localhost:9876...
  3. [info] [fix.outgoing] [simulator] 8=FIXT.1.1^A9=94^A35=A^A34=1^A49=BUY^A52=20170806-20:25:24.194^A56=SELL^A98=0^A108=60^A141=Y^A553=user1^A554=pwd1^A1137=9^A10=012^A
  4. [info] [fix.incoming] [simulator] 8=FIXT.1.1^A9=75^A35=A^A34=1^A49=SELL^A52=20170806-20:25:24.196^A56=BUY^A98=0^A108=60^A141=Y^A1137=9^A10=234^A
  5. [info] Session msg received: %ExFix.InMessage{complete: true, error_reason: nil, fields: [{"35", "A"}, {"34", "1"}, {"49", "SELL"}, {"52", "20170806-20:25:24.196"}, {"56", "BUY"}, {"98", "0"}, {"108", "60"}, {"141", "Y"}, {"1137", "9"}], msg_type: "A", original_fix_msg: <<56, 61, 70, 73, 88, 84, 46, 49, 46, 49, 1, 57, 61, 55, 53, 1, 51, 53, 61, 65, 1, 51, 52, 61, 49, 1, 52, 57, 61, 83, 69, 76, 76, 1, 53, 50, 61, 50, 48, 49, 55, 48, 56, 48, 54, ...>>, other_msgs: "", poss_dup: false, rest_msg: "", seqnum: 1, subject: nil, valid: true}
  6. [info] session_update_status [simulator] - Status: :connected
  7. [info] [fix.outgoing] [simulator] 8=FIXT.1.1^A9=126^A35=D^A34=2^A49=BUY^A52=20170806-20:25:24.202^A56=SELL^A1=1234^A11=cod12345^A38=10^A40=2^A44=1.23^A54=1^A55=SYM1^A60=20170806-20:25:24.202^A10=081^A
  8. [info] [fix.incoming] [simulator] 8=FIXT.1.1^A9=118^A35=8^A34=2^A49=SELL^A52=20170806-20:25:24.203^A56=BUY^A1=1234^A6=1.23^A14=10^A17=2^A31=1.23^A37=2^A39=2^A54=1^A55=SYM1^A150=2^A151=0^A10=151^A
  9. [info] App msg received: %ExFix.InMessage{complete: true, error_reason: nil, fields: [{"35", "8"}, {"34", "2"}, {"49", "SELL"}, {"52", "20170806-20:25:24.203"}, {"56", "BUY"}, {"1", "1234"}, {"6", "1.23"}, {"14", "10"}, {"17", "2"}, {"31", "1.23"}, {"37", "2"}, {"39", "2"}, {"54", "1"}, {"55", "SYM1"}, {"150", "2"}, {"151", "0"}], msg_type: "8", original_fix_msg: <<56, 61, 70, 73, 88, 84, 46, 49, 46, 49, 1, 57, 61, 49, 49, 56, 1, 51, 53, 61, 56, 1, 51, 52, 61, 50, 1, 52, 57, 61, 83, 69, 76, 76, 1, 53, 50, 61, 50, 48, 49, 55, 48, 56, 48, ...>>, other_msgs: "", poss_dup: false, rest_msg: "", seqnum: 2, subject: nil, valid: true}

Two-phase parse

This library allows to parse the FIX message until certain tags are found. At that
point it is possible to send the message to another process (for example, a Book process,
Account process, etc) to complete the parse and execute business logic specifically for
the message’s subject.

As can be seen in the benchmarks below, the two-phase parse substantially decreases
the time spent in the network client process.

To Do list

This is a work in progress. Here is a list of some pending tasks, PRs are welcome.

  • Documentation and more examples
  • Session Logon with ResetSeqNumFlag=N.
  • Multiple hosts configuration, for failover
  • Socket client optimizations
  • Application level:
    • Dictionary based message parse/validation/serialization
    • Automatic generation of parser/validator/serializer from XML dictionary file
    • Repeating groups
  • FIX session acceptor
  • Session scheduling (integration with 3rd party job management libraries)
  • etc.

Benchmarks

Elixir can’t beat the performance of C/C++/Rust, but this library lets you avoid
communication time between an external FIX initiator and the Erlang VM.
In addition, there are fewer dependencies and number of possible failure points.

  • HW: Laptop Dell Latitude E5570 Intel(R) Core(TM) i7-6600U CPU @ 2.60GHz 16 GB RAM
  • Elixir 1.5.1 / Erlang 19.2
  • Parser benchmark: Execution Report with 155 bytes.
  • Serializer benchmark: New Order Single with 115 bytes.
  1. $ mix bench -d 2
  2. ...
  3. ## ExFixBench
  4. benchmark name iterations average time
  5. Parse - Stage 1 (without validation) 1000000 5.25 µs/op
  6. Parse - Stage 1 500000 6.88 µs/op
  7. Serialize 500000 11.66 µs/op
  8. Parse - Full Msg (without validation) 500000 13.58 µs/op
  9. Parse - Full Msg 500000 15.31 µs/op

Maintainer

Santiago Fernandez <santif@gmail.com> (Author)

License

Copyright (c) 2021 Matriz S.A.
http://www.matriz.com.ar

Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  1. http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.