Skip to content

Commit

Permalink
Split webserver 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaron committed Aug 10, 2023
1 parent 1b7a523 commit 7b925bc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
31 changes: 23 additions & 8 deletions roboquant-extra/src/test/kotlin/org/roboquant/samples/alpaca.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ import org.roboquant.strategies.EMAStrategy
import java.time.Instant
import kotlin.io.path.div


private val symbols = listOf(
"AAPL",
"IBM",
"JPM",
"MSFT",
"TSLA",
"GOOGL",
"AMZN",
"BRK.A",
"V",
"BABA",
"NVDA",
"JNJ",
"TSM",
"WMT"
).toTypedArray()

private fun alpacaBroker() {
val broker = AlpacaBroker()
println(broker.account.fullSummary())
Expand Down Expand Up @@ -94,15 +112,13 @@ private fun alpacaTradeStocks() {

private fun alpacaPaperTrade() {
val feed = AlpacaLiveFeed()

val symbols = feed.availableStocks.take(10).symbols
println(symbols.toList())

feed.subscribeStocks(*symbols, type = PriceActionType.QUOTE)
feed.subscribeStocks(*symbols, type = PriceActionType.PRICE_BAR)
feed.heartbeatInterval = 30_000
val strategy = EMAStrategy.PERIODS_5_15
val roboquant = Roboquant(strategy, AccountMetric(), ProgressMetric(), logger = MemoryLogger(false))
val tf = Timeframe.next(10.minutes)
val broker = AlpacaBroker()
val roboquant =
Roboquant(strategy, AccountMetric(), ProgressMetric(), broker = broker, logger = MemoryLogger(false))
val tf = Timeframe.next(60.minutes)

val server = WebServer()
server.start()
Expand All @@ -113,7 +129,6 @@ private fun alpacaPaperTrade() {
println(roboquant.broker.account.summary())
}


/**
* Alpaca historic feed example where we retrieve 1-minutes price-bars in batches of 1 day for 100 days.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ internal class DetailHandler(private val runs: Map<String, WebServer.RunInfo>) :
}).then(function(data) {
console.log(data);
lineSeries.setData(data);
chart.timeScale().fitContent();
});
console.log(value);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ internal class MetricHandler(private val runs: Map<String, WebServer.RunInfo>) :
val run = elem.first()
val metricName = elem.last()

val data = StringBuilder("[")
val info = runs.getValue(run)
val metrics = info.roboquant.logger.getMetric(metricName, run)

val data = StringBuilder("[")
metrics.forEachIndexed { index, observation ->
data.append("""{"time":${observation.time.epochSecond},"value":${observation.value}}""")
if (index < (metrics.size -1)) data.append(",")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ import kotlin.collections.set
*
* @property enableMetrics should metrics be exposed, default is false
*/
abstract class BasePolicy(private val prefix: String = "policy.", var enableMetrics: Boolean = false) : Policy {
abstract class BasePolicy(private val prefix: String = "policy.", ) : Policy {

/**
* should metrics be exposed, default is false
*/
var enableMetrics: Boolean = false

private val metrics = mutableMapOf<String, Double>()

Expand Down
6 changes: 5 additions & 1 deletion roboquant/src/test/kotlin/org/roboquant/samples/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ fun testingStrategies() {


fun signalsOnly() {
class MyPolicy : BasePolicy(enableMetrics = true, prefix = "") {
class MyPolicy : BasePolicy(prefix = "") {

init {
enableMetrics = true
}

override fun act(signals: List<Signal>, account: Account, event: Event): List<Order> {
for (signal in signals) {
Expand Down

0 comments on commit 7b925bc

Please sign in to comment.