Slide 1

Slide 1 text

trading bot hero @danielepolencic

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

once upon a time...

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

! money transfer

Slide 6

Slide 6 text

have you ever noticed?

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

eur/gbp 0.88 gbp/eur 1.1364

Slide 16

Slide 16 text

send £1000 to

Slide 17

Slide 17 text

£1 ➡ €1.13640

Slide 18

Slide 18 text

! €1136.40

Slide 19

Slide 19 text

send the money back

Slide 20

Slide 20 text

€1 ➡ £0.88

Slide 21

Slide 21 text

€1136.40 * 0.88

Slide 22

Slide 22 text

£1000.032

Slide 23

Slide 23 text

! £1000.032

Slide 24

Slide 24 text

surely it's just transferwise

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

£1 ➡ €1.13612

Slide 27

Slide 27 text

BUY €1136.12 price gbp/eur 1.13612

Slide 28

Slide 28 text

€1 ➡ £0.8802

Slide 29

Slide 29 text

SELL €1136.12 price eur/gbp 0.8802

Slide 30

Slide 30 text

£1000.012

Slide 31

Slide 31 text

! £1000.012

Slide 32

Slide 32 text

this doesn't make any sense

Slide 33

Slide 33 text

£1000

Slide 34

Slide 34 text

£1000 £1000

Slide 35

Slide 35 text

£1000

Slide 36

Slide 36 text

£1000 GBP/EUR 1.2

Slide 37

Slide 37 text

£1000 GBP/EUR 1.2 €1150

Slide 38

Slide 38 text

£1000 €1200

Slide 39

Slide 39 text

£1000 £1200 GBP/EUR 1.2

Slide 40

Slide 40 text

£1000 €1200 GBP/EUR 1.2 £1000 €1200

Slide 41

Slide 41 text

£1000 £1200 GBP/EUR 1.2

Slide 42

Slide 42 text

£0 €1200 GBP/EUR 1.2 £999

Slide 43

Slide 43 text

£0 €0 GBP/EUR 1.2 £999 €1199

Slide 44

Slide 44 text

£0 €0 GBP/EUR 1.2 £999 €1199

Slide 45

Slide 45 text

£1000 €1200 GBP/EUR 1.2 GBP/USD 1.3

Slide 46

Slide 46 text

£1000 €1200 GBP/EUR 1.2 GBP/USD 1.3 €1200 €1200

Slide 47

Slide 47 text

! liquidity

Slide 48

Slide 48 text

! just pennies

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

BUY @1.25 SELL @0.8032

Slide 52

Slide 52 text

1.25 * 0.8032

Slide 53

Slide 53 text

! £1004

Slide 54

Slide 54 text

2 transactions per day 20 days per month

Slide 55

Slide 55 text

2 * 20 * £4 = £160* * best case scenario

Slide 56

Slide 56 text

time to build something

Slide 57

Slide 57 text

✨ 3 wishes

Slide 58

Slide 58 text

1. ⭐

Slide 59

Slide 59 text

! to price changes

Slide 60

Slide 60 text

1.13612 * 0.88 = 0.99978 1.13612 * 0.882 = 1.00205

Slide 61

Slide 61 text

! to price changes ! to order changes

Slide 62

Slide 62 text

created modified partially matched matched in full

Slide 63

Slide 63 text

! to price changes ! to order changes ! to balance changes

Slide 64

Slide 64 text

deposit withdraw orders matched in full

Slide 65

Slide 65 text

!"# 2. ⭐ time travel

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

⏪ market triggers debugging &

Slide 68

Slide 68 text

3. ! bulletproof testing

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

"As part of its normal process, the UTP distributed test data and certain third parties improperly propagated the data." - NASDAQ

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

oops

Slide 73

Slide 73 text

mvp

Slide 74

Slide 74 text

scalable architecture ! MQ mysql

Slide 75

Slide 75 text

APPLICATION

Slide 76

Slide 76 text

APPLICATION WORKERS

Slide 77

Slide 77 text

APPLICATION

Slide 78

Slide 78 text

APPLICATION

Slide 79

Slide 79 text

! overengineered

Slide 80

Slide 80 text

! sync state

Slide 81

Slide 81 text

APPLICATION

Slide 82

Slide 82 text

APPLICATION ORDER 1GBP/USD ORDER 2EUR/USD ORDER 3GBP/USD

Slide 83

Slide 83 text

