Skip to content

Commit

Permalink
#13 #16 Adds various fixes related to time calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
preslavrachev committed Aug 25, 2017
1 parent d383af0 commit b405548
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.preslavrachev.cryptotrader

import com.preslavrachev.cryptotrader.extension.minusSecondPeriods
import com.preslavrachev.cryptotrader.extension.toCandlestick
import com.preslavrachev.cryptotrader.extension.toUtcUnixTimestamp
import com.preslavrachev.cryptotrader.extension.toUnixTimestamp
import com.preslavrachev.cryptotrader.mvc.model.OrderTypeEnum
import com.preslavrachev.cryptotrader.mvc.model.TradeRecord
import com.preslavrachev.cryptotrader.persistence.repository.TradeRecordRepository
Expand Down Expand Up @@ -43,7 +43,7 @@ class CryptotraderApplication {

@Scheduled(fixedRate = 5 * 60 * 1000)
fun testStrategy() {
val end = LocalDateTime.now().toUtcUnixTimestamp()
val end = LocalDateTime.now().toUnixTimestamp()
val start = end.minusSecondPeriods(5, 300)
val chartData = poloniexApiService.getChartData(start = start, end = end)
val leftSide = chartData.dropLast(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package com.preslavrachev.cryptotrader.extension

import java.time.LocalDateTime

fun LocalDateTime.toUtcUnixTimestamp(): Long {
return this.toEpochSecond(java.time.ZoneOffset.UTC)
fun LocalDateTime.toUnixTimestamp(): Long {
return this.toEpochSecond(java.time.ZoneOffset.ofHours(2))
}

fun Long.minusSecondPeriods(periods: Long, secondsInPeriod: Long): Long {
val timePeriod = periods * secondsInPeriod
assert(timePeriod > this) { "The time period cannot exceed the current time!" }
assert(timePeriod <= this) { "The time period cannot exceed the current time!" }

return this - timePeriod
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.preslavrachev.cryptotrader.mvc.controller
import com.preslavrachev.cryptotrader.extension.minusSecondPeriods
import com.preslavrachev.cryptotrader.extension.toUtcUnixTimestamp
import com.preslavrachev.cryptotrader.extension.toUnixTimestamp
import com.preslavrachev.cryptotrader.mvc.model.Order
import com.preslavrachev.cryptotrader.session.AppSession
import org.springframework.stereotype.Controller
Expand All @@ -24,7 +24,7 @@ class DashboardController {
@RequestMapping("hello", method = arrayOf(RequestMethod.GET))
@ResponseBody
fun hello(): PoloniexApiService.ChartDataEntryList {
val end = LocalDateTime.now().toUtcUnixTimestamp()
val end = LocalDateTime.now().toUnixTimestamp()
val start = end.minusSecondPeriods(100, 300)
return poloniexApiService.getChartData(start = start, end = end)
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/remote/poloniex/service/PoloniexApiService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class PoloniexApiService {
.queryParam(COMMAND_PARAM, CommandEnum.RETURN_CHART_DATA.label)
.queryParam(CURRENCY_PAIR_PARAM, CurrencyPairEnum.USDT_BTC)
.queryParam(PERIOD_PARAM, 300)
.queryParam(START_PARAM, 1500000000)
.queryParam(END_PARAM, 1500030000)
.queryParam(START_PARAM, start)
.queryParam(END_PARAM, end)
.build()

val response = restTemplate.getForEntity(url, ChartDataEntryList::class.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.preslavrachev.cryptotrader.instrument

import com.preslavrachev.cryptotrader.config.MainAppConfig
import com.preslavrachev.cryptotrader.extension.minusSecondPeriods
import com.preslavrachev.cryptotrader.extension.toCandlestick
import com.preslavrachev.cryptotrader.extension.toUnixTimestamp
import com.preslavrachev.cryptotrader.trading.instrument.candlestick.CandlestickPatternEnum
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.SpringRunner
import remote.poloniex.service.PoloniexApiService
import java.time.LocalDateTime
import javax.inject.Inject


Expand All @@ -22,8 +25,9 @@ class CandlestickITest {
@Ignore("Use only manually for now")
@Test()
fun testCandlestickPatterns(): Unit {

val chartData = poloniexApiService.getChartData(start = 1500000000, end = 1500030000)
val end = LocalDateTime.now().toUnixTimestamp()
val start = end.minusSecondPeriods(30, 300)
val chartData = poloniexApiService.getChartData(start = start, end = end)
val leftSide = chartData.dropLast(1)
val rightSide = chartData.drop(1)
val data = leftSide.zip(rightSide)
Expand Down

0 comments on commit b405548

Please sign in to comment.