Ejemplo n.º 1
0
 public void Basics()
 {
     Account a = new Account();
     Assert.That(!a.isValid);
     const string myid = "jfranta";
     a = new Account(myid);
     Assert.That(a.isValid);
     Assert.That(a.ID == myid);
 }
Ejemplo n.º 2
0
 public Order BestBidOrOffer(string sym, bool side,Account Account)
 {
     Order best = new OrderImpl();
     if (!MasterOrders.ContainsKey(Account)) return best;
     foreach (OrderImpl o in MasterOrders[Account])
     {
         if (o.symbol != sym) continue;
         if (o.Side != side) continue;
         if (!best.isValid)
         {
             best = new OrderImpl(o);
             continue;
         }
         Order test = BestBidOrOffer(best, o);
         if (test.isValid) best = new OrderImpl(test);
     }
     return best;
 }
Ejemplo n.º 3
0
 public Order BestBidOrOffer(string sym, bool side,Account Account)
 {
     Order best = new OrderImpl();
     if (!MasterOrders.ContainsKey(Account)) return best;
     List<Order> orders = MasterOrders[Account];
     for (int i = 0; i<orders.Count; i++)
     {
         Order o = orders[i];
         if (o.symbol != sym) continue;
         if (o.side != side) continue;
         if (!best.isValid)
         {
             best = new OrderImpl(o);
             continue;
         }
         Order test = BestBidOrOffer(best, o);
         if (test.isValid) best = new OrderImpl(test);
     }
     return best;
 }
