Browse Source

Some fixes in ATR calculation and backtest driver

master
Denis Tereshkin 7 years ago
parent
commit
7899ccd72e
  1. 5
      src/ATrade/Driver/Backtest.hs
  2. 4
      src/ATrade/RoboCom/Indicators.hs
  3. 2
      test/Test/RoboCom/Indicators.hs

5
src/ATrade/Driver/Backtest.hs

@ -104,8 +104,7 @@ backtestMain dataDownloadDelta defaultState initCallback callback = do
let finalState = execState (unBacktestingMonad s) $ defaultBacktestState defaultState params tickerList let finalState = execState (unBacktestingMonad s) $ defaultBacktestState defaultState params tickerList
print $ cash finalState print $ cash finalState
print $ tradesLog finalState print $ tradesLog finalState
forM_ (logs finalState) putStrLn forM_ (reverse . logs $ finalState) putStrLn
print $ (M.keys . seBars . strategyEnvironment) finalState
loadStrategyConfig :: (FromJSON c) => Params -> IO ([Ticker], c) loadStrategyConfig :: (FromJSON c) => Params -> IO ([Ticker], c)
loadStrategyConfig params = do loadStrategyConfig params = do
@ -162,7 +161,7 @@ backtestMain dataDownloadDelta defaultState initCallback callback = do
env <- gets strategyEnvironment env <- gets strategyEnvironment
let oldTimestamp = seLastTimestamp env let oldTimestamp = seLastTimestamp env
let newTimestamp = barTimestamp bar 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 curState <- gets robotState
modify' (\s -> s { strategyEnvironment = newenv }) modify' (\s -> s { strategyEnvironment = newenv })
handleEvents [NewBar bar]) handleEvents [NewBar bar])

4
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 typicalPrice a b c = (a + b + c) / 3
atr :: Int -> [Bar] -> Double 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 where
trueranges :: [Double] trueranges :: [Double]
trueranges = zipWith trueRange bars (tail bars) trueranges = zipWith trueRange bars (tail bars)

2
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, 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] 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 where
bars = reverse [bar 48.70 47.79 48.16, bars = reverse [bar 48.70 47.79 48.16,
bar 48.72 48.14 48.61, bar 48.72 48.14 48.61,

Loading…
Cancel
Save