Beispiel #1
0
 public void AlwaysEnter()
 {
     Always b = new Always();
     b.AllowMultipleOrders = true;
     Assert.That(b.MinSize == 100);
     int good = 0;
     int i = 0;
     Order o = new Order();
     o = b.Trade(timesales[i++], new BarList(), new Position(s), new BoxInfo());
     if (o.isValid)
         good++;
     Assert.That(b.Turns == 0);
     Assert.That(b.Adjusts == 0);
     o = b.Trade(timesales[i++], new BarList(), new Position(s), new BoxInfo());
     if (o.isValid)
         good++;
     Assert.That(b.Turns == 0);
     Assert.That(b.Adjusts == 1);
     o = b.Trade(timesales[i++], new BarList(), new Position(s), new BoxInfo());
     if (o.isValid)
         good++;
     Assert.That(b.Turns == 0);
     Assert.That(b.Adjusts == 2);
     o = b.Trade(timesales[i++], new BarList(), new Position(s), new BoxInfo());
     if (o.isValid)
         good++;
     Assert.That(b.Turns == 0);
     Assert.That(b.Adjusts == 3);
     // first trade was pre-market so we only have 3 total;
     Assert.That(good == 3);
     // no debugs were sent
     Assert.That(debugs == 0);
 }
Beispiel #2
0
 public void Defaults()
 {
     // assert that a default order is:
     // not valid, not filled
     Order o = new Order();
     Assert.That(!o.isValid);
     Assert.That(!o.isFilled);
 }
Beispiel #3
0
 public void MarketOrder()
 {
     const string s = "SYM";
     Order o = new Order(s,100);
     Assert.That(o.isValid);
     Assert.That(o.isMarket);
     Assert.That(!o.isLimit);
     Assert.That(!o.isStop);
     Assert.That(!o.isFilled);
     Assert.That(o.symbol == s);
 }
 public void newOrder(Order o, bool allclients)
 {
     if (this.InvokeRequired)
         this.Invoke(new tlneworderdelegate(newOrder), new object[] { o,allclients });
     else
     {
         for (int i = 0; i < client.Count; i++)
             if ((client[i] != null) && (client[i] != "") && (stocks[i].Contains(o.symbol) || allclients))
                 WMUtil.SendMsg(o.Serialize(), TL2.ORDERNOTIFY, Handle, client[i]);
     }
 }
Beispiel #5
0
        public void SerializationAndDeserialization()
        {
            // create an order
            const string s = "TST";
            const string x = "NYSE";
            const string a = "ACCOUNT";
            const string u = "COMMENT";
            const string ot = "GTC";
            const decimal p = 10;
            const int z = 100;
            const Currency c = Currency.USD;
            const SecurityType t = SecurityType.STK;
            Order o = new Order(s, z);
            o.sec = 2;
            o.date = 20080718;
            o.time = 948;
            o.price = p;
            o.Account = a;
            o.Exchange = x;
            o.Currency = c;
            o.Security = t;
            o.comment = u;
            o.TIF = ot;
            // convert it to a message
            string msg = o.Serialize();

            // convert it back to an object and validate nothing was lost
            string exception=null;
            Order n = new Order();
            try
            {
                n = Order.Deserialize(msg);
            }
            catch (Exception ex) { exception = ex.ToString(); }
            Assert.That(exception==null, msg+" "+exception);
            Assert.That(n.Account == a,n.Account);
            Assert.That(n.symbol == s,n.symbol);
            Assert.That(n.size == z,n.size.ToString());
            Assert.That(n.price == p,n.price.ToString());
            Assert.That(n.Exchange == x,n.Exchange);
            Assert.That(n.comment == u,n.comment);
            Assert.That(n.Security == t,n.Security.ToString());
            Assert.That(n.Currency == c,n.Currency.ToString());
            Assert.That(n.TIF == ot, n.TIF);
            Assert.That(n.date == o.date, n.date.ToString());
            Assert.That(n.time == o.time, n.time.ToString());
            Assert.That(n.sec == o.sec, n.sec.ToString());
        }
Beispiel #6
0
        public void OrderTests()
        {
            // no orders yet
            Assert.That(orders == 0, orders.ToString());
            // no fill requests yet
            Assert.That(fillrequest == 0, fillrequest.ToString());

            // client wants to buy 100 TST at market
            Order o = new Order("TST", 100);
            // if it works it'll return zero
            int error = c.SendOrder(o);
            Assert.That(error==0,error.ToString());
            // client should have received notification that an order entered his account
            Assert.That(orders == 1, orders.ToString());
            // server should have gotten a request to fill an order
            Assert.That(fillrequest == 1, fillrequest.ToString());
        }
