Ejemplo n.º 1
0
 // if this transaction violates any of our portfolio rules, this method will throw an exception
 public void ApplyRules(Portfolio p, Trade t)
 {
     foreach (PortfolioRule r in Rules)
     {
         r.Apply(p, t);
     }
 }
Ejemplo n.º 2
0
        public void AllocationRuleYes()
        {
            Portfolio p = new Portfolio();

            Position pos1 = new Position();
            pos1.status = positionStatus.Open;
            pos1.Symbol = new Symbol("SYM1");
            pos1.price = 10;
            pos1.quantity = 1;

            Position pos2 = new Position();
            pos2.status = positionStatus.Open;
            pos1.Symbol = new Symbol("SYM2");
            pos2.price = 10;
            pos2.quantity = 1;

            p.Positions = new List<Position>();
            p.Positions.Add(pos1);
            p.Positions.Add(pos2);

            Trade t = new Trade();
            t.type = tradeTypes.Buy;
            t.Symbol = new Symbol("SYM2");
            t.price = 1;
            t.quantity = 2;

            AllocationRule r = new AllocationRule(0.6);
            Assert.IsTrue(r.Apply(p, t));
        }
Ejemplo n.º 3
0
 public void TestSellQuantityTooLarge()
 {
     Trade t = new Trade();
     t.Status = tradeStatus.Active;
     t.type = tradeTypes.Buy;
     t.quantity = 5;
     Assert.Throws<ArgumentException>(() => t.sell(10));
 }
 public void PlaceABuyTrade()
 {
     Portfolio port = new Portfolio();
     port.Cash = 1000;
     Position pos = new Position();
     pos.PositionId = 3;
     pos.quantity = 0;
     pos.price = 0;
     Trade t = new Trade();
     PortfolioManager pm = new PortfolioManager();  // this test might need to initialize the rules
     pm.ProcessBuyTrade(t, "GOOG", 10, 2.5, pos, port);
     Assert.AreEqual("GOOG", t.SymbolName);
     Assert.AreEqual(10, t.quantity);
     Assert.AreEqual(2.5, t.price);
     Assert.AreEqual(tradeTypes.Buy, t.type);
     Assert.AreEqual(3, t.PositionId);
     Assert.AreEqual(10, pos.quantity);
     Assert.AreEqual(25, pos.price);
     Assert.AreEqual(975, port.Cash);
 }
Ejemplo n.º 5
0
        public void TestSell()
        {
            Symbol s = new Symbol("GOOG");
            Position pos = new Position();
            Trade t = new Trade();
            t.Status = tradeStatus.Active;
            t.type = tradeTypes.Buy;
            t.Position = pos;
            t.Symbol = s;
            t.timestamp = new DateTime(2013, 6, 1);
            t.price = 10;
            t.quantity = 5;

            Trade sell = t.sell(2);
            Assert.AreEqual(s, sell.Symbol);
            Assert.AreEqual(pos, sell.Position);
            Assert.AreEqual(tradeStatus.Closed, sell.Status);
            Assert.AreEqual(tradeTypes.Sell, sell.type);
            Assert.AreEqual(2, sell.quantity);
            Assert.AreNotEqual(t.timestamp, sell.timestamp);
            Assert.AreEqual(t, sell.RelatedTrade);
        }
Ejemplo n.º 6
0
        public Trade sell(int sellQuantity)
        {
            if (sellQuantity < 1 || sellQuantity > this.quantity) throw new ArgumentException("sellQuantity");
            //if (this.type == tradeTypes.Sell) throw new NotImplementedException();
            //if (this.Status == tradeStatus.Closed) throw new NotImplementedException();

            Trade t = new Trade();
            t.Symbol = Symbol;
            t.RelatedTrade = this;
            t.Position = Position;
            t.type = tradeTypes.Sell;
            t.Status = tradeStatus.Closed;
            t.timestamp = DateTime.Now;
            t.quantity = sellQuantity;

            this.quantity -= sellQuantity;
            if (this.quantity == 0) this.Status = tradeStatus.Closed;
            return t;
        }
Ejemplo n.º 7
0
        public void EnoughFundsRuleNo()
        {
            Portfolio p = new Portfolio();
            p.Cash = 1;

            Trade t = new Trade();
            t.type = tradeTypes.Buy;
            t.quantity = 2;
            t.price = 10.25;

            EnoughFundsRule r = new EnoughFundsRule();
            Assert.Throws<InsufficientFunds>(() => r.Apply(p, t));
        }