APPLICATION GET ORDERS ORDER 1GBP/USD ORDER 2EUR/USD ORDER 3GBP/USD

Slide 84

Slide 84 text

APPLICATION ORDER 1GBP/USD ORDER 2EUR/USD ORDER 3GBP/USD ORDER 1GBP/USD ORDER 2EUR/USD ORDER 3GBP/USD

Slide 85

Slide 85 text

APPLICATION ORDER 1GBP/USD ORDER 2EUR/USD ORDER 3GBP/USD ORDER 1GBP/USD ORDER 2EUR/USD ORDER 3GBP/USD GET PRICE GBP/USD

Slide 86

Slide 86 text

APPLICATION ORDER 1GBP/USD ORDER 2EUR/USD ORDER 3GBP/USD ORDER 1GBP/USD ORDER 2EUR/USD ORDER 3GBP/USD GBP/USD 1.3

Slide 87

Slide 87 text

APPLICATION ORDER 1GBP/USD ORDER 2EUR/USD ORDER 3GBP/USD ORDER 1GBP/USD ORDER 2EUR/USD ORDER 3GBP/USD COMPLETED

Slide 88

Slide 88 text

APPLICATION ORDER 1GBP/USD ORDER 2EUR/USD ORDER 3GBP/USD ORDER 1GBP/USD ORDER 2EUR/USD ORDER 3GBP/USD COMPLETED UPDATE ORDER 2

Slide 89

Slide 89 text

APPLICATION ORDER 1GBP/USD ORDER 2EUR/USD ORDER 3GBP/USD ORDER 1GBP/USD ORDER 2EUR/USD ORDER 3GBP/USD COMPLETED NOPE

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

mvp

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

(state, message) -> state

Slide 94

Slide 94 text

const message = { type: 'PRICE_TICK', price: 1.3508 }; const state = { currentPrice: 1.3502 };

Slide 95

Slide 95 text

function update(state, message) { switch(message.type) { case 'PRICE_TICK': return { ...state, currentPrice: message.price }; default: return state; } } const newState = update(state, message); console.log(newState); // {currentPrice: 1.3508};

Slide 96

Slide 96 text

APPLICATION

Slide 97

Slide 97 text

redux leveldb docker

Slide 98

Slide 98 text

! redux actions & orchestration

Slide 99

Slide 99 text

Bad ❌ function update(state, message) { switch(message.type) { case 'PRICE_TICK': /* SIDE EFFECT! */ updateOrderWithPrice(message.price); return { ...state, currentPrice: message.price }; default: return state; } }

Slide 100

Slide 100 text

Good function getPrice(dispatch) { dispatch({type: 'PRICE_TICK'}); updateOrderWithPrice(message.price).then(order => { dispatch({type: 'ORDER_UPDATED', order}); }); }

Slide 101

Slide 101 text

Good function(state, action) { switch(action.type) { case 'PRICE_TICK': return { ...state, currentPrice: message.price }; case 'ORDER_UPDATED': return { ...state, openOrder: message.order }; } }

Slide 102

Slide 102 text

flow and state are separated

Slide 103

Slide 103 text

redux saga redux thunk mobx + rxjs

Slide 104

Slide 104 text

mvp

Slide 105

Slide 105 text

(state, msg) -> [state, cmds]

Slide 106

Slide 106 text

function(state, message) { switch(message.type) { case 'PRICE_TICK': return [ {...state, currentPrice: message.price}, updateOrderWithPrice(message.price) ]; } }

Slide 107

Slide 107 text

updateOrderWithPrice(message.price) function updateOrderWithPrice(price) { return { commandType: 'UPDATE_ORDER', price }; } console.log(updateOrderWithPrice(1.2)) // { // price: 1.2, // commandType: 'UPDATE_ORDER' // }

Slide 108

Slide 108 text

UPDATE(STATE, MESSAGE)

Slide 109

Slide 109 text

UPDATE(STATE, MESSAGE) STATE COMMANDS

Slide 110

Slide 110 text

UPDATE(STATE, MESSAGE) STATE COMMANDS INTERPRETER

Slide 111

Slide 111 text

UPDATE(STATE, MESSAGE) STATE COMMANDS INTERPRETER HTTP CALL CONSOLE.LOG SETTIMEOUT

Slide 112

Slide 112 text

interpreter pattern (or free monad)

Slide 113

Slide 113 text

