Upgrade to Pro — share decks privately, control downloads, hide ads and more …

From Zero to Forex Trading bot Hero

From Zero to Forex Trading bot Hero

During this talk, you will discover Daniele's journey building a trading bot. From building a basic prototype in Typescript to using functional programming techniques to trade autonomously across multiple foreign exchanges and generate thousands of dollars in revenue.

Daniele Polencic

July 12, 2017
Tweet

More Decks by Daniele Polencic

Other Decks in Technology

Transcript

  1. "As part of its normal process, the UTP distributed test

    data and certain third parties improperly propagated the data." - NASDAQ
  2. 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};
  3. Bad ❌ function update(state, message) { switch(message.type) { case 'PRICE_TICK':

    /* SIDE EFFECT! */ updateOrderWithPrice(message.price); return { ...state, currentPrice: message.price }; default: return state; } }
  4. Good ✅ function(state, action) { switch(action.type) { case 'PRICE_TICK': return

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

    currentPrice: message.price}, updateOrderWithPrice(message.price) ]; } }
  6. updateOrderWithPrice(message.price) function updateOrderWithPrice(price) { return { commandType: 'UPDATE_ORDER', price };

    } console.log(updateOrderWithPrice(1.2)) // { // price: 1.2, // commandType: 'UPDATE_ORDER' // }
  7. AST const fetch = { commandType: 'FETCH', url: '/any_url' };

    const log = { commandType: 'LOG', value: 'Hello World!' };
  8. interpreter function interpreter(commands) { commands.forEach(command => { switch(command.commandType) { case

    'FETCH': request(command.url); case 'LOG': console.log(command.value); } }); }
  9. 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); });
  10. 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); }); it('should cancel the order', () => { const order = {id: 1, price: 1.12, active: true}; const currentTickPrice = 1.22; expect(updateOrder(order, currentTickPrice)).toEqual(CANCEL); });
  11. 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); }); 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); });
  12. 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');
  13. 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); });
  14. 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); });
  15. 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); });
  16. 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); });
  17. 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); });
  18. orders with state const order = { id: 1, price:

    1.12, active: true // new field };
  19. orders interace IOrder = { id: number price: number };

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

    number active: boolean }; const order: IOrder = { // ERROR! `active` is missing id: 1, price: 1.12 };
  21. 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();
  22. 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