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 }
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)); }
void Adjust(Position p) { if (_positions.ContainsKey(p.FullSymbol)) { Position o = _positions[p.FullSymbol]; o.Adjust(p); _positions[p.FullSymbol] = o; } else { _positions.Add(p.FullSymbol, p); } }
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); }
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); }