예제 #1
0
func (h *Handler) HandleCancel(req interface{}) (interface{}, error) {
	//找到相应的订单,不存在则返回失败
	var (
		cancelReq  *CancelReq
		cancelResp = &CancelResp{}
		err        error
		ok         bool
	)

	if cancelReq, ok = req.(*CancelReq); ok != true {
		return nil, fmt.Errorf("类型有误 ")
	}
	tx := h.db.Begin()
	defer func() {
		if h.db.GetErrors() != nil {
			log.Printf("DB error! now roll back. Err is %v", h.db.GetErrors())
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()
	var existOrder order.Order
	cancelResp.Result = false
	cancelResp.OrderId = cancelReq.OrderId
	if h.db.Where("orderId = ? and stockCode = ?", cancelReq.OrderId, cancelReq.Symbol).First(&existOrder).RecordNotFound() {
		return nil, fmt.Errorf("找不到相应的记录 %v", err)
	}

	existOrder.State = order.ORDER_CANCELLED
	if err := h.db.Save(&existOrder).Error; err != nil {
		return nil, fmt.Errorf("更新失败 %v", err)
	}
	cancelResp.Result = true
	return cancelResp, err
}
예제 #2
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
}
예제 #3
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
}