index.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { getQuoteDayInfoByCodeFindPrice } from "@/services/bus/goods";
  2. import { getRules } from "@/services/bus/rules";
  3. import { WrMarketTradeConfig } from "@/services/go/wrtrade/interface";
  4. import { CommomTradeForm, ListingTradeNumAndPrice } from './interface';
  5. // 获取交易规则
  6. export function useTradeRule() {
  7. // 交易规则
  8. const rules = getRules()
  9. console.log('rules', rules)
  10. // 获取履约规则中具体某个值
  11. // 如果没有找到就默认为0
  12. function getItemTradeRule(value: keyof WrMarketTradeConfig, initValue = 0) {
  13. return rules.length ? rules[0][value] : initValue
  14. }
  15. // 买保证金模式 1: 比率 2: 固定
  16. function isRatioInBuyMarginType() {
  17. const type = getItemTradeRule('buymarginalgorithm', 1)
  18. return type === 1
  19. }
  20. // 卖保证金模式 1: 比率 2: 固定
  21. function isRationInSellMarginType() {
  22. const type = getItemTradeRule('sellchargealgorithm', 1)
  23. return type === 1
  24. }
  25. return {
  26. getItemTradeRule,
  27. isRatioInBuyMarginType,
  28. isRationInSellMarginType,
  29. buymarginvalue: getItemTradeRule('buymarginvalue') as number,
  30. sellmarginvalue: getItemTradeRule('sellmarginvalue') as number
  31. }
  32. }
  33. // 买 挂牌/摘牌 固定价 最大数量 = 可用资金/(买方履约保证金比例*挂牌价格)
  34. export function useBuyFixedPricMaxNum<T extends CommomTradeForm>(formState: T, canUseMoney: Function) {
  35. let result = 0
  36. // 可用资金
  37. const money = canUseMoney() > 0 ? canUseMoney() : 0
  38. const { isRatioInBuyMarginType, buymarginvalue } = useTradeRule()
  39. // 最大数量 = 可用资金/(履约保证金*挂牌价格)
  40. const marginValue = isRatioInBuyMarginType() ? (buymarginvalue * formState.FixedPrice) : (buymarginvalue + formState.FixedPrice)
  41. // 处理 0 特殊情况
  42. if (marginValue) {
  43. result = Math.floor(money / marginValue)
  44. }
  45. return result
  46. }
  47. // 卖 挂牌/摘牌 固定价 最大数量 = 可用资金/(卖方履约保证金比例*挂牌价格)
  48. export function useSellFixedPriceMaxNum<T extends CommomTradeForm>(formState: T, canUseMoney: Function) {
  49. let result = 0
  50. // 可用资金
  51. const money = canUseMoney() > 0 ? canUseMoney() : 0
  52. const { isRationInSellMarginType, sellmarginvalue } = useTradeRule()
  53. // 最大数量 = 可用资金/(履约保证金*挂牌价格)
  54. const marginValue = isRationInSellMarginType() ? (sellmarginvalue * formState.FixedPrice) : (sellmarginvalue + formState.FixedPrice)
  55. if (marginValue) { // 处理 0 特殊情况
  56. result = Math.floor(money / marginValue)
  57. }
  58. return result
  59. }
  60. // 获取浮动价
  61. export function useFloatPrice<T extends CommomTradeForm>(formState: T, goodscode: string) {
  62. let result = '--'
  63. if (formState.OrderQty) {
  64. const goodsPrice = getQuoteDayInfoByCodeFindPrice(goodscode)
  65. if (goodsPrice && goodsPrice !== '--') { // 有实时行情价格
  66. result = ((goodsPrice as number) * formState.OrderQty + formState.PriceMove * formState.OrderQty).toFixed(2)
  67. }
  68. }
  69. return result
  70. }
  71. // 浮动价 挂牌/摘牌 金额
  72. export function useFloatPriceMoney<T extends CommomTradeForm>(formState: T, goodscode: string) {
  73. let result = 0
  74. const goodsPrice = getQuoteDayInfoByCodeFindPrice(goodscode)
  75. if (goodsPrice && goodsPrice !== '--') { // 有实时行情价格
  76. // 估算总价=挂牌基差+期货合约价;
  77. const predictTotal = formState.PriceMove + (goodsPrice as number);
  78. // 估算总额=估算总价*摘牌数量;
  79. result = predictTotal * formState.OrderQty
  80. }
  81. return result
  82. }
  83. // 获取 挂牌 的 最大可挂数量,金额,保证金
  84. export function useListingTradeNumAndPrice<T extends CommomTradeForm>({ formState, goodscode, isFloat, canUseMoney }: ListingTradeNumAndPrice<T>) {
  85. function getMaxNum(value: number, isBuy: boolean) {
  86. // 可用资金
  87. const money = canUseMoney() > 0 ? canUseMoney() : 0
  88. // 挂牌 最大数量=可用资金/(买方履约保证金比例*挂牌价格)
  89. let result = 0
  90. if (isFloat()) {
  91. const price = useFloatPrice<T>(formState, goodscode)
  92. if (price !== '--') {
  93. result = money / (Number(price) * value)
  94. }
  95. } else {
  96. result = isBuy ? useBuyFixedPricMaxNum(formState, canUseMoney) : useSellFixedPriceMaxNum(formState, canUseMoney)
  97. }
  98. return +result.toFixed(0)
  99. }
  100. // 挂买 最大数量
  101. function getListingBuyMaxNum() {
  102. const { buymarginvalue } = useTradeRule()
  103. return getMaxNum(buymarginvalue, true)
  104. }
  105. // 挂卖 最大数量
  106. function getListingSellMaxNum() {
  107. const { sellmarginvalue } = useTradeRule()
  108. return getMaxNum(sellmarginvalue, false)
  109. }
  110. // 金额
  111. function getMoney() {
  112. let result = 0
  113. if (isFloat()) {
  114. result = useFloatPriceMoney(formState, goodscode)
  115. } else {
  116. // 摘牌金额=挂牌价格*摘牌数量
  117. result = formState.OrderQty * formState.FixedPrice
  118. }
  119. return Number(result.toFixed(2))
  120. }
  121. // 挂买 履约保证金
  122. function getListingBuyMargin() {
  123. // 浮动价 履约保证金=估算总额*买方履约保证金比例
  124. // 一口价 履约保证金=挂牌金额*买方履约保证金比例
  125. const { isRatioInBuyMarginType, buymarginvalue } = useTradeRule()
  126. const margin = isRatioInBuyMarginType() ? (buymarginvalue * getMoney()) : (buymarginvalue + getMoney())
  127. return Number((margin).toFixed(2))
  128. }
  129. // 挂卖 履约保证金
  130. function getListingSellMargin() {
  131. // 浮动价 履约保证金=估算总额*买方履约保证金比例
  132. // 一口价 履约保证金=挂牌金额*买方履约保证金比例
  133. const { isRationInSellMarginType, sellmarginvalue } = useTradeRule()
  134. const margin = isRationInSellMarginType() ? (sellmarginvalue * getMoney()) : (sellmarginvalue + getMoney())
  135. return Number((margin).toFixed(2))
  136. }
  137. function getFloatPrice() {
  138. useFloatPrice(formState, goodscode)
  139. }
  140. return { getFloatPrice, getListingBuyMaxNum, getListingSellMaxNum, getListingBuyMargin, getListingSellMargin, getMoney }
  141. }
  142. // 获取 摘牌 买 比例 的 最大数量
  143. export function useBuyDelistingRatioMaxNum(OrderQty: number, canUseMoney: Function) {
  144. let result = 0;
  145. const { buymarginvalue } = useTradeRule()
  146. const money = canUseMoney() > 0 ? canUseMoney() : 0
  147. if (money && !isNaN(money)) {
  148. const num = +(money / buymarginvalue).toFixed(0);
  149. // 买 最大可摘数量=min{挂牌数量,可用资金/(履约保证金比例)}
  150. result = Math.min(OrderQty, num);
  151. }
  152. return result
  153. }