Beispiel #7
0
 public Order BestBidOrOffer(string sym, bool side,Account Account)
 {
     Order best = new Order();
     if (!MasterOrders.ContainsKey(Account)) return best;
     foreach (Order o in MasterOrders[Account])
     {
         if (o.symbol != sym) continue;
         if (o.Side != side) continue;
         if (!best.isValid)
         {
             best = new Order(o);
             continue;
         }
         Order test = BestBidOrOffer(best, o);
         if (test.isValid) best = new Order(test);
     }
     return best;
 }
Beispiel #8
0
 public Order BestBidOrOffer(string symbol,bool side)
 {
     Order best = new Order();
     Order next = new Order();
     foreach (Account a in MasterOrders.Keys)
     {
         // get our first order
         if (!best.isValid)
         {
             // if we don't have a valid one yet, check this account
             best = new Order(BestBidOrOffer(symbol,side,a));
             continue;  // keep checking the accounts till we find a valid one
         }
         // now we have our first order, which will be best if we can't find a second one
         next = new Order(BestBidOrOffer(symbol,side,a));
         if (!next.isValid) continue; // keep going till we have a second order
         best = BestBidOrOffer(best, next); // when we have two, compare and get best
         // then keep fetching next valid order to see if it's better
     }
     return best; // if there's no more orders left, this is best
 }
Beispiel #9
0
 public void Basics()
 {
     Broker broker = new Broker();
     broker.GotFill += new FillDelegate(broker_GotFill);
     broker.GotOrder += new OrderDelegate(broker_GotOrder);
     broker.GotWarning += new DebugDelegate(broker_GotWarning);
     Order o = new Order();
     uint failsoninvalid= broker.sendOrder(o);
     Assert.That(failsoninvalid==0);
     Assert.That(warn == 1);
     Assert.That(orders == 0);
     Assert.That(fills == 0);
     o = new BuyMarket(s, 100);
     Assert.That(broker.sendOrder(o)>0);
     Assert.That(orders == 1);
     Assert.That(fills == 0);
     Assert.That(broker.Execute(Tick.NewTrade(s,10,200)) == 1);
     Assert.That(fills == 1);
     // no warnings since first warning
     Assert.That(warn == 1);
 }
Beispiel #10
0
        public Ticket(Order working)
        {
            InitializeComponent();
            work = working;
            if (work.Security == SecurityType.FUT)
            {
                osize.Increment = 1;
                osize.Value = 1;
            }


            isize = work.UnSignedSize;
            Text = work.symbol;

            osize.Text = work.ToString();
            oprice.Text = work.Price.ToString();
            if (work.Side) { obuybut.Checked = true; osellbut.Checked = false; }
            else { osellbut.Checked = true; obuybut.Checked = false; }
            oprice.MouseWheel += new MouseEventHandler(order_MouseWheel);
            
            osize.MouseWheel += new MouseEventHandler(osize_MouseWheel);
        }
Beispiel #11
0
 public Order(Order copythis)
 {
     this.symbol = copythis.symbol;
     this.stopp = copythis.stopp;
     this.comment = copythis.comment;
     this.cur = copythis.cur;
     this.accountid = copythis.accountid;
     this.date = copythis.date;
     this.ex = copythis.ex;
     this.price = copythis.price;
     this.Security = copythis.Security;
     this.side = copythis.side;
     this.size = copythis.size;
     this.time = copythis.time;
     this.LocalSymbol = copythis.LocalSymbol;
     this.id = copythis.id;
     this.TIF = copythis.TIF;
     // shouldn't be used but we'll take them anyways
     this.xdate = copythis.xdate;
     this.xprice = copythis.xprice;
     this.xsec = copythis.xsec;
     this.xsize = copythis.xsize;
     this.xtime = copythis.xtime;
 }
Beispiel #12
0
 void tl_gotOrder(Order o)
 {
     if (orderidx(o.id)==-1) // if we don't have this order, add it
         ordergrid.Rows.Add(new object[] { o.id, o.symbol, (o.Side ? "BUY" : "SELL"),o.UnSignedSize, (o.price == 0 ? "Market" : o.price.ToString()), (o.stopp==0 ? "" : o.stopp.ToString()), o.Account });
 }
Beispiel #13
0
 void t_neworder(Order sendOrder)
 {
     int res = tl.SendOrder(sendOrder);
     if (res != 0)
     {
         string err = Util.PrettyError(tl.BrokerName,res);
         status(err);
         show(sendOrder.ToString() + "( " + err + " )");
     }
 }
