Example #1
0
 public void Basics()
 {
     PositionImpl p = new PositionImpl(s);
     Assert.AreEqual(0,p.Size);
     Assert.That(p.Symbol!="","hassymbol");
     Assert.AreEqual(0,p.AvgPrice);
     Assert.That(p.isFlat,"isflat");
     Assert.That(p.isValid,"isvalid");
     PositionImpl p2 = new PositionImpl(s, 10, 100,0);
     PositionImpl p2copy = new PositionImpl(p2);
     Assert.AreEqual(p2.AvgPrice, p2copy.AvgPrice);
     Assert.AreEqual(p2.Size, p2copy.Size);
     Assert.AreEqual(p2.ClosedPL, p2copy.ClosedPL);
     Assert.AreEqual(p2.Symbol, p2copy.Symbol);
     p.Adjust(p2);
     Assert.That(p.Size == 100);
     Assert.IsTrue(p.Symbol!="", "hassymbol");
     Assert.That(p.AvgPrice == 10);
     Assert.IsFalse(p.isFlat);
     Assert.IsTrue(p.isLong);
     Assert.IsTrue(p.isValid);
     bool invalidexcept = false;
     PositionImpl p3 = null;
     try
     {
         p3 = new PositionImpl(s, 0, 100, 0);
     }
     catch
     {
         invalidexcept = true;
     }
     Assert.That(invalidexcept);
     p3 = new PositionImpl(s, 12, 100,0);
     p.Adjust(p3);
     Assert.AreEqual(11,p.AvgPrice);
     Assert.That(p.isLong);
     Assert.That(p.isValid);
     Assert.That(!p.isFlat);
     Assert.That(p.Size == 200);
     p.Adjust(new TradeImpl(s, 13, -100,dt));
     Assert.That(p.AvgPrice == 11);
     Assert.That(p.isLong);
     Assert.That(p.isValid);
     Assert.That(!p.isFlat);
     Assert.That(p.Size == 100);
     TradeImpl lasttrade = new TradeImpl(s, 12, -100,dt);
     decimal profitFromP2toLASTTRADE = Calc.ClosePL(p2, lasttrade);
     Assert.That(profitFromP2toLASTTRADE == (lasttrade.xprice-p2.AvgPrice)*Math.Abs(lasttrade.xsize));
 }
Example #2
0
        public void PositionAccountTest()
        {
            TradeImpl t = new TradeImpl("TST", 100, 100);
            t.Account = "ME";
            TradeImpl t2 = new TradeImpl("TST", 200, 200);
            Assert.That(t.isValid);
            Assert.That(t2.isValid);
            t2.Account = "HIM";
            PositionImpl p = new PositionImpl(t);
            p.Adjust(t);
            bool failed = false;            try
            {
                p.Adjust(t2);
            }
            catch (Exception) { failed = true; }
            Assert.IsTrue(failed);

        }
Example #3
0
        /// <summary>
        /// Gets the closed points (points = PL on per-share basis) for given symbol/account.
        /// </summary>
        /// <param name="symbol">The symbol.</param>
        /// <param name="account">The account.</param>
        /// <returns>points</returns>
        public decimal GetClosedPT(string symbol, Account account)
        {
            PositionImpl pos    = new PositionImpl(symbol);
            decimal      points = 0;

            if (!MasterTrades.ContainsKey(account.ID))
            {
                return(points);
            }
            foreach (TradeImpl t in MasterTrades[account.ID])
            {
                points += Calc.ClosePT(pos, t);
                pos.Adjust(t);
            }
            return(points);
        }
Example #4
0
        /// <summary>
        /// Gets the open position for the specified account.
        /// </summary>
        /// <param name="symbol">The symbol to get a position for.</param>
        /// <param name="a">the account.</param>
        /// <returns>current position</returns>
        public Position GetOpenPosition(string symbol, Account a)
        {
            Position pos = new PositionImpl(symbol);

            if (!MasterTrades.ContainsKey(a.ID))
            {
                return(pos);
            }
            foreach (TradeImpl trade in MasterTrades[a.ID])
            {
                if (trade.symbol == symbol)
                {
                    pos.Adjust(trade);
                }
            }
            return(pos);
        }
