Exemplo n.º 1
0
 public void ExecuteUpData(object para)
 {
     try
     {
         CheckFullStopModel cfsm = para as CheckFullStopModel;
         //添加持仓集合
         if (CommHelper.CFSmvmList.ContainsKey(cfsm.contract_id + cfsm.direction))
         {
             CheckFullStopModelViewModel temp = CommHelper.CFSmvmList[cfsm.contract_id + cfsm.direction].FirstOrDefault(x => x.StoplossId == cfsm.stoploss_id);
             if (temp != null)
             {
                 temp.StoplossPrice = cfsm.stoploss_price;
                 LogHelper.Info("止盈止损修改" + cfsm.contract_code + ":" + cfsm.direction + ";" + cfsm.create_time + "止损价:" + cfsm.stoploss_price);
             }
             PositionAllViewModel       pavm  = PositionAllViewModel.Instance();
             PotionDetailModelViewModel pdmvm = pavm.PMList.FirstOrDefault(x => x.ContractId == cfsm.contract_id && x.Direction == cfsm.direction);
             if (pdmvm != null)
             {
                 string[] values = pdmvm.LossVolume.Split('/');
                 pdmvm.LossVolume = cfsm.stoploss_price + "/" + values[1];
             }
         }
     }
     catch (Exception ex)
     {
         LogHelper.Info(ex.ToString());
     }
 }
 public static PositionAllViewModel Instance()
 {
     if (_PositionViewModel == null)
     {
         _PositionViewModel = new PositionAllViewModel();
     }
     return(_PositionViewModel);
 }
        public void ExecutePositionInfoData(object para)
        {
            try
            {
                PotionModel                rtm       = para as PotionModel;
                PositionAllViewModel       pavm      = PositionAllViewModel.Instance();
                PositionViewModel          pvm       = PositionViewModel.Instance();
                PotionDetailModelViewModel pdmvmtemp = pavm.DetPMList.FirstOrDefault(x => x.PsitionId == rtm.position_id);

                if (rtm.position_volume == 0)
                {
                    if (pdmvmtemp != null)
                    {
                        pavm.DetPMList.Remove(pdmvmtemp);
                        LogHelper.Info("4007  移除 " + rtm.contract_code + ":" + rtm.direction);
                        pavm.PMList.Clear();
                        pvm.PMList.Clear();
                    }

                    int count = SQLiteHelper.ExecuteNonQuery(SQLiteHelper.DBPath, CommandType.Text, "delete from AutoStopLoss where ContractID='" + rtm.contract_id + "' and PostionID='" + rtm.position_id + "';");
                }
                else
                {
                    if (pdmvmtemp != null)
                    {
                        pdmvmtemp.PositionVolume = rtm.position_volume;
                        pdmvmtemp.AbleVolume     = rtm.position_volume;
                        pdmvmtemp.UseMargin      = rtm.use_margin;
                        pdmvmtemp.SysUseMargin   = rtm.sys_margin;
                    }
                    else
                    {
                        PotionDetailModel pdm = new PotionDetailModel();
                        pdm.able_volume       = rtm.position_volume;
                        pdm.contract_code     = rtm.contract_code;
                        pdm.contract_id       = rtm.contract_id;
                        pdm.direction         = rtm.direction;
                        pdm.open_date         = rtm.position_date;
                        pdm.open_price        = rtm.position_price;
                        pdm.open_time         = rtm.position_time;
                        pdm.position_volume   = rtm.position_volume;
                        pdm.position_id       = rtm.position_id;
                        pdm.shadow_orderID    = rtm.shadow_orderID;
                        pdm.shadow_positionid = rtm.shadow_positionid;
                        pdm.shadow_tradeid    = rtm.shadow_tradeid;

                        pdm.trade_id   = "";
                        pdm.user_id    = rtm.user_id;
                        pdm.use_margin = rtm.use_margin;
                        pdm.sys_margin = rtm.sys_margin;
                        VarietyModel vm     = null;
                        string[]     values = pdm.contract_id.Split(' ');
                        if (values.Length == 3)
                        {
                            string varietie = values[1];
                            if (ContractVariety.Varieties.ContainsKey(varietie))
                            {
                                vm = ContractVariety.Varieties[varietie];
                            }
                            if (vm != null)
                            {
                                pdm.precision = vm.precision;
                            }
                        }

                        pdmvmtemp = new PotionDetailModelViewModel(pdm);
                        pavm.DetPMList.Add(pdmvmtemp);
                    }
                }
                if (ContractVariety.ContracPostionID.ContainsKey(rtm.contract_id))
                {
                    AutoStopLossModel aslm = CommParameterSetting.AutoStopLossModel.FirstOrDefault(x => x.Direction == rtm.direction && x.Agreement == rtm.contract_id);
                    if (aslm != null)
                    {
                        if (!ContractVariety.PostionPrice.ContainsKey(rtm.position_id))
                        {
                            ContractVariety.PostionPrice.Add(rtm.position_id, new AutoLossPrice());
                        }
                        string[] VarietiesKey = rtm.contract_id.Split(' ');
                        if (VarietiesKey.Length == 3)
                        {
                            string       varietie = VarietiesKey[1];
                            VarietyModel vm       = null;
                            if (ContractVariety.Varieties.ContainsKey(varietie))
                            {
                                vm = ContractVariety.Varieties[varietie];
                            }
                            if (vm != null)
                            {
                                if (rtm.direction == "B")
                                {
                                    ContractVariety.PostionPrice[rtm.position_id].LossPrice = rtm.position_price - aslm.StopLossPotion * vm.tick_size;
                                }
                                else
                                {
                                    ContractVariety.PostionPrice[rtm.position_id].LossPrice = rtm.position_price + aslm.StopLossPotion * vm.tick_size;
                                }
                                ContractVariety.PostionPrice[rtm.position_id].NewPrice = rtm.position_price;
                                if (!ContractVariety.ContracPostionID.ContainsKey(rtm.contract_id))
                                {
                                    ContractVariety.ContracPostionID.Add(rtm.contract_id, new List <string>());
                                }
                                ContractVariety.ContracPostionID[rtm.contract_id].Add(rtm.position_id);
                                int count = SQLiteHelper.ExecuteNonQuery(SQLiteHelper.DBPath, CommandType.Text, "insert INTO AutoStopLoss VALUES('" + rtm.position_id + "','" + rtm.contract_id + "','" + UserInfoHelper.UserId + "'," + ContractVariety.PostionPrice[rtm.position_id].LossPrice + "," + ContractVariety.PostionPrice[rtm.position_id].NewPrice + ");");
                            }
                        }
                    }
                }
                PotionDetailModelViewModel[] temp = pavm.DetPMList.ToArray();
                //合约名加方向作为K
                Dictionary <string, PotionDetailModelViewModel> dicod = new Dictionary <string, PotionDetailModelViewModel>();

                foreach (PotionDetailModelViewModel item in temp)
                {
                    PotionDetailModelViewModel pm1 = item.Clone(item);
                    if (!dicod.Keys.Contains(pm1.ContractCode + pm1.Direction))
                    {
                        pm1.AllPrice = pm1.OpenPrice * pm1.PositionVolume;
                        dicod.Add(pm1.ContractCode + pm1.Direction, pm1);
                        //MainViewModel.GetInstance().SubscribedStocks.Add(pm1.ContractCode);
                    }
                    else
                    {
                        dicod[item.ContractCode + item.Direction].AbleVolume         = dicod[item.ContractCode + item.Direction].AbleVolume + item.AbleVolume;
                        dicod[item.ContractCode + item.Direction].PositionProfitLoss = dicod[item.ContractCode + item.Direction].PositionProfitLoss + item.PositionProfitLoss;
                        dicod[item.ContractCode + item.Direction].PositionVolume     = dicod[item.ContractCode + item.Direction].PositionVolume + item.PositionVolume;
                        dicod[item.ContractCode + item.Direction].UseMargin          = dicod[item.ContractCode + item.Direction].UseMargin + item.UseMargin;
                        dicod[item.ContractCode + item.Direction].SysUseMargin       = dicod[item.ContractCode + item.Direction].SysUseMargin + item.SysUseMargin;
                        dicod[item.ContractCode + item.Direction].AllPrice          += item.PositionVolume * item.OpenPrice;
                        dicod[item.ContractCode + item.Direction].OpenPrice          = dicod[item.ContractCode + item.Direction].AllPrice / dicod[item.ContractCode + item.Direction].PositionVolume;
                    }
                }
                if (dicod.Keys.Count > 0)
                {
                    pavm.PMList.Clear();
                    pvm.PMList.Clear();
                    foreach (PotionDetailModelViewModel item in dicod.Values)
                    {
                        pavm.PMList.Add(item);
                        pvm.PMList.Add(item);
                    }
                    pvm.SelectedItem = pvm.PMList.FirstOrDefault(x => x.ContractCode == rtm.contract_code && x.Direction == rtm.direction);
                }
                if (!MainViewModel.GetInstance().SubscribedStocks.Contains(rtm.contract_code))
                {
                    MainViewModel.GetInstance().SubscribedStocks.Add(rtm.contract_code);
                    ScoketManager.GetInstance().SendSubscribeInfo(7, rtm.contract_code);
                }

                foreach (string key in CommHelper.CFSmvmList.Keys)
                {
                    PotionDetailModelViewModel temp1 = PositionViewModel.Instance().PMList.FirstOrDefault(x => (x.ContractId + x.Direction) == key);
                    if (temp1 == null)
                    {
                        continue;
                    }
                    double maxprice  = 99999999999;
                    double minprice  = 0;
                    double maxprice1 = 0;
                    double minprice1 = 99999999999;
                    int    maxnum    = 0;
                    int    minnum    = 0;
                    foreach (CheckFullStopModelViewModel item in CommHelper.CFSmvmList[key])
                    {
                        if (item == null)
                        {
                            continue;
                        }
                        if (item.StoplossPrice > 0 || item.StopprofitPrice > 0)
                        {
                            if (item.Direction == "B")
                            {
                                if (item.StopprofitPrice > 0)
                                {
                                    if (item.StopprofitPrice < maxprice)
                                    {
                                        maxprice = item.StopprofitPrice;
                                        maxnum   = item.OrderVolume;
                                    }
                                    else if (item.StopprofitPrice == maxprice)
                                    {
                                        maxnum += item.OrderVolume;
                                    }
                                }
                                if (item.StoplossPrice > 0)
                                {
                                    if (item.StoplossPrice > minprice)
                                    {
                                        minprice = item.StoplossPrice;
                                        minnum   = item.OrderVolume;
                                    }
                                    else if (item.StoplossPrice == minprice)
                                    {
                                        minnum += item.OrderVolume;
                                    }
                                }
                                if (maxprice < 99999999999 && maxnum > 0)
                                {
                                    temp1.ProfitVolume = maxprice + "/" + maxnum;
                                }
                                if (minprice > 0 && minnum > 0)
                                {
                                    temp1.LossVolume = minprice + "/" + minnum;
                                }
                            }
                            else
                            {
                                if (item.StopprofitPrice > 0)
                                {
                                    if (item.StopprofitPrice > maxprice1)
                                    {
                                        maxprice1 = item.StopprofitPrice;
                                        maxnum    = item.OrderVolume;
                                    }
                                    else if (item.StopprofitPrice == maxprice1)
                                    {
                                        maxnum += item.OrderVolume;
                                    }
                                }
                                if (item.StoplossPrice > 0)
                                {
                                    if (item.StoplossPrice < minprice1)
                                    {
                                        minprice1 = item.StoplossPrice;
                                        minnum    = item.OrderVolume;
                                    }
                                    else if (item.StoplossPrice == minprice1)
                                    {
                                        minnum += item.OrderVolume;
                                    }
                                }
                                if (maxprice1 > 0 && maxnum > 0)
                                {
                                    temp1.ProfitVolume = maxprice1 + "/" + maxnum;
                                }
                                if (minprice < 99999999999 && minnum > 0)
                                {
                                    temp1.LossVolume = minprice1 + "/" + minnum;
                                }
                            }
                        }
                    }
                }
                TransactionViewModel.Instance().AgreementChangedExecuteChanged();
                if (pavm.PMList.FirstOrDefault(x => x.ContractCode == rtm.contract_code && x.Direction == rtm.direction) == null)
                {
                    if (CommHelper.CFSmvmList.ContainsKey(rtm.contract_code + rtm.direction))
                    {
                        CommHelper.CFSmvmList[rtm.contract_code + rtm.direction].Clear();
                        LogHelper.Info("4007中移除止盈止损" + rtm.contract_id + rtm.direction + "清除数据");
                    }
                }

                //PositionViewModel.Instance().JSAbleVolume();
                TransactionViewModel.Instance().FigureUpNum(TransactionViewModel.Instance()._futures);
            }
            catch (Exception ex)
            {
                LogHelper.Info(ex.ToString());
            }
        }
        public void ExecutePositionDetialData(object para)
        {
            try
            {
                PotionDetailModel    pm   = para as PotionDetailModel;
                PositionAllViewModel pavm = PositionAllViewModel.Instance();
                if (pavm.DetPMList.FirstOrDefault(x => x.PsitionId == pm.position_id) != null)
                {
                    return;
                }
                if (pm.bLast)
                {
                    PotionDetailModelViewModel[] temp = pavm.DetPMList.ToArray();
                    //合约名加方向作为K
                    Dictionary <string, PotionDetailModelViewModel> dicod = new Dictionary <string, PotionDetailModelViewModel>();
                    foreach (PotionDetailModelViewModel item in temp)
                    {
                        PotionDetailModelViewModel pm1 = item.Clone(item);
                        if (!dicod.Keys.Contains(pm1.ContractCode + pm1.Direction))
                        {
                            pm1.AllPrice = pm1.OpenPrice * pm1.PositionVolume;
                            dicod.Add(pm1.ContractCode + pm1.Direction, pm1);
                            MainViewModel.GetInstance().SubscribedStocks.Add(pm1.ContractCode);
                        }
                        else
                        {
                            dicod[item.ContractCode + item.Direction].AbleVolume         = dicod[item.ContractCode + item.Direction].AbleVolume + item.AbleVolume;
                            dicod[item.ContractCode + item.Direction].PositionProfitLoss = dicod[item.ContractCode + item.Direction].PositionProfitLoss + item.PositionProfitLoss;
                            dicod[item.ContractCode + item.Direction].PositionVolume     = dicod[item.ContractCode + item.Direction].PositionVolume + item.PositionVolume;
                            dicod[item.ContractCode + item.Direction].UseMargin          = dicod[item.ContractCode + item.Direction].UseMargin + item.UseMargin;
                            dicod[item.ContractCode + item.Direction].SysUseMargin       = dicod[item.ContractCode + item.Direction].SysUseMargin + item.SysUseMargin;
                            dicod[item.ContractCode + item.Direction].AllPrice          += item.PositionVolume * item.OpenPrice;
                            dicod[item.ContractCode + item.Direction].OpenPrice          = dicod[item.ContractCode + item.Direction].AllPrice / dicod[item.ContractCode + item.Direction].PositionVolume;
                        }
                    }
                    if (dicod.Keys.Count > 0)
                    {
                        foreach (PotionDetailModelViewModel item in dicod.Values)
                        {
                            pavm.PMList.Add(item);
                            PositionViewModel.Instance().PMList.Add(item);
                        }
                    }
                    ScoketManager.GetInstance().SendSubscribeInfo(7, MainViewModel.GetInstance().SubscribedStocks);
                    //发送命令
                    ReqPotion rp = new ReqPotion();
                    rp.cmdcode = RequestCmdCode.SelectStopLoss;
                    rp.content = new ReqLoginName()
                    {
                        user_id = UserInfoHelper.UserId
                    };
                    ScoketManager.GetInstance().SendTradeWSInfo(JsonConvert.SerializeObject(rp));
                    //PositionViewModel.Instance().JSAbleVolume();
                    TransactionViewModel.Instance().FigureUpNum(TransactionViewModel.Instance()._futures);
                    return;
                }

                VarietyModel vm     = null;
                string[]     values = pm.contract_id.Split(' ');
                if (values.Length == 3)
                {
                    string varietie = values[1];
                    if (ContractVariety.Varieties.ContainsKey(varietie))
                    {
                        vm = ContractVariety.Varieties[varietie];
                    }
                    if (vm != null)
                    {
                        pm.precision = vm.precision;
                    }
                }

                PotionDetailModelViewModel pvm = new PotionDetailModelViewModel(pm);
                pavm.DetPMList.Add(pvm);
            }
            catch (Exception ex)
            {
                LogHelper.Info(ex.ToString());
            }
        }