/// <summary> /// Executes this custom strategy /// </summary> /// <param name="data">AbhiGoalCustomData - the custom data</param> /// <param name="transactionSize">decimal - the standard transaction size as defined in the algorithm</param> /// <param name="status">string - a status report on the execution for tracing</param> /// <param name="quantity">int - the number of shares purchased/sold</param> /// <param name="t">TradeBar - the current trade bar when the OnData was fired</param> /// <returns>int - orderID the id of the order if it is available. /// 0 = no order placed /// -1 = order not filled, such as a limit order that did not fill right away</returns> public int ExecuteStrategy(AbhiGoalCustomData data, TradeBar t, decimal transactionSize, out string status, out int quantity) { var s = data.symbol; status = ""; int orderId = 0; quantity = 0; OrderTicket orderTicket = null; try { switch (data.kno) { case 41: quantity = Convert.ToInt32(CalculateBuyQuantity(data.pct, data.next_close, transactionSize)); orderTicket = _algorithm.Buy(data.symbol, quantity); status = "Buy"; if (orderId == -1) status = "No trade history"; break; case 31: if (_algorithm.Portfolio[data.symbol].HoldStock) { quantity = Convert.ToInt32(CalculateSellQuantity(data.pct, _algorithm.Portfolio[s].Quantity)); orderTicket = _algorithm.Sell(data.symbol, quantity); status = "Sell"; if (orderId == -1) status = "No trade history"; } else { status = "Not in portfolio"; } break; default: throw new Exception("bad kno"); } } catch (Exception e) { Console.WriteLine(e); } return orderId; }
/// <summary> /// Logs the OrderEvent Transaction /// </summary> /// <param name="orderEvent">the OrderEvent being logged</param> /// <param name="abhiGoalCustomData">the custom data bar</param> public void ReportDailyBar(AbhiGoalCustomData abhiGoalCustomData, TradeBar tradeBar, string status, decimal quantity) { #region "Print" string msg = (string.Format( "{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23},{24}", abhiGoalCustomData.date, abhiGoalCustomData.next_date, abhiGoalCustomData.next_close, abhiGoalCustomData.symbol, abhiGoalCustomData.close, abhiGoalCustomData.kno, abhiGoalCustomData.w, abhiGoalCustomData.h, abhiGoalCustomData.e, abhiGoalCustomData.n, abhiGoalCustomData.rd, abhiGoalCustomData.pct, abhiGoalCustomData.cr, abhiGoalCustomData.bsval1, abhiGoalCustomData.bsval2, abhiGoalCustomData.bsval3, abhiGoalCustomData.bsval4, status, quantity, "", DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond, "" )); _logHandler.Debug(msg); #endregion }
/// <summary> /// Parces a line from a csv file into an object /// </summary> /// <param name="line">string - the csv line</param> /// <returns>AbhiGoalCustomData - the data object</returns> /// <exception cref="Exception"></exception> public AbhiGoalCustomData ParceRowIntoObject(string line) { #region "process row into the object" if (line.Length == 0) return null; if (line.StartsWith("date")) return null; string[] row = line.Split(','); AbhiGoalCustomData abhiGoalCustomData = new AbhiGoalCustomData(); DateTime dt; // date var result = DateTime.TryParse(row[0], out dt); if (!result) throw new InvalidDataException(string.Format("Invalid date in row {0}, column 1", count)); else { abhiGoalCustomData.date = dt; abhiGoalCustomData.Time = dt + new TimeSpan(10,0,0); } // next_date result = DateTime.TryParse(row[1], out dt); if (!result) throw new InvalidDataException(string.Format("Invalid date in row {0}, column 2", count)); else { abhiGoalCustomData.next_date = dt; } //next_close decimal next_close; result = decimal.TryParse(row[2], out next_close); if (!result) throw new InvalidDataException(string.Format("Invalid close in row {0}, column 3", count)); else { abhiGoalCustomData.next_close = next_close; } // symbol abhiGoalCustomData.symbol = row[3]; abhiGoalCustomData.Symbol = row[3]; // close decimal close; result = decimal.TryParse(row[4], out close); abhiGoalCustomData.close = close; // kno int kno; string row5 = row[5].Trim(); result = int.TryParse(row5.Substring(0, row5.Length - 1), out kno); abhiGoalCustomData.kno = kno; abhiGoalCustomData.w = row[6]; abhiGoalCustomData.h = row[7]; abhiGoalCustomData.e = row[8]; abhiGoalCustomData.n = row[9]; abhiGoalCustomData.rd = row[10]; // pct decimal pct; result = decimal.TryParse(row[11], out pct); abhiGoalCustomData.pct = pct; abhiGoalCustomData.cr = row[12]; abhiGoalCustomData.bsval1 = row[13]; abhiGoalCustomData.bsval2 = row[14]; abhiGoalCustomData.bsval3 = row[15]; abhiGoalCustomData.bsval4 = row[16]; #endregion return abhiGoalCustomData; }
/// <summary> /// 3. READER METHOD: Read 1 line from data source and convert it into Object. /// Each line of the CSV File is presented in here. The backend downloads your file, loads it into memory and then line by line /// feeds it into your algorithm /// </summary> /// <param name="line">string line from the data source file submitted above</param> /// <param name="config">Subscription data, symbol name, data type</param> /// <param name="currentDate">Current date we're requesting. This allows you to break up the data source into daily files.</param> /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param> /// <returns>New Bitcoin Object which extends BaseData.</returns> public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime currentDate, bool isLiveMode) { var instruction = new AbhiGoalCustomData(); if (isLiveMode) { //Example Line Format: //{"high": "441.00", "last": "421.86", "timestamp": "1411606877", "bid": "421.96", "vwap": "428.58", "volume": "14120.40683975", "low": "418.83", "ask": "421.99"} try { AhiGoalLineParser dp = new AhiGoalLineParser(); instruction = dp.ParceRowIntoObject(line); } catch { /* Do nothing, possible error in json decoding */ } return instruction; } //Example Line Format: //date,next_date,next_close,symbol,close,kno,w,h,e,n,rd,pct,cr,bsval1,bsval2,bsval3,bsval4 //07/07/2015,12/31/1969,,CLF,3.53,415,1,3,1,4,R,83,5,B,U,Y,N try { AhiGoalLineParser dp = new AhiGoalLineParser(); instruction = dp.ParceRowIntoObject(line); } catch { /* Do nothing, skip first title row */ } return instruction; }