Beispiel #1
0
func (h *Handler) handleLimitTrade(req *TradeReq, orderPtr *order.Order) (*TradeResp, error) {
	var resp = &TradeResp{}
	reqPrice, err := strconv.ParseFloat(req.Price, 64)
	if err != nil {
		return nil, fmt.Errorf("解析出错%v", err)
	}
	if req.Type == PRICE_LIMIT_BUY {
		//查找所有的卖单
		//按价格和数量,从低到高成交
		h.db.Where("stockCode = ? and type in(?,?) and remainAmount > 0 and state != ?", req.Symbol, PRICE_MARKET_SELL, PRICE_LIMIT_SELL, order.ORDER_CANCELLED).Order("priceE2 asc").Find(&orders)
		for _, v := range orders {
			if int64(reqPrice*100) >= v.PriceE2 {
				if orderPtr.RemainAmount <= v.RemainAmount { //全部买进
					log.Printf("buy orderId %s, deal amount %d, sell orderId %s", orderPtr.OrderId, orderPtr.RemainAmount, v.OrderId)
					v.RemainAmount -= orderPtr.RemainAmount
					orderPtr.RemainAmount = 0
					h.db.Save(&v)
					break
				} else { //部分买进
					log.Printf("buy orderId %s, deal amount %d, sell orderId %s", orderPtr.OrderId, v.RemainAmount, v.OrderId)
					orderPtr.RemainAmount -= v.RemainAmount
					v.RemainAmount = 0
				}
			}
		}
		h.db.Save(orderPtr)
	} else if req.Type == PRICE_LIMIT_SELL {
		//todo: 考虑事务的异常
		log.Printf("h %v ", h)
		//查找所有的卖单
		//按价格和数量,从低到高成交
		h.db.Where("stockCode = ? and type in(?,?) and remainAmount > 0 and state != ?", req.Symbol, PRICE_LIMIT_BUY, PRICE_MARKET_BUY, order.ORDER_CANCELLED).Order("priceE2 desc").Find(&orders)
		for _, v := range orders {
			if int64(reqPrice*100) <= v.PriceE2 {
				if orderPtr.RemainAmount <= v.RemainAmount { //全部卖出
					log.Printf("buy orderId %s, deal amount %d, sell orderId %s", v.OrderId, orderPtr.RemainAmount, orderPtr.OrderId)
					v.RemainAmount -= orderPtr.RemainAmount
					orderPtr.RemainAmount = 0
					h.db.Save(&v)
					break
				} else { //部分卖出
					log.Printf("buy orderId %s, deal amount %d, sell orderId %s", v.OrderId, v.RemainAmount, orderPtr.OrderId)
					v.RemainAmount = 0
					orderPtr.RemainAmount -= v.RemainAmount
				}
			}
		}
		h.db.Save(orderPtr)
	} else {
		return nil, fmt.Errorf("error type %s", req.Type)
	}
	resp.Result = true
	resp.OrderID = orderPtr.OrderId
	return resp, nil
}
Beispiel #2
0
func (h *Handler) handleMarketTrade(req *TradeReq, orderPtr *order.Order) (*TradeResp, error) {
	reqPrice, err := strconv.ParseFloat(req.Price, 64)
	if err != nil {
		return nil, fmt.Errorf("解析出错 %v", err)
	}
	var resp = &TradeResp{}
	if req.Type == PRICE_MARKET_BUY {
		//查找所有的卖单
		//按价格和数量,从低到高成交
		if err := h.db.Where("stockCode = ? and type in( ?,?) and remainAmount > 0 and state != ?", req.Symbol, PRICE_LIMIT_SELL, PRICE_MARKET_SELL, order.ORDER_CANCELLED).Order("priceE2 asc").Find(&orders).Error; err != nil {
			return nil, fmt.Errorf("找不到合适的stock记录 %s, err %v", req.Symbol, err)
		}
		for _, v := range orders {
			if int64(reqPrice*100) >= v.PriceE2 {
				if orderPtr.RemainAmount <= v.RemainAmount { //足够多可以买
					v.RemainAmount -= orderPtr.RemainAmount
					orderPtr.RemainAmount = 0
					if err := h.db.Save(&v).Error; err != nil {
						return nil, fmt.Errorf("保存出错 %v", err)
					}
					log.Printf("buy orderId %s, deal amount %d, sell orderId %s", orderPtr.OrderId, orderPtr.RemainAmount, v.OrderId)
					//h.WriteDealData(time.Now().Format("20060102 15:04:05"), orderPtr.OrderId, orderPtr.Type, v.OrderId, v.Type, v.PriceE2, orderPtr.RemainAmount)
					break
				} else {
					orderPtr.RemainAmount -= v.RemainAmount
					log.Printf("buy orderId %s, deal amount %d, sell orderId %s", orderPtr.OrderId, v.RemainAmount, v.OrderId)
					//h.WriteDealData(time.Now().Format("20060102 15:04:05"), orderPtr.OrderId, orderPtr.Type, v.OrderId, v.Type, v.PriceE2, v.RemainAmount)
					v.RemainAmount = 0
				}
			}
		}
		//剩余撤销
		log.Printf("buy orderId %s, cancel amount %d", orderPtr.OrderId, orderPtr.RemainAmount)
		orderPtr.RemainAmount = 0
		h.db.Save(orderPtr)
	} else if req.Type == PRICE_MARKET_SELL {
		//查找所有的卖单
		//按价格和数量,从低到高成交
		if err := h.db.Where("stockCode = ? and type in( ?,?) and remainAmount > 0 and state != ?", req.Symbol, PRICE_LIMIT_BUY, PRICE_MARKET_BUY, order.ORDER_CANCELLED).Order("priceE2 desc").Find(&orders).Error; err != nil {
			return nil, err
		}
		for _, v := range orders {
			if int64(reqPrice*100) <= v.PriceE2 {
				if orderPtr.RemainAmount <= v.RemainAmount { //全卖
					log.Printf("buy orderId %s, deal amount %d, sell orderId %s", v.OrderId, orderPtr.RemainAmount, orderPtr.OrderId)
					//h.WriteDealData(time.Now().Format("20060102 15:04:05"), v.OrderId, v.Type, orderPtr.OrderId, orderPtr.Type, v.PriceE2, orderPtr.RemainAmount)
					v.RemainAmount = 0
					v.RemainAmount -= orderPtr.RemainAmount
					orderPtr.RemainAmount = 0
					h.db.Save(&v)
					break
				} else { //部分卖
					log.Printf("buy orderId %s, deal amount %d, sell orderId %s", v.OrderId, v.RemainAmount, orderPtr.OrderId)
					//h.WriteDealData(time.Now().Format("20060102 15:04:05"), v.OrderId, v.Type, orderPtr.OrderId, orderPtr.Type, v.PriceE2, v.RemainAmount)
					orderPtr.RemainAmount -= v.RemainAmount
					v.RemainAmount = 0
				}
			}
		}
		//剩余的撤单
		log.Printf("sell orderId %s, cancel amount %d", orderPtr.OrderId, orderPtr.RemainAmount)
		orderPtr.RemainAmount = 0
		h.db.Save(orderPtr)
	} else {
		return nil, fmt.Errorf("error type %s", req.Type)
	}
	resp.Result = true
	resp.OrderID = orderPtr.OrderId
	return resp, nil
}