Last active
April 21, 2023 16:08
-
-
Save ImanMousavi/bc50967773317c2c9ddd709bb8c151ff to your computer and use it in GitHub Desktop.
dd
This file contains hidden or 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
package main | |
import ( | |
"cryptodax/new-race-robot/exchange" | |
"cryptodax/new-race-robot/models" | |
"cryptodax/new-race-robot/utils" | |
"log" | |
"math" | |
"math/rand" | |
"time" | |
) | |
//func init() { | |
// fmt.Println("Welcome to init() function") | |
//} | |
var stdlog, errlog *log.Logger | |
func randFloats(min, max float64, n int) float64 { | |
res := math.Pow(10, float64(n)) | |
rnd := min + rand.Float64()*(max-min) | |
return math.Floor(rnd*res) / res | |
} | |
func getPrice(market models.Stetting, marketSetting models.Markets) (float64, error) { | |
var price float64 | |
var err error | |
switch market.Source { | |
case "nobitex": | |
price, err = new(exchange.Nobitex).GetPrice(marketSetting.BaseUnit, marketSetting.QuoteUnit) | |
case "coinmarketcap": | |
price, err = new(exchange.Nobitex).GetPrice(marketSetting.BaseUnit, marketSetting.QuoteUnit) | |
case "binance": | |
price, err = new(exchange.Binance).GetPrice(marketSetting.BaseUnit, marketSetting.QuoteUnit) | |
} | |
return price, err | |
} | |
func init() { | |
rand.Seed(time.Now().UnixNano()) | |
} | |
func main() { | |
models.ConnectDatabase() | |
stettingObj := &models.Stetting{} | |
log.Print("Bot Ver: 1.2") | |
for { | |
select { | |
case <-time.After(time.Duration(rand.Intn(15)+10) * time.Second): | |
data := stettingObj.List() | |
if len(data) == 0 { | |
panic("Failed to get data !") | |
} | |
for _, market := range data { | |
canTry := market.LastTry.Add(time.Second*time.Duration(market.Interval)).Unix() < time.Now().UTC().Unix() | |
if !canTry { | |
continue | |
} | |
rubyKube := &exchange.Rubykube{} | |
marketSetting := rubyKube.GetMarketSetting(market.Market) | |
if len(marketSetting.ID) == 0 { | |
log.Printf("%s Market %s NOT find", "****", market.Market) | |
continue | |
} | |
price, err := getPrice(market, marketSetting) | |
if err != nil { | |
log.Println("MakeOrder error: ", err.Error()) | |
err = utils.MakeOrder(price, marketSetting, market) | |
if market.MakeOrder { | |
rubyKube.TradingMarket(price, marketSetting, market) | |
} | |
if err != nil { | |
log.Println("MakeOrder error: ", err.Error()) | |
} else { | |
now := time.Now().UTC() | |
log.Printf("Market SET %s, %v", market.Market, now) | |
market.LastTry = now | |
models.DB.Save(&market) | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment