代码拉取完成,页面将自动刷新
package main
import (
"BandBinance/config"
"BandBinance/domain"
"BandBinance/exchange"
"BandBinance/notify"
"BandBinance/store"
"BandBinance/strategy"
"fmt"
"github.com/adshao/go-binance/v2"
"github.com/pkg/errors"
"gorm.io/gorm"
"log"
"strconv"
"time"
)
func main() {
prefix := "创建卖单"
for true {
// 找出币配置
configs, err := store.GridSymbolConfig.FindEnable()
if err != nil {
log.Printf("%s 币种配置没有", prefix)
continue
}
for i := range configs {
err = ProcessBuyOrderForSell(configs[i])
if err != nil {
log.Printf("%s 处理币种配置, id=%s, symbol=%s, %s", prefix, configs[i].ID, configs[i].Symbol, err)
}
}
time.Sleep(time.Duration(config.SellSleep) * time.Second)
}
}
// ProcessBuyOrderForSell 遍历买入单, 更新状态, 并且创建卖出单, 事务回滚,
func ProcessBuyOrderForSell(symbolConfig domain.GridSymbolConfig) error {
gridTrades, err := strategy.GetTrades(symbolConfig)
if err != nil {
return errors.Wrapf(err, fmt.Sprintf("获取网格, %s", err))
}
orders, err := store.FindTradeOrder(symbolConfig.Symbol, string(binance.SideTypeBuy), string(binance.OrderStatusTypeNew), symbolConfig.Version)
if err != nil {
// 找不到订单,跳过
return errors.Wrap(err, fmt.Sprintf("遍历买入单创建卖出单, 找不到订单, %s", err))
}
for i := range orders {
// 查询订单状态
binanceOrder, err := exchange.GetOrder(orders[i].OrderId, symbolConfig.Symbol)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("获取币安订单失败,id=%d, order_id=%d", orders[i].ID, orders[i].OrderId))
}
// 事务控制
err = store.Tx(func(tx *gorm.DB) error {
// 如果已经成交, 则创建卖出单
if binanceOrder.Status == string(binance.OrderStatusTypeFilled) {
// 更新订单状态
orders[i].BuyPrice = binanceOrder.BuyPrice
orders[i].DealTime = binanceOrder.DealTime
orders[i].Quantity = binanceOrder.Quantity
orders[i].Status = binanceOrder.Status
err = store.TradeOrder.Update(tx, &orders[i])
if err != nil {
log.Printf("更新买入单失败, orderId = %d", binanceOrder.OrderId)
return err
}
// 找到网格
gridTrade := gridTrades[orders[i].Index]
var sellOrder domain.TradeOrder
sellOrder.TradeType = string(binance.SideTypeSell)
sellOrder.Index = orders[i].Index
sellOrder.Version = orders[i].Version
sellOrder.Quantity = orders[i].Quantity
sellOrder.BuyPrice = gridTrade.HighPrice
sellOrder.CreateTime = time.Now().UnixMilli()
sellOrder.Status = string(binance.OrderStatusTypeNew)
sellOrder.ClientId = orders[i].ClientId
sellOrder.Symbol = orders[i].Symbol
// 保存卖出单
err = store.TradeOrder.CreateTradeOrder(tx, &sellOrder)
if err != nil {
log.Printf("保存卖出单失败, 卖出单id=%d", sellOrder.OrderId)
return err
}
log.Printf("卖出单id=%d", sellOrder.ID)
// 创建卖出单
clientId := strconv.FormatInt(time.Now().UnixMilli(), 10)
binanceOrderId, err := exchange.CreateOrder(binanceOrder.Quantity, gridTrade.HighPrice, binance.SideTypeSell, clientId, symbolConfig.Symbol, symbolConfig.RetryTimes, float64(symbolConfig.RetryGap))
if err != nil {
log.Printf("创建卖出单失败, 买入单id=%d", binanceOrder.OrderId)
return err
}
// 更新卖出单
sellOrder.OrderId = binanceOrderId
err = store.TradeOrder.Update(tx, &sellOrder)
if err != nil {
log.Printf("更新卖出单order_id失败, 买入单id=%d, order_id = %d", binanceOrder.ID, binanceOrder.OrderId)
return err
}
notify.FeiShu.NotifyTrade(sellOrder)
}
return nil
})
}
return err
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。