
Facilitates interaction with Alpaca Trading API for account management, order placement, and real-time data streaming, including historical trade data access and test mode for simulating stock prices.
A Kotlin Multiplatform library for interacting with the Alpaca Trading API.
Add the following to your build.gradle.kts file:
dependencies {
implementation("io.github.jeffreyliu8:alpaca-kotlin:0.0.11")
}Please check out the test file here https://github.com/jeffreyliu8/alpaca-kotlin/blob/main/library/src/commonTest/kotlin/MyCommonUnitTest.kt
import alpaca.AlpacaClient
import alpaca.AlpacaClientImpl
// For paper trading
val client: AlpacaClient = AlpacaClientImpl(
isPaper = false,
apiKey = apiKey,
apiSecret = apiSecret,
)
// For live trading
val liveClient = AlpacaClientImpl(
isPaper = true,
apiKey = apiKey,
apiSecret = apiSecret,
)
// Get account information
val account = client.getAccount()
println(account)
// Get all positions
val positions = client.getPositions()
println(positions)
// Place an order
val order = client.placeOrder(
AlpacaOrderRequest(
symbol = "AAPL",
qty = "1",
side = AlpacaBuySell.BUY,
type = "market",
timeInForce = "day"
)
)
println(order)You can stream account updates and stock prices using Kotlin Flows.
runBlocking {
launch {
client.streamAccount().collect { accountUpdate ->
println("Received account update: $accountUpdate")
}
}
}runBlocking {
launch {
client.monitorStockPrice(setOf("AAPL", "GOOG")).collect { stockPriceUpdate ->
println("Received stock price update: $stockPriceUpdate")
}
}
}You can use the test mode to get a fake stock price for testing purposes.
runBlocking {
client.monitorStockPrice(
setOf("FAKEPACA"),
stockExchange = AlpacaStockExchangeOption.TEST
).collect {
println(it)
}
}This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.
Use ./gradlew publishToMavenCentral to make a new release
A Kotlin Multiplatform library for interacting with the Alpaca Trading API.
Add the following to your build.gradle.kts file:
dependencies {
implementation("io.github.jeffreyliu8:alpaca-kotlin:0.0.11")
}Please check out the test file here https://github.com/jeffreyliu8/alpaca-kotlin/blob/main/library/src/commonTest/kotlin/MyCommonUnitTest.kt
import alpaca.AlpacaClient
import alpaca.AlpacaClientImpl
// For paper trading
val client: AlpacaClient = AlpacaClientImpl(
isPaper = false,
apiKey = apiKey,
apiSecret = apiSecret,
)
// For live trading
val liveClient = AlpacaClientImpl(
isPaper = true,
apiKey = apiKey,
apiSecret = apiSecret,
)
// Get account information
val account = client.getAccount()
println(account)
// Get all positions
val positions = client.getPositions()
println(positions)
// Place an order
val order = client.placeOrder(
AlpacaOrderRequest(
symbol = "AAPL",
qty = "1",
side = AlpacaBuySell.BUY,
type = "market",
timeInForce = "day"
)
)
println(order)You can stream account updates and stock prices using Kotlin Flows.
runBlocking {
launch {
client.streamAccount().collect { accountUpdate ->
println("Received account update: $accountUpdate")
}
}
}runBlocking {
launch {
client.monitorStockPrice(setOf("AAPL", "GOOG")).collect { stockPriceUpdate ->
println("Received stock price update: $stockPriceUpdate")
}
}
}You can use the test mode to get a fake stock price for testing purposes.
runBlocking {
client.monitorStockPrice(
setOf("FAKEPACA"),
stockExchange = AlpacaStockExchangeOption.TEST
).collect {
println(it)
}
}This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.
Use ./gradlew publishToMavenCentral to make a new release