Example #5
0
        /// <summary>
        /// Gets the closed points (points = PL on per-share basis) for given symbol/account.
        /// </summary>
        /// <param name="symbol">The symbol.</param>
        /// <param name="account">The account.</param>
        /// <returns>points</returns>
        public decimal GetClosedPT(string symbol, Account account)
        {
            PositionImpl pos    = new PositionImpl(symbol);
            decimal      points = 0;

            if (!MasterTrades.ContainsKey(account.ID))
            {
                return(points);
            }
            List <Trade> trades = MasterTrades[account.ID];

            for (int i = 0; i < trades.Count; i++)
            {
                points += Calc.ClosePT(pos, trades[i]);
                pos.Adjust(trades[i]);
            }
            return(points);
        }
Example #6
0
        /// <summary>
        /// Gets the closed PL for a particular symbol and brokerage account.
        /// </summary>
        /// <param name="symbol">The symbol.</param>
        /// <param name="a">The Account.</param>
        /// <returns>Closed PL</returns>
        public decimal GetClosedPL(string symbol, Account a)
        {
            Position pos = new PositionImpl(symbol);
            decimal  pl  = 0;

            if (!MasterTrades.ContainsKey(a.ID))
            {
                return(pl);
            }
            foreach (Trade trade in MasterTrades[a.ID])
            {
                if (trade.symbol == pos.Symbol)
                {
                    pl += pos.Adjust(trade);
                }
            }
            return(pl);
        }
Example #7
0
        /// <summary>
        /// Gets the open position for the specified account.
        /// </summary>
        /// <param name="symbol">The symbol to get a position for.</param>
        /// <param name="a">the account.</param>
        /// <returns>current position</returns>
        public Position GetOpenPosition(string symbol, Account a)
        {
            Position pos = new PositionImpl(symbol);

            if (!MasterTrades.ContainsKey(a.ID))
            {
                return(pos);
            }
            List <Trade> trades = MasterTrades[a.ID];

            for (int i = 0; i < trades.Count; i++)
            {
                if (trades[i].symbol == symbol)
                {
                    pos.Adjust(trades[i]);
                }
            }
            return(pos);
        }
Example #8
0
        /// <summary>
        /// Gets the closed PL for a particular symbol and brokerage account.
        /// </summary>
        /// <param name="symbol">The symbol.</param>
        /// <param name="a">The Account.</param>
        /// <returns>Closed PL</returns>
        public decimal GetClosedPL(string symbol, Account a)
        {
            Position pos = new PositionImpl(symbol);
            decimal  pl  = 0;

            if (!MasterTrades.ContainsKey(a.ID))
            {
                return(pl);
            }
            List <Trade> trades = MasterTrades[a.ID];

            for (int i = 0; i < trades.Count; i++)
            {
                if (trades[i].symbol == pos.symbol)
                {
                    pl += pos.Adjust(trades[i]);
                }
            }
            return(pl);
        }
Example #9
0
 /// <summary>
 /// Gets the closed points (points = PL on per-share basis) for given symbol/account.
 /// </summary>
 /// <param name="symbol">The symbol.</param>
 /// <param name="account">The account.</param>
 /// <returns>points</returns>
 public decimal GetClosedPT(string symbol, Account account)
 {
     PositionImpl pos = new PositionImpl(symbol);
     decimal points = 0;
     if (!MasterTrades.ContainsKey(account.ID)) return points;
     List<Trade> trades = MasterTrades[account.ID];
     for (int i = 0; i < trades.Count; i++)
     {
         points += Calc.ClosePT(pos, trades[i]);
         pos.Adjust(trades[i]);
     }
     return points;
 }
Example #10
0
 /// <summary>
 /// Gets the closed PL for a particular symbol and brokerage account.
 /// </summary>
 /// <param name="symbol">The symbol.</param>
 /// <param name="a">The Account.</param>
 /// <returns>Closed PL</returns>
 public decimal GetClosedPL(string symbol, Account a)
 {
     Position pos = new PositionImpl(symbol);
     decimal pl = 0;
     if (!MasterTrades.ContainsKey(a.ID)) return pl;
     List<Trade> trades = MasterTrades[a.ID];
     for (int i = 0; i < trades.Count; i++)
     {
         if (trades[i].symbol == pos.Symbol)
             pl += pos.Adjust(trades[i]);
     }
     return pl;
 }
