Ejemplo n.º 1
0
        public void PositionAccountTest()
        {
            ForexSecurity ts      = new ForexSecurity(s);
            IAccount      account = new SimAccount("ME");

            account.Securities.AddSecurity(ts);

            TradeImpl t = new TradeImpl("TST", 100, 100);

            t.Account  = account;
            t.Security = ts;
            TradeImpl t2 = new TradeImpl("TST", 200, 200);

            Assert.True(t.IsValid);
            Assert.True(t2.IsValid);
            t2.AccountName = "HIM";
            PositionImpl p = new PositionImpl(t);

            p.Adjust(t);
            bool failed = false;

            try
            {
                p.Adjust(t2);
            }
            catch (Exception) { failed = true; }
            Assert.True(failed);
        }
Ejemplo n.º 2
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));
        }
Ejemplo n.º 3
0
        public void UsingTrades()
        {
            // long
            string        s  = "IBM";
            ForexSecurity ts = new ForexSecurity(s);

            ts.LotSize  = 1;
            ts.PipValue = 1;
            ts.PipSize  = 1;

            PortfolioManager portfolio = new TradingPortfolio();
            IAccount         account   = new SimAccount("TEST");

            portfolio.SetAccount(account);
            portfolio.Securities.AddSecurity(ts);

            TradeImpl t1 = new TradeImpl(s, 80, 100, dt);

            t1.Account  = account;
            t1.Security = ts;
            PositionImpl p = new PositionImpl(t1);

            Assert.True(p.IsLong);
            Assert.True(p.Size == 100);

            TradeImpl t2 = new TradeImpl(s, 84, -100, dt);

            t2.Account  = account;
            t2.Security = ts;

            decimal pl = p.Adjust(t2);

            Assert.True(p.IsFlat);
            Assert.Equal((84 - 80) * 100, pl);

            // short
            TradeImpl t3 = new TradeImpl(s, 84, -100, dt);

            t3.Account  = account;
            t3.Security = ts;

            p = new PositionImpl(t3);
            Assert.True(!p.IsLong);
            Assert.True(p.Size == -100);

            TradeImpl t4 = new TradeImpl(s, 80, 100, dt);

            t4.Account  = account;
            t4.Security = ts;

            pl = p.Adjust(new TradeImpl(t4));
            Assert.True(pl == (84 - 80) * 100);
            Assert.True(p.IsFlat);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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(ISecurity symbol, SimAccount a)
        {
            PositionImpl 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.Name)
                {
                    pos.Adjust(trades[i]);
                }
            }
            return(pos);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        public void Basics()
        {
            PositionImpl p = new PositionImpl(ls, 0, 0, 0, new SimAccount("TST"));

            //Set security options
            ls.LotSize  = 100;
            ls.PipValue = 2;
            ls.PipSize  = 1;

            Assert.Equal(0, p.Size);
            Assert.True(p.Security.Name != "", "hassymbol");
            Assert.Equal(0, p.AvgPrice);
            Assert.True(p.IsFlat, "isflat");
            Assert.True(p.IsValid, "isvalid");
            PositionImpl p2     = new PositionImpl(ls, 10, 100, 0);
            PositionImpl p2copy = new PositionImpl(p2);

            Assert.Equal(p2.AvgPrice, p2copy.AvgPrice);
            Assert.Equal(p2.Size, p2copy.Size);
            Assert.Equal(p2.GrossPnL, p2copy.GrossPnL);
            Assert.Equal(p2.Security, p2copy.Security);
            p.Adjust(p2);
            Assert.True(p.Size == 100);
            Assert.True(p.Security.Name != "", "hassymbol");
            Assert.True(p.AvgPrice == 10);
            Assert.False(p.IsFlat);
            Assert.True(p.IsLong);
            Assert.True(p.IsValid);
            bool         invalidexcept = false;
            PositionImpl p3            = null;

            try
            {
                p3 = new PositionImpl(ls, 0, 100, 0);
            }
            catch
            {
                invalidexcept = true;
            }
            Assert.True(invalidexcept);
            p3 = new PositionImpl(ls, 12, 100, 0);
            p.Adjust(p3);
            Assert.Equal(11, p.AvgPrice);
            Assert.True(p.IsLong);
            Assert.True(p.IsValid);
            Assert.True(!p.IsFlat);
            Assert.True(p.Size == 200);
            p.Adjust(new TradeImpl(s, 13, -100, dt)
            {
                Security = ls, Account = p.Account
            });
            Assert.True(p.AvgPrice == 11);
            Assert.True(p.IsLong);
            Assert.True(p.IsValid);
            Assert.True(!p.IsFlat);
            Assert.True(p.Size == 100);
            TradeImpl lasttrade = new TradeImpl(s, 12, -100, dt);
            decimal   profitFromP2toLASTTRADE = Calc.ClosePL(p2, lasttrade);

            Assert.True(profitFromP2toLASTTRADE == (lasttrade.Xprice - p2.AvgPrice) * Math.Abs(lasttrade.Xsize / ls.LotSize) * ls.PipValue);
        }