Atmo101
Atmo101

Reputation: 81

Parsing FIX messages to json in Quickfix/Go in Go

We are using Quickfix/Go as a FIX engine, and would like to parse the messages into human readable json fieldName:value.

There's an already answered question similar but it only has Java and C# which all provide a function to iterate over Field Groups : How to log QuickFix message in human-readable format

I have written some code that parses the xml dictionary and gets the FieldMap from message header, trailer and body. Then this function below should print the fieldMap, but there are groups, and I couldn't access these groups using the Quickfix/Go methods

func PrintFieldMap(fieldMap quickfix.FieldMap, dd *datadictionary.DataDictionary) {
    for _, tag := range fieldMap.Tags() {
        value, _ := fieldMap.GetString(quickfix.Tag(tag))

        fieldType, ok := dd.FieldTypeByTag[int(tag)]

        if ok {
            fieldName := fieldType.Name()
            fieldType := fieldType.Type

            fmt.Printf("%s - %d :   %s \n", fieldName, tag, value)
            if fieldType == "NUMINGROUP" {
                fmt.Println("this is a group")
            }
        } else {
            fmt.Println("tag not in dict:", tag)
        }
    }

}

so using the same raw message from the stackoverflow answered post :

8=FIX.4.4\0019=247\00135=s\00134=5\00149=sender\00152=20060319-09:08:20.881\00156=target\00122=8\00140=2\00144=9\00148=ABC\00155=ABC\00160=20060319-09:08:19\001548=184214\001549=2\001550=0\001552=2\00154=1\001453=2\001448=8\001447=D\001452=4\001448=AAA35777\001447=D\001452=3\00138=9\00154=2\001453=2\001448=8\001447=D\001452=4\001448=aaa\001447=D\001452=3\00138=9\00110=056\001

I get :

******* Header *******
BodyLength - 9  :       247 
MsgType - 35    :       s 
MsgSeqNum - 34  :       5 
SenderCompID - 49       :       sender 
SendingTime - 52        :       20060319-09:08:20.881 
TargetCompID - 56       :       target 
BeginString - 8 :       FIX.4.4 
******* Body *******
OrdType - 40    :       2 
TransactTime - 60       :       20060319-09:08:19 
CrossPrioritization - 550       :       0 
NoSides - 552   :       2 
this is a group
Symbol - 55     :       ABC 
CrossType - 549 :       2 
PartyRole - 452 :       3 
PartyID - 448   :       aaa 
PartyIDSource - 447     :       D 
SecurityIDSource - 22   :       8 
SecurityID - 48 :       ABC 
CrossID - 548   :       184214 
NoPartyIDs - 453        :       2 
this is a group
Price - 44      :       9 
Side - 54       :       2 
OrderQty - 38   :       9 
******* Trailer *******
CheckSum - 10   :       056

to access the groups, the doc gives the method : func (m FieldMap) GetGroup(parser FieldGroupReader) MessageRejectError but there is no way to create a FieldGroupReader, and docs don't go into how to use.

Upvotes: 2

Views: 594

Answers (1)

Atmo101
Atmo101

Reputation: 81

After looking at the source code of QuickFix/go test cases, I've found how to get the grouped fields :

    template := quickfix.GroupTemplate{
        quickfix.GroupElement(448),
        quickfix.GroupElement(447),
        quickfix.GroupElement(452),
    }
    tag := quickfix.Tag(453)
    f := quickfix.NewRepeatingGroup(tag, template)
    err = bodyFieldMap.GetGroup(f)
    if err != nil {
        fmt.Println("some error:", err)
    }
    for i := 0; i < f.Len(); i++ {
        str448, _ := f.Get(0).GetString(448)
        str447, _ := f.Get(0).GetString(447)
        str452, _ := f.Get(0).GetString(452)

        fmt.Println("str448:", str448)
        fmt.Println("str447:", str447)
        fmt.Println("str452:", str452)
        fmt.Println("--------------")
    }

gives the two groups :

str448: 8
str447: D
str452: 4
--------------
str448: aaa
str447: D
str452: 3
--------------

Upvotes: 2

Related Questions