我是 scala 的新手,所以不知道如何解决这个问题?基本上,我正在尝试为一系列报价找到移动平均线交叉点。我不确定如何获取以前的值以将它们与当前值进行比较?
if ( fastMovingAverage(n-1) > slowMovingAverage(n-1) && fastMovingAverage(n) < slowMovingAverage(n) )
then do some action
package com.example.csv
import scala.io.Source
object FileParser {
val TIMESTAMP_LOCATION = 3
val BID_LOCATION = 4
val OFFER_LOCATION = 5
val FAST_WINDOW_SIZE = 5
val SLOW_WINDOW_SIZE = 10
def main(args: Array[String]) = {
val records = Source.fromFile("Sample.csv")
.getLines()
.drop(1)
.map(_.split(","))
.takeWhile( _ != null)
.sliding(SLOW_WINDOW_SIZE , 1)
.foreach(x => movingAverage(x))
}
def movingAverage(numbers: Seq[Array[String]]) = {
val listOfBids = numbers.map(x => x(BID_LOCATION).toDouble)
val slowAverage = listOfBids.reduceLeft(_ + _)/numbers.length
val fastListOfBids = listOfBids.takeRight(FAST_WINDOW_SIZE)
val fastAverage = fastListOfBids.reduceLeft(_ + _)/fastListOfBids.length
println("Slow Average " + slowAverage + " Fast Average " + fastAverage)
}
}