Skip to content

Commit

Permalink
Easier TaLib integration 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Dekkers committed Jun 2, 2023
1 parent 9976f46 commit 6c845f9
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docs/THIRDPARTY.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= Third Party Open Source

_Roboquant_ couldn't have been developed without the use of free and open source third party software. This document list some great open software libraries and other artefacts used in roboquant.
_Roboquant_ couldn't have been developed without the use of free and open source third party software. This document lists some great open software libraries and other artefacts used in roboquant.

== Software
* https://kotlinlang.org[Kotlin]: modern, concise and safe programming language that makes writing code a joy.
Expand Down
4 changes: 2 additions & 2 deletions docs/TODO.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Version 0.8 is all about making sure that (back-)testing works, and the most com
* [x] Add documentation on how to install and get started
* [x] Support for advanced order types
* [x] Add info on key design concepts behind the software, so it becomes easier to contribute
* [x] Bring back Interactive Brokers integration. The code was already developed but due to license restrictions the TwsApi.jar file could not be included with roboquant.
* [x] Bring back Interactive Brokers integration. The code was already developed, but due to license restrictions the TwsApi.jar file could not be included with roboquant.
* [x] See how to best fit crypto trading with current account structure

== Version 0.9.x (2022)
Expand Down Expand Up @@ -51,6 +51,6 @@ The topics mentioned here are some of the ideas for the future releases:
* [ ] More ready-to-use strategies out of the box
* [ ] Add more advanced slippage- and fee-models
* [ ] More complex deep learning strategies
* [ ] Right now messages and formats support English format only, add L10N/I18N support
* [ ] Right now messages support the English format only, add L10N/I18N support
* [ ] Make video(s) that show the steps to develop and test your own strategies
* [ ] Come up with a way users can easily share strategies, code snippets and other best practices
Binary file modified docs/roboquant_header.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 12 additions & 8 deletions roboquant-ta/src/main/kotlin/org/roboquant/ta/RSIStrategy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,21 @@ class RSIStrategy(
* @see RecordingStrategy.generate
*/
override fun generate(event: Event): List<Signal> {
history.addAll(event, windowSize + 1, "CLOSE")
history.addAll(event, 1, "CLOSE")
val result = mutableListOf<Signal>()
for (asset in event.prices.keys) {
val data = history.getValue(asset)
if (data.isFull()) {
val rsi = taLib.rsi(data.toDoubleArray(), windowSize)
record(asset.symbol, rsi)
if (rsi > highThreshold)
result.add(Signal(asset, Rating.SELL))
else if (rsi < lowThreshold)
result.add(Signal(asset, Rating.BUY))
try {
if (data.isFull()) {
val rsi = taLib.rsi(data.toDoubleArray(), windowSize)
record(asset.symbol, rsi)
if (rsi > highThreshold)
result.add(Signal(asset, Rating.SELL))
else if (rsi < lowThreshold)
result.add(Signal(asset, Rating.BUY))
}
} catch (ex: InsufficientData) {
data.increaeseCapacity(ex.minSize)
}
}
return result
Expand Down
12 changes: 6 additions & 6 deletions roboquant-ta/src/main/kotlin/org/roboquant/ta/TaLibIndicator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class TaLibIndicator (
* Return a `Relative Strength Indicator` for the provided [barCount]
*/
fun rsi(barCount: Int = 10) : TaLibIndicator {
return TaLibIndicator() {
return TaLibIndicator {
mapOf("rsi$barCount" to rsi(it, barCount))
}
}
Expand All @@ -77,7 +77,7 @@ class TaLibIndicator (
* Return a `Bollinger Bands` Indicator for the provided [barCount]
*/
fun bbands(barCount: Int = 10) : TaLibIndicator {
return TaLibIndicator() {
return TaLibIndicator {
val (high, mid, low) = bbands(it, barCount)
val prefix = "bb$barCount"
mapOf("$prefix.low" to low, "$prefix.high" to high, "$prefix.mid" to mid)
Expand All @@ -88,7 +88,7 @@ class TaLibIndicator (
* Return an `Exponential Moving Average` Indicator for the provided [barCount]
*/
fun ema(barCount: Int = 10) : TaLibIndicator {
return TaLibIndicator() {
return TaLibIndicator {
mapOf("ema$barCount" to ema(it, barCount))
}
}
Expand All @@ -97,7 +97,7 @@ class TaLibIndicator (
* Return a `Simple Moving Average` Indicator for the provided [barCount]
*/
fun sma(barCount: Int = 10) : TaLibIndicator {
return TaLibIndicator() {
return TaLibIndicator {
mapOf("sma$barCount" to sma(it, barCount))
}
}
Expand All @@ -106,7 +106,7 @@ class TaLibIndicator (
* Return a `Money Flow In` Indicator for the provided [barCount]
*/
fun mfi(barCount: Int = 10) : TaLibIndicator {
return TaLibIndicator() {
return TaLibIndicator {
mapOf("mfi$barCount" to mfi(it, barCount))
}
}
Expand All @@ -115,7 +115,7 @@ class TaLibIndicator (
* Return a `Stochastic` Indicator for the provided periods
*/
fun stochastic(fastKPeriod: Int = 5, slowKPeriod: Int = 3, slowDPeriod : Int = slowKPeriod) : TaLibIndicator {
return TaLibIndicator() {
return TaLibIndicator {
val (d, k) = stoch(it, fastKPeriod, slowKPeriod, slowDPeriod = slowDPeriod)
mapOf("stochatic.d" to d, "stochatic.k" to k)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class TaLibMetric(

private val buffers = mutableMapOf<Asset, PriceBarSerie>()
private val taLib = TaLib()
// private val logger: Logger = Logging.getLogger(TALibMetric::class)

/**
* @see Metric.calculate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class TaLibSignalStrategy(
* Breakout strategy that supports different entry and exit periods
*/
fun breakout(entryPeriod: Int = 100, exitPeriod: Int = 50): TaLibSignalStrategy {
return TaLibSignalStrategy() { asset, series ->
return TaLibSignalStrategy { asset, series ->
when {
recordHigh(series.high, entryPeriod) -> Signal(asset, Rating.BUY, SignalType.BOTH)
recordLow(series.low, entryPeriod) -> Signal(asset, Rating.SELL, SignalType.BOTH)
Expand All @@ -72,7 +72,7 @@ class TaLibSignalStrategy(
*/
fun macd(): TaLibSignalStrategy {

val strategy = TaLibSignalStrategy() { asset, prices ->
val strategy = TaLibSignalStrategy { asset, prices ->
val (_, _, diff) = macd(prices, 12, 26, 9)
val (_, _, diff2) = macd(prices, 12, 26, 9, 1)
when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import java.time.Instant
import kotlin.math.absoluteValue

/**
* Fee model using a fixed percentage fee of total absolute value of the execution.
* The PercentageFeeModel defines a percentage of total value as the fee. For every trade it will calculate to the
* total value and then use the [feePercentage] as the fee.
*
* @property feePercentage fee as a percentage of total execution cost, 0.01 = 1%. Default is 0.0
* @constructor Create a new percentage fee model
Expand Down
3 changes: 3 additions & 0 deletions roboquant/src/main/kotlin/org/roboquant/common/TimePeriod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

@file:Suppress("unused")

package org.roboquant.common

import java.time.*
Expand All @@ -27,6 +29,7 @@ private fun createDuration(hours: Int, minutes: Int, seconds: Int, nanos: Long):
return result
}


@Deprecated("Renamed to TimePeriod", ReplaceWith("TimePeriod","org.roboquant.common.TimePeriod"))
typealias TradingPeriod=TimePeriod

Expand Down

0 comments on commit 6c845f9

Please sign in to comment.