コード例 #1
0
ファイル: Portfolio.cs プロジェクト: lynxliu/Backtesting
        public IOrder GenerateOrderByCapital(string ticker, double price, double capital, OrderType orderType, string currency = null)
        {
            if (price < CommonProc.EPSILON)
            {
                return(null);
            }
            var t = capital / price;

            if (t < CommonProc.EPSILON)
            {
                return(null);
            }
            var shares = Convert.ToInt32(t);

            shares = Math.Max(Order.MinOperationShares, shares);
            if (string.IsNullOrEmpty(currency))
            {
                currency = "RMB";
            }
            Order o = new Order();

            o.Ticker         = ticker;
            o.Price          = price;
            o.Currency       = currency;
            o.OrderDirection = orderType;
            o.OrderTime      = DateTime.Now;
            if (orderType == OrderType.Buy)
            {
                var account = GetAccount(currency);
                if (account == null)
                {
                    return(null);
                }
                if (account.CurrentValue.Number >= price * shares && shares >= Order.MinOperationShares)//min buy is 100;
                {
                    o.Shares = (int)shares;
                }
            }
            else
            {
                if (IsUnlimited)
                {
                    o.Shares = shares;
                }
                else
                {
                    var position = PositionList.FirstOrDefault(v => v.InstrumentTicker == ticker);
                    if (position != null && position.Shares > 0)
                    {
                        o.Shares = (int)Math.Min(Convert.ToInt32(position.Shares), shares);
                    }
                }
            }
            if (o.Shares > 0)
            {
                return(o);
            }
            return(null);
        }
コード例 #2
0
ファイル: Portfolio.cs プロジェクト: lynxliu/Backtesting
 public IPosition GetTargetPosition(IInstrument inst)
 {
     if (inst == null)
     {
         return(null);
     }
     return(PositionList.FirstOrDefault(v => v.InstrumentTicker == inst.Ticker));
 }
コード例 #3
0
ファイル: Portfolio.cs プロジェクト: lynxliu/Backtesting
        public double GetWeight(string ticker)
        {
            var p = PositionList.FirstOrDefault(v => v.InstrumentTicker == ticker);

            if (p == null)
            {
                return(0);
            }
            return(p.CurrentValue.Number / PositionCapital.Number);
        }
コード例 #4
0
ファイル: Portfolio.cs プロジェクト: lynxliu/Backtesting
        public IOrder GenerateOrderByPercent(string ticker, double price, double percent, OrderType orderType, string currency = null)
        {
            if (string.IsNullOrEmpty(currency))
            {
                currency = "RMB";
            }
            Order o = new Order();

            o.Ticker         = ticker;
            o.Price          = price;
            o.Currency       = currency;
            o.OrderDirection = orderType;
            o.OrderTime      = DateTime.Now;
            if (orderType == OrderType.Buy)
            {
                var account = GetAccount(currency);
                if (account == null)
                {
                    return(null);
                }
                var num = Convert.ToInt32(account.CurrentValue.Number * percent / price) / Order.MinOperationShares;
                num *= Order.MinOperationShares;
                if (num >= Order.MinOperationShares)
                {
                    o.Shares = num;
                }
            }
            else
            {
                if (IsUnlimited)
                {
                    o.Shares = Order.MinOperationShares;
                }
                else
                {
                    var position = PositionList.FirstOrDefault(v => v.InstrumentTicker == ticker);
                    if (position != null && position.Shares > 0)
                    {
                        var num = Convert.ToInt32(position.Shares * percent / price) / Order.MinOperationShares;
                        num *= Order.MinOperationShares;
                        if (num >= Order.MinOperationShares)
                        {
                            o.Shares = num;
                        }
                    }
                }
            }
            if (o.Shares > 0)
            {
                return(o);
            }
            return(null);
        }
コード例 #5
0
ファイル: Portfolio.cs プロジェクト: lynxliu/Backtesting
        void ProcessOrder(IOrder order)
        {
            try
            {
                if (order == null)
                {
                    return;
                }
                var inst = GetInstrumentList().FirstOrDefault(v => v.Ticker == order.Ticker);
                if (inst == null)
                {
                    return;
                }

                var p = PositionList.FirstOrDefault(v => v.InstrumentTicker == order.Ticker);
                if (p == null)
                {
                    var pos = new Position();
                    pos.InstrumentName   = inst.Name;
                    pos.InstrumentTicker = inst.Ticker;
                    pos.CurrentPrice     = order.Price;
                    pos.ProcessOrder(order);
                    pos.CreateTime = order.OrderTime;
                    pos.DataTime   = order.OrderTime;
                    if (ProcessMoney(order.CashFlow))
                    {
                        PositionList.Add(pos);
                    }
                }
                else
                {
                    if (ProcessMoney(order.CashFlow))
                    {
                        p.ProcessOrder(order);
                    }
                }
            }
            catch (Exception ex)
            {
                LogSupport.Error(ex);
            }
        }
