Execution layer for algorithmic trading
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

311 lines
16 KiB

7 years ago
{-# LANGUAGE OverloadedStrings #-}
module Test.RoboCom.Indicators
(
unitTests
) where
7 years ago
import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck as QC
import Test.Tasty.SmallCheck as SC
7 years ago
7 years ago
import ATrade.Types
import qualified Data.Text as T
import Data.Time.Calendar
import Data.Time.Clock
7 years ago
7 years ago
import ATrade.RoboCom.Indicators
7 years ago
unitTests = testGroup "RoboCom.Indicators" [
testEma,
testRsi,
testRsi2,
testAtr,
testAtr2,
7 years ago
testCci,
testBbandUpper,
testPercentRank
]
newbar b = Bar { barSecurity = T.pack "", barTimestamp = UTCTime (fromGregorian 1970 1 1) 0, barOpen = 0, barHigh = 0, barLow = 0, barClose = b, barVolume = 0}
assertEqualWithEpsilon eps one two = assertBool ("|" ++ show one ++ " - " ++ show two ++ "| < " ++ show eps) $ abs (one - two) < eps
testEma = testCase "EMA calculation" $ assertEqualWithEpsilon 1 (ema 4 bars) 1256.19
where
bars = reverse [1243.0, 1226.3, 1231.5, 1249, 1257.4, 1246.2, 1242.5, 1245.1, 1256.1, 1248.5, 1245, 1267.1]
testRsi = testCase "RSI calculation" $ assertEqualWithEpsilon 0.1 (rsi 2 bars) 96.94
where
bars = reverse [1, 3, 5, 7, 4, 2, 7, 4, 2, 12, 13, 11, 15, 32]
testRsi2 = testCase "RSI calculation" $ assertEqualWithEpsilon 1 (rsi 6 bars) 18.11
where
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.01 (atr 14 bars) 1.32
7 years ago
where
bars = reverse [bar 48.70 47.79 48.16,
bar 48.72 48.14 48.61,
bar 48.90 48.39 48.75,
bar 48.87 48.37 48.63,
bar 48.82 48.24 48.74,
bar 49.05 48.64 49.03,
bar 49.20 48.94 49.07,
bar 49.35 48.86 49.32,
bar 49.92 49.50 49.91,
bar 50.19 49.87 50.13,
bar 50.12 49.20 49.53,
bar 49.66 48.90 49.50,
bar 49.88 49.43 49.75,
bar 50.19 49.73 50.03,
bar 50.36 49.26 50.31,
bar 50.57 50.09 50.52,
bar 50.65 50.30 50.41,
bar 50.43 49.21 49.34,
bar 49.63 48.98 49.37,
bar 50.33 49.61 50.23,
bar 50.29 49.20 49.24,
bar 50.17 49.43 49.93,
bar 49.32 48.08 48.43,
bar 48.50 47.64 48.18,
bar 48.32 41.55 46.57,
bar 46.80 44.28 45.41,
bar 47.80 47.31 47.77,
bar 48.39 47.20 47.72,
bar 48.66 47.90 48.62,
bar 48.79 47.73 47.85 ]
bar h l c = Bar { barSecurity = "", barTimestamp = UTCTime (fromGregorian 1970 1 1) 0, barOpen = 0, barHigh = h, barLow = l, barClose = c, barVolume = 0}
testAtr2 = testCase "ATR calculation - case 2" $ assertEqualWithEpsilon 1 (atr 18 bars) 230.8
where
bars = reverse [
bar 122870.0000000 122900.0000000 122350.0000000 122480.0000000,
bar 122470.0000000 122590.0000000 122420.0000000 122540.0000000,
bar 122540.0000000 122680.0000000 122490.0000000 122680.0000000,
bar 122680.0000000 122760.0000000 122630.0000000 122650.0000000,
bar 122640.0000000 122720.0000000 122570.0000000 122610.0000000,
bar 122610.0000000 122660.0000000 122450.0000000 122500.0000000,
bar 122510.0000000 122640.0000000 122490.0000000 122580.0000000,
bar 122580.0000000 122730.0000000 122490.0000000 122700.0000000,
bar 122690.0000000 122840.0000000 122690.0000000 122780.0000000,
bar 122780.0000000 122800.0000000 122620.0000000 122670.0000000,
bar 122670.0000000 122800.0000000 122640.0000000 122700.0000000,
bar 122700.0000000 122810.0000000 122700.0000000 122710.0000000,
bar 122720.0000000 122800.0000000 122650.0000000 122730.0000000,
bar 122710.0000000 122990.0000000 122680.0000000 122920.0000000,
bar 122940.0000000 123190.0000000 122940.0000000 122960.0000000,
bar 122960.0000000 123100.0000000 122930.0000000 123090.0000000,
bar 123090.0000000 123130.0000000 122940.0000000 122950.0000000,
bar 122950.0000000 123010.0000000 122870.0000000 122970.0000000,
bar 122960.0000000 122970.0000000 122700.0000000 122730.0000000,
bar 122720.0000000 122790.0000000 122550.0000000 122560.0000000,
bar 122570.0000000 122680.0000000 122460.0000000 122540.0000000,
bar 122540.0000000 122700.0000000 122510.0000000 122620.0000000,
bar 122620.0000000 122870.0000000 122600.0000000 122860.0000000,
bar 122860.0000000 123170.0000000 122830.0000000 123050.0000000,
bar 123050.0000000 123080.0000000 122910.0000000 122920.0000000,
bar 122920.0000000 123100.0000000 122850.0000000 123020.0000000,
bar 123010.0000000 123280.0000000 122960.0000000 123230.0000000,
bar 123240.0000000 123240.0000000 123090.0000000 123220.0000000,
bar 123220.0000000 123230.0000000 123140.0000000 123200.0000000,
bar 123200.0000000 123290.0000000 123180.0000000 123190.0000000,
bar 123200.0000000 123530.0000000 123200.0000000 123470.0000000,
bar 123470.0000000 123510.0000000 123340.0000000 123390.0000000,
bar 123390.0000000 123430.0000000 123330.0000000 123430.0000000,
bar 123430.0000000 123450.0000000 123320.0000000 123340.0000000,
bar 123340.0000000 123360.0000000 123280.0000000 123350.0000000,
bar 123340.0000000 123370.0000000 123220.0000000 123260.0000000,
bar 123250.0000000 123350.0000000 123230.0000000 123250.0000000,
bar 123240.0000000 123280.0000000 123160.0000000 123170.0000000,
bar 123170.0000000 123260.0000000 123150.0000000 123210.0000000,
bar 123210.0000000 123330.0000000 123200.0000000 123300.0000000,
bar 123300.0000000 123310.0000000 123220.0000000 123250.0000000,
bar 123240.0000000 123280.0000000 123220.0000000 123270.0000000,
bar 123270.0000000 123350.0000000 123230.0000000 123260.0000000,
bar 123260.0000000 123260.0000000 123180.0000000 123190.0000000,
bar 123190.0000000 123190.0000000 123160.0000000 123180.0000000,
bar 123170.0000000 123180.0000000 123040.0000000 123060.0000000,
bar 123070.0000000 123210.0000000 123020.0000000 123200.0000000,
bar 123090.0000000 123300.0000000 122310.0000000 122440.0000000,
bar 122440.0000000 122620.0000000 122400.0000000 122550.0000000,
bar 122540.0000000 122720.0000000 122460.0000000 122710.0000000,
bar 122720.0000000 122730.0000000 122530.0000000 122570.0000000,
bar 122570.0000000 122680.0000000 122490.0000000 122590.0000000,
bar 122580.0000000 122750.0000000 122550.0000000 122640.0000000,
bar 122650.0000000 122660.0000000 122510.0000000 122620.0000000,
bar 122620.0000000 122680.0000000 122500.0000000 122560.0000000,
bar 122570.0000000 122670.0000000 122520.0000000 122610.0000000,
bar 122610.0000000 122790.0000000 122590.0000000 122690.0000000,
bar 122690.0000000 122830.0000000 122680.0000000 122800.0000000,
bar 122810.0000000 122810.0000000 122560.0000000 122620.0000000,
bar 122620.0000000 122700.0000000 122570.0000000 122640.0000000,
bar 122640.0000000 122730.0000000 122620.0000000 122670.0000000,
bar 122680.0000000 122830.0000000 122630.0000000 122780.0000000,
bar 122770.0000000 123090.0000000 122740.0000000 122980.0000000,
bar 123000.0000000 123090.0000000 122940.0000000 122960.0000000,
bar 122960.0000000 122980.0000000 122850.0000000 122920.0000000,
bar 122930.0000000 123030.0000000 122920.0000000 122950.0000000,
bar 122940.0000000 122990.0000000 122880.0000000 122970.0000000,
bar 122980.0000000 123080.0000000 122860.0000000 122880.0000000,
bar 122880.0000000 122980.0000000 122870.0000000 122980.0000000,
bar 122980.0000000 123130.0000000 122780.0000000 122950.0000000,
bar 122960.0000000 123020.0000000 122860.0000000 122870.0000000,
bar 122870.0000000 122890.0000000 122570.0000000 122650.0000000,
bar 122640.0000000 122710.0000000 122500.0000000 122580.0000000,
bar 122580.0000000 122690.0000000 122400.0000000 122430.0000000,
bar 122430.0000000 122700.0000000 122410.0000000 122690.0000000,
bar 122690.0000000 122720.0000000 122320.0000000 122480.0000000,
bar 122480.0000000 122560.0000000 122380.0000000 122540.0000000,
bar 122540.0000000 122650.0000000 122450.0000000 122480.0000000,
bar 122480.0000000 122540.0000000 122400.0000000 122430.0000000,
bar 122440.0000000 122450.0000000 122110.0000000 122200.0000000,
bar 122190.0000000 122350.0000000 122110.0000000 122320.0000000,
bar 122330.0000000 122480.0000000 122200.0000000 122430.0000000,
bar 122380.0000000 122570.0000000 121870.0000000 122210.0000000,
bar 122210.0000000 122300.0000000 122170.0000000 122230.0000000,
bar 122220.0000000 122340.0000000 122210.0000000 122320.0000000,
bar 122310.0000000 122320.0000000 122190.0000000 122290.0000000,
bar 122300.0000000 122440.0000000 122300.0000000 122410.0000000,
bar 122400.0000000 122490.0000000 122370.0000000 122450.0000000,
bar 122460.0000000 122770.0000000 122450.0000000 122760.0000000,
bar 122760.0000000 122850.0000000 122720.0000000 122810.0000000,
bar 122820.0000000 122890.0000000 122760.0000000 122830.0000000,
bar 122830.0000000 122950.0000000 122780.0000000 122780.0000000,
bar 122790.0000000 122790.0000000 122670.0000000 122740.0000000,
bar 122750.0000000 122770.0000000 122670.0000000 122720.0000000,
bar 122720.0000000 122880.0000000 122700.0000000 122750.0000000,
bar 122750.0000000 122760.0000000 122650.0000000 122680.0000000,
bar 122670.0000000 122770.0000000 122670.0000000 122740.0000000,
bar 122740.0000000 122820.0000000 122690.0000000 122750.0000000,
bar 122750.0000000 122790.0000000 122690.0000000 122740.0000000,
bar 122740.0000000 122770.0000000 122610.0000000 122680.0000000,
bar 122670.0000000 122800.0000000 122670.0000000 122770.0000000,
bar 122770.0000000 122910.0000000 122710.0000000 122790.0000000,
bar 122700.0000000 122730.0000000 121680.0000000 121780.0000000 ]
bar o h l c = Bar { barSecurity = "", barTimestamp = UTCTime (fromGregorian 1970 1 1) 0, barOpen = o, barHigh = h, barLow = l, barClose = c, barVolume = 0}
7 years ago
testCci = testCase "CCI calculation" $ do
assertEqualWithEpsilon 0.1 (cci 12 bars) 212.39
where
bars = reverse [
bar 195.2900000 194.3900000 195.1200000,
bar 195.2100000 194.7200000 195.0600000,
bar 195.8800000 195.0000000 195.7600000,
bar 196.3000000 195.6600000 196.0600000,
bar 196.4900000 195.8400000 196.0000000,
bar 196.6000000 195.9700000 196.5500000,
bar 197.0500000 196.5400000 196.7000000,
bar 196.8200000 196.3000000 196.4700000,
bar 196.4800000 196.0500000 196.2000000,
bar 196.3700000 195.8900000 196.1500000,
bar 196.8500000 196.0600000 196.5500000,
bar 196.7100000 196.2000000 196.7100000,
bar 196.9900000 196.4600000 196.5100000,
bar 196.5900000 195.8400000 195.9700000,
bar 196.2800000 195.4500000 195.6700000,
bar 195.6300000 194.0000000 194.0000000,
bar 194.6500000 193.3300000 194.4500000,
bar 194.5100000 194.0000000 194.0500000,
bar 193.7700000 192.3800000 193.0900000,
bar 193.5000000 192.5600000 192.9700000,
bar 193.9500000 192.7600000 193.8400000,
bar 194.5000000 193.7600000 194.2600000,
bar 194.8700000 193.8800000 194.6800000,
bar 194.7800000 194.1100000 194.4900000,
bar 194.7300000 194.1300000 194.2700000,
bar 194.8300000 194.1200000 194.6700000,
bar 195.1200000 193.8800000 193.8900000,
bar 194.2800000 193.7700000 194.0200000,
bar 194.1600000 193.8000000 194.0300000,
bar 194.0100000 193.4500000 193.8000000,
bar 193.9900000 193.6500000 193.9100000,
bar 194.9000000 193.5700000 194.1600000,
bar 194.2000000 193.1500000 193.4500000,
bar 193.8900000 193.1800000 193.4700000,
bar 194.1000000 193.1000000 193.1300000,
bar 193.8500000 193.1000000 193.8500000,
bar 194.9200000 194.1500000 194.1700000,
bar 194.7000000 193.9500000 194.6100000,
bar 195.2000000 194.5000000 194.5200000,
bar 195.6800000 194.5200000 195.5200000,
bar 195.7500000 195.0700000 195.2700000,
bar 195.4000000 194.7100000 194.9000000,
bar 195.1600000 193.9400000 194.0600000,
bar 194.1900000 193.3300000 193.3800000,
bar 193.8200000 193.2000000 193.7200000,
bar 193.6900000 193.2500000 193.6600000,
bar 194.1700000 193.3700000 194.0800000,
bar 194.4300000 193.7600000 194.1900000,
bar 194.4200000 194.0100000 194.3100000,
bar 194.3600000 193.8300000 194.2900000,
bar 194.3500000 193.5100000 193.9400000,
bar 194.2500000 193.7500000 194.1200000,
bar 194.1700000 193.8000000 193.8400000,
bar 194.2700000 193.8000000 193.8000000,
bar 197.1400000 195.5600000 196.6100000,
bar 197.0400000 196.5500000 197.0000000,
bar 198.6900000 196.8500000 198.6800000,
bar 199.4700000 198.5600000 199.4300000,
bar 201.7100000 199.4300000 199.8900000,
bar 200.1500000 199.1100000 200.1300000,
bar 200.7300000 199.1200000 199.7100000,
bar 200.5000000 199.6000000 200.3800000,
bar 201.9500000 200.2500000 201.9500000,
bar 204.0000000 201.8900000 203.2000000,
bar 203.9900000 203.0700000 203.5800000,
bar 206.7000000 203.5000000 205.6500000,
bar 206.5000000 204.8900000 206.5000000,
bar 206.5000000 204.5500000 206.0000000,
bar 206.1000000 203.2500000 203.6600000,
bar 205.4400000 203.5000000 205.1200000,
bar 205.9100000 203.7000000 204.2800000,
bar 205.9600000 204.1300000 205.9600000,
bar 208.0000000 204.0600000 206.8300000,
bar 207.5600000 206.5300000 207.2300000,
bar 209.3500000 207.1000000 208.9700000,
bar 209.8000000 208.8200000 209.7000000,
bar 209.9700000 209.0500000 209.4200000,
bar 209.7300000 209.2800000 209.6600000,
bar 211.7700000 209.6600000 211.2300000,
bar 211.3000000 210.0000000 210.4900000,
bar 211.1000000 210.4500000 211.0000000,
bar 211.0000000 209.6200000 210.0100000,
bar 210.2300000 209.6600000 210.1000000,
bar 210.5600000 209.1600000 209.5000000,
bar 209.9100000 209.0900000 209.7100000,
bar 210.1900000 209.2900000 210.0500000,
bar 210.3000000 209.8000000 209.8600000,
bar 210.0200000 208.8900000 209.3100000,
bar 210.0800000 209.2100000 209.9700000,
bar 209.9500000 209.0900000 209.0900000,
bar 210.9600000 209.1200000 210.1900000,
bar 210.6500000 209.4000000 210.3700000,
bar 212.2600000 210.3000000 210.9800000,
bar 211.4500000 210.0000000 210.4800000,
bar 210.6900000 209.7300000 210.0300000,
bar 210.3200000 209.8400000 210.0700000,
bar 210.4000000 210.0000000 210.3200000,
bar 210.4000000 210.2000000 210.3300000,
bar 211.0000000 210.2800000 210.4200000,
bar 210.5000000 210.0100000 210.3400000,
bar 210.6000000 210.0700000 210.5400000,
bar 211.1200000 210.3200000 211.0400000,
bar 211.1700000 210.7300000 211.0200000,
bar 211.1500000 210.7500000 210.7600000,
bar 217.8000000 210.8000000 216.3500000,
bar 219.2000000 215.8200000 219.0400000,
bar 220.8400000 218.2600000 220.4400000,
bar 221.5000000 220.0500000 220.0500000
]
bar h l c = Bar { barSecurity = "", barTimestamp = UTCTime (fromGregorian 1970 1 1) 0, barOpen = 0, barHigh = h, barLow = l, barClose = c, barVolume = 0}
testBbandUpper = testCase "Bollinger bands (upper) calculation" $ assertEqualWithEpsilon 0.1 (bbandUpper 5 1.5 bars) 1764.12
where
bars = reverse [1750.0, 1749.99, 1761.0, 1771.0, 1758.94, 1759.36, 1758.55, 1760.0, 1751.0, 1756.80, 1748.15, 1722.90, 1726]
testPercentRank = testCase "PercentRank calculation" $ assertEqualWithEpsilon 0.01 (percentRank 10 bars) 0.9
where
bars = reverse [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]