Skip to content

Commit

Permalink
better Future creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Dekkers committed May 11, 2023
1 parent 97f7ea7 commit c08b188
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 21 deletions.
6 changes: 0 additions & 6 deletions roboquant-jupyter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@
<artifactId>echarts-java</artifactId>
<version>1.0.7</version>
</dependency>
<!-- dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>dataframe-core</artifactId>
<version>0.11.0-dev-1608</version>
<scope>provided</scope>
</dependency -->
</dependencies>

</project>
2 changes: 1 addition & 1 deletion roboquant/src/main/kotlin/org/roboquant/Roboquant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Roboquant(

start(runInfo.phase)
try {
var orders = listOf<Order>()
var orders = emptyList<Order>()
while (true) {
val event = channel.receive()
orders = step(orders, event, runInfo)
Expand Down
15 changes: 9 additions & 6 deletions roboquant/src/main/kotlin/org/roboquant/common/Asset.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package org.roboquant.common
import java.math.BigDecimal
import java.math.RoundingMode
import java.time.LocalDate
import java.time.Month
import java.time.format.DateTimeFormatter

/**
Expand Down Expand Up @@ -102,21 +103,22 @@ data class Asset(
}

/**
* Returns a future contract based on the provided parameters
* Returns a future contract based on the provided parameters. It will generate a [symbol] name using the
* Future Contract Trading Code standard.
*/
fun futureContract(
symbol: String,
month: Char,
month: Month,
year: Int,
currencyCode: String = "USD",
exchangeCode: String = "",
multiplier: Double = 1.0,
id: String = ""
): Asset {
val months = listOf('F', 'G', 'H', 'J', 'K', 'M', 'N', 'Q', 'U', 'V', 'X', 'Z')
require(month in months) { "$month not one of $months" }
val months = arrayOf('F', 'G', 'H', 'J', 'K', 'M', 'N', 'Q', 'U', 'V', 'X', 'Z')
val monthEncoding = months[month.value - 1]
val yearCode = year.toString()
val futureSymbol = "$symbol$month${yearCode.takeLast(2)}"
val futureSymbol = "$symbol$monthEncoding${yearCode.takeLast(2)}"
return Asset(futureSymbol, AssetType.FUTURES, currencyCode, exchangeCode, multiplier, id)
}

Expand Down Expand Up @@ -159,7 +161,7 @@ data class Asset(
}

/**
* Faster hashcode than default
* Faster hashcode that is well distributed than the default data class generated one
*/
override fun hashCode(): Int {
return symbol.hashCode()
Expand All @@ -183,6 +185,7 @@ data class Asset(
* It supports fractional sizes by providing a number of [fractions] bigger than 0.
*/
fun contractSize(amount: Double, price: Double, fractions: Int = 0): Size {
require (fractions >= 0) { "factions has to be >= 0, found $fractions" }
val singleContractValue = value(Size.ONE, price).value
val size = BigDecimal(amount / singleContractValue).setScale(fractions, RoundingMode.DOWN)
return Size(size)
Expand Down
12 changes: 4 additions & 8 deletions roboquant/src/test/kotlin/org/roboquant/common/AssetTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package org.roboquant.common
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.time.LocalDate
import java.time.Month
import kotlin.test.assertContains
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
Expand All @@ -45,15 +46,11 @@ internal class AssetTest {
val a = Asset.optionContract("SPX", LocalDate.parse("2014-11-22"), 'P', "19.50")
assertEquals("SPX 141122P00019500", a.symbol)

val b = Asset.futureContract("GC", 'Z', 18)
val b2 = Asset.futureContract("GC", 'Z', 2018)
val b = Asset.futureContract("GC", Month.DECEMBER, 18)
val b2 = Asset.futureContract("GC", Month.DECEMBER, 2018)
assertEquals("GCZ18", b.symbol)
assertEquals(b, b2)

assertThrows<IllegalArgumentException> {
Asset.futureContract("GC", 'P', 18)
}

val c = Asset.forexPair("EUR_USD")
assertEquals("EUR/USD", c.symbol)

Expand Down Expand Up @@ -101,8 +98,7 @@ internal class AssetTest {
val s2 = a.contractSize(1000.0, 1.0, 4)
assertEquals(Size(10), s2)

val s3 = a.contractSize(250.0, 1.0, 4)
assertEquals(Size("2.5"), s3)
assertThrows<java.lang.IllegalArgumentException> { a.contractSize(250.0, 1.0, -1) }
}

}

0 comments on commit c08b188

Please sign in to comment.