ent
    go timeStamp remains
    where
        readEvent p byteString = case p byteString of
            Fail remains context msg -> throwError $ ParseError remains context msg
            Partial p' -> readEvent p' BS.empty
            Done remains event -> return $ (event, remains)

        go prevStamp byteString = do
            (event@(Event timeStamp _), remains) <- readEvent (parse parser) byteString
            when (timeStamp > prevStamp) $ throwError $ SimulationDataError $ "Wrong timestamp order: " ++ (show timeStamp) ++ " is lager than " ++ (show prevStamp)
            yield event
            go timeStamp remains
-}

-- | Convert candles to quotes
-- Most markets provide historical data in form of candles (open, maximal, minimal and close prices)
-- This function allow to convert it to quotes needed for historical simulations.
candleToQuotes :: ( Ord (Money market)
                 , Fractional (Amount market)
                 , Fractional (Timestamp market)
                 ) => Candle market -> [Quote market]
candleToQuotes (Candle instrumentId begin end open high low close amount) =
    let quarterAmount = (amount / (fromInteger 4))
        quarterPeriod = (end - begin) / (fromInteger 4)
        quote t p = Quote instrumentId t quarterAmount p
        (a, b) = if (open >= close) then (high, low)
                                    else (low, high)
    in [ quote begin open, quote (begin + quarterPeriod) a, quote (begin + (quarterPeriod * (fromInteger 2))) b, quote end close]
candleToQuotes :: ( Ord (Money market)
                 , Fractional (Amount market)
                 , Fractional (Timestamp market)
                 ) => Candle market -> [Quote market]
candleToQuotes (Candle instrumentId begin end open high low close amount) =
    let quarterAmount = (amount / (fromInteger 4))
        quarterPeriod = (end - begin) / (fromInteger 4)
        quote t p = Quote instrumentId t quarterAmount p
        (a, b) = if (open >= close) then (high, low)
                                    else (low, high)
    in [ quote begin open, quote (begin + quarterPeriod) a, quote (begin + (quarterPeriod * (fromInteger 2))) b, quote end close]
candleToQuotes (Candle instrumentId begin end open high low close amount) =
    let quarterAmount = (amount / (fromInteger 4))
        quarterPeriod = (end - begin) / (fromInteger 4)
        quote t p = Quote instrumentId t quarterAmount p
        (a, b) = if (open >= close) then (high, low)
                                    else (low, high)
    in [ quote begin open, quote (begin + quarterPeriod) a, quote (begin + (quarterPeriod * (fromInteger 2))) b, quote end close]