Quellcode durchsuchen

风险管理3月12日提交代码-liu.bolan-期货交易请求

Liu.bolan vor 4 Jahren
Ursprung
Commit
dd59b7bf92

+ 0 - 3
RMA/app/src/main/java/cn/muchinfo/rma/business/future/FutureManager.kt

@@ -231,9 +231,6 @@ class FutureManager {
         params: Map<String, String>,
         responseBack: (isSuccess: Boolean, respData: List<FutureEntrustData>?, error: Error?) -> Unit
     ) {
-        val params = mutableMapOf<String, String>()
-        params["accountID"] = SPUtils.getInstance().getLong(Constant.SELECT_ACCOUNT_ID).toString()
-    ){
         MyOkHttpUtils().query(
             URL = SPUtils.getInstance().getString(Constant.goCommonSearchUrl) + "/Ermcp/QueryErmcpHisOrderDetails",
             params = params,

+ 0 - 9
RMA/app/src/main/java/cn/muchinfo/rma/global/GlobalDataCollection.kt

@@ -10,15 +10,6 @@ import java.util.concurrent.locks.Lock
 import java.util.concurrent.locks.ReentrantLock
 
 class GlobalDataCollection : BaseGlobalData() {
-    // 把行情系统需要的数据  进行整理归类 在这里进行存储 goodsCode
-    private var mGoodsInfoClass: HashMap<String, QuoteDayData>? = null
-
-    fun getmGoodsInfoClass(): HashMap<String, QuoteDayData> {
-        if (mGoodsInfoClass == null) {
-            mGoodsInfoClass = HashMap()
-        }
-        return mGoodsInfoClass!!
-    }
 
     /**
      * 获取错误信息

+ 42 - 1
RMA/app/src/main/java/cn/muchinfo/rma/global/data/AccountData.kt

@@ -1,5 +1,7 @@
 package cn.muchinfo.rma.global.data
 
+import cn.muchinfo.rma.global.GlobalDataCollection
+
 /**
  * 资金账户信息
  * @property accountflag Int
@@ -106,9 +108,48 @@ open class AccountData(
     val tradestatuschangetime : String = "",//激活时间
     val transferamount : Double = 0.0,//今日划转金额(母子账号资金划转,从划入账号为正,从账号划出为负)
     val usedmargin : Double = 0.0,//占用保证金
-    val userid : Long = 0//用户ID
+    val userid : Long = 0,//用户ID
+
+    var close_watch_profit_and_loss : Double = 0.0, //平仓盯市盈亏  7.5
+    var keep_watch_profit_and_loss : Double = 0.0, //持仓盯市盈亏
+    var canUserAmount : Double = 0.0,//可用资金
+    var rightsAndInterests : Double = 0.0//权益
 ) {
 
+    /**
+     * 获取基本信息(主要计算持仓浮动盈亏和平仓浮动盈亏)
+     * 盯市浮盈【实时行情更新】(MTP:浮动盈亏、持仓盈亏) 买方向 = (最新价 - 持仓均价) * 买期末头寸 * 合约单位;卖方向 = (持仓均价 - 最新价) * 卖期末头寸 * 合约单位
+     * 逐笔浮盈【实时行情更新】(MTP:开仓盈亏、平仓盈亏) 买方向 = (最新价 - 开仓均价) * 买期末头寸 * 合约单位;卖方向 = (开仓均价 - 最新价) * 卖期末头寸 * 合约单位
+     */
+    fun setNormalData() {
+        val futureHoldDatalist = GlobalDataCollection.instance?.futureHoldData
+        val quotes = GlobalDataCollection.instance?.goodsInfoAndQuotesList
+        futureHoldDatalist?.forEach { data ->
+            val quoteData = quotes?.find {
+                data.outgoodscode == it.outgoodscode
+            }?.quoteDayData
+            /** 计算持仓浮动盈亏汇总 **/
+            var positionpl = 0.0
+            if (data.buyorsell == 0){//买
+                positionpl = quoteData?.last?.minus(data.positionaverageprice)?.times(data.curpositionqty)?.times(data.agreeunit) ?: 0.0
+            }else if (data.buyorsell == 1){
+                positionpl = data.positionaverageprice.minus(quoteData?.last ?: 0.0).times(data.curpositionqty).times(data.agreeunit)
+            }
+            keep_watch_profit_and_loss = (keep_watch_profit_and_loss + positionpl)
+            /** 计算平仓浮动盈亏汇总 **/
+            var openpl = 0.0
+            if (data.buyorsell == 0){
+                openpl = quoteData?.last?.minus(data.openaverageprice)?.times(data.curpositionqty)?.times(data.agreeunit) ?: 0.0
+            }else if (data.buyorsell == 1){
+                openpl = data.openaverageprice.minus(quoteData?.last ?: 0.0).times(data.curpositionqty).times(data.agreeunit)
+            }
+            close_watch_profit_and_loss = (close_watch_profit_and_loss + openpl)
+        }
+
+        canUserAmount = currentbalance.minus(usedmargin).minus(freezecharge).minus(freezemargin).minus(otherfreezemargin)
+        rightsAndInterests = currentbalance.plus(keep_watch_profit_and_loss)
+    }
+
 
 
 

+ 0 - 30
RMA/app/src/main/java/cn/muchinfo/rma/global/data/AccountShowData.kt

@@ -53,35 +53,5 @@ class AccountShowData {
         this.out_money = out_money
     }
 
-    /**
-     * 获取基本信息(主要计算持仓浮动盈亏和平仓浮动盈亏)
-     * 盯市浮盈【实时行情更新】(MTP:浮动盈亏、持仓盈亏) 买方向 = (最新价 - 持仓均价) * 买期末头寸 * 合约单位;卖方向 = (持仓均价 - 最新价) * 卖期末头寸 * 合约单位
-     * 逐笔浮盈【实时行情更新】(MTP:开仓盈亏、平仓盈亏) 买方向 = (最新价 - 开仓均价) * 买期末头寸 * 合约单位;卖方向 = (开仓均价 - 最新价) * 卖期末头寸 * 合约单位
-     */
-    fun setNormalData() {
-        val futureHoldDatalist = GlobalDataCollection.instance?.futureHoldData
-        val quotes = GlobalDataCollection.instance?.goodsInfoAndQuotesList
-        futureHoldDatalist?.forEach { data ->
-            val quoteData = quotes?.find {
-                data.outgoodscode == it.outgoodscode
-            }?.quoteDayData
-            /** 计算持仓浮动盈亏汇总 **/
-            var positionpl = 0.0
-            if (data.buyorsell == 0){//买
-                positionpl = quoteData?.last?.minus(data.positionaverageprice)?.times(data.curpositionqty)?.times(data.agreeunit) ?: 0.0
-            }else if (data.buyorsell == 1){
-                positionpl = data.positionaverageprice.minus(quoteData?.last ?: 0.0).times(data.curpositionqty).times(data.agreeunit)
-            }
-            keep_watch_profit_and_loss = (keep_watch_profit_and_loss.toDouble() + positionpl).toString()
-            /** 计算平仓浮动盈亏汇总 **/
-            var openpl = 0.0
-            if (data.buyorsell == 0){
-                openpl = quoteData?.last?.minus(data.openaverageprice)?.times(data.curpositionqty)?.times(data.agreeunit) ?: 0.0
-            }else if (data.buyorsell == 1){
-                openpl = data.openaverageprice.minus(quoteData?.last ?: 0.0).times(data.curpositionqty).times(data.agreeunit)
-            }
-            close_watch_profit_and_loss = (close_watch_profit_and_loss.toDouble() + openpl).toString()
-        }
-    }
 
 }

+ 7 - 1
RMA/app/src/main/java/cn/muchinfo/rma/global/data/QuoteDayData.kt

@@ -131,7 +131,13 @@ data class QuoteDayData(
     // 增加字段
     var ontheprice : Double = 0.0//上一口价
 ){
-
+    /**
+     * 现价与上一口价的差额 大于0则是涨,小于0是跌
+     * @return Double
+     */
+    fun pricesDifference() : Double{
+        return last.minus(ontheprice)
+    }
 }
 
 

+ 4 - 4
RMA/app/src/main/java/cn/muchinfo/rma/view/MyApplication.kt

@@ -40,6 +40,7 @@ import cn.muchinfo.rma.view.base.BaseApplication
 import cn.muchinfo.rma.view.base.app.Constant
 import cn.muchinfo.rma.view.eventbus.EventConstent
 import cn.muchinfo.rma.view.eventbus.MessageEvent
+import cn.muchinfo.rma.view.eventbus.QuoteMessageEvent
 import com.blankj.utilcode.util.AppUtils
 import com.blankj.utilcode.util.LogUtils
 import com.blankj.utilcode.util.SPUtils
@@ -166,10 +167,8 @@ class MyApplication : BaseApplication() {
 
 
     @Subscribe(threadMode = ThreadMode.MAIN)
-    fun onMessageEvent(messageEvent : MessageEvent) {
-        if (messageEvent.messageType == EventConstent.USERACCOUNTCHANGE){
+    fun onQuoteMessageEvent(quoteMessageEvent : QuoteMessageEvent) {
 
-        }
     }
 
 
@@ -277,8 +276,9 @@ class MyApplication : BaseApplication() {
                         val goodsQuoteTiks = QuoteAdapter.splitQuoteGoods(data.content)
                         // 写入盘面
                         val goodsIDs = futureManager?.updateQuoteInfo(goodsQuoteTiks!!)
-
+                        EventBus.getDefault().post(goodsIDs?.let { QuoteMessageEvent(goodsid = it) })
                         // TODO: - 计算持仓盈亏和资金账户
+                        GlobalDataCollection.instance?.accountData?.setNormalData()
                     }
                 }
             }

+ 1 - 1
RMA/app/src/main/java/cn/muchinfo/rma/view/base/future/costs/CostInfoFragment.kt

@@ -45,7 +45,7 @@ class CostInfoFragment(type: String?) : BaseFragment<CostViewModel>() {
     fun initData(){
         accountData = GlobalDataCollection.instance?.accountData
         val showAccountData = AccountShowData()
-        showAccountData.setNormalData()
+//        showAccountData.setNormalData()
         userAccountData.postValue(showAccountData)
         if (currencyName != null){
             //暂时写死人民币

+ 34 - 6
RMA/app/src/main/java/cn/muchinfo/rma/view/base/future/markets/MarketBaseModel.kt

@@ -22,6 +22,8 @@ import cn.muchinfo.rma.view.base.future.trade.GoodsTradeActivity
 import com.blankj.utilcode.util.ActivityUtils
 import cn.muchinfo.rma.view.base.chart.ChartActivity
 import com.blankj.utilcode.util.ToastUtils
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.launch
 
 class MarketBaseModel : BaseViewModel() {
     var list: List<GoodsInfo>? = null
@@ -50,6 +52,7 @@ class MarketBaseModel : BaseViewModel() {
      * @param callback Function2<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] Error?, Unit>
      */
     fun queryQuoteDay(
+        tag : String,
         first: Int,
         last: Int,
         callback: (isCompleted: Boolean, err: Error?) -> Unit
@@ -60,15 +63,40 @@ class MarketBaseModel : BaseViewModel() {
         }
         MyApplication.getInstance()?.futureManager?.queryQuoteDay(params = params) { isSuccess, respData, error ->
             if (isSuccess) {
-                val map = GlobalDataCollection.instance?.getmGoodsInfoClass()
-                respData?.forEach {
-                    map?.put(it.goodscode, it)
-                }
                 callback(true, null)
             } else {
                 callback(false, error)
             }
         }
+
+        list?.subList(first,last)?.let { addSubscriptQuote(tag = tag,goodsInfoList = it) }
+    }
+
+    /**
+     * 订阅行情
+     * @param tag String
+     * @param goodsInfoList List<GoodsInfo>
+     */
+    fun addSubscriptQuote(tag : String,goodsInfoList : List<GoodsInfo>){
+        GlobalScope.launch {
+            MyApplication.getInstance()?.quoteManager?.addSubscriptQuote(
+                tag = tag,goodsCodeSet = goodsInfoList.map { it.outgoodscode }.toSet()
+            ){isCompleted, err ->  
+
+            }
+        }
+    }
+
+    /**
+     * 取消订阅
+     * @param tag String
+     */
+    fun removeSubscriptQuote(tag : String){
+        GlobalScope.launch {
+            MyApplication.getInstance()?.quoteManager?.removeSubscriptQuote(
+                tag = tag
+            )
+        }
     }
 
     /**
@@ -123,7 +151,7 @@ class MarketBaseModel : BaseViewModel() {
             holder.idGoodsCode?.text = list?.get(position)?.goodscode
 
             //获取盘面信息
-            val quoteDayData = GlobalDataCollection.instance?.getmGoodsInfoClass()?.get(list?.get(position)?.outgoodscode)
+            val quoteDayData = GlobalDataCollection.instance?.goodsInfoAndQuotesList?.find { it.outgoodscode == list?.get(position)?.outgoodscode }?.quoteDayData
             if (quoteDayData != null){
                 //最新价->今开->昨结 价格
                 if (quoteDayData.last != 0.0){//优先最新价
@@ -147,7 +175,7 @@ class MarketBaseModel : BaseViewModel() {
                     holder.idUpDown?.text = "--"
                 }
 
-                 val upDown = quoteDayData.last.minus(quoteDayData.preclose)
+                 val upDown = quoteDayData.pricesDifference()
                 if (upDown == 0.0){
                     holder.idUpDown?.background = noChange
                     holder.idUpDown?.setTextColor(noChangeBlack)

+ 67 - 12
RMA/app/src/main/java/cn/muchinfo/rma/view/base/future/markets/MarketFragment.kt

@@ -16,7 +16,11 @@ import cn.muchinfo.rma.global.data.account.loginQeruy.GoodsInfo
 import cn.muchinfo.rma.global.database.AppDatabase
 import cn.muchinfo.rma.global.room.Builder
 import cn.muchinfo.rma.view.base.BaseFragment
+import cn.muchinfo.rma.view.eventbus.QuoteMessageEvent
 import com.blankj.utilcode.util.LogUtils
+import org.greenrobot.eventbus.EventBus
+import org.greenrobot.eventbus.Subscribe
+import org.greenrobot.eventbus.ThreadMode
 
 /**
  * 报价牌的通用fragment
@@ -29,10 +33,17 @@ class MarketFragment(data: ExternalExchanges) : BaseFragment<MarketBaseModel>()
         fun getInstance(data: ExternalExchanges) = MarketFragment(data)
     }
 
+    //此页面是否在显示,true行情下发可刷新列表 false则不刷新
+    var canrefrashQuote: Boolean = false
+
     //region views
     private var _view: View? = null
     private var idMarketList: RecyclerView? = null // 列表
 
+    val thisTag by lazy {
+        this.getTag()
+    }
+
     var fistShow: Int = 0
     var lastShow: Int = 0
 //endregion
@@ -48,9 +59,45 @@ class MarketFragment(data: ExternalExchanges) : BaseFragment<MarketBaseModel>()
         return _view
     }
 
+    @Subscribe(threadMode = ThreadMode.MAIN)
+    fun onQuoteMessageEvent(quoteMessageEvent: QuoteMessageEvent) {
+        /** 是否可刷新ui **/
+        if (canrefrashQuote) {
+            /** 行情推送过来的goodsid **/
+            quoteMessageEvent.goodsid.forEach { goodid ->
+                //找到相应商品信息
+                val goodsInfo = viewModel.list?.find { it.goodsid == goodid }
+                viewModel.list?.indexOf(goodsInfo)?.let {
+                    if (it != -1) {//刷新相应item
+                        idMarketList?.adapter?.notifyItemChanged(it)
+                    }
+                }
+            }
+        }
+    }
+
+    //注册eventbus
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+        EventBus.getDefault().register(this)
+    }
+
+    override fun onDestroy() {
+        super.onDestroy()
+        EventBus.getDefault().unregister(this)
+    }
+
+    //加锁在显示时行情推送刷新ui
     override fun onResume() {
         super.onResume()
-        LogUtils.eTag("dakjsjk","qedadas" + data?.autoid)
+        canrefrashQuote = true
+    }
+
+    //加锁在不显示时行情推送不刷新ui且remove订阅信息
+    override fun onPause() {
+        super.onPause()
+        thisTag?.let { viewModel.removeSubscriptQuote(tag = it) }
+        canrefrashQuote = false
     }
 
 
@@ -63,10 +110,10 @@ class MarketFragment(data: ExternalExchanges) : BaseFragment<MarketBaseModel>()
         val list = Builder.getDatabase<AppDatabase>().goodsInfoDao.getGoodsGroups(
             goodsGroupData?.goodsgroupid ?: 0
         )
-        if (list.size > 10){
+        if (list.size > 10) {
             lastShow = 10
-        }else{
-            if (list.size > 0){
+        } else {
+            if (list.size > 0) {
                 lastShow = list.size
             }
         }
@@ -76,10 +123,14 @@ class MarketFragment(data: ExternalExchanges) : BaseFragment<MarketBaseModel>()
         viewModel.list = list
         val adapter = viewModel.getAdapter()
         idMarketList?.adapter = adapter
-        if (fistShow != -1 && lastShow != -1){
-            viewModel.queryQuoteDay(first = fistShow,last = lastShow){isCompleted, err ->
-                if (isCompleted){
-                    adapter.notifyItemRangeChanged(fistShow,lastShow)
+        if (fistShow != -1 && lastShow != -1) {
+            viewModel.queryQuoteDay(
+                tag = thisTag ?: "",
+                first = fistShow,
+                last = lastShow
+            ) { isCompleted, err ->
+                if (isCompleted) {
+                    adapter.notifyItemRangeChanged(fistShow, lastShow)
                 }
             }
         }
@@ -93,10 +144,14 @@ class MarketFragment(data: ExternalExchanges) : BaseFragment<MarketBaseModel>()
                 if (newState == RecyclerView.SCROLL_STATE_IDLE) {// 这个判断是当recyclerview不滚动时
                     fistShow = linearLayoutManager.findFirstVisibleItemPosition()//获取第一个显示条目
                     lastShow = linearLayoutManager.findLastVisibleItemPosition()//获取最后一个显示条目
-                    if (fistShow != -1 && lastShow != -1){
-                        viewModel.queryQuoteDay(first = fistShow,last = lastShow){isCompleted, err ->
-                            if (isCompleted){
-                                adapter.notifyItemRangeChanged(fistShow,lastShow)
+                    if (fistShow != -1 && lastShow != -1) {
+                        viewModel.queryQuoteDay(
+                            tag = thisTag ?: "",
+                            first = fistShow,
+                            last = lastShow
+                        ) { isCompleted, err ->
+                            if (isCompleted) {
+                                adapter.notifyItemRangeChanged(fistShow, lastShow)
                             }
                         }
                     }

+ 1 - 5
RMA/app/src/main/java/cn/muchinfo/rma/view/base/future/trade/GoodsTradeViewModel.kt

@@ -73,10 +73,6 @@ class GoodsTradeViewModel : BaseViewModel() {
         }
         MyApplication.getInstance()?.futureManager?.queryQuoteDay(params = params) { isSuccess, respData, error ->
             if (isSuccess) {
-                val map = GlobalDataCollection.instance?.getmGoodsInfoClass()
-                respData?.forEach {
-                    map?.put(it.goodscode, it)
-                }
                 if (respData?.isEmpty()?.not() == true) {
                     quoteDayData.postValue(respData.get(0))
                 }
@@ -98,7 +94,7 @@ class GoodsTradeViewModel : BaseViewModel() {
     fun calculateMoney(){
         val accountData = GlobalDataCollection.instance?.accountData
         val showAccountData = AccountShowData()
-        showAccountData.setNormalData()
+//        showAccountData.setNormalData()
         canUseWorth.postValue(accountData?.currentbalance?.minus(accountData.usedmargin)?.minus(
             accountData.freezecharge
         )?.minus(