-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7a79a2
commit d901a5a
Showing
17 changed files
with
352 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...-binance/src/main/kotlin/com/njkim/reactivecrypto/binance/http/BinanceAccountOperation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2019 namjug-kim | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.njkim.reactivecrypto.binance.http | ||
|
||
import com.njkim.reactivecrypto.core.common.model.account.Balance | ||
import com.njkim.reactivecrypto.core.http.AccountOperation | ||
import com.quantinel.remarketer.strategy.sdk.binance.BinanceRawPrivateHttpClient | ||
import reactor.core.publisher.Flux | ||
|
||
class BinanceAccountOperation( | ||
accessKey: String, | ||
secretKey: String, | ||
private val binanceRawPrivateHttpClient: BinanceRawPrivateHttpClient | ||
) : AccountOperation(accessKey, secretKey) { | ||
override fun balance(): Flux<Balance> { | ||
return binanceRawPrivateHttpClient | ||
.userData() | ||
.account() | ||
.flatMapIterable { it.balances } | ||
.map { | ||
Balance( | ||
it.asset, | ||
it.free, | ||
it.locked | ||
) | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...crypto-binance/src/main/kotlin/com/njkim/reactivecrypto/binance/http/BinanceHttpClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2019 namjug-kim | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.njkim.reactivecrypto.binance.http | ||
|
||
import com.njkim.reactivecrypto.core.http.ExchangeHttpClient | ||
import com.njkim.reactivecrypto.core.http.PrivateHttpClient | ||
import com.njkim.reactivecrypto.core.http.PublicHttpClient | ||
import com.quantinel.remarketer.strategy.sdk.binance.BinanceRawHttpClient | ||
|
||
class BinanceHttpClient : ExchangeHttpClient() { | ||
override fun privateApi(accessKey: String, secretKey: String): PrivateHttpClient { | ||
val binanceRawPrivateHttpClient = BinanceRawHttpClient() | ||
.private(accessKey, secretKey) | ||
return BinancePrivateHttpClient(accessKey, secretKey, binanceRawPrivateHttpClient) | ||
} | ||
|
||
override fun publicApi(): PublicHttpClient { | ||
TODO("not implemented") | ||
} | ||
} |
151 changes: 151 additions & 0 deletions
151
...to-binance/src/main/kotlin/com/njkim/reactivecrypto/binance/http/BinanceOrderOperation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Copyright 2019 namjug-kim | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.njkim.reactivecrypto.binance.http | ||
|
||
import com.njkim.reactivecrypto.core.common.model.ExchangeVendor | ||
import com.njkim.reactivecrypto.core.common.model.currency.CurrencyPair | ||
import com.njkim.reactivecrypto.core.common.model.order.* | ||
import com.njkim.reactivecrypto.core.common.model.paging.CursorPageable | ||
import com.njkim.reactivecrypto.core.common.model.paging.FirstPageRequest | ||
import com.njkim.reactivecrypto.core.common.model.paging.Page | ||
import com.njkim.reactivecrypto.core.common.model.paging.Pageable | ||
import com.njkim.reactivecrypto.core.http.OrderOperation | ||
import com.quantinel.remarketer.strategy.sdk.binance.BinanceRawPrivateHttpClient | ||
import reactor.core.publisher.Mono | ||
import java.math.BigDecimal | ||
|
||
class BinanceOrderOperation( | ||
accessKey: String, | ||
secretKey: String, | ||
private val binanceRawPrivateHttpClient: BinanceRawPrivateHttpClient | ||
) : OrderOperation(accessKey, secretKey) { | ||
override fun orderStatus(orderId: String): Mono<OrderStatus> { | ||
val splits = orderId.split("-") | ||
val pair = CurrencyPair.parse(splits[0]) | ||
val binanceOrderId = splits[1].toLong() | ||
|
||
return binanceRawPrivateHttpClient.userData() | ||
.order(pair, binanceOrderId) | ||
.map { | ||
OrderStatus( | ||
createOrderId(it.symbol, it.orderId), | ||
it.status.toOrderStatusType(), | ||
if (it.side == TradeSideType.BUY) OrderSideType.BID else OrderSideType.ASK, | ||
it.symbol, | ||
it.price, | ||
it.origQty, | ||
it.executedQty, | ||
createDateTime = it.time | ||
) | ||
} | ||
} | ||
|
||
override fun limitOrder( | ||
pair: CurrencyPair, | ||
tradeSideType: TradeSideType, | ||
price: BigDecimal, | ||
quantity: BigDecimal | ||
): Mono<OrderPlaceResult> { | ||
return binanceRawPrivateHttpClient.trade() | ||
.limitOrder(pair, tradeSideType, quantity, TimeInForceType.GTC, price) | ||
.map { OrderPlaceResult("${it.orderId}") } | ||
} | ||
|
||
override fun marketOrder( | ||
pair: CurrencyPair, | ||
tradeSideType: TradeSideType, | ||
quantity: BigDecimal | ||
): Mono<OrderPlaceResult> { | ||
return binanceRawPrivateHttpClient.trade() | ||
.marketOrder(pair, tradeSideType, quantity) | ||
.map { OrderPlaceResult(createOrderId(it.symbol, it.orderId)) } | ||
} | ||
|
||
/** | ||
* @param orderId $currencyPair-$originOrderId | ||
*/ | ||
override fun cancelOrder(orderId: String): Mono<OrderCancelResult> { | ||
val splits = orderId.split("-") | ||
val pair = CurrencyPair.parse(splits[0]) | ||
val binanceOrderId = splits[1].toLong() | ||
|
||
return binanceRawPrivateHttpClient.trade() | ||
.cancelOrder(pair, binanceOrderId) | ||
.map { OrderCancelResult() } | ||
} | ||
|
||
override fun openOrders(pair: CurrencyPair, pageable: Pageable): Mono<Page<OrderStatus>> { | ||
return binanceRawPrivateHttpClient | ||
.userData() | ||
.openOrders(pair) | ||
.map { | ||
OrderStatus( | ||
createOrderId(it.symbol, it.orderId), | ||
it.status.toOrderStatusType(), | ||
if (it.side == TradeSideType.BUY) OrderSideType.BID else OrderSideType.ASK, | ||
it.symbol, | ||
it.price, | ||
it.origQty, | ||
it.executedQty, | ||
createDateTime = it.time | ||
) | ||
} | ||
.buffer() | ||
.next() | ||
.map { | ||
Page( | ||
it, | ||
FirstPageRequest(0) | ||
) | ||
} | ||
} | ||
|
||
override fun tradeHistory(pair: CurrencyPair, pageable: Pageable): Mono<Page<TickData>> { | ||
val cursorPageable = when (pageable) { | ||
is FirstPageRequest -> pageable.toCursorPageable() | ||
is CursorPageable -> pageable.next() as CursorPageable | ||
else -> throw UnsupportedOperationException("not allow ${pageable.javaClass.simpleName}") | ||
} | ||
|
||
return binanceRawPrivateHttpClient | ||
.userData() | ||
.myTrades(pair, fromId = cursorPageable.cursor?.toLong()) | ||
.map { | ||
TickData( | ||
"${it.id}", | ||
it.time, | ||
it.price, | ||
it.qty, | ||
it.symbol, | ||
ExchangeVendor.BINANCE, | ||
if (it.isBuyer) TradeSideType.BUY else TradeSideType.SELL | ||
) | ||
} | ||
.buffer() | ||
.next() | ||
.map { | ||
Page( | ||
it, | ||
CursorPageable(cursorPageable.cursor, it.lastOrNull()?.uniqueId, 500) | ||
) | ||
} | ||
} | ||
|
||
private fun createOrderId(pair: CurrencyPair, orderId: Long): String { | ||
return "$pair-$orderId" | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...binance/src/main/kotlin/com/njkim/reactivecrypto/binance/http/BinancePrivateHttpClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2019 namjug-kim | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.njkim.reactivecrypto.binance.http | ||
|
||
import com.njkim.reactivecrypto.core.http.AccountOperation | ||
import com.njkim.reactivecrypto.core.http.OrderOperation | ||
import com.njkim.reactivecrypto.core.http.PrivateHttpClient | ||
import com.quantinel.remarketer.strategy.sdk.binance.BinanceRawPrivateHttpClient | ||
|
||
class BinancePrivateHttpClient( | ||
accessKey: String, | ||
secretKey: String, | ||
private val binanceRawPrivateHttpClient: BinanceRawPrivateHttpClient | ||
) : PrivateHttpClient(accessKey, secretKey) { | ||
override fun account(): AccountOperation { | ||
return BinanceAccountOperation(accessKey, secretKey, binanceRawPrivateHttpClient) | ||
} | ||
|
||
override fun order(): OrderOperation { | ||
return BinanceOrderOperation(accessKey, secretKey, binanceRawPrivateHttpClient) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.