public void ClosePL() { decimal pl = .98m; Assert.That(BoxMath.ClosePT(lp, lc) == pl); Assert.That(BoxMath.ClosePT(sp, sc) == -pl); Assert.That(BoxMath.ClosePL(lp, lc) == pl * (lsize / 2)); // matches closing size Assert.That(BoxMath.ClosePL(sp, sc) == pl * ssize); }
public void OpenPL() { decimal pl = .98m; Assert.That(BoxMath.OpenPT(last, entry, Long) == pl); Assert.That(BoxMath.OpenPT(last, entry, Short) == -pl); Assert.That(BoxMath.OpenPT(last, entry, lsize) == pl); Assert.That(BoxMath.OpenPT(last, entry, ssize) == -pl); Assert.That(BoxMath.OpenPT(last, lp) == pl); Assert.That(BoxMath.OpenPT(last, sp) == -pl); Assert.That(BoxMath.OpenPL(last, lp) == lp.Size * pl); Assert.That(BoxMath.OpenPL(last, sp) == sp.Size * pl); }
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)); }
const decimal s = -.11m; // stop loss // here are the trading rules that implement our strategy's intention protected override int Read(Tick tick, BarList bl, BoxInfo bi) { // indicator setup bars = bl; if (!bl.Has(2)) { return(0); // must have at least one one bar } // asymmetry tests if (((h - o) > a) && ((o - l) > a)) { Shutdown("Not enough Asymetry to trade."); return(0); } if ((h - l) < r) { return(0); // must have the range } if (((h - o) <= a) && ((h - tick.trade) > e)) { return(MaxSize); } if (((o - l) <= a) && ((tick.trade - l) > e)) { return(MaxSize * -1); } // profit and loss tests decimal PL = BoxMath.OpenPT(tick.trade, AvgPrice, PosSize); if (PL > p) { return(Flat); } if (PL < s) { return(Flat); } return(0); }
decimal tl_gotSrvAcctOpenPLRequest(string s) { // make sure broker exists if (h == null) { return(0); } // prepare the account we're getting open pl for string acct = s == "" ? Broker.DEFAULTBOOK : s; // get trades from this account List <Trade> fills = h.SimBroker.GetTradeList(new Account(acct)); // setup storage for positions we'll create from trades Dictionary <string, Position> posdict = new Dictionary <string, Position>(); // go through every trade and populate the position foreach (Trade t in fills) { Position p = null; if (!posdict.TryGetValue(t.symbol, out p)) { posdict.Add(t.symbol, new Position(t)); } else { posdict[t.symbol].Adjust(t); } } // for every-non flat position, calculate the pl and add to the total decimal totalopenpl = 0; foreach (Position p in posdict.Values) { if (!p.Flat) { totalopenpl += BoxMath.OpenPL(last[p.Symbol], p); } } return(totalopenpl); }