Skip to content

Commit

Permalink
Implement daily and weekly gain
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Bobiński committed Nov 1, 2024
1 parent 43f7d4a commit 2070adc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/main/kotlin/net/bobinski/backend/AnalysisEndpoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,19 @@ object AnalysisEndpoint {
name = checkNotNull(info.name),
date = LocalDate.now(Clock.systemUTC()).toKotlinLocalDate(),
lastPrice = CalculateLastPrice(history),
gain = Analysis.Gain(monthly = Double.NaN, quarterly = Double.NaN, yearly = Double.NaN)
.takeIf { lacking }
?: Analysis.Gain(
monthly = CalculateGain.monthly(history),
quarterly = CalculateGain.quarterly(history),
yearly = CalculateGain.yearly(history)
),
gain = Analysis.Gain(
daily = Double.NaN,
weekly = Double.NaN,
monthly = Double.NaN,
quarterly = Double.NaN,
yearly = Double.NaN
).takeIf { lacking } ?: Analysis.Gain(
daily = CalculateGain.daily(history),
weekly = CalculateGain.weekly(history),
monthly = CalculateGain.monthly(history),
quarterly = CalculateGain.quarterly(history),
yearly = CalculateGain.yearly(history)
),
rsi = Analysis.Rsi(daily = Double.NaN, weekly = Double.NaN, monthly = Double.NaN)
.takeIf { lacking }
?: Analysis.Rsi(
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/net/bobinski/data/Analysis.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ data class Analysis(

@Serializable
data class Gain(
val daily: Double,
val weekly: Double,
val monthly: Double,
val quarterly: Double,
val yearly: Double
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/net/bobinski/logic/CalculateGain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import java.time.LocalDate

object CalculateGain {

fun daily(data: Collection<HistoricalPrice>) =
calculateFor(data, LocalDate.now(Clock.systemUTC()).minusDays(1))

fun weekly(data: Collection<HistoricalPrice>) =
calculateFor(data, LocalDate.now(Clock.systemUTC()).minusWeeks(1))

fun monthly(data: Collection<HistoricalPrice>) =
calculateFor(data, LocalDate.now(Clock.systemUTC()).minusMonths(1))

Expand Down

0 comments on commit 2070adc

Please sign in to comment.