Skip to content

Commit

Permalink
bring new patterns to trades/quotes and @SafeVarargs everywhere (#61)
Browse files Browse the repository at this point in the history
* bring new patterns to trades/quotes and @SafeVarargs everywhere

* update samples

* moe ComparisonQueryFilterParameters into the right package

* remove broken imports

* fix broken imports
  • Loading branch information
mmoghaddam385 authored Dec 21, 2022
1 parent dadc5be commit 803121d
Show file tree
Hide file tree
Showing 49 changed files with 222 additions and 132 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Everything you'd expect from a client SDK plus...

## REST Request Options

_Added in v4.1.0_

For most use-cases, the default request options will do the trick,
but if you need some more flexibility on a per-request basis,
you can configure individual REST API requests with `PolygonRestOption`s.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ public static void IteratorSample(PolygonRestClient client) {
public static void TradesIteratorSample(PolygonRestClient client) {
System.out.println("Running trade iterator");
TradesParameters params = new TradesParametersBuilder()
.ticker("F")
.limit(1)
.build();

client.listTrades(params)
client.listTrades("F", params)
.asStream() // Convert to a Java stream for ease of use.
.limit(2)
.forEach((trade -> System.out.println(trade.getPrice())));
Expand All @@ -41,11 +40,10 @@ public static void TradesIteratorSample(PolygonRestClient client) {
public static void QuotesIteratorSample(PolygonRestClient client) {
System.out.println("Running Quote iterator");
QuotesParameters params = new QuotesParametersBuilder()
.ticker("F")
.limit(1)
.build();

client.listQuotes(params)
client.listQuotes("F", params)
.asStream() // Convert to a Java stream for ease of use.
.limit(2)
.forEach((Quote -> System.out.println(Quote.getAskPrice())));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.polygon.kotlin.sdk.sample;

import io.polygon.kotlin.sdk.ComparisonQueryFilterParameters;
import io.polygon.kotlin.sdk.ComparisonQueryFilterParametersBuilder;
import io.polygon.kotlin.sdk.rest.ComparisonQueryFilterParameters;
import io.polygon.kotlin.sdk.rest.ComparisonQueryFilterParametersBuilder;
import io.polygon.kotlin.sdk.rest.*;
import io.polygon.kotlin.sdk.rest.experimental.FinancialsParameters;
import io.polygon.kotlin.sdk.rest.experimental.FinancialsParametersBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,18 @@ fun iteratorExample(polygonClient: PolygonRestClient) {

fun tradesIteratorExample(polygonClient: PolygonRestClient) {
println("Running trade iterator:")
val params = TradesParameters(
ticker = "F",
limit = 1
)
val params = TradesParameters(limit = 1)

polygonClient.listTrades(params).asSequence()
polygonClient.listTrades("F", params).asSequence()
.take(2)
.forEachIndexed { index, tradeRes -> println("${index}: ${tradeRes.price}") }
}

fun quotesIteratorExample(polygonClient: PolygonRestClient) {
println("Running quote iterator:")
val params = QuotesParameters(
ticker = "F",
limit = 1
)
val params = QuotesParameters(limit = 1)

polygonClient.listQuotes(params).asSequence()
polygonClient.listQuotes("F", params).asSequence()
.take(2)
.forEachIndexed { index, quoteRes -> println("${index}: (${quoteRes.participantTimestamp}) | ${quoteRes.bidPrice} / ${quoteRes.askPrice}") }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.polygon.kotlin.sdk.sample

import com.tylerthrailkill.helpers.prettyprint.pp
import io.ktor.client.plugins.*
import io.polygon.kotlin.sdk.ComparisonQueryFilterParameters
import io.polygon.kotlin.sdk.rest.ComparisonQueryFilterParameters
import io.polygon.kotlin.sdk.DefaultOkHttpClientProvider
import io.polygon.kotlin.sdk.HttpClientProvider
import io.polygon.kotlin.sdk.rest.*
Expand Down Expand Up @@ -377,20 +377,14 @@ fun cryptoL2SnapshotSample(polygonClient: PolygonRestClient) {

fun getTradesSample(polygonClient: PolygonRestClient) {
println("F Trades:")
var params = TradesParameters(
ticker = "F",
limit = 2
)
polygonClient.getTradesBlocking(params).pp()
val params = TradesParameters(limit = 2)
polygonClient.getTradesBlocking("F", params).pp()
}

fun getQuotesSample(polygonClient: PolygonRestClient) {
println("F Quotes")
var params = QuotesParameters(
ticker = "F",
limit = 2
)
polygonClient.getQuotesBlocking(params).pp()
val params = QuotesParameters(limit = 2)
polygonClient.getQuotesBlocking("F", params).pp()

}

Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/io/polygon/kotlin/sdk/rest/Aggregates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/** See [PolygonRestClient.getAggregatesBlocking] */
@SafeVarargs
suspend fun PolygonRestClient.getAggregates(
params: AggregatesParameters,
vararg opts: PolygonRestOption
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.polygon.kotlin.sdk
package io.polygon.kotlin.sdk.rest

import io.ktor.http.*

Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/io/polygon/kotlin/sdk/rest/GroupedDaily.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.ktor.http.*
import io.polygon.kotlin.sdk.rest.reference.PolygonReferenceClient

/** See [PolygonRestClient.getGroupedDailyAggregatesBlocking] */
@SafeVarargs
suspend fun PolygonRestClient.getGroupedDailyAggregates(
params: GroupedDailyParameters,
vararg opts: PolygonRestOption
Expand Down
56 changes: 35 additions & 21 deletions src/main/kotlin/io/polygon/kotlin/sdk/rest/PolygonRestClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ constructor(
*
* API Doc: https://polygon.io/docs/stocks/get_v2_aggs_ticker__stocksticker__range__multiplier___timespan___from___to
*/
@SafeVarargs
fun getAggregatesBlocking(params: AggregatesParameters, vararg opts: PolygonRestOption): AggregatesDTO =
runBlocking { getAggregates(params, *opts) }

/** See [getAggregatesBlocking] */
@SafeVarargs
fun getAggregates(
params: AggregatesParameters,
callback: PolygonRestApiCallback<AggregatesDTO>,
Expand All @@ -92,13 +94,15 @@ constructor(
*
* API Doc: https://polygon.io/docs/stocks/get_v2_aggs_grouped_locale_us_market_stocks__date
*/
@SafeVarargs
fun getGroupedDailyAggregatesBlocking(
params: GroupedDailyParameters,
vararg opts: PolygonRestOption
): AggregatesDTO =
runBlocking { getGroupedDailyAggregates(params, *opts) }

/** See [getGroupedDailyAggregatesBlocking] */
@SafeVarargs
fun getGroupedDailyAggregates(
params: GroupedDailyParameters,
callback: PolygonRestApiCallback<AggregatesDTO>,
Expand All @@ -115,31 +119,35 @@ constructor(
* https://polygon.io/docs/options/get_v3_trades__optionsticker
* https://polygon.io/docs/crypto/get_v3_trades__cryptoticker
*/

@SafeVarargs
fun getTradesBlocking(
ticker: String,
params: TradesParameters,
vararg opts: PolygonRestOption
): TradesResponse =
runBlocking { getTrades(params, *opts) }
runBlocking { getTrades(ticker, params, *opts) }

@SafeVarargs
fun getTrades(
ticker: String,
params: TradesParameters,
callback: PolygonRestApiCallback<TradesResponse>,
vararg opts: PolygonRestOption
) {
coroutineToRestCallback(callback, { getTrades(params, *opts) })
coroutineToRestCallback(callback, { getTrades(ticker, params, *opts) })
}

/**
* listTrades is an iterator wrapper for getTrades
*/
@SafeVarargs
fun listTrades(
ticker: String,
params: TradesParameters,
vararg opts: PolygonRestOption
): RequestIterator<TradeResult> =
): RequestIterator<Trade> =
RequestIterator(
{ getTradesBlocking(params, *opts) },
{ getTradesBlocking(ticker, params, *opts) },
requestIteratorFetch<TradesResponse>()
)

Expand All @@ -153,20 +161,40 @@ constructor(
* https://polygon.io/docs/forex/get_v3_quotes__fxticker
* https://polygon.io/docs/options/get_v3_quotes__optionsticker
*/
@SafeVarargs
fun getQuotesBlocking(
ticker: String,
params: QuotesParameters,
vararg opts: PolygonRestOption
): QuotesResponse =
runBlocking { getQuotes(params, *opts) }
runBlocking { getQuotes(ticker, params, *opts) }

@SafeVarargs
fun getQuotes(
ticker: String,
params: QuotesParameters,
callback: PolygonRestApiCallback<QuotesResponse>,
vararg opts: PolygonRestOption
) {
coroutineToRestCallback(callback, { getQuotes(params, *opts) })
coroutineToRestCallback(callback, { getQuotes(ticker, params, *opts) })
}

/**
* Get an iterator to iterate through all pages of results for the given parameters.
*
* See [getQuotesBlocking] if you instead need to get exactly one page of results.
* See section "Pagination" in the README for more details on iterators.
*/
@SafeVarargs
fun listQuotes(
ticker: String,
params: QuotesParameters,
vararg opts: PolygonRestOption
): RequestIterator<Quote> = RequestIterator(
{ getQuotesBlocking(ticker, params, *opts) },
requestIteratorFetch<QuotesResponse>()
)

/**
* Get the simple moving average (SMA) for a ticker symbol over a given time range.
*
Expand Down Expand Up @@ -257,20 +285,6 @@ constructor(
) =
coroutineToRestCallback(callback, { getTechnicalIndicatorRSI(ticker, params, *opts) })


/**
* listQuotes is an iterator wrapper for getQuotes
*/
@SafeVarargs
fun listQuotes(
params: QuotesParameters,
vararg opts: PolygonRestOption
): RequestIterator<QuoteResult> =
RequestIterator(
{ getQuotesBlocking(params, *opts) },
requestIteratorFetch<QuotesResponse>()
)

private val baseUrlBuilder: URLBuilder
get() = httpClientProvider.getDefaultRestURLBuilder().apply {
host = polygonApiDomain
Expand Down
49 changes: 15 additions & 34 deletions src/main/kotlin/io/polygon/kotlin/sdk/rest/Quotes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,38 @@ import io.ktor.http.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/** See [PolygonRestClient.getQuotesBlocking] */
suspend fun PolygonRestClient.getQuotes(
ticker: String,
params: QuotesParameters,
vararg opts: PolygonRestOption
): QuotesResponse = fetchResult({
path(
"v3",
"quotes",
params.ticker
ticker
)
params.timestamp?.let { parameters["timestamp"] = it.toString() }
params.timestampLT?.let { parameters["timestamp.lt"] = it.toString() }
params.timestampLTE?.let { parameters["timestamp.lte"] = it.toString() }
params.timestampGT?.let { parameters["timestamp.gt"] = it.toString() }
params.timestampGTE?.let { parameters["timestamp.gte"] = it.toString() }

applyComparisonQueryFilterParameters("timestamp", params.timestamp)
params.order?.let { parameters["order"] = it }
params.limit?.let { parameters["limit"] = it.toString() }
params.sort?.let{ parameters["sort"] = it }
}, *opts)


@Builder
data class QuotesParameters(
/**
* The ticker symbol to get quotes for.
*/
val ticker: String,

/**
* Query by query using nanosecond Unix epoch time.
* Use timestampLT/LTE/GT/GTE for additional filtering
*/
val timestamp: Long? = null,

/**
* Return results where this field is less than the value.
*/
val timestampLT: Long? = null,

/**
* Return results where this field is less than or equal to the value.
*/
val timestampLTE: Long? = null,

/**
* Return results where this field is greater than the value.
* Query by timestamp. Either a date with the format YYYY-MM-DD or a nanosecond timestamp.
*/
val timestampGT: Long? = null,
val timestamp: ComparisonQueryFilterParameters<String>? = null,

/**
* Return results where this field is greater than or equal to the value.
* Order results based on the sort field.
* Can be "asc" or "desc"
*/
val timestampGTE: Long? = null,
val order: String? = null,

/**
* Limit the number of results returned, default is 10 and max is 50000.
Expand All @@ -64,7 +45,7 @@ data class QuotesParameters(
val limit: Int? = null,

/**
* Field used for ordering. See docs for valid fields
* Field used for ordering. See API docs for valid fields
*/
val sort: String? = null
)
Expand All @@ -74,11 +55,11 @@ data class QuotesResponse(
val status: String? = null,
@SerialName("request_id") val requestID: String? = null,
@SerialName("next_url") override val nextUrl: String? = null,
override val results: List<QuoteResult>? = null
) : Paginatable<QuoteResult>
override val results: List<Quote>? = null
) : Paginatable<Quote>

@Serializable
data class QuoteResult(
data class Quote(
@SerialName("ask_exchange") val askExchange: Int? = null,
@SerialName("ask_price") val askPrice: Double? = null,
@SerialName("bid_exchange") val bidExchange: Int? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package io.polygon.kotlin.sdk.rest

import com.thinkinglogic.builder.annotation.Builder
import io.ktor.http.*
import io.polygon.kotlin.sdk.ComparisonQueryFilterParameters
import io.polygon.kotlin.sdk.applyComparisonQueryFilterParameters
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand Down
Loading

0 comments on commit 803121d

Please sign in to comment.