public void OnData(Futures data1) { if (Time.Year == 2006) { System.Diagnostics.Debugger.Break(); } if (data1.Symbol == symbols[0]) { prices.Push1(data1.Open); } if (data1.Symbol == symbols[1]) { prices.Push2(data1.Open); } if (prices.QLength1 == numBars && prices.QLength2 == numBars) { mult = BrentSearch.FindRoot(prices.multFunc, -2, 2, 1e-8); resid = prices.Open1 - (mult * prices.Open2); if (Portfolio[symbols[0]].IsLong && resid > 0) { Order(symbols[0], -1); Order(symbols[1], 1); //Debug(data1.Time.ToString()+" Resid: "+resid.ToString()+" Close BuySell-Open1: "+prices.Open1.ToString()+" Open2: "+prices.Open2.ToString()); } if (Portfolio[symbols[0]].IsShort && resid < 0) { Order(symbols[0], 1); Order(symbols[1], -1); //Debug(data1.Time.ToString()+" Resid: "+resid.ToString()+" Close SellBuy-Open1: "+prices.Open1.ToString()+" Open2: "+prices.Open2.ToString()); } if (!Portfolio[symbols[0]].HoldStock && !Portfolio[symbols[1]].HoldStock) { if (resid > cLevel) { //int tradeSize = (int)(Portfolio.Cash / balRisk); Order(symbols[0], -1); Order(symbols[1], 1); //Debug(data1.Time.ToString()+" Resid: "+resid.ToString()+" SellBuy-Open1: "+prices.Open1.ToString()+" Open2: "+prices.Open2.ToString()); } if (resid < -cLevel) { //int tradeSize = (int)(Portfolio.Cash / balRisk); Order(symbols[0], 1); Order(symbols[1], -1); //Debug(data1.Time.ToString()+" Resid: "+resid.ToString()+" BuySell-Open1: "+prices.Open1.ToString()+" Open2: "+prices.Open2.ToString()); } } } }
//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); } }
public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint datafeed) { Futures contract = new Futures(); try { string[] data = line.Split(','); contract.Time = DateTime.ParseExact(data[0], "M/d/yyyy", CultureInfo.InvariantCulture); contract.Open = Convert.ToDecimal(data[1]); contract.High = Convert.ToDecimal(data[2]); contract.Low = Convert.ToDecimal(data[3]); contract.Close = Convert.ToDecimal(data[4]); contract.Symbol = config.Symbol; contract.Value = contract.Close; } catch { /* Do nothing, skip first title row */ } return(contract); }
public void OnData(Futures data1) { if (Time.Year == 2006) System.Diagnostics.Debugger.Break(); if (data1.Symbol == symbols[0]) { prices.Push1(data1.Open); } if (data1.Symbol == symbols[1]) { prices.Push2(data1.Open); } if (prices.QLength1 == numBars && prices.QLength2 == numBars) { mult = BrentSearch.FindRoot(prices.multFunc, -2, 2, 1e-8); resid = prices.Open1 - (mult * prices.Open2); if (Portfolio[symbols[0]].IsLong && resid > 0) { Order(symbols[0], -1); Order(symbols[1], 1); //Debug(data1.Time.ToString()+" Resid: "+resid.ToString()+" Close BuySell-Open1: "+prices.Open1.ToString()+" Open2: "+prices.Open2.ToString()); } if (Portfolio[symbols[0]].IsShort && resid < 0) { Order(symbols[0], 1); Order(symbols[1], -1); //Debug(data1.Time.ToString()+" Resid: "+resid.ToString()+" Close SellBuy-Open1: "+prices.Open1.ToString()+" Open2: "+prices.Open2.ToString()); } if (!Portfolio[symbols[0]].HoldStock && !Portfolio[symbols[1]].HoldStock) { if (resid > cLevel) { //int tradeSize = (int)(Portfolio.Cash / balRisk); Order(symbols[0], -1); Order(symbols[1], 1); //Debug(data1.Time.ToString()+" Resid: "+resid.ToString()+" SellBuy-Open1: "+prices.Open1.ToString()+" Open2: "+prices.Open2.ToString()); } if (resid < -cLevel) { //int tradeSize = (int)(Portfolio.Cash / balRisk); Order(symbols[0], 1); Order(symbols[1], -1); //Debug(data1.Time.ToString()+" Resid: "+resid.ToString()+" BuySell-Open1: "+prices.Open1.ToString()+" Open2: "+prices.Open2.ToString()); } } } }
public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint datafeed) { Futures contract = new Futures(); try { string[] data = line.Split(','); contract.Time = DateTime.ParseExact(data[0], "M/d/yyyy", CultureInfo.InvariantCulture); contract.Open = Convert.ToDecimal(data[1]); contract.High = Convert.ToDecimal(data[2]); contract.Low = Convert.ToDecimal(data[3]); contract.Close = Convert.ToDecimal(data[4]); contract.Symbol = config.Symbol; contract.Value = contract.Close; } catch { /* Do nothing, skip first title row */ } return contract; }