Skip to content

Instantly share code, notes, and snippets.

@eiryu
Last active March 1, 2019 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eiryu/65aa245cd0e39978ff37a2dd36d7935d to your computer and use it in GitHub Desktop.
Save eiryu/65aa245cd0e39978ff37a2dd36d7935d to your computer and use it in GitHub Desktop.
Zaifコイン積み立て Groovy
// Groovy Version: 2.5.0 JVM: 1.8.0_181 Vendor: Azul Systems, Inc. OS: Mac OS X
import java.time.LocalDateTime
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.TemporalAccessor
def records = []
new File('/Users/eiryu/Downloads/zaif').eachFileRecurse { file ->
def m = (file.name =~ /zaif_コイン積み立て_(\d\d\d\d)(\d\d)\.csv/)
if (m.matches()) {
boolean isFirstLine = true
new File(file.absolutePath).eachLine { line ->
def columns = line.split(',')
def yearOfFile = m[0][1] as Integer
def monthOfFile = m[0][2] as Integer
int year = yearOfFile
def monthAndDay = columns[0].split('/')
def month = monthAndDay[0]
def day = monthAndDay[1]
// 11月以降のファイルの中で、月が2月までのものは来年の日付なので年を加算
if (monthOfFile >= 11 && (month.toInteger() <= 2)) {
++year
}
def paid = columns[2].replaceAll('円', '')
def volume = columns[3].replaceAll('BTC', '')
Record record = new Record()
record.timestamp = LocalDateTime.of(year, month.toInteger(), day.toInteger(), 0, 0)
record.volume = volume.toBigDecimal()
record.price = paid.toBigDecimal() / volume.toBigDecimal()
// 月の最初の取引に手数料を全て付ける
if (isFirstLine) {
record.fee = 250
isFirstLine = false
}
records += record
}
}
}
// header
println 'Timestamp,Action,Source,Base,Volume,Price,Counter,Fee,FeeCcy'
records.sort { it.timestamp }.each { println it }
class Record {
LocalDateTime timestamp
String action = 'BUY'
String source = 'manual input'
String base = 'BTC'
BigDecimal volume
int price
String counter = 'JPY'
int fee
String feeCcy = 'JPY'
@Override
String toString() {
return "${timestamp.format(DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss'))},$action,$source,$base,$volume,$price,$counter,$fee,$feeCcy"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment