|
|
@@ -1,9 +1,7 @@
|
|
|
import { reactive, computed, toRefs } from 'vue'
|
|
|
import { isMatch } from 'lodash'
|
|
|
-import { handlePriceColor, round } from '@/filters'
|
|
|
import { BuyOrSell } from '@/constants/order'
|
|
|
import { queryTradePosition, querySBYJMyOrders } from '@/services/api/order'
|
|
|
-import { useGlobalStore } from './global'
|
|
|
import { useUserStore } from './user'
|
|
|
import { useFuturesStore } from './futures'
|
|
|
import { defineStore } from '../store'
|
|
|
@@ -14,7 +12,6 @@ import quoteSocket from '@/services/websocket/quote'
|
|
|
* 持仓存储对象
|
|
|
*/
|
|
|
export const usePositionStore = defineStore(() => {
|
|
|
- const globalStore = useGlobalStore()
|
|
|
const userStore = useUserStore()
|
|
|
const futuresStore = useFuturesStore()
|
|
|
const subscribe = quoteSocket.createSubscribe()
|
|
|
@@ -40,48 +37,25 @@ export const usePositionStore = defineStore(() => {
|
|
|
}
|
|
|
|
|
|
// 持仓汇总计算列表
|
|
|
- const positionComputedList = computed(() => {
|
|
|
- const result: (Model.TradePositionRsp & {
|
|
|
- lastColor: string; // 最新价颜色
|
|
|
- closepl: number; // 浮动盈亏
|
|
|
- closeplColor: string; // 浮动盈亏颜色
|
|
|
- marketValue: number; // 市值
|
|
|
- })[] = []
|
|
|
-
|
|
|
- state.positionList.forEach((item) => {
|
|
|
- const quote = futuresStore.getGoodsQuote(item.goodscode)
|
|
|
- const refQuote = futuresStore.getGoodsQuote(item.refgoodscode)
|
|
|
-
|
|
|
- const last = refQuote.value?.last || quote.value?.last || 0 // 有 refgoodscode 的优先取 refgoodscode 行情
|
|
|
- const presettle = quote.value?.presettle || 0
|
|
|
- const price = last || presettle // 没有最新价取昨结价
|
|
|
-
|
|
|
- let exchangerate = 1
|
|
|
- // 查找汇率
|
|
|
- if (item.currencyid !== item.tacurrencyid) {
|
|
|
- const currency = userStore.userData.exchangeRateConfigs.find((e) => e.descurrencyid === item.tacurrencyid && e.oricurrencyid === item.currencyid)
|
|
|
- exchangerate = currency?.exchangerate ?? 0
|
|
|
- }
|
|
|
+ const positionComputedList = computed(() => state.positionList.map((item) => {
|
|
|
+ const quote = futuresStore.getQuoteItem({ goodscode: item.goodscode })
|
|
|
+ const refQuote = futuresStore.getQuoteItem({ goodscode: item.refgoodscode })
|
|
|
+ const quoteItem = refQuote || quote
|
|
|
+
|
|
|
+ let exchangerate = 1
|
|
|
+ // 查找汇率
|
|
|
+ if (item.currencyid !== item.tacurrencyid) {
|
|
|
+ const currency = userStore.userData.exchangeRateConfigs.find((e) => e.descurrencyid === item.tacurrencyid && e.oricurrencyid === item.currencyid)
|
|
|
+ exchangerate = currency?.exchangerate ?? 0
|
|
|
+ }
|
|
|
|
|
|
- // 计算市值 = 现价 * 数量 * 合约单位 * 汇率
|
|
|
- const marketValue = price * item.curpositionqty * item.agreeunit * exchangerate
|
|
|
- const roundedMarketValue = round(marketValue, quote.value?.decimalplace)
|
|
|
- // 计算浮动盈亏 任务 #5600 #6013
|
|
|
- const closepl = (price && globalStore.getSystemInfo('riskType') !== 1) ? (roundedMarketValue - item.curholderamount) * (item.buyorsell === BuyOrSell.Buy ? 1 : -1) : 0
|
|
|
- const roundedClosepl = round(closepl, quote.value?.decimalplace)
|
|
|
-
|
|
|
- result.push({
|
|
|
- ...item,
|
|
|
- lastprice: price,
|
|
|
- lastColor: handlePriceColor(price, presettle),
|
|
|
- closepl: roundedClosepl,
|
|
|
- closeplColor: handlePriceColor(roundedClosepl, 0),
|
|
|
- marketValue: roundedMarketValue,
|
|
|
- })
|
|
|
- })
|
|
|
+ const floatingPL = futuresStore.calcFloatingPL(quoteItem, item.buyorsell, item.curpositionqty, item.curholderamount, exchangerate)
|
|
|
|
|
|
- return result
|
|
|
- })
|
|
|
+ return {
|
|
|
+ ...item,
|
|
|
+ ...floatingPL
|
|
|
+ }
|
|
|
+ }))
|
|
|
|
|
|
// 过滤持仓列表
|
|
|
const filterPositionList = (props: Partial<Model.TradePositionRsp>) => {
|
|
|
@@ -150,19 +124,15 @@ export const useSBYJOrderStore = defineStore(() => {
|
|
|
|
|
|
// 计算浮动盈亏
|
|
|
const calcFloatpl = ({ tHDetailEx }: Model.SBYJMyOrderRsp) => {
|
|
|
- const quote = futuresStore.getGoodsQuote(tHDetailEx.goodsID)
|
|
|
- const { ask = 0, bid = 0, agreeunit = 0 } = quote.value ?? {}
|
|
|
- const price = tHDetailEx.buyOrSell === BuyOrSell.Buy ? bid : ask // 根据方向取买卖价
|
|
|
- // 计算浮动盈亏 (价格 * 手数 * 合约乘数 - 持仓金额) * 方向标识
|
|
|
- const float = price ? price * tHDetailEx.holderQty * agreeunit - tHDetailEx.holderAmount : 0
|
|
|
- const result = float * (tHDetailEx.buyOrSell === BuyOrSell.Buy ? 1 : -1)
|
|
|
- return round(result, quote.value?.decimalplace)
|
|
|
+ const quote = futuresStore.getQuoteItem({ goodsid: tHDetailEx.goodsID })
|
|
|
+ const floatingPL = futuresStore.calcFloatingPL(quote, tHDetailEx.buyOrSell, tHDetailEx.holderQty, tHDetailEx.holderAmount)
|
|
|
+ return floatingPL.profitLoss
|
|
|
}
|
|
|
|
|
|
// 计算定金率
|
|
|
const calcDepositRate = ({ tHDetailEx }: Model.SBYJMyOrderRsp) => {
|
|
|
- const quote = futuresStore.getGoodsQuote(tHDetailEx.goodsID)
|
|
|
- const { ask = 0, bid = 0, agreeunit = 0 } = quote.value ?? {}
|
|
|
+ const quote = futuresStore.getQuoteItem({ goodsid: tHDetailEx.goodsID })
|
|
|
+ const { ask = 0, bid = 0, agreeunit = 0 } = quote ?? {}
|
|
|
const price = tHDetailEx.buyOrSell === BuyOrSell.Buy ? bid : ask // 根据方向取买卖价
|
|
|
|
|
|
// 计算浮动盈亏 (价格 * 手数 * 合约乘数 - 持仓金额) * 方向标识
|
|
|
@@ -185,8 +155,8 @@ export const useSBYJOrderStore = defineStore(() => {
|
|
|
// 已付定金+补充定金
|
|
|
const useMargin = e.tHDetailEx.payedDeposit + e.tHDetailEx.restockDeposit
|
|
|
|
|
|
- const quote = futuresStore.getGoodsQuote(e.tHDetailEx.goodsID)
|
|
|
- const { ask = 0, bid = 0, agreeunit = 0 } = quote.value ?? {}
|
|
|
+ const quote = futuresStore.getQuoteItem({ goodsid: e.tHDetailEx.goodsID })
|
|
|
+ const { ask = 0, bid = 0, agreeunit = 0 } = quote ?? {}
|
|
|
const price = e.tHDetailEx.buyOrSell === BuyOrSell.Buy ? bid : ask // 根据方向取买卖价
|
|
|
|
|
|
// 计算浮动盈亏 (价格 * 手数 * 合约乘数 - 持仓金额) * 方向标识
|
|
|
@@ -202,8 +172,8 @@ export const useSBYJOrderStore = defineStore(() => {
|
|
|
// 已付定金+补充定金
|
|
|
const useMargin = e.tHDetailEx.payedDeposit + e.tHDetailEx.restockDeposit
|
|
|
|
|
|
- const quote = futuresStore.getGoodsQuote(e.tHDetailEx.goodsID)
|
|
|
- const { ask = 0, bid = 0, agreeunit = 0 } = quote.value ?? {}
|
|
|
+ const quote = futuresStore.getQuoteItem({ goodsid: e.tHDetailEx.goodsID })
|
|
|
+ const { ask = 0, bid = 0, agreeunit = 0 } = quote ?? {}
|
|
|
const price = e.tHDetailEx.buyOrSell === BuyOrSell.Buy ? bid : ask // 根据方向取买卖价
|
|
|
|
|
|
// 计算浮动盈亏 (价格 * 手数 * 合约乘数 - 持仓金额) * 方向标识
|