clojure-quant / fix-encoding

0 stars 1 forks source link

fix-message decoding (groups = vector) #1

Open awb99 opened 10 months ago

awb99 commented 10 months ago

The FIX Protocol requires that fields within repeating groups follow the same order as listed in the message definition within the FIX Specification document.

; fix-dictionary definition
{:msg-type “W”
 :name “MarketDataFullRefresh”
 :type :vector
 :fields [{:tag 55 :name :symbol :type :string}
            {:tag 109 :name subscription-id :type :integer :optional true}
           {:tag 205 :name :no-quotes :type :vector
            :fields [{:tag 209 :name :price :type :double}
                        {:tag 210 :name :side :type :enum]}
                        {:tag 211 :name :ordertype :type :enum :optional true]}
]}

; example fix message
; note that all optional fields are missing, and this is still a valid fix message.
[55 "EURUSD"
 205 2    ; group quotes, 2 quotes follow, tags need to be in order
 209 1.1077 ;quote1 price
 210 1 ;quote1 side
 209 1.0998 ;quote2 price
 210 2] ; quote2 side

; decoded message
{:symbol "EURUSD"
 :quotes [{:side :sell :price 1.1077}
               {:side :buy :price 1.0998}]}

;; note: fix messages also have a header (ids and time) and a footer (checksum)

Seq of tag-value-tuples

(declare decode-vector)

(defn decode-tag-value [field tag-value read-next]
(let [[tag value] tag-value]
  (if tag
    (let [{:keys [name type fields]} field]
(case type
  :string [name value]
  :integer [name (to-int value)]
  :double [name (to-double value)]
  :vector [name (decode-vector value fields read-next)]))
  [:end nil])))

(defn decode-component [fields read-next read-undo]
(map (fn [field]
            (let [tag-value (read-next)]
            (if (= (:tag tag-value) (:tag field)
               (decode-tag-value field tag-value read-next)
               (do (read-undo)      
               nil)) fields))

(defn decode-vector [nr fields read-next read-undo]
  (map. #(decode-component fields read-next read-undo) (range nr)))

(defn decode-msg [fields read
awb99 commented 10 months ago

api differences are explained here as well: https://github.com/newstrading/NT-universalfx/tree/master/Documents/TradingApis

awb99 commented 10 months ago

nice fix message specification for ctrader: https://help.ctrader.com/fix/specification/#background-of-fix-api-in-fx-trading its downloaded to spec/ctrader

awb99 commented 10 months ago

rapidaddition specs are in spec/rapid onix specs are in spec/onix

awb99 commented 10 months ago

FIX Dictionaries