Skip to content

Commit

Permalink
update market
Browse files Browse the repository at this point in the history
ZiluTian committed May 22, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 1764f4a commit 18df4eb
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions example/src/main/scala/example/stockMarket/stock.scala
Original file line number Diff line number Diff line change
@@ -4,9 +4,12 @@ package stockMarket
// Artificial economic life: a simple model of a stockmarket
class Stock(var priceAdjustmentFactor: Double) {
var prices: ListBuffer[Double] = new ListBuffer()
private val INCREASE: Int = 1
private val DECREASE: Int = 2
private val NO_CHANGE: Int = 0

private var period: Int = 0
private var currentPrice: Double = -1

private var lastDividend: Double = 0
// Update 10-day-average and return whether the updated val has increased
private val updateLastAvg10: trackLastAvg = new trackLastAvg(10)
@@ -20,25 +23,28 @@ class Stock(var priceAdjustmentFactor: Double) {
// return whether the new avg has increased comparing with the prev avg
def update: Int = {
// moving window
// if not enough information, compare previous average
val calculated_avg: Option[Double] = if (period > window) {
Some(prices.slice(period-window, period).sum/window)
} else {
None
}

var ans: Int = 2
(calculated_avg, lastAvg) match {
case (None, _) => 0
case (None, _) => NO_CHANGE
case (Some(x), None) => {
lastAvg = calculated_avg
0
INCREASE
}
case (Some(x), Some(y)) => {
lastAvg = calculated_avg
if (x > y){
ans = 1
INCREASE
} else if (x == y) {
NO_CHANGE
} else {
DECREASE
}
lastAvg = calculated_avg
ans
}
}
}
@@ -49,19 +55,17 @@ class Stock(var priceAdjustmentFactor: Double) {
period += 1
prices.append(p)

// Short-term info: whether dividend has increased. 0 means no dividend
// Short-term info: whether dividend has increased
val dividendIncrease: Int = {
if (d == 0){
0
val ans = if (d == lastDividend) {
NO_CHANGE
} else if (d > lastDividend) {
INCREASE
} else {
val lastDividend_copy = lastDividend
lastDividend = d
if(lastDividend_copy < d){
1
} else {
2
}
DECREASE
}
lastDividend = d
ans
}

// Mid-term info: whether 10-day avg has increased

0 comments on commit 18df4eb

Please sign in to comment.