ta.TaLibMetric.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of roboquant-ta Show documentation
Show all versions of roboquant-ta Show documentation
Technical analysis support for the roboquant algorithmic trading platform
package org.roboquant.ta
import org.roboquant.brokers.Account
import org.roboquant.common.Asset
import org.roboquant.common.AssetFilter
import org.roboquant.feeds.Event
import org.roboquant.feeds.PriceBar
import org.roboquant.metrics.MetricResults
import org.roboquant.metrics.SimpleMetric
import org.roboquant.strategies.utils.PriceBarSeries
/**
* Add a technical indicator as a metric, for example a moving average.
*
* @property name the name of the metric
* @property history
* @property assetFilter determines which assets to process, default is [AssetFilter.noFilter]
* @property block
* @constructor Create new metric
*/
class TaLibMetric(
private val name: String,
private val history: Int = 15,
private val assetFilter: AssetFilter = AssetFilter.noFilter(),
private var block: TaLib.(series: PriceBarSeries) -> Double
) : SimpleMetric() {
private val buffers = mutableMapOf()
private val taLib = TaLib()
// private val logger: Logger = Logging.getLogger(TALibMetric::class)
override fun calc(account: Account, event: Event): MetricResults {
val metrics = mutableMapOf()
val actions = event.prices.values.filterIsInstance().filter { assetFilter.filter(it.asset) }
for (priceAction in actions) {
val asset = priceAction.asset
val buffer = buffers.getOrPut(asset) { PriceBarSeries(asset, history) }
if (buffer.add(priceAction)) {
val metric = block.invoke(taLib, buffer)
val name = "$name.${asset.symbol.lowercase()}"
metrics[name] = metric
}
}
return metrics
}
override fun reset() {
super.reset()
buffers.clear()
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy