You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.2 KiB

package socket
import (
"context"
"encoding/json"
"github.com/shopspring/decimal"
"matchmaking-system/internal/data"
"matchmaking-system/internal/data/socket/optionData"
"matchmaking-system/internal/data/socket/publicData"
"matchmaking-system/internal/data/tradedeal/option"
"matchmaking-system/internal/data/tradedeal/virtual"
"matchmaking-system/internal/pkg/flags"
"matchmaking-system/internal/pkg/logging/applogger"
"matchmaking-system/internal/pkg/setting"
"time"
)
// orderSubShareInrSubscribe
//
// @Description: 用户期权-印度股订单订阅
// @receiver u
// @param psgMsg
func (u *Client) orderSubOptionInrSubscribe(psgMsg *SymbolMessage) {
for {
userId, err := GetUserIdByToken(u.token)
if err != nil {
time.Sleep(5 * time.Second)
cleanSubscriptionKey(u)
break
}
if userId != flags.AdministratorsId && psgMsg.Symbol == setting.SubscribeOptionInr {
orderTokenKey := virtual.OrderIdListKey(setting.OptionInrSubscribe, userId)
orderList, err := data.Reds.HGetAll(context.Background(), orderTokenKey).Result()
if err != nil {
time.Sleep(5 * time.Second)
continue
}
for _, value := range orderList {
var msg option.OptionInrTallyCache
if err = json.Unmarshal([]byte(value), &msg); err != nil {
time.Sleep(5 * time.Second)
continue
}
orderModel := optionData.OptionInrOrderProcessing(0, msg)
if orderModel != nil {
_, ok := u.symbol.Load(psgMsg.Symbol)
if ok {
// 清理印度股订阅缓存订单中(撤单|平仓)状态的订单
if orderModel.Status == flags.Cancel || orderModel.Status == flags.Close {
err = data.Reds.HDel(context.Background(), orderTokenKey, msg.OrderId).Err()
if err != nil {
time.Sleep(5 * time.Second)
applogger.Error("OptionInrSubscribe.HDel:%v", err)
continue
}
}
orderStr, err := json.Marshal(orderModel)
if err != nil {
applogger.Error("User's Indian stock subscription cache order error:%v", err)
time.Sleep(5 * time.Second)
continue
}
u.msg <- orderStr // 用户(挂单|持仓)订阅
} else {
applogger.Info("User cancels Indian stock order subscription.")
return
}
}
}
}
time.Sleep(600 * time.Millisecond)
}
}
// orderSubSumOptionInrSubscribe
//
// @Description: 用户期权-印度股订单总浮动盈亏订阅
// @receiver u
// @param psgMsg
func (u *Client) orderSubSumOptionInrSubscribe(psgMsg *SymbolMessage) {
for {
userId, err := GetUserIdByToken(u.token)
if err != nil {
time.Sleep(5 * time.Second)
cleanSubscriptionKey(u)
break
}
if userId != flags.AdministratorsId && psgMsg.Symbol == setting.SubscribeSumOptionInr {
orderTokenKey := virtual.OrderIdListKey(setting.OptionInrSubscribe, userId)
orderList, err := data.Reds.HGetAll(context.Background(), orderTokenKey).Result()
if err != nil {
time.Sleep(5 * time.Second)
continue
}
var userSumOrderPL decimal.Decimal
for _, value := range orderList {
var msg option.OptionInrTallyCache
if err = json.Unmarshal([]byte(value), &msg); err != nil {
time.Sleep(5 * time.Second)
continue
}
if msg.Status == flags.Position {
orderModel := optionData.OptionInrOrderProcessing(0, msg)
if orderModel != nil {
_, ok := u.symbol.Load(psgMsg.Symbol)
if ok {
if len(orderModel.Price) == 0 || len(orderModel.OpenPrice) == 0 {
continue
}
price := decimal.RequireFromString(orderModel.Price) // 买一卖一价格
openPrice := decimal.RequireFromString(orderModel.OpenPrice) // 开仓价格
orderNum := decimal.RequireFromString(orderModel.OrderNumber) // 订单数量
// 浮动盈亏(P/L)
// 1>buy call & buy put
// 1> bid买一价为0, P/L显示为 -
// 2> bid买一价大于0, P/L =(bid买一价 - Cost Price)*Contracts Quantity
// 2>sell call & sell put
// 1> ask卖一价为0, P/L显示为 -
// 2> ask卖一价大于0, P/L =(Cost Price - ASK卖一价)*Contracts Quantity
pl := decimal.Zero
switch msg.Order.TradeType {
case flags.OptionCalls: // call
switch msg.Order.TradingType {
case flags.OptionBuy: // call - buy
pl = price.Sub(openPrice).Mul(orderNum)
case flags.OptionSell: // call - sell
pl = openPrice.Sub(price).Mul(orderNum)
}
case flags.OptionPuts: // put
switch msg.Order.TradingType {
case flags.OptionBuy: // put - buy
pl = price.Sub(openPrice).Mul(orderNum)
case flags.OptionSell: // put - sell
pl = openPrice.Sub(price).Mul(orderNum)
}
}
userSumOrderPL = userSumOrderPL.Add(pl)
} else {
applogger.Info("User cancels Indian stock order subscription.")
return
}
}
}
}
// 用户持仓总浮动盈亏订阅
sum := publicData.OptionSum{
Type: "price",
Value: userSumOrderPL.String(),
}
orderStr, err := json.Marshal(sum)
if err != nil {
applogger.Error("User's Indian stock subscription cache order error:%v", err)
time.Sleep(5 * time.Second)
continue
}
u.msg <- orderStr
}
time.Sleep(600 * time.Millisecond)
}
}