Skip to content

Commit

Permalink
feat: v 1.0 - working base
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Mar 16, 2022
1 parent 10f0396 commit 753b59d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
45 changes: 27 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package main
import (
"fmt"
"log"
"os"
"strconv"
"time"

"github.com/FrostyDog/SAM/utility"

"github.com/Kucoin/kucoin-go-sdk"
"github.com/google/uuid"
)
Expand All @@ -24,7 +27,7 @@ var currentPrice string
var targetOperation string
var targetPrice string

var transactionExist bool = false
var transactionNotExists bool = false
var nextOperation string = "sell"

var numberOfTransaction = 0
Expand All @@ -45,22 +48,23 @@ func main() {
func checkOrder(s *kucoin.ApiService) {
var params = map[string]string{
"tradeType": "TRADE",
"staus": "active",
"status": "active",
}

var paginationParam = kucoin.PaginationParam{PageSize: 10, CurrentPage: 0}
var paginationParam = kucoin.PaginationParam{PageSize: 10, CurrentPage: 1}

resp, err := s.Orders(params, &paginationParam)
if err != nil {
fmt.Println("Failed at orders")
}

as := kucoin.OrdersModel{}
resp.ReadData(&as)

transactionExist = len(as) != 0
_, err = resp.ReadPaginationData(&as) // put variable instead of blank to see pagination/page resutlss
if err != nil {
fmt.Println("Failed at reading pagination")
}

log.Printf("length of the orders: %d", len(as) == 0)
transactionNotExists = len(as) == 0
}

func launchTicker(s *kucoin.ApiService) {
Expand All @@ -69,22 +73,22 @@ func launchTicker(s *kucoin.ApiService) {
for _ = range ticker.C {
getPrice(s, dSymbol)
checkOrder(s)
if !transactionExist {

if transactionNotExists {
calculatePrice(nextOperation)

if nextOperation == "sell" {
sellCoin(s, "", targetPrice)
}

if nextOperation == "buy" {
} else {
buyCoin(s, "", targetPrice)
}

numberOfTransaction++
}

if numberOfTransaction == 5 {
if numberOfTransaction == 10 {
ticker.Stop()
os.Exit(0)
}
}

Expand All @@ -97,16 +101,16 @@ func calculatePrice(side string) {
fmt.Println("error accured during parsing")
}
var p float64 = t + t*0.003
targetPrice = fmt.Sprint(p)
targetPrice = fmt.Sprint(utility.RoundFloat(p, 5))
}

if side == "buy" {
t, err := strconv.ParseFloat(currentPrice, 64)
if err != nil {
fmt.Println("error accured during parsing")
}
var p float64 = t - t*0.003
targetPrice = fmt.Sprint(p)
var p float64 = t - t*0.0025
targetPrice = fmt.Sprint(utility.RoundFloat(p, 5))
}

}
Expand All @@ -125,8 +129,6 @@ func getPrice(s *kucoin.ApiService, symbol string) {
}

currentPrice = as.Price

log.Printf("Token %s: with Price %s", as.BestAsk, as.Price)
}

func buyCoin(s *kucoin.ApiService, sy string, price string) {
Expand All @@ -141,6 +143,10 @@ func buyCoin(s *kucoin.ApiService, sy string, price string) {

_, err := s.CreateOrder(&o)

if err != nil {
log.Fatal(err)
}

if err == nil {
fmt.Println("buy operation done")
nextOperation = "sell"
Expand All @@ -160,8 +166,11 @@ func sellCoin(s *kucoin.ApiService, sy string, price string) {

_, err := s.CreateOrder(&o)

if err == nil {
if err != nil {
log.Fatal(err)
}

if err == nil {
fmt.Println("sell operation done")
nextOperation = "buy"
}
Expand Down
8 changes: 8 additions & 0 deletions utility/utility.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package utility

import "math"

func RoundFloat(val float64, precision uint) float64 {
ratio := math.Pow(10, float64(precision))
return math.Round(val*ratio) / ratio
}

0 comments on commit 753b59d

Please sign in to comment.