コード例 #6
0
        public void Refresh()
        {
            if (TargetObject == null)
            {
                return;
            }
            if (TargetObject.CurrentPortfolio == null)
            {
                return;
            }

            PositionList.Clear();
            TargetObject.CurrentPortfolio.PositionList.ForEach(v => PositionList.Add(v));

            DataList.Clear();
            TargetObject.MADataList.ForEach(v => DataList.Add(v));

            if (!string.IsNullOrEmpty(TargetObject.CurrentTicker))
            {
                CurrentPosition = PositionList.FirstOrDefault(v => v.InstrumentTicker == TargetObject.CurrentTicker);
            }
        }
コード例 #7
0
        public VariableTermManualFactoryViewModel()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                PositionAllowed         = true;
                TransportationLineAllow = Visibility.Visible;
                MainRowList             = new SortableCollectionView <TblVariableTermManualFactoryViewModel>();
                SelectedMainRow         = new TblVariableTermManualFactoryViewModel();
                GetPoisition(LoggedUserInfo.Code);
                GetEmpTransportationLine(LoggedUserInfo.Code);
                Client.GetEmpPositionCompleted += (s, sv) =>
                {
                    PositionList = sv.Result.ToList();
                    if (PositionList.Count() == 1)
                    {
                        SelectedMainRow.Position = PositionList.FirstOrDefault();
                        Position = PositionList.FirstOrDefault();
                    }
                };
                Client.GetEmpTransportationLineCompleted += (s, sv) =>
                {
                    TransportationList = sv.Result.ToList();
                    if (TransportationList.Count() == 1)
                    {
                        SelectedMainRow.Transportation = TransportationList.FirstOrDefault();
                        Transportation = TransportationList.FirstOrDefault();
                    }
                };

                Client.GetTblVariableTermManualCompleted += (s, sv) =>
                {
                    foreach (var row in sv.Result)
                    {
                        var newrow = new TblVariableTermManualFactoryViewModel
                        {
                            EmpPerRow       = new EmployeesView(),
                            SalaryTerPerRow = new TblSalaryTerm()
                        };

                        newrow.EmpPerRow       = EmpList.FirstOrDefault(w => w.Emplid == row.Emplid);
                        newrow.SalaryTerPerRow = SalaryTermList.FirstOrDefault(w => w.Iserial == row.TermId);
                        newrow.InjectFrom(row);
                        MainRowList.Add(newrow);
                    }
                    Loading = false;

                    if (FullCount == 0 && MainRowList.Count == 0)
                    {
                        AddNewMainRow(false);
                    }
                };
                StoreVisibility    = LoggedUserInfo.Company.Code == "HQ" ? Visibility.Collapsed : Visibility.Visible;
                PositionVisibility = LoggedUserInfo.Company.Code != "HQ" ? Visibility.Collapsed : Visibility.Visible;

                Client.UpdateOrInsertTblVariableTermManualCompleted += (s, x) =>
                {
                    var savedRow = (TblVariableTermManualFactoryViewModel)MainRowList.GetItemAt(x.outindex);

                    if (savedRow != null)
                    {
                        savedRow.InjectFrom(x.Result);
                    }
                };
                Client.DeleteTblVariableTermManualCompleted += (s, ev) =>
                {
                    if (ev.Error != null)
                    {
                        throw ev.Error;
                    }
                    var oldrow = MainRowList.FirstOrDefault(x => x.Iserial == ev.Result);
                    if (oldrow != null)
                    {
                        MainRowList.Remove(oldrow);
                    }
                };

                Client.GetEmpByAttOperatorCompleted += (s, sv) =>
                {
                    MainRowList.Clear();
                    foreach (var emp in sv.Result)
                    {
                        MainRowList.Add(new TblVariableTermManualFactoryViewModel
                        {
                            EmpPerRow       = emp,
                            Emplid          = emp.Emplid,
                            Position        = emp.Position,
                            Transportation  = emp.TransportationLine,
                            TransDate       = DayDate,
                            SalaryTerPerRow = SalaryTerPerRow,
                            Hours           = Hours,
                            Days            = Days
                        });
                    }

                    Loading = false;
                };

                Client.GetTblSalaryTermAsync();

                Client.GetTblSalaryTermCompleted += (s, sv) =>
                {
                    SalaryTermList = sv.Result;
                };
                Client.GetEmpTablebyStoreAndCompanyCompleted += (s, sv) =>
                {
                    EmpList = new ObservableCollection <EmployeesView>();
                    EmpList = sv.Result;
                    Loading = false;

                    Client.GetTblVariableTermManualAsync(new ObservableCollection <string>(EmpList.Select(x => x.Emplid)));
                };
            }
        }