Skip to content

Commit

Permalink
use normalization for showing multiple timeseries on the same chart.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaron committed Feb 7, 2024
1 parent a5a9bf4 commit 3090cdd
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ internal class AvroSamples {
println(avgEquity)
}


@Test
@Ignore
internal fun testingStrategies() {
Expand All @@ -153,7 +152,6 @@ internal class AvroSamples {

}


@Test
@Ignore
internal fun signalsOnly() {
Expand Down Expand Up @@ -186,7 +184,6 @@ internal class AvroSamples {
roboquant.run(feed, Timeframe.past(5.years))
}


@Test
@Ignore
internal fun simple() {
Expand All @@ -197,7 +194,6 @@ internal class AvroSamples {
println(roboquant.broker.account.fullSummary())
}


@Test
@Ignore
internal fun aggregator() {
Expand All @@ -208,7 +204,6 @@ internal class AvroSamples {
}
}


@Test
@Ignore
internal fun forexRun() {
Expand All @@ -230,7 +225,6 @@ internal class AvroSamples {
}
}


@Test
@Ignore
internal fun profileTest() {
Expand All @@ -239,7 +233,6 @@ internal class AvroSamples {
rq.run(feed)
}


@Test
@Ignore
internal fun performanceTest() {
Expand All @@ -259,7 +252,6 @@ internal class AvroSamples {
}
}


@Test
@Ignore
internal fun cfd() {
Expand Down Expand Up @@ -303,7 +295,6 @@ internal class AvroSamples {
println(account.trades.summary())
}


@Test
@Ignore
internal fun feedRecorder() {
Expand All @@ -325,7 +316,6 @@ internal class AvroSamples {
println(t2)
}


@Test
@Ignore
internal fun generateDemoFeed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ internal class BinanceSamples {
val result = json["result"].asJsonArray
for (row in result) {
val arr = row.asJsonArray
val time = Instant.ofEpochMilli(arr[6].asLong) // use the close time
val pb = PriceBar(
Instant.ofEpochMilli(arr[6].asLong) // use the close time
PriceBar(
asset,
arr[1].asDouble,
arr[2].asDouble,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class TimeSeriesChart(

/**
* Create a TimeSeriesChart chart for a number of [metrics] that can be found in the [logger].
* Each metric will be indexed to 1.0 for its first value.
* Each metric will be normalized so they fit a similar scale.
* If there is more than one run recorded by the logger, you need to specify the [run] you want to use.
*/
fun fromMetrics(
Expand All @@ -68,7 +68,7 @@ class TimeSeriesChart(
require(metrics.isNotEmpty()) { "Provide at least 1 metric"}
require(run != null || logger.getRuns().size == 1) { "More than 1 run found, please specify a run"}
val r = run ?: logger.getRuns().first()
val data = metrics.associateWith { it }.mapValues { logger.getMetric(it.key, r).index() }
val data = metrics.associateWith { it }.mapValues { logger.getMetric(it.key, r).normalize() }
return TimeSeriesChart(data, fractionDigits = fractionDigits)
}

Expand Down
5 changes: 5 additions & 0 deletions roboquant/src/main/kotlin/org/roboquant/common/TimeSeries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ class TimeSeries(val timeline: Timeline, val values: DoubleArray) : Iterable<Obs
*/
fun index(start: Double = 1.0) = TimeSeries(timeline, values.index(start))

/**
* Normalize the values
*/
fun normalize() = TimeSeries(timeline, values.normalize())

/**
* Return the observation that contains the maximum value.
*/
Expand Down
11 changes: 11 additions & 0 deletions roboquant/src/main/kotlin/org/roboquant/common/extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.roboquant.common

import kotlinx.coroutines.delay
import org.hipparchus.stat.descriptive.moment.StandardDeviation
import org.roboquant.common.Config.EPS
import java.lang.Integer.max
import java.lang.Integer.min
Expand Down Expand Up @@ -221,6 +222,16 @@ fun DoubleArray.index(start: Double = 1.0): DoubleArray {
return this / first
}

/**
* Returns a normalized array
*/
fun DoubleArray.normalize(): DoubleArray {
val data = clean()
val mean = data.average()
val std = StandardDeviation().evaluate(data, mean) + 0.000001
return (this - mean) / std
}

/**
* Returns the index of the first maximum value
*/
Expand Down

0 comments on commit 3090cdd

Please sign in to comment.