diff --git a/src/ATrade/Driver/Backtest.hs b/src/ATrade/Driver/Backtest.hs index bf9901e..9c50edc 100644 --- a/src/ATrade/Driver/Backtest.hs +++ b/src/ATrade/Driver/Backtest.hs @@ -104,8 +104,7 @@ backtestMain dataDownloadDelta defaultState initCallback callback = do let finalState = execState (unBacktestingMonad s) $ defaultBacktestState defaultState params tickerList print $ cash finalState print $ tradesLog finalState - forM_ (logs finalState) putStrLn - print $ (M.keys . seBars . strategyEnvironment) finalState + forM_ (reverse . logs $ finalState) putStrLn loadStrategyConfig :: (FromJSON c) => Params -> IO ([Ticker], c) loadStrategyConfig params = do @@ -162,7 +161,7 @@ backtestMain dataDownloadDelta defaultState initCallback callback = do env <- gets strategyEnvironment let oldTimestamp = seLastTimestamp env let newTimestamp = barTimestamp bar - let newenv = env { seBars = updateBars (seBars env) bar } + let newenv = env { seBars = updateBars (seBars env) bar, seLastTimestamp = newTimestamp } curState <- gets robotState modify' (\s -> s { strategyEnvironment = newenv }) handleEvents [NewBar bar]) diff --git a/src/ATrade/RoboCom/Indicators.hs b/src/ATrade/RoboCom/Indicators.hs index 8c7ad80..31109a1 100644 --- a/src/ATrade/RoboCom/Indicators.hs +++ b/src/ATrade/RoboCom/Indicators.hs @@ -40,7 +40,9 @@ cci period bars = (head tp - tpMean) / (0.015 * meanDev) typicalPrice a b c = (a + b + c) / 3 atr :: Int -> [Bar] -> Double -atr period bars = foldl (\x y -> (x * (period' - 1) + y) / period') 0 (reverse $ take (5 * period) trueranges) +atr period bars = case reverse (take (5 * period) trueranges) of + (firstValue:rest) -> foldl (\x y -> (x * (period' - 1) + y) / period') firstValue rest + _ -> 0 where trueranges :: [Double] trueranges = zipWith trueRange bars (tail bars) diff --git a/test/Test/RoboCom/Indicators.hs b/test/Test/RoboCom/Indicators.hs index 2f98086..d30f395 100644 --- a/test/Test/RoboCom/Indicators.hs +++ b/test/Test/RoboCom/Indicators.hs @@ -43,7 +43,7 @@ testRsi2 = testCase "RSI calculation" $ assertEqualWithEpsilon 1 (rsi 6 bars) 18 bars = reverse [1156.2, 1158.8, 1158.3, 1160.3, 1160.9, 1159.8, 1163.0, 1156.3, 1156.0, 1155.3, 1153.8, 1156.2, 1154.1, 1155.9, 1158.1, 1155.8, 1155.9, 1154.5, 1149.8, 1146.5, 1152.1, 1154.0, 1150.2, 1139.5, 1132.6] -testAtr = testCase "ATR calculation" $ assertEqualWithEpsilon 0.1 (atr 14 bars) 1.32 +testAtr = testCase "ATR calculation" $ assertEqualWithEpsilon 0.01 (atr 14 bars) 1.32 where bars = reverse [bar 48.70 47.79 48.16, bar 48.72 48.14 48.61,