Skip to content

Instantly share code, notes, and snippets.

@Schellinkhoutkamp
Created April 5, 2019 12:10
Show Gist options
  • Save Schellinkhoutkamp/d35fad418a85ce6d68daa6c045ca91e4 to your computer and use it in GitHub Desktop.
Save Schellinkhoutkamp/d35fad418a85ce6d68daa6c045ca91e4 to your computer and use it in GitHub Desktop.
Jared algos
import (
"context"
"fmt"
"math"
"strconv"
"time"
"github.com/adshao/go-binance"
)
var (
//Binance
binanceAPIKey = "Bh2a6vVKF0JhO20wFbifSV68G67OAyRPjlX3Mmetvo4BKwskoXEf6yAtTjnhfLVM"
binanceSecretKey = "cz0JL5qJtUTI7wY3slVctM45VJDyjpyKpTMmiGeqVmZJanWEgBmjYCER3FR8UWFd"
)
func humanTimeToUnixNanoTime(input time.Time) int64 {
return int64(time.Nanosecond) * input.UnixNano() / int64(time.Millisecond)
}
func main() {
client := binance.NewClient(binanceAPIKey, binanceSecretKey)
targetSymbol := "BTCUSDT"
// reference https://golang.org/pkg/time/#Date
// func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
// NOTE : Binance only accepts unix nano timestamp, not unix time stamp
// For example
// 1527616800 <----- will return empty result or wrong date range
// 1527616899999 <--- ok
//startTimeHumanReadable := time.Date(2018, time.October, 10, 0, 0, 0, 0, time.UTC)
//start := humanTimeToUnixNanoTime(startTimeHumanReadable)
//endTimeHumanReadable := time.Date(2018, time.October, 24, 0, 0, 0, 0, time.UTC)
//end := humanTimeToUnixNanoTime(endTimeHumanReadable)
targetInterval := "1m" /// case sensitive, 1D != 1d or 30M != 30m
data, err := client.NewKlinesService().Symbol(targetSymbol).Interval(targetInterval).Do(context.Background())
if err != nil {
fmt.Println(err)
return
}
totalGain := 0.0
totalLoss := 0.0
for i := 1; i < len(data); i++ {
previous := data[i].Close
current := data[i-1].Close
// convert string to float64
previousClose, _ := strconv.ParseFloat(previous, 64)
currentClose, _ := strconv.ParseFloat(current, 64)
difference := currentClose - previousClose
if difference >= 0 {
totalGain += difference
} else {
totalLoss -= difference
}
}
rs := totalGain / math.Abs(totalLoss)
rsi := 100 - 100/(1+rs)
fmt.Println("RSI for "+targetSymbol+" : ", rsi)
}
func rc(t int, x []float64) float64 {
if len(x)-2 < t {
return 0
}
s := float64(0)
for j := 0; j < t; j++ {
s += x[j] - x[j+1]
}
return (s / float64(t))
}
func sign(x float64) float64 {
if x > 0 {
return 1
} else if x < 0 {
return -1
} else {
return 0
}
}
func dirChange(f []float64, s []float64) bool {
if sign(f[0]-s[0]) != sign(f[1]-s[1]) {
return true
}
return false
}
func sma(t int, xc []float64) float64 {
som := float64(0)
if len(xc) > t {
xc = xc[0:t]
}
for i := 0; i < len(xc); i++ {
som += xc[i]
}
return som / float64(len(xc))
}
func (indicator AlgoIndicators) algo(symbol map[string][]float64) {
index := symbol["BNCE_XBTUSD"]
magnTresh := index[0] / 500 // start trading from this drop magnitude
magnSpeed := magnTresh / 100 // minimal consistent change per second
indicator["FAST"][0] = sma(10, index)
indicator["SLOW"][0] = sma(30, index)
indicator["FASTRC"][0] = rc(30, indicator["FAST"])
indicator["FASTRCF"][0] = rc(10, indicator["FAST"])
indicator["FASTRCFF"][0] = rc(3, indicator["FAST"])
indicator["RCDIFF"][0] = indicator["FASTRCF"][0] - indicator["FASTRC"][0]
indicator["FASTRCFFRC"][0] = rc(3, indicator["FASTRCFF"])
if dirChange(indicator["FAST"], indicator["SLOW"]) || len(indicator["FAST"]) < 2 {
indicator["DIRTIME"][0] = 0
indicator["DIRMAGN"][0] = 0
//mark("hoi")
} else {
indicator["DIRTIME"][0] = indicator["DIRTIME"][1] + 1
indicator["DIRMAGN"][0] = indicator["DIRMAGN"][1] + indicator["FAST"][0] - indicator["FAST"][1]
}
//log(indicator["FAST"][0], indicator["FAST"][1])
func() {
if len(indicator["SLOW"]) > int(indicator["DIRTIME"][0]*2) {
if indicator["FAST"][0] < indicator["SLOW"][int(indicator["DIRTIME"][0]*2)]+(indicator["DIRTIME"][0]*magnSpeed)/2 { //// should really be a long term drop, not a peak
if indicator["DIRMAGN"][0] < (-magnTresh) && indicator["DIRMAGN"][0] < (-indicator["DIRTIME"][0])*magnSpeed {
mark("m")
if indicator["RCDIFF"][0] > 0 && indicator["FASTRCF"][0] > 0 && indicator["FASTRCFFRC"][0] > 0 && indicator["FAST"][0] < indicator["SLOW"][0] {
trade("DROPREBOUND", "BNCE", "XBTUSD", false, 1, 1)
}
}
}
}
if indicator["RCDIFF"][0] < 0 && indicator["FASTRCFF"][0] < 0 {
trade("DROPREBOUND", "BNCE", "XBTUSD", false, -1, 1)
} // over the top
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment