void dofillupdate(ref structSTITradeUpdate t) { Trade f = new TradeImpl(); f.symbol = t.bstrSymbol; f.Account = t.bstrAccount; long id = 0; if (long.TryParse(t.bstrClOrderId, out id)) { f.id = id; } else { f.id = t.nOrderRecordId; } f.xprice = (decimal)t.fExecPrice; f.xsize = t.nQuantity; long now = Convert.ToInt64(t.bstrUpdateTime); int xsec = (int)(now % 100); long rem = (now - xsec) / 100; f.side = t.bstrSide == "B"; f.xtime = ((int)(rem % 10000)) * 100 + xsec; f.xdate = (int)((now - f.xtime) / 1000000); f.ex = t.bstrDestination; pt.Adjust(f); tl.newFill(f); if (VerboseDebugging) { debug("new trade sent: " + f.ToString() + " " + f.id); } }
public void MaxDD() { PositionTracker pt = new PositionTracker(); const string sym = "TST"; System.Collections.Generic.List <TradeLink.API.Trade> fills = new System.Collections.Generic.List <TradeLink.API.Trade>(); TradeImpl t = new TradeImpl(sym, 10, 100); System.Collections.Generic.List <decimal> ret = new System.Collections.Generic.List <decimal>(); fills.Add(t); pt.Adjust(t); t = new TradeImpl(sym, 11, -100); fills.Add(t); ret.Add(pt.Adjust(t)); t = new TradeImpl(sym, 11, -100); pt.Adjust(t); fills.Add(t); t = new TradeImpl(sym, 13, 100); fills.Add(t); ret.Add(pt.Adjust(t)); decimal maxdd = Calc.MaxDDVal(ret.ToArray()); decimal maxddp = Calc.MaxDDPct(fills); Assert.AreEqual(-300, maxdd); Assert.AreEqual(-.18m, Math.Round(maxddp, 2)); }
private void TradeHandler(object sender, TradeArgs e) { itemTrade itrade = e.ItemTrade; // Order o = new OrderImpl(iorder.msecsym, iorder.IsBuyOrder(), iorder.mqty, Convert.ToDecimal(iorder.mprice), Convert.ToDecimal(iorder.mstopprice), "", iorder.mc_date, iorder.mc_date, iorder.morderid); Trade trade = new TradeImpl(); trade.symbol = itrade.msecsym; itemOrder lorder = socketOrderServer.sitemOrder.FindItem(itrade.morderid); if (lorder == null) { return; } trade.side = lorder.IsBuyOrder(); trade.xprice = Convert.ToDecimal(itrade.mprice); trade.xsize = itrade.mqty; DateTime mdate = ComFucs.GetDate(itrade.mm_date); trade.Account = ""; trade.xdate = mdate.Day + mdate.Month * 100 + mdate.Year * 10000; trade.xtime = mdate.Second + mdate.Minute * 100 + mdate.Hour * 10000; tl.newFill(trade); v(trade.symbol + " received fill ack for: " + trade.ToString()); }
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); }
public void Adjust() { string s = "IBM"; ForexSecurity ts = new ForexSecurity(s); IAccount account = new SimAccount("TEST", "testing", 1000M, 100, "SIM"); account.Securities.AddSecurity(ts); TradeImpl t1 = new TradeImpl(s, 100, 100); t1.Account = account; t1.Security = ts; PositionTracker pt = new PositionTracker(account); // make we have no position yet Assert.True(pt[t1.Symbol].IsFlat); // send some adjustments decimal cpl = 0; cpl += pt.Adjust(t1); cpl += pt.Adjust(t1); // verify that adjustments took hold Assert.Equal(0, cpl); Assert.Equal(200, pt[t1.Symbol].Size); }
public void InitAndAdjust() { string sym = "IBM"; ForexSecurity ts = new ForexSecurity(sym); IAccount account = new SimAccount("TEST"); account.Securities.AddSecurity(ts); // startup position tracker PositionTracker pt = new PositionTracker(account); PositionTracker pt2 = new PositionTracker(account); // give pt our initial position PositionImpl init = new PositionImpl(ts, 0, 0, 0, account); pt.Adjust(init); pt2.Adjust(init); // fill a trade in both places TradeImpl fill = new TradeImpl(ts.Name, 100, 100); fill.Account = account; fill.Security = ts; pt.Adjust(fill); pt2.Adjust(fill); // make sure it's only 100 in both places Assert.Equal(100, pt[sym].Size); Assert.Equal(100, pt2[sym].Size); }
public void Defaults() { TradeImpl t = new TradeImpl(); Assert.That(!t.isValid, t.ToString()); Assert.That(!t.isFilled, t.ToString()); }
public void Defaults() { TradeImpl t = new TradeImpl(); t.IsValid.Should().BeFalse(t.ToString()); t.IsFilled.Should().BeFalse(t.ToString()); }
public void SerializeDeserialize() { // create object string sym = "TST"; decimal price = 10; int size = 100; DateTime date = DateTime.Now; TradeImpl t = new TradeImpl(sym, price, size, date); long magicid = 555; t.id = magicid; t.Exchange = "NYMEX"; // serialize it for transmission string msg = TradeImpl.Serialize(t); // deserialize it string threwexception = null; Trade newtrade = null; try { newtrade = TradeImpl.Deserialize(msg); } catch (Exception ex) { threwexception = ex.ToString(); } Assert.That(threwexception == null, threwexception); Assert.That(newtrade.isFilled, newtrade.ToString()); Assert.That(newtrade.isValid, newtrade.ToString()); Assert.That(newtrade.symbol == sym, newtrade.symbol); Assert.That(newtrade.xprice == price, newtrade.xprice.ToString()); Assert.That(newtrade.xdate != 0); Assert.That(newtrade.xtime != 0); Assert.That(newtrade.xsize == size); Assert.That(newtrade.id == magicid); Assert.AreEqual(newtrade.ex, t.Exchange); }
protected void Setup() { lp = new PositionImpl(stock, entry, lsize); sp = new PositionImpl(stock, entry, ssize); //closing trades lc = new TradeImpl(stock, last, lsize / -2); sc = new TradeImpl(stock, last, -ssize); }
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); }
public void MultipleAccount() { // setup defaults for 1st and 2nd accounts and positions string sym = "TST"; string a1 = "account1"; string a2 = "account2"; int s1 = 300; int s2 = 500; decimal p = 100m; // create position tracker PositionTracker pt = new PositionTracker(); // set initial position in 1st account pt.Adjust(new PositionImpl(sym, p, s1, 0, a1)); // set initial position in 2nd account pt.Adjust(new PositionImpl(sym, p, s2, 0, a2)); // verify I can query default account and it's correct Assert.AreEqual(s1, pt[sym].Size); // change default to 2nd account pt.DefaultAccount = a2; // verify I can query default and it's correct Assert.AreEqual(s2, pt[sym].Size); // verify I can query 1st account and correct Assert.AreEqual(s1, pt[sym, a1].Size); // verify I can query 2nd account and correct Assert.AreEqual(s2, pt[sym, a2].Size); // get fill in sym for 1st account TradeImpl f = new TradeImpl(sym, p, s1); f.Account = a1; pt.Adjust(f); // get fill in sym for 2nd account TradeImpl f2 = new TradeImpl(sym, p, s2); f2.Account = a2; pt.Adjust(f2); // verify that I can querry 1st account and correct Assert.AreEqual(s1 * 2, pt[sym, a1].Size); // verify I can query 2nd account and correct Assert.AreEqual(s2 * 2, pt[sym, a2].Size); // reset pt.Clear(); // ensure I can query first and second account and get flat symbols Assert.AreEqual(0, pt[sym].Size); Assert.AreEqual(0, pt[sym, a1].Size); Assert.AreEqual(0, pt[sym, a2].Size); Assert.IsTrue(pt[sym, a1].isFlat); Assert.IsTrue(pt[sym, a2].isFlat); Assert.IsTrue(pt[sym].isFlat); Assert.AreEqual(string.Empty, pt.DefaultAccount); }
public override void FillReport(OrderFillReport oReport) { Trade t = new TradeImpl(oReport.Symbol, (decimal)oReport.FillPrice, oReport.FillSize); t.id = Convert.ToInt64(oReport.Tag); t.ex = oReport.Exchange; t.Account = oReport.Account.AccountId; t.xdate = Util.ToTLDate(); t.xtime = Util.ToTLTime(); tl.newFill(t); }
public TestCalc() { ForexSecurity ls = new ForexSecurity(stock); lp = new PositionImpl(ls, entry, lsize); sp = new PositionImpl(ls, entry, ssize); //closing trades lc = new TradeImpl(ls.Name, last, lsize / -2); sc = new TradeImpl(ls.Name, last, -ssize); }
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)); }
public override void GotFill(Trade fill) { base.GotFill(fill); // make sure every fill is tracked against a position track_positions.Adjust(fill); // chart fills sendchartlabel(fill.xprice, time, TradeImpl.ToChartLabel(fill), fill.side ? System.Drawing.Color.Green : System.Drawing.Color.Red); //senddebug("GotFill(): sym: " + fill.symbol + " size:" + fill.xsize + " price: " + fill.xprice + " time: " + fill.xtime + " side: " + fill.side + " id: " + fill.id); }
// we're reading these values from file, // bc it's faster than recalculating each time public static TradeResult Init(string resultline) { string[] res = resultline.Split(','); TradeResult r = new TradeResult(); r.Source = TradeImpl.FromString(resultline); r.OpenPL = Convert.ToDecimal(res[s]); r.ClosedPL = Convert.ToDecimal(res[s + 1]); r.OpenSize = Convert.ToInt32(res[s + 2]); r.ClosedSize = Convert.ToInt32(res[s + 3]); r.AvgPrice = Convert.ToDecimal(res[s + 4]); return(r); }
public void Construction() { TradeImpl t = new TradeImpl("TST", 10, 100, DateTime.Now); Assert.That(t.isValid, t.ToString()); Assert.That(t.isFilled, t.ToString()); //midnight check t.xdate = 20081205; t.xtime = 0; Assert.That(t.isValid); t.xtime = 0; t.xdate = 0; Assert.That(!t.isValid); }
public void Construction() { TradeImpl t = new TradeImpl("TST", 10, 100, DateTime.Now); t.IsValid.Should().BeTrue(t.ToString()); t.IsFilled.Should().BeTrue(t.ToString()); //midnight check t.Xdate = 20081205; t.Xtime = 0; t.IsValid.Should().BeTrue(); t.Xtime = 0; t.Xdate = 0; t.IsValid.Should().BeFalse(); }
public void ClosedPL() { const string sym = "RYN"; PositionTracker pt = new PositionTracker(); Position p = new PositionImpl(sym, 44.39m, 800, 0); pt.Adjust(p); System.IO.StreamReader sr = new System.IO.StreamReader("TestPositionClosedPL.txt"); string[] file = sr.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); foreach (string line in file) { Trade t = TradeImpl.FromString(line); pt.Adjust(t); } Assert.AreEqual(-66, pt[sym].ClosedPL); }
protected virtual int OnStockExecMsgTrade(UInt32 hStock, GTSession.GTTrade32 trade) { // map a trade object Trade t = new TradeImpl(trade.szStock, (decimal)trade.dblExecPrice, trade.nExecShares); // map remaining fields t.Account = trade.szAccountID; t.id = (uint)trade.dwTicketNo; t.side = trade.chExecSide == 'B'; t.xdate = trade.nExecDate; t.xtime = trade.nExecTime; // notify clients tl.newFill(t); return(0); }
public override void GotFill(Trade fill) { // make sure every fill is tracked against a position track_positions.Adjust(fill); // chart fills sendchartlabel(fill.xprice, time, TradeImpl.ToChartLabel(fill), fill.side ? System.Drawing.Color.Green : System.Drawing.Color.Red); senddebug("GotFill(): sym: " + fill.symbol + " size:" + fill.xsize + " price: " + fill.xprice + " time: " + fill.xtime + " side: " + fill.side + " id: " + fill.id); // get index for this symbol //int idx = _wait.getindex(fill.symbol); // ignore unknown symbols //if (idx < 0) return; // stop waiting //_wait[fill.symbol] = false; }
void m_Session_OnExecutionMessage(object sender, BWExecution executionMsg) { foreach (KeyValuePair <long, int> ordID in _bwOrdIds) { if (ordID.Value == executionMsg.OrderID) { Trade t = new TradeImpl(executionMsg.Symbol, (decimal)executionMsg.Price, executionMsg.Size); t.side = (executionMsg.Side == ORDER_SIDE.SIDE_COVER) || (executionMsg.Side == ORDER_SIDE.SIDE_BUY); t.xtime = TradeLink.Common.Util.DT2FT(executionMsg.ExecutionTime); t.xdate = TradeLink.Common.Util.ToTLDate(executionMsg.ExecutionTime); t.Account = executionMsg.UserID.ToString(); t.id = ordID.Key; t.ex = executionMsg.MarketMaker; tl.newFill(t); } } }
public override void GotFill(Trade fill) { // make sure every fill is tracked against a position pt.Adjust(fill); // get index for this symbol int idx = _wait.getindex(fill.symbol); // ignore unknown symbols if (idx < 0) { return; } // stop waiting _wait[fill.symbol] = false; // chart fills sendchartlabel(fill.xprice, time, TradeImpl.ToChartLabel(fill), fill.side ? System.Drawing.Color.Green : System.Drawing.Color.Red); }
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); }
public void FillTests() { // no executions yet Assert.That(fills == 0, fills.ToString()); // have to subscribe to a stock to get notified on fills for said stock c.Subscribe(new BasketImpl(new SecurityImpl(SYM))); // prepare and send an execution from client to server TradeImpl t = new TradeImpl(SYM, 100, 300, DateTime.Now); s.newFill(t); // make sure client received and counted it Assert.That(fills == 1, fills.ToString()); // make sure fill was copied Assert.AreEqual(fills, copyfills); }
public void RoundTurnStat() { rt = new Results(); // get some trades Trade[] fills = new TradeImpl[] { // go long new TradeImpl(sym, p, s), // increase bet new TradeImpl(sym, p + inc, s * 2), // take some profits new TradeImpl(sym, p + inc * 2, s * -1), // go flat (round turn) new TradeImpl(sym, p + inc * 2, s * -2), // go short new TradeImpl(sym, p, s * -2), // decrease bet new TradeImpl(sym, p, s), // exit (round turn) new TradeImpl(sym, p + inc, s), // do another entry new TradeImpl(sym, p, s) }; // compute results foreach (Trade fill in fills) { rt.GotFill(fill); } rt = rt.FetchResults(); // check trade count Assert.AreEqual(fills.Length, rt.Trades, "trade is missing from results"); // check round turn count Assert.AreEqual(2, rt.RoundTurns, "missing round turns"); // verify trade winners Assert.AreEqual(2, rt.Winners, "missing trade winner"); // verify round turn winners Assert.AreEqual(1, rt.RoundWinners, "missing round turn winners"); // verify round turn losers Assert.AreEqual(1, rt.RoundLosers, "missing round turn loser"); }
public void MaxDD() { const string sym = "TST"; SimAccount account = new SimAccount("TEST", "testing", 1000M, 100); ForexSecurity sec = new ForexSecurity(sym); sec.PipValue = 1; sec.LotSize = 1; sec.PipSize = 1; account.Securities.AddSecurity(sec); IPositionTracker pt = account.Positions; System.Collections.Generic.List <Trade> fills = new System.Collections.Generic.List <Trade>(); TradeImpl t = new TradeImpl(sym, 10, 100); t.Security = sec; t.Account = account; System.Collections.Generic.List <decimal> ret = new System.Collections.Generic.List <decimal>(); fills.Add(t); pt.Adjust(t); t = new TradeImpl(sym, 11, -100); t.Security = sec; t.Account = account; fills.Add(t); ret.Add(pt.Adjust(t)); t = new TradeImpl(sym, 11, -100); t.Security = sec; t.Account = account; pt.Adjust(t); fills.Add(t); t = new TradeImpl(sym, 13, 100); t.Account = account; t.Security = sec; fills.Add(t); ret.Add(pt.Adjust(t)); decimal maxdd = Calc.MaxDdVal(ret.ToArray()); decimal maxddp = Calc.MaxDDPct(fills); Assert.Equal(-300, maxdd); Assert.Equal(-.18m, Math.Round(maxddp, 2)); }
public void InitAndAdjust() { const string sym = "IBM"; // startup position tracker PositionTracker pt = new PositionTracker(); PositionTracker pt2 = new PositionTracker(); // give pt our initial position Position init = new PositionImpl(sym, 0, 0); pt.Adjust(init); pt2.Adjust(init); // fill a trade in both places Trade fill = new TradeImpl(sym, 100, 100); pt.Adjust(fill); pt2.Adjust(fill); // make sure it's only 100 in both places Assert.AreEqual(100, pt[sym].Size); Assert.AreEqual(100, pt2[sym].Size); }
public void Adjust() { const string s = "IBM"; TradeImpl t1 = new TradeImpl(s, 100, 100); PositionTracker pt = new PositionTracker(); // make we have no position yet Assert.IsTrue(pt[t1.symbol].isFlat); // send some adjustments decimal cpl = 0; cpl += pt.Adjust(t1); cpl += pt.Adjust(t1); // verify that adjustments took hold Assert.AreEqual(0, cpl); Assert.AreEqual(200, pt[t1.symbol].Size); }