/// <summary> /// On receiving new tradebar data it will be passed into this function. The general pattern is: /// "public void OnData( CustomType name ) {...s" /// </summary> /// <param name="data">TradeBars data type synchronized and pushed into this function. The tradebars are grouped in a dictionary.</param> public void OnData(TradeBars data) { //int x = 0; //int y = 10; //int z = y / x; //if (!Portfolio.Invested) //{ // SetHoldings("SPY", 1); //} if (!Portfolio.HoldStock && data.ContainsKey("SPY")) { Order("SPY", (int)Math.Floor(Portfolio.Cash / data["SPY"].Close)); Debug("Debug Purchased MSFT: " + Portfolio.Cash); } if (Time.TimeOfDay.TotalSeconds % 10 == 0) { int i = Transactions.GetIncrementOrderId(); var order = new Order("BTC", 10, OrderType.Market, Time, data["BTC"].Price, "Tag: Test"); order.Status = OrderStatus.Filled; Transactions.Orders.AddOrUpdate<int, Order>(i, order); } }
/// <summary> /// Hungarian translation of the order class for the js side of IDE. /// </summary> /// <param name="cOrder"></param> public HungarianOrder(Order cOrder) { this.id = cOrder.Id; this.dtTime = cOrder.Time.Round(TimeSpan.FromSeconds(1)); this.dPrice = cOrder.Price; this.eType = cOrder.Type; this.sSymbol = cOrder.Symbol; this.eStatus = cOrder.Status; this.iQuantity = cOrder.Quantity; }
/// <summary> /// Helper Constructor using Order to Initialize. /// </summary> /// <param name="order">Order for this order status</param> /// <param name="message">Message from exchange or QC.</param> public OrderEvent(Order order, string message = "") { this.OrderId = order.Id; this.Status = order.Status; this.Message = message; this.Symbol = order.Symbol; //Initialize to zero, manually set fill quantity this.FillQuantity = 0; this.FillPrice = 0; }
//LIMIT FILL MODEL: public virtual void LimitFill(Security security, ref Order order) { //Initialise; decimal marketDataMinPrice = 0; decimal marketDataMaxPrice = 0; try { //If its cancelled don't need anymore checks: if (order.Status == OrderStatus.Canceled) return; //Depending on the resolution, return different data types: Futures contract = security.GetLastData() as Futures; if (contract == null) { //Shouldnt happen. } marketDataMinPrice = contract.Low; marketDataMaxPrice = contract.High; //Valid Live/Model Order: if (order.Direction == OrderDirection.Buy) { //Buy limit seeks lowest price if (marketDataMinPrice < order.Price) { order.Status = OrderStatus.Filled; } } else if (order.Direction == OrderDirection.Sell) { //Sell limit seeks highest price possible if (marketDataMaxPrice > order.Price) { order.Status = OrderStatus.Filled; } } } catch (Exception err) { algo.Error("CustomTransactionModel.TransOrderDirection.LimitFill():" + err.Message); } }
//FILL THE ORDER public virtual void Fill(Security vehicle, ref Order order) { try { switch (order.Type) { case OrderType.Limit: LimitFill(vehicle, ref order); break; case OrderType.Stop: StopFill(vehicle, ref order); break; case OrderType.Market: MarketFill(vehicle, ref order); break; } } catch (Exception err) { algo.Error("CustomTransactionModel.TransOrderDirection.Fill():" + err.Message); } }
/// <summary> /// Add a TransOrderDirection /// </summary> public virtual void AddOrder(Order order) { lock (OrderCache) { OrderCache.Add(order); } }
/// <summary> /// Helper Constructor using Order to Initialize. /// </summary> /// <param name="order">Order for this order status</param> /// <param name="message">Message from exchange or QC.</param> public OrderEvent(Order order, string message) { this.Id = order.Id; this.Status = order.Status; this.FillPrice = order.Price; this.Message = message; this.FillQuantity = order.Quantity; }
//SLIPPAGE MODEL: public virtual decimal GetSlippageApproximation(Security security, Order order) { decimal slippage = .01m; //Calc slippage here: Based on order and security. return slippage; }
//STOP FILL MODEL - public virtual void StopFill(Security security, ref Order order) { try { //If its cancelled don't need anymore checks: if (order.Status == OrderStatus.Canceled) return; //Check if the Stop Order was filled: opposite to a limit order if (order.Direction == OrderDirection.Sell) { //-> 1.1 Sell Stop: If Price below setpoint, Sell: if (security.Price > order.Price) { order.Status = OrderStatus.Filled; } } else if (order.Direction == OrderDirection.Buy) { //-> 1.2 Buy Stop: If Price Above Setpoint, Buy: if (security.Price < order.Price) { order.Status = OrderStatus.Filled; } } } catch (Exception err) { algo.Error("CustomTransactionModel.TransOrderDirection.StopFill():" + err.Message); } }
//MARKET FILL MODEL: public virtual void MarketFill(Security security, ref Order order) { try { //Calculate the model slippage: e.g. 0.01c decimal slip = GetSlippageApproximation(security, order); switch (order.Direction) { case OrderDirection.Buy: order.Price = security.Price; order.Price += slip; break; case OrderDirection.Sell: order.Price = security.Price; order.Price -= slip; break; } //Market orders fill instantly. order.Status = OrderStatus.Filled; } catch (Exception err) { algo.Error("CustomTransactionModel.TransOrderDirection.MarketFill():" + err.Message); } }