Ejemplo n.º 4
0
 public Order BestBidOrOffer(string symbol,bool side)
 {
     Order best = new OrderImpl();
     Order next = new OrderImpl();
     Account[] accts = new Account[MasterOrders.Count];
     MasterOrders.Keys.CopyTo(accts, 0);
     for (int i = 0; i < accts.Length; i++)
     {
         Account a = accts[i];
         // get our first order
         if (!best.isValid)
         {
             // if we don't have a valid one yet, check this account
             best = new OrderImpl(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 OrderImpl(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
 }
Ejemplo n.º 5
0
 protected void AddAccount(Account a)
 {
     Account t = null;
     if (acctlist.TryGetValue(a.ID, out t)) return; // already had it
     MasterOrders.Add(a, new List<Order>());
     MasterTrades.Add(a.ID, new List<Trade>());
     acctlist.Add(a.ID, a);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Executes any open orders allowed by the specified tick.
        /// </summary>
        /// <param name="tick">The tick.</param>
        /// <returns>the number of orders executed using the tick.</returns>
        public int Execute(Tick tick)
        {
            if (_pendorders == 0) return 0;
            if (!tick.isTrade && !_usebidaskfill) return 0;
            int filledorders = 0;
            Account[] accts = new Account[MasterOrders.Count];
            MasterOrders.Keys.CopyTo(accts, 0);
            for (int idx = 0; idx < accts.Length; idx++)
            { // go through each account
                Account a = accts[idx];
                // if account has requested no executions, skip it
                if (!a.Execute) continue;
                // make sure we have a record for this account
                if (!MasterTrades.ContainsKey(a.ID))
                    MasterTrades.Add(a.ID, new List<Trade>());
                // track orders being removed and trades that need notification
                List<int> notifytrade = new List<int>();
                List<int> remove = new List<int>();
                // go through each order in the account
                for (int i = 0; i < MasterOrders[a].Count; i++)
                { 
                    Order o = MasterOrders[a][i];
                    if (tick.symbol != o.symbol) continue; //make sure tick is for the right stock
                    bool filled = false;
                    if (o.TIF == "OPG")
                    {
                        // if it's already opened, we missed our shot
                        if (hasopened.Contains(o.symbol)) continue;
                        // otherwise make sure it's really the opening
                        if (((o.symbol.Length < 4) && (tick.ex.Contains("NYS"))) ||
                            (o.symbol.Length > 3))
                        {
                            // it's the opening tick, so fill it as an opg
                            filled = o.Fill(tick,_usebidaskfill, true);
                            // mark this symbol as already being open
                            hasopened.Add(tick.symbol);
                        }

                    } 
                    else // otherwise fill order normally
                        filled = o.Fill(tick,_usebidaskfill,false); // fill our trade
                    if (filled)
                    {
                        // remove filled size from size available in trade
                        tick.size -= o.UnsignedSize;
                        // get copy of trade for recording
                        Trade trade = new TradeImpl((Trade)o);
                        // if trade represents entire requested order, mark order for removal
                        if (trade.UnsignedSize == o.UnsignedSize)
                            remove.Add(i);
                        else // otherwise reflect order's remaining size
                            o.size = (o.UnsignedSize - trade.UnsignedSize) * (o.side ? 1 : -1);
                        // record trade
                        MasterTrades[a.ID].Add(trade); 
                        // mark it for notification
                        notifytrade.Add(MasterTrades[a.ID].Count-1);
                        // count the trade
                        filledorders++; 
                    }
                }
                int rmcount = remove.Count;
                // remove the filled orders
                for (int i = remove.Count - 1; i >= 0; i--)
                    MasterOrders[a].RemoveAt(remove[i]);
                // unmark filled orders as pending
                _pendorders -= rmcount;
                if (_pendorders < 0) _pendorders = 0;
                // notify subscribers of trade
                if ((GotFill != null) && a.Notify)
                    for (int tradeidx = 0; tradeidx<notifytrade.Count; tradeidx++)
                        GotFill(MasterTrades[a.ID][notifytrade[tradeidx]]); 

            }
            return filledorders;
        }
Ejemplo n.º 7
0
 public void CancelOrders(Account a) 
 {
     if (!MasterOrders.ContainsKey(a)) return;
     foreach (Order o in MasterOrders[a])
         if ((GotOrderCancel != null) && a.Notify)
             GotOrderCancel(o.symbol,o.side,o.id); //send cancel notifcation to any subscribers
     MasterOrders[a].Clear();  // clear the account
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Gets the closed PL for an entire account. (all symbols)
 /// </summary>
 /// <param name="a">The account.</param>
 /// <returns>Closed PL</returns>
 public decimal GetClosedPL(Account a)
 {
     Dictionary<string, Position> poslist = new Dictionary<string, Position>();
     Dictionary<string,decimal> pllist = new Dictionary<string,decimal>();
     if (!MasterTrades.ContainsKey(a.ID)) return 0;
     List<Trade> trades = MasterTrades[a.ID];
     for (int i = 0; i < trades.Count; i++)
     {
         Trade trade = trades[i];
         if (!poslist.ContainsKey(trade.symbol))
         {
             poslist.Add(trade.symbol, new PositionImpl(trade.symbol));
             pllist.Add(trade.symbol, 0);
         }
         pllist[trade.symbol] += poslist[trade.symbol].Adjust(trade);
     }
     decimal pl = 0;
     string[] syms = new string[pllist.Count];
     pllist.Keys.CopyTo(syms, 0);
     for (int i = 0; i<syms.Length; i++)
         pl += pllist[syms[i]];
     return pl;
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Gets the closed Points on a specific account, all symbols.
        /// </summary>
        /// <param name="account">The account.</param>
        /// <returns></returns>
        public decimal GetClosedPT(Account account)
        {
            Dictionary<string, PositionImpl> poslist = new Dictionary<string, PositionImpl>();
            Dictionary<string, decimal> ptlist = new Dictionary<string, decimal>();
            if (!MasterTrades.ContainsKey(account.ID)) return 0;
            List<Trade> trades = MasterTrades[account.ID];
            for (int i = 0; i < trades.Count; i++)
            {
                Trade trade = trades[i];
                if (!poslist.ContainsKey(trade.symbol))
                {
                    poslist.Add(trade.symbol, new PositionImpl(trade.symbol));
                    ptlist.Add(trade.symbol, 0);
                }
                ptlist[trade.symbol] += Calc.ClosePT(poslist[trade.symbol], trade);
                poslist[trade.symbol].Adjust(trade);
            }
            decimal points = 0;
            string[] syms = new string[ptlist.Count];
            ptlist.Keys.CopyTo(syms, 0);
            for (int i = 0; i < syms.Length; i++)
                points += ptlist[syms[i]];
            return points;

        }
Ejemplo n.º 10
0
 /// <summary>
 /// Gets the complete execution list for this account
 /// </summary>
 /// <param name="a">account to request blotter from.</param>
 /// <returns></returns>
 public List<Trade> GetTradeList(Account a) { List<Trade> res; bool worked = MasterTrades.TryGetValue(a.ID, out res); return worked ? res : new List<Trade>(); }
Ejemplo n.º 11
0
 /// <summary>
 /// Gets the open position for the specified account.
 /// </summary>
 /// <param name="symbol">The symbol to get a position for.</param>
 /// <param name="a">the account.</param>
 /// <returns>current position</returns>
 public Position GetOpenPosition(string symbol,Account a)
 {
     Position pos = new PositionImpl(symbol);
     if (!MasterTrades.ContainsKey(a.ID)) return pos;
     List<Trade> trades = MasterTrades[a.ID];
     for (int i = 0; i<trades.Count; i++)
         if (trades[i].symbol==symbol) 
             pos.Adjust(trades[i]);
     return pos;
 }
Ejemplo n.º 12
0
        public void MultiAccount()
        {
            const string sym = "TST";

            const string me = "tester";
            const string other = "anotherguy";
            const int od = 20070926;
            const int ot = 95400;
            Account a = new Account(me);
            Account b = new Account(other);
            Account c = new Account("sleeper");
            OrderImpl oa = new BuyMarket(sym,100);
            OrderImpl ob = new BuyMarket(sym, 100);
            oa.time = ot;
            oa.date = od;
            ob.time = ot;
            ob.date = od;

            oa.Account = me;
            ob.Account = other;
            // send order to account for jfranta
            broker.SendOrderStatus(oa);
            broker.SendOrderStatus(ob);
            TickImpl t = new TickImpl(sym);
            t.trade = 100m;
            t.size = 200;
            t.date = od;
            t.time = ot;
            Assert.AreEqual(2,broker.Execute(t));
            Position apos = broker.GetOpenPosition(sym,a);
            Position bpos = broker.GetOpenPosition(sym,b);
            Position cpos = broker.GetOpenPosition(sym, c);
            Assert.That(apos.isLong);
            Assert.AreEqual(100,apos.Size);
            Assert.That(bpos.isLong);
            Assert.AreEqual(100,bpos.Size);
            Assert.That(cpos.isFlat);
            // make sure that default account doesn't register
            // any trades
            Assert.That(broker.GetOpenPosition(sym).isFlat);
        }
Ejemplo n.º 13
0
 public void CancelOrders() 
 {
     Account[] accts = new Account[MasterOrders.Count];
     MasterOrders.Keys.CopyTo(accts, 0);
     for (int idx = 0; idx < accts.Length; idx++)
         CancelOrders(accts[idx]);
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Gets the open position for the specified account.
 /// </summary>
 /// <param name="symbol">The symbol to get a position for.</param>
 /// <param name="a">the account.</param>
 /// <returns>current position</returns>
 public Position GetOpenPosition(string symbol,Account a)
 {
     Position pos = new PositionImpl(symbol);
     if (!MasterTrades.ContainsKey(a.ID)) return pos;
     foreach (TradeImpl trade in MasterTrades[a.ID]) 
         if (trade.symbol==symbol) 
             pos.Adjust(trade);
     return pos;
 }
Ejemplo n.º 15
0
        /// <summary>
        /// Gets the closed Points on a specific account, all symbols.
        /// </summary>
        /// <param name="account">The account.</param>
        /// <returns></returns>
        public decimal GetClosedPT(Account account)
        {
            Dictionary<string, PositionImpl> poslist = new Dictionary<string, PositionImpl>();
            Dictionary<string, decimal> ptlist = new Dictionary<string, decimal>();
            if (!MasterTrades.ContainsKey(account.ID)) return 0;
            foreach (TradeImpl trade in MasterTrades[account.ID])
            {
                if (!poslist.ContainsKey(trade.symbol))
                {
                    poslist.Add(trade.symbol, new PositionImpl(trade.symbol));
                    ptlist.Add(trade.symbol, 0);
                }
                ptlist[trade.symbol] += Calc.ClosePT(poslist[trade.symbol], trade);
                poslist[trade.symbol].Adjust(trade);
            }
            decimal points = 0;
            foreach (string sym in ptlist.Keys)
                points += ptlist[sym];
            return points;

        }
Ejemplo n.º 16
0
 /// <summary>
 /// Gets the closed points (points = PL on per-share basis) for given symbol/account.
 /// </summary>
 /// <param name="symbol">The symbol.</param>
 /// <param name="account">The account.</param>
 /// <returns>points</returns>
 public decimal GetClosedPT(string symbol, Account account)
 {
     PositionImpl pos = new PositionImpl(symbol);
     decimal points = 0;
     if (!MasterTrades.ContainsKey(account.ID)) return points;
     foreach (TradeImpl t in MasterTrades[account.ID])
     {
         points += Calc.ClosePT(pos, t);
         pos.Adjust(t);
     }
     return points;
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Gets the closed PL for an entire account. (all symbols)
 /// </summary>
 /// <param name="a">The account.</param>
 /// <returns>Closed PL</returns>
 public decimal GetClosedPL(Account a)
 {
     Dictionary<string, Position> poslist = new Dictionary<string, Position>();
     Dictionary<string,decimal> pllist = new Dictionary<string,decimal>();
     if (!MasterTrades.ContainsKey(a.ID)) return 0;
     foreach (TradeImpl trade in MasterTrades[a.ID])
     {
         if (!poslist.ContainsKey(trade.symbol))
         {
             poslist.Add(trade.symbol, new PositionImpl(trade.symbol));
             pllist.Add(trade.symbol, 0);
         }
         pllist[trade.symbol] += poslist[trade.symbol].Adjust(trade);
     }
     decimal pl = 0;
     foreach (string sym in pllist.Keys)
         pl += pllist[sym];
     return pl;
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Gets the closed PL for a particular symbol and brokerage account.
 /// </summary>
 /// <param name="symbol">The symbol.</param>
 /// <param name="a">The Account.</param>
 /// <returns>Closed PL</returns>
 public decimal GetClosedPL(string symbol, Account a)
 {
     Position pos = new PositionImpl(symbol);
     decimal pl = 0;
     if (!MasterTrades.ContainsKey(a.ID)) return pl;
     foreach (Trade trade in MasterTrades[a.ID])
     {
         if (trade.symbol == pos.Symbol)
             pl += pos.Adjust(trade);
     }
     return pl;
 }
Ejemplo n.º 19
0
        public void CancelOrders(Account a) 
        {
            if (!MasterOrders.ContainsKey(a)) return;
            List<Order> orders = MasterOrders[a];
            for (int i = 0; i < orders.Count; i++)
            {
                Order o = orders[i];
                //send cancel notifcation to any subscribers
                if ((GotOrderCancel != null) && a.Notify)
                    GotOrderCancel(o.symbol, o.side, o.id);
                if ((GotOrderCancelEvent != null) && a.Notify)
                    GotOrderCancelEvent(o.id); 

            }
            MasterOrders[a].Clear();  // clear the account
        }
Ejemplo n.º 20
0
 public bool Equals(Account a)
 {
     return this._id.Equals(a.ID);
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Gets the list of open orders for this account.
 /// </summary>
 /// <param name="a">Account.</param>
 /// <returns></returns>
 public List<Order> GetOrderList(Account a) { List<Order> res; bool worked = MasterOrders.TryGetValue(a, out res); return worked ? res : new List<Order>(); }
Ejemplo n.º 22
0
        protected void AddOrder(Order o,Account a) 
        {
            if (!a.isValid) throw new Exception("Invalid account provided"); // account must be good
            if ((FillMode== FillMode.OwnBook) && a.Execute)
            {
                // get best bid or offer from opposite side,
                // see if we can match against this BBO and cross locally
                Order match = BestBidOrOffer(o.symbol, !o.side);

                // first we need to make sure the book we're matching to allows executions
                Account ma = new Account();
                if (acctlist.TryGetValue(match.Account,out ma) && ma.Execute) 
                {
                    // if it's allowed, try to match it
                    bool filled = o.Fill(match);
                    int avail = o.UnsignedSize;
                    // if it matched 
                    if (filled)
                    {
                        // record trade
                        Trade t = (Trade)o;
                        MasterTrades[a.ID].Add(t); 
                        // notify the trade occured
                        if (GotFill != null) 
                            GotFill(t);

                        // update the order's size (in case it was a partial fill)
                        o.size = (avail - Math.Abs(t.xsize)) * (o.side ? 1 : -1);

                        // if it was a full fill, no need to add order to the book
                        if (Math.Abs(t.xsize) == avail) return;
                    }
                }
            }
            // add any remaining order to book as new liquidity route
            List<Order> tmp;
            // see if we have a book for this account
            if (!MasterOrders.TryGetValue(a, out tmp))
            {
                tmp = new List<Order>();
                MasterOrders.Add(a, tmp); // if not, create one
            }
            o.Account = a.ID; // make sure order knows his account
            tmp.Add(o); // record the order
            // increment pending count
            _pendorders++; 
        }
Ejemplo n.º 23
0
 /// <summary>
 /// Gets the closed PL for a particular symbol and brokerage account.
 /// </summary>
 /// <param name="symbol">The symbol.</param>
 /// <param name="a">The Account.</param>
 /// <returns>Closed PL</returns>
 public decimal GetClosedPL(string symbol, Account a)
 {
     Position pos = new PositionImpl(symbol);
     decimal pl = 0;
     if (!MasterTrades.ContainsKey(a.ID)) return pl;
     List<Trade> trades = MasterTrades[a.ID];
     for (int i = 0; i < trades.Count; i++)
     {
         if (trades[i].symbol == pos.Symbol)
             pl += pos.Adjust(trades[i]);
     }
     return pl;
 }
Ejemplo n.º 24
0
 /// <summary>
 /// cancel order from any account
 /// </summary>
 /// <param name="orderid"></param>
 /// <returns></returns>
 public bool CancelOrder(long orderid)
 {
     bool worked = false;
     Account[] accts = new Account[MasterOrders.Count];
     MasterOrders.Keys.CopyTo(accts,0);
     for (int i = 0; i<accts.Length; i++)
         worked |= CancelOrder(accts[i], orderid);
     return worked;
 }
Ejemplo n.º 25
0
 /// <summary>
 /// Gets the closed points (points = PL on per-share basis) for given symbol/account.
 /// </summary>
 /// <param name="symbol">The symbol.</param>
 /// <param name="account">The account.</param>
 /// <returns>points</returns>
 public decimal GetClosedPT(string symbol, Account account)
 {
     PositionImpl pos = new PositionImpl(symbol);
     decimal points = 0;
     if (!MasterTrades.ContainsKey(account.ID)) return points;
     List<Trade> trades = MasterTrades[account.ID];
     for (int i = 0; i < trades.Count; i++)
     {
         points += Calc.ClosePT(pos, trades[i]);
         pos.Adjust(trades[i]);
     }
     return points;
 }
Ejemplo n.º 26
0
 /// <summary>
 /// cancel order for specific account only
 /// </summary>
 /// <param name="a"></param>
 /// <param name="orderid"></param>
 /// <returns></returns>
 public bool CancelOrder(Account a, long orderid)
 {
     List<Order> orderlist = new List<Order>();
     if (!MasterOrders.TryGetValue(a, out orderlist)) return false;
     int cancelidx = -1;
     for (int i = orderlist.Count-1; i>=0; i--) // and every order
         if (orderlist[i].id == orderid) // if we have order with requested id
         {
             if ((GotOrderCancel != null) && a.Notify)
                 GotOrderCancel(orderlist[i].symbol, orderlist[i].side,orderid); //send cancel notifcation to any subscribers
             if ((GotOrderCancelEvent != null) && a.Notify)
                 GotOrderCancelEvent(orderid);
             cancelidx = i;
         }
     if (cancelidx == -1) return false;
     MasterOrders[a].RemoveAt(cancelidx);
     return true;
 }
Ejemplo n.º 27
0
 public Order BestOffer(string symbol, Account  account) { return BestBidOrOffer(symbol,false,account); }
Ejemplo n.º 28
0
        /// <summary>
        /// Sends the order to the broker. (uses the default account)
        /// </summary>
        /// <param name="o">The order to be send.</param>
        /// <returns>status code</returns>
        public int SendOrderStatus(Order o) 
        {
            if (!o.isValid) return (int)MessageTypes.INVALID_ORDERSIZE;
            // make sure book is clearly stamped
            if (o.Account.Equals(string.Empty, StringComparison.OrdinalIgnoreCase)) 
            {
                o.Account = DEFAULT.ID;
                return SendOrderAccount(o, DEFAULT);
            }
            // get account
            Account a;
            if (!acctlist.TryGetValue(o.Account, out a))
            {
                a = new Account(o.Account);
                AddAccount(a);
            }
            return SendOrderAccount(o, a);

        }
Ejemplo n.º 29
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>status code</returns>
        public int SendOrderAccount(Order o,Account a)
        {
            if (o.id == 0) // if order id isn't set, set it
                o.id = _nextorderid++;
            if ((GotOrder != null) && a.Notify)
                GotOrder(o);
            AddOrder(o, a);

            return (int)MessageTypes.OK;
        }
Ejemplo n.º 30
0
        /// <summary>
        /// Sends the order to the broker. (uses the default account)
        /// </summary>
        /// <param name="o">The order to be send.</param>
        /// <returns>status code</returns>
        public int sendOrder(Order o) 
        {
            if (!o.isValid) return (int)MessageTypes.INVALID_ORDERSIZE;
            if (o.Account == "") // make sure book is clearly stamped
            {
                o.Account = DEFAULT.ID;
                return sendOrder(o, DEFAULT);
            }
            Account a = new Account();
            if (!acctlist.TryGetValue(o.Account,out a))
                AddAccount(new Account(o.Account));
            return sendOrder(o, acctlist[o.Account]);

        }