Beispiel #14
0
 void rightticket(object sender, EventArgs e)
 {
     Security s = GetVisibleSecurity(qg.CurrentRowIndex);
     if (s.Type == SecurityType.IDX) return;
     string sym = s.Symbol;
     Order o = new Order(sym,-1*tl.PosSize(sym));
     o.Exchange = s.DestEx;
     o.Security = s.Type;
     o.LocalSymbol = sym;
     Ticket t = new Ticket(o);
     
     t.neworder += new QuotopiaOrderDel(t_neworder);
     spillTick +=new TickDelegate(t.newTick);
     orderStatus+=new OrderStatusDel(t.orderStatus);
     System.Drawing.Point p = new System.Drawing.Point(MousePosition.X, MousePosition.Y);
     p.Offset(-315, 20);
     t.SetDesktopLocation(p.X, p.Y);
     t.Show();
 }
Beispiel #15
0
 void mybroker_GotOrder(Order o)
 {
     if (orders.InvokeRequired)
         Invoke(new OrderDelegate(mybroker_GotOrder), new object[] { o });
     else
     {
         orders.Rows.Add(o.date, o.time, o.symbol, o.isMarket ? "Mkt" : (o.isStop ? "Stp" : "Lmt"), o.size, o.side, o.price, o.stopp, o.comment);
         orders.AutoResizeColumns();
     }
 }
Beispiel #16
0
 void broker_GotOrder(Order o)
 {
     orders++;
 }
Beispiel #17
0
        // event handlers

        void tl_gotSrvFillRequest(Order o)
        {
            s.newOrder(o);
            fillrequest++;
        }
Beispiel #18
0
        void kadinamain_KadTick(Tick t)
        {
            if ((t.sym == "") || (t.sym!=stock.Symbol)) return;
            // get stats prior to execution
            decimal cpl = broker.GetClosedPL(t.sym);
            decimal cpt = broker.GetClosedPT(t.sym);
            int x = broker.Execute(t);
            if (x != 0) // mark tick/row if an execution happened
                xrows.Add(dt.Rows.Count - 1);

            nowtime = t.time.ToString() + ":" + t.sec.ToString();

            if (bl == null) 
                bl = new BarList(BarInterval.FiveMin, t.sym);
            bl.AddTick(t);
            Order o = new Order();
            Position mypos = broker.GetOpenPosition(t.sym);
            if (mybox != null)
            {
                o = mybox.Trade(t, bl, mypos, BoxInfo.FromBarList(bl));
                mybox_IndicatorUpdate(mybox.Indicators);
            }

            if (o.isValid) // mark tick/row if an order happened
                orows.Add(dt.Rows.Count - 1);
            broker.sendOrder(o);
            string flags = "";
            flags += o.isValid ? "O" : "";
            flags += x > 0 ? "X" : "";
            
            // tick grid
            NewTRow(new object[] { nowtime, t.trade, t.size, t.bid, t.ask, t.bs, t.os, flags });
            // position grid
            if (x != 0)
            {
                // get difference between stats pre and stats post
                cpl = broker.GetClosedPL(t.sym) - cpl;
                cpt = broker.GetClosedPT(t.sym) - cpt;
                ptab.Rows.Add(nowtime, (mypos.Flat ? "FLAT" : (mypos.Side ? "LONG" : "SHORT")), mypos.Size, mypos.AvgPrice, cpl.ToString("C2"),cpt.ToString("N1"));
            }
        }
Beispiel #19
0
 /// <summary>
 /// Sends the order to the broker for a specific account.
 /// </summary>
 /// <param name="o">The order to be sent.</param>
 /// <param name="a">the account to send with the order.</param>
 /// <returns>order id if order was accepted, zero otherwise</returns>
 public uint sendOrder(Order o,Account a)
 {
     if ((!o.isValid) || (!a.isValid))
     {
         if (GotWarning != null)
             GotWarning(!o.isValid ? "Invalid order: " + o.ToString() : "Invalid Account" + a.ToString());
         return 0;
     }
     AddOrder(o, a);
     if ((GotOrder != null) && a.Notify) 
         GotOrder(o);
     return o.id;
 }
Beispiel #20
0
        /// <summary>
        /// Sends the order to the broker. (uses the default account)
        /// </summary>
        /// <param name="o">The order to be send.</param>
        /// <returns>true if the order was accepted.</returns>
        public uint sendOrder(Order o) 
        { 
            if (o.Account=="")
                return sendOrder(o, DEFAULT);
            return sendOrder(o, new Account(o.Account));

        }