Ejemplo n.º 8
0
        public void EnoughFundsRuleYes()
        {
            Portfolio p = new Portfolio();
            p.Cash = 5000;

            Trade t = new Trade();
            t.type = tradeTypes.Buy;
            t.quantity = 2;
            t.price = 10.25;

            EnoughFundsRule r = new EnoughFundsRule();
            Assert.IsTrue(r.Apply(p, t));
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            Console.WriteLine("Testing the entity framework...");
            TraderContext db = new TraderContext();

            Console.WriteLine("Clearing the database...");
            var d_quotes = from q in db.Quotes select q;
            foreach (var i in d_quotes) db.Quotes.Remove(i);
            var d_symbol = from s in db.Symbols select s;
            foreach (var i in d_symbol) db.Symbols.Remove(i);
            var d_position = from p in db.Positions select p;
            foreach (var i in d_position) db.Positions.Remove(i);
            var d_trade = from t in db.Trades select t;
            foreach (var i in d_trade) db.Trades.Remove(i);

            Console.WriteLine("Adding records to database...");
            db.Symbols.Add(new Symbol("GOOG"));
            db.Symbols.Add(new Symbol("LNC"));

            db.SaveChanges();

            Quote q1 = new Quote();
            q1.timestamp = DateTime.Now;
            q1.price = 50.47;
            q1.Symbol = db.Symbols.First();
            db.Quotes.Add(q1);

            Position p1 = new Position();
            p1.SymbolName = "GOOG";
            p1.price = 5000;
            p1.quantity = 150;
            p1.status = positionStatus.Open;
            db.Positions.Add(p1);

            db.SaveChanges();

            Trade t1 = new Trade();
            t1.timestamp = DateTime.Now;
            t1.quantity = 40;
            t1.price = 10.10;
            t1.type = tradeTypes.Buy;
            t1.SymbolName = "GOOG";
            db.Trades.Add(t1);

            var q_addtopos = from p in db.Positions.Include("Trades") where p.SymbolName == "GOOG" select p;
            Position goog_pos = q_addtopos.FirstOrDefault();
            goog_pos.Trades.Add(t1);

            db.SaveChanges();

            WatchList w = new WatchList();
            w.ListName = "Default";
            db.WatchLists.Add(w);
            db.SaveChanges();

            WatchListItem wi1 = new WatchListItem();
            wi1.ListName = "Default";
            wi1.SymbolName = "GOOG";
            db.WatchListItems.Add(wi1);

            WatchListItem wi2 = new WatchListItem();
            wi2.ListName = "Default";
            wi2.SymbolName = "LNC";
            db.WatchListItems.Add(wi2);

            db.SaveChanges();

            Console.WriteLine("Retrieving records from database...");
            Console.WriteLine("\nSymbols");
            var query = from s in db.Symbols orderby s.name select s;
            foreach (var i in query)
            {
                Console.WriteLine("Symbol: " + i.name);
            }

            Console.WriteLine("\nQuotes");
            var q_search = from q in db.Quotes select q;
            foreach (var i in q_search)
            {
                Console.WriteLine("Record: " + i.QuoteId.ToString());
                Console.WriteLine("Symbol: " + i.Symbol.name);
                Console.WriteLine("Timestamp: " + i.timestamp.ToString());
                Console.WriteLine("Price: " + i.price.ToString());
            }

            Console.WriteLine("\nPositions");
            var p_search = from p in db.Positions select p;
            foreach (var i in p_search)
            {
                Console.WriteLine("Record: " + i.PositionId.ToString());
                Console.WriteLine("Symbol: " + i.Symbol.name);
                Console.WriteLine("Trade count: " + i.Trades.Count.ToString());
                foreach (var t in i.Trades)
                {
                    Console.WriteLine("\tTrade Id: " + t.TradeId.ToString());
                    Console.WriteLine("\tType: " + t.type.ToString());
                    Console.WriteLine("\tPrice: " + t.price.ToString());
                    Console.WriteLine("\tQuantity: " + t.quantity.ToString());
                    Console.WriteLine("\tTimestamp: " + t.timestamp.ToString());
                }
            }

            Console.WriteLine("\nWatchlist");
            var wlist = from l in db.WatchLists.Include("Items") select l;
            WatchList deflist = wlist.FirstOrDefault();
            foreach (var i in deflist.Items)
            {
                Console.WriteLine("\t" + i.Symbol.name);
            }
            Console.WriteLine("\nTesting complete.");
            Console.ReadLine();
            db.Dispose();
        }
Ejemplo n.º 10
0
 // builds a new Trade object to reflect the requested transaction, updates the position and portfolio
 public void ProcessBuyTrade(Trade t, string symbolName, int quantity, double price, Position pos, Portfolio port)
 {
     t.type = tradeTypes.Buy;
     t.SymbolName = symbolName;
     t.quantity = quantity;
     t.timestamp = DateTime.Now;
     t.price = price;
     ApplyRules(port, t);
     t.PositionId = pos.PositionId;
     pos.quantity += t.quantity;
     pos.price += t.price * t.quantity;
     port.Cash -= (t.price * t.quantity) - t.PaidCommission;
 }