-
Notifications
You must be signed in to change notification settings - Fork 5
/
Main.scala
51 lines (41 loc) · 1.4 KB
/
Main.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package nikosoft
/**
* TEMPORARY FILE just to show the idea
*/
object Main extends App {
case class Candle(close: BigDecimal)
case class MACD(value: BigDecimal)
trait IndicatorType[INPUT, OUTPUT] {
def calculate: INPUT => OUTPUT
}
case object MACDIndicatorType extends IndicatorType[(BigDecimal, Option[MACD]), MACD] {
def calculate: ((BigDecimal, Option[MACD])) => MACD = {
case (value, None) => MACD(value)
case (value, Some(prev)) => MACD(prev.value + value)
}
}
abstract class Indicator[INPUT, OUTPUT] {
val indicatorType: IndicatorType[_, OUTPUT]
protected var values: Seq[OUTPUT] = Seq.empty
protected def enrichFunction: INPUT => OUTPUT
def enrich(input: INPUT): Seq[OUTPUT] = {
val enriched = enrichFunction(input)
values = enriched +: values
values
}
def _values = values
}
class MACDCandleCloseIndicator extends Indicator[Candle, MACD] {
protected def enrichFunction: Candle => MACD = candle => values match {
case lastMACD +: tail => indicatorType.calculate((candle.close, Option(lastMACD)))
case Nil => indicatorType.calculate((candle.close, None))
}
val indicatorType = MACDIndicatorType
}
val indicator = new MACDCandleCloseIndicator()
indicator.enrich(Candle(10))
indicator.enrich(Candle(10))
indicator.enrich(Candle(10))
indicator.enrich(Candle(10))
indicator._values.foreach(println)
}