Beispiel #1
0
 public void Basics()
 {
     Position p = new Position(s);
     Assert.That(p.Size == 0);
     Assert.That(p.hasSymbol);
     Assert.That(p.AvgPrice == 0);
     Assert.That(p.Flat);
     Assert.That(p.isValid);
     Position p2 = new Position(s, 10, 100);
     p.Adjust(p2);
     Assert.That(p.Size == 100);
     Assert.That(p.hasSymbol);
     Assert.That(p.AvgPrice == 10);
     Assert.That(!p.Flat);
     Assert.That(p.Side);
     Assert.That(p.isValid);
     Position p3 = new Position(s, 0, 100);
     Assert.That(!p3.isValid);
     p3 = new Position(s, 10, 0);
     Assert.That(!p3.isValid);
     p3 = new Position(s, 12, 100);
     p.Adjust(p3);
     Assert.That(p.AvgPrice == 11);
     Assert.That(p.Side);
     Assert.That(p.isValid);
     Assert.That(!p.Flat);
     Assert.That(p.Size == 200);
     p.Adjust(new Trade(s, 13, -100,dt));
     Assert.That(p.AvgPrice == 11);
     Assert.That(p.Side);
     Assert.That(p.isValid);
     Assert.That(!p.Flat);
     Assert.That(p.Size == 100);
     Trade lasttrade = new Trade(s, 12, -100,dt);
     decimal profitFromP2toLASTTRADE = BoxMath.ClosePL(p2, lasttrade);
     Assert.That(profitFromP2toLASTTRADE == (lasttrade.xprice-p2.AvgPrice)*Math.Abs(lasttrade.xsize));
 }
Beispiel #2
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)
        {
            Position pos    = new Position(symbol);
            decimal  points = 0;

            if (!MasterTrades.ContainsKey(account.ID))
            {
                return(points);
            }
            foreach (Trade t in MasterTrades[account.ID])
            {
                points += BoxMath.ClosePT(pos, t);
                pos.Adjust(t);
            }
            return(points);
        }
Beispiel #3
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 Position(symbol);

            if (!MasterTrades.ContainsKey(a.ID))
            {
                return(pos);
            }
            foreach (Trade trade in MasterTrades[a.ID])
            {
                if (trade.symbol == symbol)
                {
                    pos.Adjust(trade);
                }
            }
            return(pos);
        }
Beispiel #4
0
 public void UsingTrades()
 {
     // long
     Position p = new Position(new Trade(s, 80, 100,dt));
     Assert.That(p.Side);
     Assert.That(p.Size == 100);
     decimal pl = p.Adjust(new Trade(s, 84, -100,dt));
     Assert.That(p.Flat);
     Assert.That(pl == (84 - 80) * 100);
     // short
     pl = 0;
     p = new Position(new Trade(s, 84, -100,dt));
     Assert.That(!p.Side);
     Assert.That(p.Size == -100);
     pl = p.Adjust(new Trade(s, 80, 100,dt));
     Assert.That(pl == (84 - 80) * 100);
     Assert.That(p.Flat);
 }
Beispiel #5
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 Position(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);
        }
Beispiel #6
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)
 {
     Position pos = new Position(symbol);
     decimal points = 0;
     if (!MasterTrades.ContainsKey(account.ID)) return points;
     foreach (Trade t in MasterTrades[account.ID])
     {
         points += BoxMath.ClosePT(pos, t);
         pos.Adjust(t);
     }
     return points;
 }
Beispiel #7
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 Position(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;
 }
Beispiel #8
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 Position(symbol);
     if (!MasterTrades.ContainsKey(a.ID)) return pos;
     foreach (Trade trade in MasterTrades[a.ID]) 
         if (trade.symbol==symbol) 
             pos.Adjust(trade);
     return pos;
 }
Beispiel #9
0
        public void MaxSizeTest()
        {
            Always b = new Always();
            b.MinSize = 100;
            b.MaxSize = 200;
            Position p = new Position(s);
            Order o = new Order();
            Assert.That(b.MinSize==100);
            Assert.That(b.MaxSize==200);
            for (int i = 0; i < timesales.Length; i++)
            {
                Tick t = new Tick(timesales[i]);
                if (o.isValid && t.isTrade)
                {
                    o.Fill(t);
                    p.Adjust((Trade)o);
                    o = new Order();
                }
                Assert.That(p.Size<=b.MaxSize);
                o = b.Trade(timesales[i], new BarList(), p, new BoxInfo());
            }
            Assert.That(p.Size == 200);

            // Now we'll set maxsize to 100
            b = new Always();
            b.MinSize = 100;
            b.MaxSize = 100;
            p = new Position(s);
            o = new Order();
            Assert.That(b.MinSize == 100);
            Assert.That(b.MaxSize == 100);
            for (int i = 0; i < timesales.Length; i++)
            {
                Tick t = new Tick(timesales[i]);
                if (o.isValid && t.isTrade)
                {
                    o.Fill(t);
                    p.Adjust((Trade)o);
                    o = new Order();
                }
                Assert.That(p.Size <= b.MaxSize);
                o = b.Trade(timesales[i], new BarList(), p, new BoxInfo());
            }
            Assert.That(p.Size == 100);

        }
Beispiel #10
0
 public void ThrottlesTest()
 {
     LimitsTest b = new LimitsTest();
     // we're skipping the first trade bc it's pre-market and we're not testing
     // that in this test
     int i = 1;
     Order o;
     Position p = new Position(s);
     Assert.That(b.Turns == 0);
     Assert.That(b.Adjusts == 0);
     Assert.That(b.TradeCaps);
     Assert.That(!b.Off);
     Assert.That(b.PosSize == 0);
     o = b.Trade(timesales[i++], new BarList(), p, new BoxInfo());
     Assert.That(o.isValid);
     // fill our order with next tick and just our position
     o.Fill(timesales[i]);
     p.Adjust((Trade)o);
     Assert.That(b.Adjusts == 1);
     Assert.That(b.Turns == 0);
     o = b.Trade(timesales[i++], new BarList(), p, new BoxInfo());
     Assert.That(o.isValid);
     Assert.That(b.Adjusts == 2);
     Assert.That(b.Turns == 1); // should be flat now
     o = b.Trade(timesales[i++], new BarList(), new Position(s), new BoxInfo());
     Assert.That(!o.isValid); // no more orders, as
     Assert.That(b.Off); // we should be shutdown
 }