MongoDB in FS
Position & Trade Management

    27th November 2012
          Daniel Roberts
Relational Database Challenges
Data Types                   Agile Development
• Unstructured data          • Iterative
• Semi-structured            • Short development
  data                         cycles
• Polymorphic data           • New workloads




Volume of Data               New Architectures
• Petabytes of data          • Horizontal scaling
• Trillions of records       • Commodity
                               servers
• Tens of millions of
  queries per second         • Cloud computing


                         2
Why MongoDB in FS?


•   Flexible schemas
•   Scale Out
•   Aggregations
                        •   High Volume Data Feeds
•   Agile Development   •   Tick Data / Time series
                        •   Risk Analytics
                        •   Product Catalogs & Trade Capture
                        •   P&L Reporting
                        •   Reference Data Management
                        •   Portfolio Management
                        •   Quantitative Analysis
                        •   Automated Trading

                        3
Agile development

RDBMS               MongoDB

                    {
                    _id: ObjectId("4c4ba5e5e8aabf3"),

                    "NewOrder-Single" : {

                    "Header" : {

                            "BeginString" : "FIX.4.2",

                            "BodyLength" : 190,

                            "MsgType" : "D",

                            "HeaderFields" : {

                            "SenderCompID" : "Client",

                                    "TargetCompID" : "TradingGateway",

                                    "MsgSeqNum" : 4,

                                    "SendingTime" : {

                                    "UTCFormat_2051-100" :

                    "Fri Jun 01 09:36:26 BST 2012”

                    }....




                4
Scalable Out




               5
Global Replication



                     Primary
         Secondary




                               Secondary




                      6
MongoDB Solutions
Use cases


 Order Capture                       Product Catalogs



 Aggregate / calculate data          Trade History


 Globally replicated reference
                                     Time Series data
 data


 Aggregation Framework               Fast, In-Place Updates




                                 8
Product Catalogs

  Catalogs of complex financial products
  • ‘Exotics’ difficult to model in relational db.
  • ‘On-boarding’ new products in hours.
  • Representation in RDBMS may require >~50 tables.
  • What’s the impact from technology?
  • Once created how do we capture the details of a new trade?

  Why MongoDB?
  • Flexible schema means we don’t need to go back to the database
    when we have a product to sell.
  • Single collection for all products... even if they vary greatly
  • Trades potentially exist for long periods
  • Newer trade can have different data with not impact on the db.




                                  9
Order Capture

                        {
                        _id: ObjectId("4c4ba5.."),
                        "NewOrder-Single" : {
                        "Header" : {

• Capture messages              "BeginString" : "FIX.4.2",
                                "BodyLength" : 190,
                                "MsgType" : "D",
• Variable formats              "HeaderFields" : {
                                "SenderCompID" : "Client",
  – Eg Fix 4.2 or 5.0            "TargetCompID" :
                        "TradingGateway",
                                     "MsgSeqNum" : 4,
                                     "SendingTime" : {
                                     "UTCFormat_2051-100" :
                        "Fri Jun 01 09:36:26 BST 2012”
                        }....



                        10
Execution reports

  {
      "_id" : ObjectId("5051d573036441c9062d6629"),
            "ExecutionReport" : {
            "StandardMessageHeader" : {
                 "BeginString" : "FIX.4.2",
                 "BodyLength" : 265,
                 "MsgType" : "8”,
            },
            "ExecutionReportBody" : {
                 "OrderID" : "OrderID_142201",
                 "ClOrdID" : "20120909-142159-4597031",
                       "Instrument" : {"Symbol" : "MSFT” },
                 "OrderQtyData" : { "OrderQty" : 100000 },
                 "OrdTYpe" : "1",
                 "Currency" : "USD",
                 "LastShares" : 50000,
                 "LastPx" : 100.5,
                 "LeavesQty" : 50000,
                 "CumQty" : 50000,
                 "AvgPx" : 100.5,
                 "TransactTime" : {
                       "UTC" : "Sun Sep 09 14:22:01 BST 2012"
                 },
                 …..
            }
  }


                                       11
Aggregation Framework

db.ExecutionReport.aggregate(
{
  $match : {"Instrument.Symbol":"MSFT"},

 $group : {
  _id : "$ExecutionReport.ReportBody.ClOrdID",

          reportsPerOrd : { $sum : 1 },

      totalNumOrdered :   { $sum :
             "$ExecutionReport.ReportBody.OrderQtyData.OrderQty"},

               avgPrice : { $avg :
                "$ExecutionReport.ExecutionReportBody.AvgPx"
      }
  }
});


                                              12
Results
{
    "result" : [
         {
                   "_id" : "20120909-142604-3800196",
                   "reportsPerOrd" : 2,
                   "totalNumOrdered" : 200000,
                   "avgPrice" : 100.5
          },
          {
                   "_id" : "20120909-142603-7560516",
                   "reportsPerOrd" : 2,
                   "totalNumOrdered" : 200000,
                   "avgPrice" : 100.25
          }
          …
    ],
    "ok" : 1
}




                                             13
Aggregate View of Positions

 { "account" : 123000234, "instrument" : "MSFT", "quantity" : 10000, "ts" :
 ISODate("2012-12-25T00:00:00Z"), "buyPrice" : 26.67 }

 { "account" : 123000234, "instrument" : "MSFT", "quantity" : 4534, "ts" :
 ISODate("2012-12-27T00:00:00Z"), "buyPrice" : 27.39 }

 { "account" : 123000234, "instrument" : "MSFT", "quantity" : 4534, "ts" :
 ISODate("2012-12-20T00:00:00Z"), "buyPrice" : 24.39 }

 { "account" : 123000234, "instrument" : "GOOG", "quantity" : 8768, "ts" :
 ISODate("2012-12-25T00:00:00Z"), "buyPrice" : 526.67 }

 { "account" : 123000234, "instrument" : "GOOG", "quantity" : 324, "ts" :
 ISODate("2012-12-20T00:00:00Z"), "buyPrice" : 630.59 }




                                      14
Calculate P&L – aggregated positions

var currentPrice = 656.54;
db.pl.aggregate([
     {$match:{"instrument" : "GOOG"}},
     {$project : {instrument:1,
         currentValue :       { $multiply:["$quantity", currentPrice] },
         purchaseValue : { $multiply:["$quantity", "$buyPrice"]}}
     },
     {$group : {_id: "$instrument",
         currentValue :       {$sum : "$currentValue"},
         purchaseValue: {$sum : "$purchaseValue"}}
     },
     {$project: {_id: 0, instrument: "$_id",
         currentValue: 1,
         purchaseValue: 1,
         profit :             {$subtract : ["$currentValue","$purchaseValue"]}}}
]);


                                       15
Results


    {
        "result" : [
             {
                   "currentValue" :    5969261.68,
                   "purchaseValue" :   4822153.72,
                   "instrument" :      "GOOG",
                   "profit" :          1147107.96
             }
        ],
        "ok" : 1
    }




                                  16
Trade History

  • Store historical trades and deltas over time
  • Maintain all data online.. Eg. 7 years
  • Recent data maintained hot in ‘Working Set’
                         300 GB Working Set




             128 GB           128 GB          128 GB
              RAM              RAM             RAM



              1 TB             1 TB            1 TB
             Data on          Data on         Data on
              Disk             Disk            Disk




             Shard 1         Shard 2          Shard 3




                                  17
Summary


  •   Product Catalogs
  •   Trade Capture
  •    Handle changes easily over time
  •   Aggregate trades / orders / executions
  •   Calculate P&L across stored positions
  •   Store trade updates and deltas




                                18
For More Information

 Resource                     User Data Management
                              Location

 MongoDB Downloads            www.mongodb.org/download

 Free Online Training         education.10gen.com

 Webinars and Events          www.10gen.com/events

 White Papers                 www.10gen.com/white-papers

 Customer Case Studies        www.10gen.com/customers

 Presentations                www.10gen.com/presentations

 Documentation                docs.mongodb.org

 Additional Info              info@10gen.com


                         19
Webinar: Position and Trade Management with MongoDB

Webinar: Position and Trade Management with MongoDB

  • 1.
    MongoDB in FS Position& Trade Management 27th November 2012 Daniel Roberts
  • 2.
    Relational Database Challenges DataTypes Agile Development • Unstructured data • Iterative • Semi-structured • Short development data cycles • Polymorphic data • New workloads Volume of Data New Architectures • Petabytes of data • Horizontal scaling • Trillions of records • Commodity servers • Tens of millions of queries per second • Cloud computing 2
  • 3.
    Why MongoDB inFS? • Flexible schemas • Scale Out • Aggregations • High Volume Data Feeds • Agile Development • Tick Data / Time series • Risk Analytics • Product Catalogs & Trade Capture • P&L Reporting • Reference Data Management • Portfolio Management • Quantitative Analysis • Automated Trading 3
  • 4.
    Agile development RDBMS MongoDB { _id: ObjectId("4c4ba5e5e8aabf3"), "NewOrder-Single" : { "Header" : { "BeginString" : "FIX.4.2", "BodyLength" : 190, "MsgType" : "D", "HeaderFields" : { "SenderCompID" : "Client", "TargetCompID" : "TradingGateway", "MsgSeqNum" : 4, "SendingTime" : { "UTCFormat_2051-100" : "Fri Jun 01 09:36:26 BST 2012” }.... 4
  • 5.
  • 6.
    Global Replication Primary Secondary Secondary 6
  • 7.
  • 8.
    Use cases OrderCapture Product Catalogs Aggregate / calculate data Trade History Globally replicated reference Time Series data data Aggregation Framework Fast, In-Place Updates 8
  • 9.
    Product Catalogs Catalogs of complex financial products • ‘Exotics’ difficult to model in relational db. • ‘On-boarding’ new products in hours. • Representation in RDBMS may require >~50 tables. • What’s the impact from technology? • Once created how do we capture the details of a new trade? Why MongoDB? • Flexible schema means we don’t need to go back to the database when we have a product to sell. • Single collection for all products... even if they vary greatly • Trades potentially exist for long periods • Newer trade can have different data with not impact on the db. 9
  • 10.
    Order Capture { _id: ObjectId("4c4ba5.."), "NewOrder-Single" : { "Header" : { • Capture messages "BeginString" : "FIX.4.2", "BodyLength" : 190, "MsgType" : "D", • Variable formats "HeaderFields" : { "SenderCompID" : "Client", – Eg Fix 4.2 or 5.0 "TargetCompID" : "TradingGateway", "MsgSeqNum" : 4, "SendingTime" : { "UTCFormat_2051-100" : "Fri Jun 01 09:36:26 BST 2012” }.... 10
  • 11.
    Execution reports { "_id" : ObjectId("5051d573036441c9062d6629"), "ExecutionReport" : { "StandardMessageHeader" : { "BeginString" : "FIX.4.2", "BodyLength" : 265, "MsgType" : "8”, }, "ExecutionReportBody" : { "OrderID" : "OrderID_142201", "ClOrdID" : "20120909-142159-4597031", "Instrument" : {"Symbol" : "MSFT” }, "OrderQtyData" : { "OrderQty" : 100000 }, "OrdTYpe" : "1", "Currency" : "USD", "LastShares" : 50000, "LastPx" : 100.5, "LeavesQty" : 50000, "CumQty" : 50000, "AvgPx" : 100.5, "TransactTime" : { "UTC" : "Sun Sep 09 14:22:01 BST 2012" }, ….. } } 11
  • 12.
    Aggregation Framework db.ExecutionReport.aggregate( { $match : {"Instrument.Symbol":"MSFT"}, $group : { _id : "$ExecutionReport.ReportBody.ClOrdID", reportsPerOrd : { $sum : 1 }, totalNumOrdered : { $sum : "$ExecutionReport.ReportBody.OrderQtyData.OrderQty"}, avgPrice : { $avg : "$ExecutionReport.ExecutionReportBody.AvgPx" } } }); 12
  • 13.
    Results { "result" : [ { "_id" : "20120909-142604-3800196", "reportsPerOrd" : 2, "totalNumOrdered" : 200000, "avgPrice" : 100.5 }, { "_id" : "20120909-142603-7560516", "reportsPerOrd" : 2, "totalNumOrdered" : 200000, "avgPrice" : 100.25 } … ], "ok" : 1 } 13
  • 14.
    Aggregate View ofPositions { "account" : 123000234, "instrument" : "MSFT", "quantity" : 10000, "ts" : ISODate("2012-12-25T00:00:00Z"), "buyPrice" : 26.67 } { "account" : 123000234, "instrument" : "MSFT", "quantity" : 4534, "ts" : ISODate("2012-12-27T00:00:00Z"), "buyPrice" : 27.39 } { "account" : 123000234, "instrument" : "MSFT", "quantity" : 4534, "ts" : ISODate("2012-12-20T00:00:00Z"), "buyPrice" : 24.39 } { "account" : 123000234, "instrument" : "GOOG", "quantity" : 8768, "ts" : ISODate("2012-12-25T00:00:00Z"), "buyPrice" : 526.67 } { "account" : 123000234, "instrument" : "GOOG", "quantity" : 324, "ts" : ISODate("2012-12-20T00:00:00Z"), "buyPrice" : 630.59 } 14
  • 15.
    Calculate P&L –aggregated positions var currentPrice = 656.54; db.pl.aggregate([ {$match:{"instrument" : "GOOG"}}, {$project : {instrument:1, currentValue : { $multiply:["$quantity", currentPrice] }, purchaseValue : { $multiply:["$quantity", "$buyPrice"]}} }, {$group : {_id: "$instrument", currentValue : {$sum : "$currentValue"}, purchaseValue: {$sum : "$purchaseValue"}} }, {$project: {_id: 0, instrument: "$_id", currentValue: 1, purchaseValue: 1, profit : {$subtract : ["$currentValue","$purchaseValue"]}}} ]); 15
  • 16.
    Results { "result" : [ { "currentValue" : 5969261.68, "purchaseValue" : 4822153.72, "instrument" : "GOOG", "profit" : 1147107.96 } ], "ok" : 1 } 16
  • 17.
    Trade History • Store historical trades and deltas over time • Maintain all data online.. Eg. 7 years • Recent data maintained hot in ‘Working Set’ 300 GB Working Set 128 GB 128 GB 128 GB RAM RAM RAM 1 TB 1 TB 1 TB Data on Data on Data on Disk Disk Disk Shard 1 Shard 2 Shard 3 17
  • 18.
    Summary • Product Catalogs • Trade Capture • Handle changes easily over time • Aggregate trades / orders / executions • Calculate P&L across stored positions • Store trade updates and deltas 18
  • 19.
    For More Information Resource User Data Management Location MongoDB Downloads www.mongodb.org/download Free Online Training education.10gen.com Webinars and Events www.10gen.com/events White Papers www.10gen.com/white-papers Customer Case Studies www.10gen.com/customers Presentations www.10gen.com/presentations Documentation docs.mongodb.org Additional Info info@10gen.com 19