AST const fetch = { commandType: 'FETCH', url: '/any_url' }; const log = { commandType: 'LOG', value: 'Hello World!' };

Slide 114

Slide 114 text

interpreter function interpreter(commands) { commands.forEach(command => { switch(command.commandType) { case 'FETCH': request(command.url); case 'LOG': console.log(command.value); } }); }

Slide 115

Slide 115 text

no side effects

Slide 116

Slide 116 text

APPLICATION INTERPRETER STORAGE HTTP

Slide 117

Slide 117 text

[ state, updateOrderWithPrice(1.2) ]

Slide 118

Slide 118 text

APPLICATION INTERPRETER STORAGE HTTP

Slide 119

Slide 119 text

APPLICATION INTERPRETER STORAGE HTTP WEBSOCKETS

Slide 120

Slide 120 text

APPLICATION INTERPRETER STORAGE HTTP WEBSOCKETS

Slide 121

Slide 121 text

APPLICATION INTERPRETER STORAGE HTTP WEBSOCKETS

Slide 122

Slide 122 text

APPLICATION INTERPRETER MOCK STORAGE MOCK HTTP v2

Slide 123

Slide 123 text

1. elm 2. redux-loop 3. redux-effects

Slide 124

Slide 124 text

state + logic easier testing interpreter

Slide 125

Slide 125 text

! monads are hard

Slide 126

Slide 126 text

mvp ! .

Slide 127

Slide 127 text

actors

Slide 128

Slide 128 text

APPLICATION ROUTER STORAGE HTTP

Slide 129

Slide 129 text

APPLICATION ROUTER STORAGE HTTP

Slide 130

Slide 130 text

APPLICATION ROUTER STORAGE HTTP { commandType: 'UPDATE_ORDER', price: 1.2 }

Slide 131

Slide 131 text

APPLICATION ROUTER STORAGE HTTP { commandType: 'UPDATE_ORDER', price: 1.2 }

Slide 132

Slide 132 text

APPLICATION ROUTER STORAGE HTTP { commandType: 'UPDATE_ORDER', price: 1.2 }

Slide 133

Slide 133 text

APPLICATION ROUTER STORAGE Z Z Z { commandType: 'UPDATE_ORDER', price: 1.2 }

Slide 134

Slide 134 text

APPLICATION ROUTER STORAGE HTTP

Slide 135

Slide 135 text

APPLICATION ROUTER STORAGE HTTP { commandType: 'RESPONSE', order: {id: 1, price: 1.2} }

Slide 136

Slide 136 text

APPLICATION ROUTER STORAGE HTTP { commandType: 'RESPONSE', order: {id: 1, price: 1.2} }

Slide 137

Slide 137 text

APPLICATION ROUTER STORAGE HTTP { commandType: 'RESPONSE', order: {id: 1, price: 1.2} }

Slide 138

Slide 138 text

ROUTER STORAGE HTTP Z Z Z { commandType: 'RESPONSE', order: {id: 1, price: 1.2} }

Slide 139

Slide 139 text

ROUTER STORAGE HTTP APPLICATION

Slide 140

Slide 140 text

APPLICATION ROUTER STORAGE HTTP

Slide 141

Slide 141 text

fully decoupled effects and business logic 200 LoC

Slide 142

Slide 142 text

testing

Slide 143

Slide 143 text

1. data driven

Slide 144

Slide 144 text

unit testing it('should update the order', () => { const order = {id: 1, price: 1.12, active: true}; const currentTickPrice = 1.11; expect(updateOrder(order, currentTickPrice)).toEqual(UPDATE); });

Slide 145

Slide 145 text

it('should update the order'); it('should do nothing'); it('should cancel the order');

Slide 146

Slide 146 text

it('should update the order'); it('should do nothing'); it('should cancel the order'); it('should NOT update the order'); it('should NOT do nothing'); it('should NOT cancel the order');

Slide 147

Slide 147 text

