加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
CreateSellOrderAPP.go 3.62 KB
一键复制 编辑 原始数据 按行查看 历史
suwenguang 提交于 2022-01-14 16:51 . 分离APP
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化