import { createOrderCreator } from '../util/orders' ; import { Store , StreamAction , Middleware , CreatedOrder } from '../types' ; import { ORDER_REQUESTED , ORDER_CREATED , ORDER_PLACED , ORDER_FILLED } from '../constants' ; let orderIdCounter = 0 ; private param { number | function } commission return { Middleware } export function createBrokerBacktest ( commission ) { return ( store ) => { const calculateCommission = typeof commission === 'function' ? commission : ( ) => commission ; const createOrder = createOrderCreator ( store ) ( calculateCommission ) ; return ( next ) => ( action ) => { switch ( action . type ) { case ORDER_REQUESTED : { const order = { ... action . payload } ; const createdOrder = createOrder ( order ) ; store . dispatch ( { type : ORDER_CREATED , payload : createdOrder } ) ; break ; } case ORDER_CREATED : { orderIdCounter += 1 ; const order = { ... action . payload } ; const id = orderIdCounter . toString ( ) ; const placedOrder = { ... order , id } ; store . dispatch ( { type : ORDER_PLACED , payload : placedOrder } ) ; store . dispatch ( { type : ORDER_FILLED , payload : { placedOrder : { ... placedOrder } , filledOrder : { ... placedOrder } , timestamp : placedOrder . timestamp } } ) ; break ; } default : break ; } return next ( action ) ; } ; } ; }