it('should update the order', () => { const order = {id: 1, price: 1.12, active: true}; const currentTickPrice = 1.11; expect(updateOrder(order, currentTickPrice)).toEqual(UPDATE); }); it('should do nothing', () => { const order = {id: 1, price: 1.12, active: true}; const currentTickPrice = 1.12; expect(updateOrder(order, currentTickPrice)).toEqual(DO_NOTHING); }); it('should cancel the order', () => { const order = {id: 1, price: 1.12, active: true}; const currentTickPrice = 1.22; expect(updateOrder(order, currentTickPrice)).toEqual(CANCEL); }); it('should NOT update the order', () => { const order = {id: 1, price: 1.12, active: true}; const currentTickPrice = 1.11; expect(updateOrder(order, currentTickPrice)).toEqual(UPDATE); }); it('should NOT do nothing', () => { const order = {id: 1, price: 1.12, active: true}; const currentTickPrice = 1.12; expect(updateOrder(order, currentTickPrice)).toEqual(DO_NOTHING); }); it('should NOT cancel the order', () => { const order = {id: 1, price: 1.12, active: true}; const currentTickPrice = 1.22; expect(updateOrder(order, currentTickPrice)).toEqual(CANCEL); });

Slide 148

Slide 148 text

duplication duplication

Slide 149

Slide 149 text

unit testing it('should update the order', () => { const order = {id: 1, price: 1.12, active: true}; // CHANGE ME const currentTickPrice = 1.11; // CHANGE ME expect(updateOrder(order, currentTickPrice)).toEqual(UPDATE); });

Slide 150

Slide 150 text

No content

Slide 151

Slide 151 text

unit testing test(updateOrder, () => { given({id: 1, price: 1.11, active: true}, 1.12).expect(UPDATE); given({id: 1, price: 1.12, active: true}, 1.12).expect(DO_NOTHING); given({id: 1, price: 1.12, active: true}, 1.22).expect(CANCEL); });

Slide 152

Slide 152 text

unit testing test(updateOrder, () => { given({id: 1, price: 1.11, active: true}, 1.12).expect(UPDATE); given({id: 1, price: 1.12, active: true}, 1.12).expect(DO_NOTHING); given({id: 1, price: 1.12, active: true}, 1.22).expect(CANCEL); given({id: 1, price: 1.11, active: false}, 1.12).expect(DO_NOTHING); given({id: 1, price: 1.12, active: false}, 1.12).expect(DO_NOTHING); given({id: 1, price: 1.12, active: false}, 1.22).expect(DO_NOTHING); });

Slide 153

Slide 153 text

2. typescript

Slide 154

Slide 154 text

orders interace IOrder = { id: number price: number }; const order: IOrder = { id: 1, price: 1.12 };

Slide 155

Slide 155 text

orders with state interace IOrder = { id: number price: number active: boolean }; const order: IOrder = { // ERROR! `active` is missing id: 1, price: 1.12 };

Slide 156

Slide 156 text

3. integration

Slide 157

Slide 157 text

No content

Slide 158

Slide 158 text

very hard to setup harder to maintain time consuming

Slide 159

Slide 159 text

but…

Slide 160

Slide 160 text

(state, msg) -> [state, cmd] + snapshot testing

Slide 161

Slide 161 text

const messages = [ {type: 'TICK', price: 1.12}, {type: 'TICK', price: 1.13}, {type: 'SUBMITTED', price: 1.13}, {type: 'COMPLETED', id: 1}, ]; const initialState = { openOrders: [] }; const finalState = messages.reduce((state, message) => { return Update(state, message); }); expect(finalState).toMatchSnapshot();

Slide 162

Slide 162 text

No content

Slide 163

Slide 163 text

show me the !

Slide 164

Slide 164 text

No content

Slide 165

Slide 165 text

No content

Slide 166

Slide 166 text

No content

Slide 167

Slide 167 text

No content

Slide 168

Slide 168 text

!✖ initial investment

Slide 169

Slide 169 text

957 transactions, 3 motnhs later…

Slide 170

Slide 170 text

No content

Slide 171

Slide 171 text

lessons learned

Slide 172

Slide 172 text

! js is awesome

Slide 173

Slide 173 text

proper type system transpilers functional, CQRS, etc.

Slide 174

Slide 174 text

! trading is coding

Slide 175

Slide 175 text

not only python/r/c++ no MBA/Phd no special HW or SW

Slide 176

Slide 176 text

No content

Slide 177

Slide 177 text

! build once, run everywhere

Slide 178

Slide 178 text

bitcoin stock forex

Slide 179

Slide 179 text

hungry for more? · The Elm Architectureҁ0.18҂effect moduleϮϯ · Effect Manager ΄ͭͥΕ · How to structure Elm with multiple models? · Tribeca · Blackbird · Extensible Effects in Node.js, Part 1

Slide 180

Slide 180 text

thanks @danielepolencic