美文网首页
代理模式解决下单需求

代理模式解决下单需求

作者: 墨源为水 | 来源:发表于2020-09-14 16:15 被阅读0次

    在做股票下单需求时,有这样几种下单模式

    • 1.限价:用户可自主输入价格,与下单数量
    • 2.市价:根据下单时的市价下单,用户只能输入下单数量。
    • 3.策略:有多种策略类型,每种策略的下单价格,与下单数量是根据公式计算出来
    在此下单模式中,
    • 1.限价有参考报价上下限的限制
    • 2.市价模式还分为:科创板市价交易类型,非科创板市价交易交易类型;科创板交易类型是最高买价与最低卖价进行交易
      *在对应下单模式中,要默认填写对应下单价格,与默认下单数量 *
    在此贴出部分代码
        private fun chooseSaleType(position:Int){
            tradeHxSaleTypeSelectedPosition = position
            val tradeHxSaleTypeEntity = currentTradeSaleTypeEntity
            if(tradeHxSaleTypeEntity.isLimitSaleType()){
                mMarketValueTx.setText("限价")
                mStepperMarketValue.hideDisableText()
            }else if(tradeHxSaleTypeEntity.isStrategySaleType()){
                mStepperMarketValue.showDisableText(tradeHxSaleTypeEntity?.name)
                mMarketValueTx.setText("策略")
            }else{
                mStepperMarketValue.showDisableText(tradeHxSaleTypeEntity?.name)
                mMarketValueTx.setText("市价")
            }
            changeSaleTradeBtn()
            calcuAmount()
            changeTradeBtnStatus()
        }
    
        private fun calcuAmount(){
            if(mStockDetailInfo == null){
                return
            }
            val result = getOrderPrice()
            ...........
            val resultPosition =
                    if(mSaleType == 0){
                        if(currentTradeSaleTypeEntity==null||currentTradeSaleTypeEntity?.isLimitSaleType()!!){
                            calcal(availbleAssetsBigDecimal, BigDecimal(mStepperMarketValue.value),getBondTypeStepValue(),3)
                        } else if(currentTradeSaleTypeEntity?.isStrategySaleType()!!){
                            getStrategyPositionNum()
                        }else{
                            calcal(availbleAssetsBigDecimal,mStockDetailInfo?.calcuMarketPrice!!,getBondTypeStepValue(),3)
                        }
                    }else{
                        if(currentTradeSaleTypeEntity!=null&&currentTradeSaleTypeEntity!!.isStrategySaleType()){
                            getStrategyPositionNum()
                        }else{
                            if(mStockDetailInfo!=null&&mStockDetailInfo?.stockPositionInfo!=null) mStockDetailInfo!!.stockPositionInfo!!.availableBigDecimal!! else zeroBigDecimal
                        }
                    }
    
            mStepperQulitityNum.tvStepperContent.setHint("可${if(mSaleType == 0) "买" else "卖"}${resultPosition}")
            ...........
        }
    
        private fun getOrderPrice():BigDecimal{
            return if(mStepperQulitityNum.value == 0.0){
                zeroBigDecimal
            }else{
                if(currentTradeSaleTypeEntity==null||currentTradeSaleTypeEntity?.isLimitSaleType()!!){
                    if(mStepperMarketValue.value == 0.0){
                        zeroBigDecimal
                    }else{
                        BigDecimal(mStepperQulitityNum.value).multiply(BigDecimal(mStepperMarketValue.value))
                    }
                }else{
                    BigDecimal(mStepperQulitityNum.value).multiply(mStockDetailInfo?.calcuMarketPrice)
                }
            }.setScale(SCALE_POINT_NUMBER,BigDecimal.ROUND_HALF_UP)
        }
    

    当事后写完代码表示,这样的代码很难维护,阅读起来也很纠结。逼自己重构了前不久写的代码;重构后的代码,我的愿景是这样的。输入股票信息,交易类型,trigger一下,就可以反应在ui界面上。用户选择交易类型,trigger一下,也会对应发生改变,用户点击下单,根据输入价格、数量,如果不满足条件,弹出对应的提示框,满足的话,调用对应股票类型的下单接口。在获取股票信息这块,是根据股票code,type调用对应的接口

    interface ITradeStrategy {
    
        fun initStrategy(stockInfoEntity: TradeHxStockDetail, availbleAssetsBigDecimal:BigDecimal)
    
        fun changeSaleType(saleType:Int)
    
        fun calcu(orderNum:Int,unitPrice:String,positionType:Int?):String
    
        fun order(orderNum:Int,unitPrice:String)
    
        fun tradeStrategyTag():String
    
        fun diablePriceInput():Boolean
    }
    
    abstract class BaseTradeStrategy :ITradeStrategy {
        protected lateinit var availbleAssetsBigDecimal:BigDecimal
        protected lateinit var stockInfoEntity: TradeHxStockDetail
    
        override fun initStrategy(stockInfoEntity: TradeHxStockDetail, availbleAssetsBigDecimal: BigDecimal) {
            this.stockInfoEntity = stockInfoEntity
            this.availbleAssetsBigDecimal = availbleAssetsBigDecimal
        }
    
        protected var mSaleType = 0
    
        override fun changeSaleType(saleType: Int) {
            mSaleType = saleType
        }
    
        protected fun calcal(result:BigDecimal,d3:BigDecimal):BigDecimal{
            return (result - result % d3).setScale( 0, BigDecimal.ROUND_DOWN )
        }
    
        private fun isBondType():Boolean{
            return StockManager.TYPE_SH_BOND.equals(stockInfoEntity.market)|| StockManager.TYPE_SZ_BOND.equals(stockInfoEntity.market)
        }
    
        protected fun getBondTypeStepValue():BigDecimal{
            return if(isBondType()) tenBigDecimal else hundredBigDecimal
        }
    }
    
    class TradeStrategyProxy() : ITradeStrategy {
        private var mTradeStrategy:ITradeStrategy?=null
    
        private var mStockInfo: TradeHxStockDetail?=null
        private var availbleAssetsBigDecimal = zeroBigDecimal
        private var mTradeStrategyMap:SparseArray<ITradeStrategy> = SparseArray()
    
        override fun initStrategy(stockInfoEntity: TradeHxStockDetail, availbleAssetsBigDecimal: BigDecimal) {
            this.mStockInfo = stockInfoEntity
            this.availbleAssetsBigDecimal = availbleAssetsBigDecimal
        }
        override fun changeSaleType(saleType: Int) {
            mTradeStrategy?.changeSaleType(saleType)
        }
    
        fun triggerType(tradeSaleTypeEntity: TradeHxSaleTypeEntity?){
            tradeSaleTypeEntity?:return
            mStockInfo?:return
            mTradeStrategy = if(tradeSaleTypeEntity.isLimitSaleType()){
                mTradeStrategyMap.get(0)?:LimitTradeStrategyImpl().also {
                    mTradeStrategyMap.put(0,it)
                }
            }else if(tradeSaleTypeEntity.isStrategySaleType()){
                mTradeStrategyMap.get(1)?:StrategyTradeStrategyImpl().also {
                    mTradeStrategyMap.put(1,it)
                }
            }else{
                mTradeStrategyMap.get(2)?:MarketTradeStrategyImpl().also {
                    mTradeStrategyMap.put(2,it)
                }
            }
            mTradeStrategy?.initStrategy(mStockInfo!!,availbleAssetsBigDecimal)
        }
    
    
        override fun calcu(orderNum: Int, unitPrice: String,positionType:Int?):String {
            return mTradeStrategy?.calcu(orderNum,unitPrice,positionType)?:""
        }
    
        override fun order(orderNum: Int, unitPrice: String) {
            mTradeStrategy?.order(orderNum,unitPrice)
        }
    
        override fun tradeStrategyTag(): String {
            return mTradeStrategy?.tradeStrategyTag()?:""
        }
    
        override fun diablePriceInput(): Boolean {
            return mTradeStrategy?.diablePriceInput()?:false
        }
    
        fun reset(){
            mStockInfo = null
            mTradeStrategy = null
        }
    
    }
    

    以上代码可代理限价,市价,策略下单模式,具体逻辑可分散在对应的下单模式中

    class LimitTradeStrategyImpl : BaseTradeStrategy() {
    
        override fun calcu(orderNum: Int, unitPrice: String,positionType:Int?):String {
            return if(mSaleType == 0){
                calcal(availbleAssetsBigDecimal, BigDecimal(unitPrice),getBondTypeStepValue(),positionType)
            }else{
                stockInfoEntity.stockPositionInfo.availableBigDecimal!!
            }.setScale(2,BigDecimal.ROUND_HALF_UP).toString()
        }
    
        override fun order(orderNum: Int, unitPrice: String) {
    
        }
    
        override fun tradeStrategyTag(): String {
            return "限价"
        }
    
        override fun diablePriceInput(): Boolean {
            return false
        }
    }
    
    class MarketTradeStrategyImpl : BaseTradeStrategy() {
    
        override fun calcu(orderNum: Int, unitPrice: String, positionType: Int?): String {
            TODO("Not yet implemented")
        }
    
        override fun order(orderNum: Int, unitPrice: String) {
            TODO("Not yet implemented")
        }
    
        override fun tradeStrategyTag(): String {
            return "市价"
        }
    
        override fun diablePriceInput(): Boolean {
            return true
        }
    }
    

    相关文章

      网友评论

          本文标题:代理模式解决下单需求

          本文链接:https://www.haomeiwen.com/subject/jqrzektx.html