Beispiel #21
0
 protected void AddOrder(Order o,Account a) 
 {
     if (!a.isValid) throw new Exception("Invalid account provided"); // account must be good
     if (!MasterOrders.ContainsKey(a))  // see if we have a book for this account
         MasterOrders.Add(a,new List<Order>()); // if not, create one
     o.Account = a.ID; // make sure order knows his account
     if (o.id == 0) // if order id isn't set, set it
         o.id = _nextorderid++;
     MasterOrders[a].Add(o); // record the order
 }
Beispiel #22
0
            protected override Order ReadOrder(Tick tick, BarList bl, BoxInfo boxinfo)
            {
                Order o = new Order();

                if (orders == 0)
                {
                    o = new LimitOrder(s, true, 200, 10);
                }
                if (orders==1)
                    CancelOrders(true);
                if (o.isValid)
                    orders++;
                return o;
            }
Beispiel #23
0
 public int SendOrder(Order order)
 {
     throw new Exception("The method or operation is not implemented.");
 }
Beispiel #24
0
 public bool Equals(Order o)
 {
     if (o == null) return false;
     bool r = true;
     r &= price == o.price;
     r &= stopp == o.stopp;
     r &= symbol == o.symbol;
     r &= accountid == o.accountid;
     r &= ex == o.ex;
     r &= TIF == o.TIF;
     r &= LocalSymbol == o.LocalSymbol;
     r &= SignedSize == o.SignedSize;
     r &= Security == o.Security;
     return r;
 }
Beispiel #25
0
 void tlclient_gotOrder(Order o)
 {
     orders++;
 }
 public void newOrder(Order o) { newOrder(o, false); }
Beispiel #27
0
 void broker_GotOrder(Order o)
 {
     ot.Rows.Add(o.time, (o.side ? "BUY" : "SELL"), o.size, o.price);
 }
Beispiel #28
0
        // takes two orders and returns the better one
        // if orders aren't for same side or symbol or not limit, returns invalid order
        // if orders are equally good, adds them together
        public Order BestBidOrOffer(Order first,Order second)
        {
            if ((first.symbol!= second.symbol) || (first.side!=second.side) || !first.isLimit || !second.isLimit)
                return new Order(); // if not comparable return an invalid order
            if ((first.side && (first.price > second.price)) || // if first is better, use it
                (!first.side && (first.price < second.price)))
                return new Order(first);
            else if ((first.side && (first.price < second.price)) || // if second is better, use it
                (!first.side && (first.price > second.price)))
                return new Order(second);

            // if order is matching then add the sizes
            Order add = new Order(first);
            add.size = add.UnSignedSize + second.UnSignedSize * (add.Side ? 1 : -1);
            return add;
        }
 /// <summary>
 /// Sends the order.
 /// </summary>
 /// <param name="o">The oorder</param>
 /// <returns>Zero if succeeded, Broker error code otherwise.</returns>
 public int SendOrder(Order o)
 {
     if (o == null) return (int)TL2.GOTNULLORDER;
     if (!o.isValid) return (int)TL2.OK;
     string m = o.Serialize();
     return (int)TLSend(TL2.SENDORDER, m);
 }
Beispiel #30
0
 /// <summary>
 /// Deserialize string to Order
 /// </summary>
 /// <returns></returns>
 public new static Order Deserialize(string message)
 {
     string[] rec = message.Split(','); // get the record
     bool side = Convert.ToBoolean(rec[(int)OrderField.Side]);
     int size = Convert.ToInt32(rec[(int)OrderField.Size]);
     decimal oprice = Convert.ToDecimal(rec[(int)OrderField.Price]);
     decimal ostop = Convert.ToDecimal(rec[(int)OrderField.Stop]);
     string sym = rec[(int)OrderField.Symbol];
     Order o = new Order(sym, side, size);
     o.price = oprice;
     o.stopp = ostop;
     o.comment = rec[(int)OrderField.Comment];
     o.Account = rec[(int)OrderField.Account];
     o.Exchange = rec[(int)OrderField.Exchange];
     o.LocalSymbol = rec[(int)OrderField.LocalSymbol];
     o.Currency = (Currency)Enum.Parse(typeof(Currency), rec[(int)OrderField.Currency]);
     o.Security = (SecurityType)Enum.Parse(typeof(SecurityType), rec[(int)OrderField.Security]);
     o.id = Convert.ToUInt32(rec[(int)OrderField.OrderID]);
     o.TIF = rec[(int)OrderField.OrderTIF];
     try
     {
         o.date = Convert.ToInt32(rec[(int)OrderField.oDate]);
         o.time = Convert.ToInt32(rec[(int)OrderField.oTime]);
         o.sec = Convert.ToInt32(rec[(int)OrderField.oSec]);
     }
     catch (Exception) { } 
     return o;
 }