Пример #1
0
        public void SerializeDeserialize()
        {
            // create object
            string sym = "TST";
            decimal price = 10;
            int size = 100;
            DateTime date = DateTime.Now;
            Trade t = new Trade(sym, price, size, date);
            uint magicid = 555;
            t.id = magicid;
            // serialize it for transmission
            string msg = t.Serialize();
            // deserialize it
            string threwexception = null;
            Trade newtrade = null;
            try
            {
                newtrade = Trade.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);
        }
Пример #2
0
 // these are for calculating closed pl
 // they do not adjust positions themselves
 /// <summary>
 /// Gets the closed PL on a per-share basis, ignoring how many shares are held.
 /// </summary>
 /// <param name="existing">The existing position.</param>
 /// <param name="closing">The portion of the position that's being closed/changed.</param>
 /// <returns></returns>
 public static decimal ClosePT(Position existing, Trade adjust)
 {
     if (!existing.isValid || !adjust.isValid) 
         throw new Exception("Invalid position provided. (existing:" + existing.ToString() + " adjustment:" + adjust.ToString());
     if (existing.Flat) return 0; // nothing to close
     if (existing.Side == adjust.Side) return 0; // if we're adding, nothing to close
     return existing.Side ? adjust.Price - existing.Price : existing.Price - adjust.Price;
 }
Пример #3
0
        protected void Setup()
        {
            lp = new Position(stock, entry, lsize);
            sp = new Position(stock, entry, ssize);

            //closing trades
            lc = new Trade(stock, last, lsize / -2);
            sc = new Trade(stock, last, -ssize);
        }
Пример #4
0
        void tl_gotFill(Trade t)
        {
            if(debugon.Checked)
                Debug(t.ToString());

            count++;
            if (!poslist.ContainsKey(t.symbol)) poslist.Add(t.symbol, new Position(t.symbol));
            poslist[t.symbol].Adjust(t);
            fillstatus("Fills: " + count);
        }
Пример #5
0
 public Trade(Trade copytrade)
 {
     // copy constructor, for copying using by-value (rather than by default of by-reference)
     id = copytrade.id;
     cur = copytrade.cur;
     type = copytrade.type;
     ex = copytrade.ex;
     accountid = copytrade.accountid;
     symbol = copytrade.symbol;
     side = copytrade.side;
     size = copytrade.size;
     price = copytrade.price;
     stopp = copytrade.stopp;
     comment = copytrade.comment;
     date = copytrade.date;
     time = copytrade.time;
     xsize = copytrade.xsize;
     xprice = copytrade.xprice;
     xsec = copytrade.xsec;
     xtime = copytrade.xtime;
     xdate = copytrade.xdate;
 }
Пример #6
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));
 }
Пример #7
0
 public void Construction()
 {
     Trade t = new Trade("TST",10,100,DateTime.Now);
     Assert.That(t.isValid,t.ToString());
     Assert.That(t.isFilled,t.ToString());
 }
Пример #8
0
 /// <summary>
 /// Deserialize string to Trade
 /// </summary>
 /// <returns></returns>
 public static Trade Deserialize(string message)
 {
     string[] rec = message.Split(',');
     bool side = Convert.ToBoolean(rec[(int)TradeField.Side]);
     int size = Convert.ToInt32(rec[(int)TradeField.Size]);
     size = Math.Abs(size) * (side ? 1 : -1);
     decimal xprice = Convert.ToDecimal(rec[(int)TradeField.Price]);
     string sym = rec[(int)TradeField.Symbol];
     Trade t = new Trade(sym, xprice, size);
     t.xdate = Convert.ToInt32(rec[(int)TradeField.xDate]);
     t.xtime = Convert.ToInt32(rec[(int)TradeField.xTime]);
     t.xsec = Convert.ToInt32(rec[(int)TradeField.xSeconds]);
     t.comment = rec[(int)TradeField.Comment];
     t.Account = rec[(int)TradeField.Account];
     t.LocalSymbol = rec[(int)TradeField.LocalSymbol];
     t.id = Convert.ToUInt32(rec[(int)TradeField.ID]);
     try
     {
         t.Currency = (Currency)Enum.Parse(typeof(Currency), rec[(int)TradeField.Currency]);
         t.Security = (SecurityType)Enum.Parse(typeof(SecurityType), rec[(int)TradeField.Security]);
     }
     catch (Exception) { }
     return t;
 }
Пример #9
0
 public static Trade FromString(string tradestring, char delimiter)
 {
     string[] rec = tradestring.Split(delimiter);
     Trade t = new Trade(rec[2], rec[3] == "BUY", Convert.ToInt32(rec[4]), Convert.ToDecimal(rec[5]), 0, rec[6], Convert.ToInt32(rec[1]), Convert.ToInt32(rec[0]));
     t.Fill(t.price);
     return t;
 }
Пример #10
0
 void broker_GotFill(Trade t)
 {
     ft.Rows.Add(t.xtime.ToString() + "." + t.xsec.ToString(), (t.Side ? "BUY" : "SELL"),t.xsize, t.xprice);
 }
