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); }
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; }
public bool Equals(Account a) { return this._id.Equals(a.ID); }
/// <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, Position> poslist = new Dictionary<string, Position>(); Dictionary<string, decimal> ptlist = new Dictionary<string, decimal>(); if (!MasterTrades.ContainsKey(account.ID)) return 0; foreach (Trade trade in MasterTrades[account.ID]) { if (!poslist.ContainsKey(trade.symbol)) { poslist.Add(trade.symbol, new Position(trade.symbol)); ptlist.Add(trade.symbol, 0); } ptlist[trade.symbol] += BoxMath.ClosePT(poslist[trade.symbol], trade); poslist[trade.symbol].Adjust(trade); } decimal points = 0; foreach (string sym in ptlist.Keys) points += ptlist[sym]; return points; }
public Order BestOffer(string symbol, Account account) { return BestBidOrOffer(symbol,false,account); }
/// <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 (Trade trade in MasterTrades[a.ID]) { if (!poslist.ContainsKey(trade.symbol)) { poslist.Add(trade.symbol, new Position(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; }
/// <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) { Position pos = new Position(symbol); decimal points = 0; if (!MasterTrades.ContainsKey(account.ID)) return points; foreach (Trade t in MasterTrades[account.ID]) { points += BoxMath.ClosePT(pos, t); pos.Adjust(t); } return points; }
/// <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 Position(symbol); if (!MasterTrades.ContainsKey(a.ID)) return pos; foreach (Trade trade in MasterTrades[a.ID]) if (trade.symbol==symbol) pos.Adjust(trade); return pos; }
/// <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 Position(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; }
/// <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>(); }
/// <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>(); }
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 }
/// <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; }
public bool CancelOrder(Account a, uint orderid) { if (!MasterOrders.ContainsKey(a)) return false; for (int i = 0; i < MasterOrders[a].Count; i++) // and every order if (MasterOrders[a][i].id == orderid) // if we have order with requested id { if ((GotOrderCancel != null) && a.Notify) GotOrderCancel(MasterOrders[a][i].symbol, MasterOrders[a][i].Side,orderid); //send cancel notifcation to any subscribers MasterOrders[a].RemoveAt(i); // remove/cancel order return true; } return false; }
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 }
public void MultiAccount() { const string sym = "TST"; Order o = new BuyMarket(sym,100); const string me = "tester"; const string other = "anotherguy"; Account a = new Account(me); Account b = new Account(other); Account c = new Account("sleeper"); // send order to account for jfranta Assert.That(broker.sendOrder(o, a)>0); Assert.That(broker.sendOrder(o, b)>0); Tick t = new Tick(sym); t.trade = 100m; t.size = 200; Assert.That(broker.Execute(t)==2); Position apos = broker.GetOpenPosition(sym,a); Position bpos = broker.GetOpenPosition(sym,b); Position cpos = broker.GetOpenPosition(sym, c); Assert.That(apos.Side && (apos.Size == 100)); Assert.That(bpos.Side && (bpos.Size == 100)); Assert.That(cpos.Flat); // make sure that default account doesn't register // any trades Assert.That(broker.GetOpenPosition(sym).Flat); }