Example #11
0
 /// <summary>
 /// Gets the open position for the specified account.
 /// </summary>
 /// <param name="symbol">The symbol to get a position for.</param>
 /// <param name="a">the account.</param>
 /// <returns>current position</returns>
 public Position GetOpenPosition(string symbol,Account a)
 {
     Position pos = new PositionImpl(symbol);
     if (!MasterTrades.ContainsKey(a.ID)) return pos;
     List<Trade> trades = MasterTrades[a.ID];
     for (int i = 0; i<trades.Count; i++)
         if (trades[i].symbol==symbol) 
             pos.Adjust(trades[i]);
     return pos;
 }
Example #12
0
 public void UsingTrades()
 {
     // long
     PositionImpl p = new PositionImpl(new TradeImpl(s, 80, 100,dt));
     Assert.That(p.isLong);
     Assert.That(p.Size == 100);
     decimal pl = p.Adjust(new TradeImpl(s, 84, -100,dt));
     Assert.That(p.isFlat);
     Assert.AreEqual((84 - 80) * 100,pl);
     // short
     pl = 0;
     p = new PositionImpl(new TradeImpl(s, 84, -100,dt));
     Assert.That(!p.isLong);
     Assert.That(p.Size == -100);
     pl = p.Adjust(new TradeImpl(s, 80, 100,dt));
     Assert.That(pl == (84 - 80) * 100);
     Assert.That(p.isFlat);
 }
Example #13
0
        public void FlipSideInOneTrade()
        {
            // this is illegal on the exchanges, but supported by certain
            // retail brokers so we're going to allow tradelink to support it
            // BE CAREFUL WITH THIS FEATURE.  make sure you won't be fined for doing this, before you do it.
            string s = "IBM";
            // long position
            PositionImpl p = new PositionImpl(s, 100m,200);
            // sell more than we've got to change sides
            TradeImpl flip = new TradeImpl(s, 99, -400);
            decimal cpl = p.Adjust(flip);
            // make sure we captured close of trade
            Assert.AreEqual(-200, cpl); 
            // make sure we captured new side and price
            Assert.AreEqual(-200, p.Size);
            Assert.AreEqual(99, p.AvgPrice);

        }
Example #14
0
 /// <summary>
 /// Gets the closed points (points = PL on per-share basis) for given symbol/account.
 /// </summary>
 /// <param name="symbol">The symbol.</param>
 /// <param name="account">The account.</param>
 /// <returns>points</returns>
 public decimal GetClosedPT(string symbol, Account account)
 {
     PositionImpl pos = new PositionImpl(symbol);
     decimal points = 0;
     if (!MasterTrades.ContainsKey(account.ID)) return points;
     foreach (TradeImpl t in MasterTrades[account.ID])
     {
         points += Calc.ClosePT(pos, t);
         pos.Adjust(t);
     }
     return points;
 }
Example #15
0
 /// <summary>
 /// Gets the closed PL for a particular symbol and brokerage account.
 /// </summary>
 /// <param name="symbol">The symbol.</param>
 /// <param name="a">The Account.</param>
 /// <returns>Closed PL</returns>
 public decimal GetClosedPL(string symbol, Account a)
 {
     Position pos = new PositionImpl(symbol);
     decimal pl = 0;
     if (!MasterTrades.ContainsKey(a.ID)) return pl;
     foreach (Trade trade in MasterTrades[a.ID])
     {
         if (trade.symbol == pos.Symbol)
             pl += pos.Adjust(trade);
     }
     return pl;
 }
Example #16
0
 /// <summary>
 /// Gets the open position for the specified account.
 /// </summary>
 /// <param name="symbol">The symbol to get a position for.</param>
 /// <param name="a">the account.</param>
 /// <returns>current position</returns>
 public Position GetOpenPosition(string symbol,Account a)
 {
     Position pos = new PositionImpl(symbol);
     if (!MasterTrades.ContainsKey(a.ID)) return pos;
     foreach (TradeImpl trade in MasterTrades[a.ID]) 
         if (trade.symbol==symbol) 
             pos.Adjust(trade);
     return pos;
 }