Пример #11
0
 /// <summary>
 /// Gets the closed PL on a position basis, the PL that is registered to the account for the entire shares transacted.
 /// </summary>
 /// <param name="existing">The existing position.</param>
 /// <param name="closing">The portion of the position being changed/closed.</param>
 /// <returns></returns>
 public static decimal ClosePL(Position existing, Trade adjust)
 {
     return ClosePT(existing, adjust) * Math.Abs(adjust.Size);
 }
Пример #12
0
        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 MarketBasket(new Stock("TST")));

            // prepare and send an execution from client to server
            Trade t = new Trade("TST", 100, 300, DateTime.Now);
            s.newFill(t);

            // make sure client received and counted it
            Assert.That(fills == 1, fills.ToString());
        }
Пример #13
0
 void tlclient_gotFill(Trade t)
 {
     fills++;
 }
Пример #14
0
 public void newFill(Trade t)
 {
     for (int i = 0; i < client.Count; i++) // send tick to each client that has subscribed to tick's stock
         if ((client[i] != null) && (stocks[i].Contains(t.symbol)))
             TLSend(client[i], new LinkMessage(TL2.EXECUTENOTIFY, t));
 }
Пример #15
0
 public Position(Trade t) 
 {
     if (!t.isValid) throw new Exception("Can't construct a position object from invalid trade.");
     sym = t.symbol; price = t.xprice; size = t.xsize; date = t.xdate; time = t.time; sec = t.xsec;
     if (size>0) size *= t.side ? 1 : -1;
 }
Пример #16
0
 void broker_GotFill(Trade t)
 {
     fills++;
 }
Пример #17
0
 public void Defaults()
 {
     Trade t = new Trade();
     Assert.That(!t.isValid, t.ToString());
     Assert.That(!t.isFilled, t.ToString());
 }
Пример #18
0
 void tl_gotFill(Trade t)
 {
     if (InvokeRequired)
         Invoke(new FillDelegate(tl_gotFill), new object[] { t });
     else
     {
         if (!t.isValid) return;
         int oidx = orderidx(t.id); // get order id for this order
         if (oidx != -1)
         {
             int osign = (t.side ? 1 : -1);
             int signedtsize = t.xsize * osign;
             int signedosize = (int)ordergrid["osize", oidx].Value;
             if (signedosize == signedtsize) // if sizes are same whole order was filled, remove
                 ordergrid.Rows.RemoveAt(oidx);
             else // otherwise remove portion that was filled and leave rest on order
                 ordergrid["osize", oidx].Value = Math.Abs(signedosize - signedtsize) * osign;
         }
         int[] rows = GetSymbolRows(t.symbol);
         int size = tl.PosSize(t.symbol);
         decimal price = tl.AvgPrice(t.symbol);
         for (int i = 0; i < rows.Length; i++)
         {
             qt.Rows[rows[i]]["PosSize"] = size.ToString();
             qt.Rows[rows[i]]["AvgPrice"] = price.ToString("N2");
         }
         TradesView.Rows.Add(t.xdate, t.xtime, t.xsec, t.symbol, (t.side ? "BUY" : "SELL"), t.xsize, t.xprice.ToString("N2"), t.comment, t.Account.ToString()); // if we accept trade, add it to list
     }
 }
Пример #19
0
 /// <summary>
 /// Notifies subscribed clients of a new execution.
 /// </summary>
 /// <param name="trade">The trade to include in the notification.</param>
 public void newFill(Trade trade) { newFill(trade, false); }
Пример #20
0
 void mybroker_GotFill(Trade t)
 {
     if (trades.InvokeRequired)
         Invoke(new FillDelegate(mybroker_GotFill), new object[] { t });
     else
     {
         trades.Rows.Add(t.xdate, t.xtime, t.symbol, t.xsize, t.side, t.xprice, t.comment);
         trades.AutoResizeColumns();
     }
 }
Пример #21
0
 public void newFill(Trade trade,bool allclients)
 {
     if (this.InvokeRequired)
         this.Invoke(new tlnewfilldelegate(newFill), new object[] { trade,allclients });
     else
     {
         // make sure our trade is filled and initialized properly
         if (!trade.isValid || !trade.isFilled) return;
         for (int i = 0; i < client.Count; i++) // send tick to each client that has subscribed to tick's stock
             if ((client[i] != null) && ((stocks[i].Contains(trade.symbol) || allclients)))
                 WMUtil.SendMsg(trade.Serialize(), TL2.EXECUTENOTIFY, Handle, client[i]);
     }
 }
Пример #22
0
 /// <summary>
 /// Adjusts the position by applying a new trade or fill.
 /// </summary>
 /// <param name="t">The fill to apply to this position.</param>
 /// <returns></returns>
 public decimal Adjust(Trade t) { return Adjust(new Position(t)); }
Пример #23
0
 void SimBroker_GotFill(Trade t)
 {
     tl.newFill(t,true);
 }