Example #1
0
        private void LoadPosition()
        {
            MyPosition    pos        = new MyPosition();
            XmlSerializer serializer = new XmlSerializer(pos.GetType());

            string path = string.Format("{0}_{1}.xml", Name, Instrument);

            try
            {
                using (FileStream stream = new FileStream(path, FileMode.Open))
                {
                    pos = (MyPosition)serializer.Deserialize(stream);
                    stream.Close();

                    if (pos.Amount != 0)
                    {
                        Portfolio.Add(pos.EntryDate, pos.Amount > 0 ? TransactionSide.Buy : TransactionSide.Sell,
                                      Math.Abs(pos.Amount), Instrument, pos.Price, "从XML中初始化");
                    }

                    Console.WriteLine(string.Format("加载路径:{0},持仓:{1},价格:{2}", path, pos.Amount, pos.Price));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
        void Download13F(object args)
        {
            this.portfolio = new Portfolio();
            foreach (Form13F form in Edgar.FetchForm13Fs(args as string, 6))
            {
                portfolio.Add(form);
            }

            this.Invoke(new EnableDelegate(this.Enable));
        }
Example #3
0
        public void QtyToNet(Instrument instrument, double NetQty, Portfolio Net)
        {
            double Qty = 0;

            SmartQuant.Position p = Net.GetPosition(instrument);
            if (p != null)
            {
                Qty = p.Qty;
            }
            double diff = NetQty - Qty;

            if (diff > 0)
            {
                Net.Add(new Fill(framework.Clock.DateTime, null, instrument, CurrencyId.CNY, SmartQuant.OrderSide.Buy, diff, 0, "Initial Position"));
            }
            else if (diff < 0)
            {
                Net.Add(new Fill(framework.Clock.DateTime, null, instrument, CurrencyId.CNY, SmartQuant.OrderSide.Sell, -diff, 0, "Initial Position"));
            }
        }
Example #4
0
        public void QtyToLongShort(Instrument instrument, double LongQty, double ShortQty, Portfolio Long, Portfolio Short)
        {
            {
                double Qty            = 0;
                SmartQuant.Position p = Long.GetPosition(instrument);
                if (p != null)
                {
                    Qty = p.Qty;
                }
                double diff = LongQty - Qty;
                if (diff > 0)
                {
                    Long.Add(new Fill(framework.Clock.DateTime, null, instrument, CurrencyId.CNY, SmartQuant.OrderSide.Buy, diff, 0, "Initial Long Position"));
                }
                else if (diff < 0)
                {
                    Long.Add(new Fill(framework.Clock.DateTime, null, instrument, CurrencyId.CNY, SmartQuant.OrderSide.Sell, -diff, 0, "Initial Long Position"));
                }
            }

            {
                double Qty            = 0;
                SmartQuant.Position p = Short.GetPosition(instrument);
                if (p != null)
                {
                    Qty = p.Qty;
                }
                double diff = ShortQty - Qty;
                if (diff > 0)
                {
                    Short.Add(new Fill(framework.Clock.DateTime, null, instrument, CurrencyId.CNY, SmartQuant.OrderSide.Sell, diff, 0, "Initial Short Position"));
                }
                else if (diff < 0)
                {
                    Short.Add(new Fill(framework.Clock.DateTime, null, instrument, CurrencyId.CNY, SmartQuant.OrderSide.Buy, -diff, 0, "Initial Short Position"));
                }
            }
        }
 public void Add(Asset s)
 {
     if (_symbols.ContainsKey(s))
     {
         _symbols[s].Add(s);
     }
     else
     {
         _symbols.Add(s, new List <Asset> {
             s
         });
     }
     Portfolio.Add(s);
 }
Example #6
0
        public PortfolioEntry Add_Html_Page(string title, string html)
        {
            var entry = new PortfolioEntry();

            entry.Title       = title;
            entry.Type        = PORTFOLIO_ENTRY_TYPE.HTML;
            entry.Description = html;
            entry.TenantId    = TenantId;

            //These fields are not used.
            entry.Image = "";

            Portfolio.Add(entry);
            return(entry);
        }
Example #7
0
        public void SetupAccount(string line)
        {
            string[] data = line.Split(" ");

            string symbol = data[0];
            double value  = double.Parse(data[1]);

            if (symbol == "Cash")
            {
                Balance = value;
            }
            else
            {
                Portfolio.Add(new Stock(symbol, value));
            }
        }
Example #8
0
        public PortfolioEntry Add_ImageAndDescription_Page(string title, string description, HttpPostedFileBase image)
        {
            var entry = new PortfolioEntry();

            entry.Type        = PORTFOLIO_ENTRY_TYPE.IMAGE;
            entry.Title       = title;
            entry.Description = description;
            entry.TenantId    = TenantId;

            if (image != null && image.ContentLength > 0)
            {
                entry.SaveImage(image);
            }

            Portfolio.Add(entry);

            return(entry);
        }
Example #9
0
        public PortfolioEntry Add_ImageAndDescription_Page(string type, string title, string description, string imagePath = null)
        {
            var entry = new PortfolioEntry();

            entry.Type        = type;
            entry.Title       = title;
            entry.Description = description;
            entry.TenantId    = TenantId;

            if (imagePath != null)
            {
                entry.Image = imagePath;
            }

            Portfolio.Add(entry);

            return(entry);
        }
Example #10
0
        public void Add_NoneOwnedPreviously_OneOwnedNow()
        {
            _portfolio.Add("NOK", 1);

            _portfolio.Owned.ShouldBeEquivalentTo(new List <StockOwnership>
            {
                new StockOwnership
                {
                    Symbol = "NOK",
                    Amount = 1
                }
            });
        }
Example #11
0
        private void HandleStockTransaction(string symbol, string type, double numShares, double totalValue)
        {
            var stock = Portfolio.FirstOrDefault(s => s.Symbol == symbol);

            // create and add stock to portfolio if not found
            if (stock == null)
            {
                stock = new Stock(symbol, 0);
                Portfolio.Add(stock);
            }

            if (type == "BUY")
            {
                Withdraw(totalValue);
                stock.Buy(numShares);
            }
            else if (type == "SELL" || type == "DIVIDEND")
            {
                stock.Sell(numShares);
                Deposit(totalValue);
            }
        }
Example #12
0
        public void PortfolioContructionTest()
        {
            Equity         e;
            Debt           s;
            InvestmentFund f;
            Rating         r;
            AssetHolding   ia;
            Portfolio      p;

            try
            {
                using (var db = new FGABusinessComponent.BusinessComponent.FGAContext("PREPROD", compiledModel))
                {
#if DEBUG
                    // xml seeder: le fichier xml est lu et est chargé comme valeurs par défaut
                    db.SaveChanges();
                    // ecriture du fichier pour permettre d avoir un fichier
                    FGABusinessComponent.BusinessComponent.Util.EFCodeFirstMethods.DumpDbCreationScriptToFile(db);
#endif

                    f = new InvestmentFund(ISIN: "ZZ11110000", FinancialInstrumentName: "Equity 1-Holding test", FinancialAssetTypeCategoryCode: FinancialAssetTypeCategoryCode.DEBT);
                    db.InvestmentFunds.Add(f);
                    p = new Portfolio(ISIN: "ZZ11110000", Date: new DateTime(9999, 12, 31), Name: "ExempleFonds1");
                    db.Portfolios.Add(p);
                    db.SaveChanges();

                    AssetPortfolioAssociation assoc = new AssetPortfolioAssociation(p, f);

                    f.UnderlyingPortfolio           = assoc;
                    assoc.InvestmentAmount          = new CurrencyAndAmount();
                    assoc.InvestmentAmount.Value    = 5;
                    assoc.InvestmentAmount.Currency = (CurrencyCode)"EUR";
                    db.AssetPortfolioAssociations.Add(assoc);
                    db.SaveChanges();

                    s = new Debt(ISIN: "PP11110000", FinancialInstrumentName: "Debt 1-Portfolio test", MaturityDate: new DateTime(2013, 1, 1), interestCoupon: new InterestCalculation(new PercentageRate(1.666), new FrequencyCode(1)));
                    db.Debts.Add(s);

                    ia = new AssetHolding(Date: this.dateOfData, ISIN: s.ISINId, HoldAsset: s, Holder: p, Quantity: 100);
                    db.AssetHoldings.Add(ia);
                    p.Add(ia);

                    e = new Equity(ISIN: "PP11110000", FinancialInstrumentName: "Equity 1-Portfolio test");
                    db.Equities.Add(e);
                    ia = new AssetHolding(Date: this.dateOfData, ISIN: e.ISINId, Holder: p, HoldAsset: e, Quantity: 200);
                    db.AssetHoldings.Add(ia);
                    p.Add(ia);

                    s = new Debt(ISIN: "PP22220000", FinancialInstrumentName: "Debt 2-Portfolio test", MaturityDate: new DateTime(2013, 1, 1), interestCoupon: new InterestCalculation(new PercentageRate(1.666), new FrequencyCode(1)));
                    db.Debts.Add(s);
                    ia = new AssetHolding(Date: this.dateOfData, ISIN: s.ISINId, Holder: p, HoldAsset: s, Quantity: 300);
                    db.AssetHoldings.Add(ia);
                    p.Add(ia);

                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex);
                throw ex;
            }
        }
 public void Add(IAsset s)
 {
     Portfolio.Add(s);
 }
Example #14
0
        private void Calculate()
        {
            if (TrainingSession.TestingHistoricalData.Quotes.Count != Signals.Count)
            {
                return;
            }

            Trade lastSellTrade = null;

            foreach (var quote in TrainingSession.TestingHistoricalData.Quotes)
            {
                var indexOfToday  = TrainingSession.TestingHistoricalData.Quotes.IndexOfKey(quote.Key);
                var indexTomorrow = indexOfToday < TrainingSession.TestingHistoricalData.Quotes.Count - 2
                    ? indexOfToday + 1
                    : indexOfToday;
                var transactionBuyPrice  = TrainingSession.TestingHistoricalData.Quotes.ElementAt(indexOfToday).Value.Close;
                var transactionSellPrice = TrainingSession.TestingHistoricalData.Quotes.ElementAt(indexOfToday).Value.Close;

                if (lastSellTrade == null || (quote.Key.Date - lastSellTrade.Date.Date).Days >=
                    TrainingSession.NumberDaysBetweenTransactions)
                {
                    if (Signals[quote.Key] == SignalEnum.Buy &&
                        Portfolio.GetMaxPurchaseVolume(TrainingSession.Stock, quote.Key,
                                                       transactionBuyPrice) > 1)
                    {
                        int maxPurchaseVolume =
                            Portfolio.GetMaxPurchaseVolume(TrainingSession.Stock, quote.Key, transactionBuyPrice);

                        var trade = new Trade
                        {
                            Type           = TransactionEnum.Buy,
                            Stock          = TrainingSession.Stock,
                            Date           = quote.Key,
                            NumberOfShares = maxPurchaseVolume,
                            Price          = transactionBuyPrice
                        };

                        Portfolio.Add(trade);
                    }
                }

                if (Signals[quote.Key] == SignalEnum.Sell &&
                    Portfolio.GetHoldings(quote.Key).ContainsKey(TrainingSession.Stock))
                {
                    lastSellTrade = new Trade
                    {
                        Type           = TransactionEnum.Sell,
                        Stock          = TrainingSession.Stock,
                        Date           = quote.Key,
                        NumberOfShares = Portfolio.GetHoldings(quote.Key)[TrainingSession.Stock],
                        Price          = transactionSellPrice
                    };

                    CompleteTransactions.Add(new CompleteTransaction(Portfolio.Trades.Last().Value, lastSellTrade));
                    Portfolio.Add(lastSellTrade);
                }
            }

            if (Portfolio.GetHoldings(TrainingSession.TestingHistoricalData.EndDate).Any())
            {
                var trade = new Trade
                {
                    Type           = TransactionEnum.Sell,
                    Stock          = TrainingSession.Stock,
                    Date           = TrainingSession.TestingHistoricalData.EndDate,
                    NumberOfShares =
                        Portfolio.GetHoldings(TrainingSession.TestingHistoricalData.EndDate)[TrainingSession.Stock],
                    Price = TrainingSession.TestingHistoricalData.Quotes[TrainingSession.TestingHistoricalData.EndDate].Close
                };

                CompleteTransactions.Add(new CompleteTransaction(Portfolio.Trades.Last().Value, trade));
                Portfolio.Add(trade);
            }
        }