/// <summary> /// Load the List with StockPoint objects /// </summary> /// <param name="stockPoints">Stock points to fill.</param> /// <param name="st">Stream with CSV data.</param> public static void LoadList(ref StockPoints stockPoints, Stream st) { StreamReader sr = new StreamReader(st); string line; int lineNumber = 1; double open; double high; double low; double close; long volume; while (!sr.EndOfStream) { line = sr.ReadLine(); string[] terms = line.Split(','); for (int i = 0; i < 7; i++) { if (terms[i] == String.Empty) { terms[i] = "0"; } } open = (double)Double.Parse(terms[0]); high = (double)Double.Parse(terms[1]); low = (double)Double.Parse(terms[2]); close = (double)Double.Parse(terms[3]); volume = Int64.Parse(terms[4]); StockPoint stockPoint = new StockPoint(terms[6], terms[5], open, high, low, close, volume); stockPoints.Add(stockPoint); lineNumber++; } }
/// <summary> /// Initializes a new instance of the Sell class. /// </summary> /// <param name="stockPoint">The stock point to sell at.</param> public Sell(StockPoint stockPoint) { this.Price = stockPoint.Open; this.Date = stockPoint.Date; this.Time = stockPoint.PointDateTime.AddMinutes(-5).ToString("HH:mm"); this.Stopped = false; }
/// <summary> /// Initializes a new instance of the Buy class. /// </summary> /// <param name="stockPoint">The stock point for the purchase.</param> /// <param name="tradeTypes">The type of the buy: short or long.</param> /// <param name="shares">The number of shares purchased.</param> public Buy(StockPoint stockPoint, TradeType tradeType, int shares) { this.Date = stockPoint.Date; this.Time = stockPoint.PointDateTime.AddMinutes(-5).ToString("HH:mm"); this.Price = stockPoint.Open; this.TradeType = tradeType; this.Shares = shares; }
/// <summary> /// Initializes a new instance of the Sell class for a stop. /// </summary> /// <param name="stockPoint">The stock point the trade was stopped out at.</param> /// <param name="price">The price the trade was stopped out at.</param> //public Sell(StockPoint stockPoint, double price, int periodDecrement) //{ // this.Price = price; // this.Date = stockPoint.Date; // //this.Time = stockPoint.Time; // this.Time = stockPoint.PointDateTime.AddMinutes(-periodDecrement).ToString("HH:mm"); // this.Stopped = true; //} public Sell(StockPoint stockPoint, int periodDecrement, bool stopped) { this.Price = stockPoint.Open; this.Date = stockPoint.Date; //this.Time = stockPoint.Time; this.Time = stockPoint.PointDateTime.AddMinutes(-periodDecrement).ToString("HH:mm"); this.Stopped = stopped; }
/// <summary> /// Initializes a new instance of the RelativeStrengthIndex class. /// </summary> /// <param name="period">The period to calculate the RelativeStrengthIndex for</param> public RelativeStrengthIndex(int period) { this.Values = null; this.minimumPoints = period + 1; this.valuesReceived = 0; this.Keys = new string[NUMBEROFKEYS]; this.Values = new double[NUMBEROFKEYS]; this.Keys[0] = "RSI(" + period + ")"; this.gains = new FDMTermProject.Library.ExponentialMovingAverage(period); this.losses = new FDMTermProject.Library.ExponentialMovingAverage(period); this.previousPoint = null; }
/// <summary> /// Initializes a new instance of the IndicatorLibraryAdapter class. /// </summary> /// <param name="indicatorFile">A file path for the csv file to read.</param> public IndicatorLibraryAdapter(string indicatorFile) { this.indicatorFile = indicatorFile; this.Data = new Dictionary<string, List<double?>>(); //this.DataQueue = new List<Queue<double>>(); this.StockPoints = new List<StockPoint>(); this.EndOfDayIndex = new List<int>(); string[] headerdata; string headdata; string[] dataSplit; string open = string.Empty, high = string.Empty, low = string.Empty, close = string.Empty, volume = string.Empty, date = string.Empty, time = string.Empty; DateTime currentTime; DateTime previousTime = default(DateTime); StockPoint currentsp; int index = 0; try { using (FileStream fs = File.Open(indicatorFile, FileMode.Open, FileAccess.Read, FileShare.None)) using (BufferedStream bs = new BufferedStream(fs)) using (StreamReader sr = new StreamReader(bs)) { string line = sr.ReadLine(); string value; headerdata = line.Split(',').Select(s => s.Trim()).ToArray(); for (int i = 0; i < headerdata.Length; i++) { headdata = headerdata[i]; this.Data.Add(headdata, new List<double?>()); } while ((line = sr.ReadLine()) != null) { dataSplit = line.Split(','); currentTime = Util.GetDateTimeValue(dataSplit[0], dataSplit[1]); for (int i = 0; i < headerdata.Length; i++) { value = dataSplit[i]; headdata = headerdata[i]; this.Data[headdata].Add(!string.IsNullOrWhiteSpace(value) ? (double?)double.Parse(value.Replace(":", "")) : null); } currentsp = new StockPoint(currentTime, double.Parse(dataSplit[2]), double.Parse(dataSplit[3]), double.Parse(dataSplit[4]), double.Parse(dataSplit[5]), long.Parse(dataSplit[6])); this.StockPoints.Add(currentsp); if (previousTime.ToShortDateString() != currentTime.ToShortDateString()) { if (index > 0) { this.EndOfDayIndex.Add(index - 1); } previousTime = currentsp.PointDateTime; } index++; } } } catch (Exception) { throw new FileLoadException("File cannot be read. Make sure the file is not opened by another program"); } //foreach (KeyValuePair<string, List<double?>> kv in this.Data) //{ // this.DataQueue.Add(new Queue<double>(kv.Value.Select(v => ConvertDouble(v)))); //} this.DataCount = index; this.EndOfDayIndex.Add(index - 1); this.StartDate = ParseDateTime(this.Data["Date"].FirstOrDefault().Value, this.Data["Time"].FirstOrDefault().Value); this.EndDate = ParseDateTime(this.Data["Date"].LastOrDefault().Value, this.Data["Time"].LastOrDefault().Value); currentLocationIndex = -1; MoveNext(); #region oldcode //this.indicatorFile = indicatorFile; //this.IndicatorLocations = new Dictionary<string, int>(); //this.streamReader = new StreamReader(File.Open(this.indicatorFile, System.IO.FileMode.Open)); //this.MoveNext(); //for (int i = 0; i < this.indicators.Length; i++) //{ // this.IndicatorLocations.Add(this.indicators[i].Trim(), i); //} //this.MoveNext(); #endregion oldcode }
//public string[] Peek() //{ // string[] nextValue = null; // if (this.currentLocationIndex < this.DataCount - 1) // { // nextValue = this.Data.Keys.Select(k => this.Data[k][this.currentLocationIndex + 1].Trim()).ToArray(); // } // return nextValue; //} /// <summary> /// Moves to the next line of the input. /// </summary> /// <returns>false if it's at the end of the file.</returns> public bool MoveNext() { if (this.Data == null) return false; this.currentLocationIndex++; if (this.currentLocationIndex >= this.DataCount - 1) { return false; } this.stockPoint = null; this.previousIndicators = this.indicators; this.indicators = this.Data.Keys.Select(k => this.Data[k][this.currentLocationIndex]).ToArray(); return true; #region oldcode //if (this.streamReader == null) //{ // this.previousIndicators = null; // return false; //} //if (this.streamReader.EndOfStream) //{ // this.streamReader.Close(); // this.streamReader = null; // return false; //} //this.indicators = this.streamReader.ReadLine().Split(','); //this.isLast = this.streamReader.Peek() == -1; #endregion oldcode }
/// <summary> /// Gets a stock point object for the current line. /// </summary> /// <returns>A stock point object for the current line.</returns> public StockPoint GetStockPoint() { if (this.stockPoint == null) { int time = (int)this.Data["Time"][currentLocationIndex].Value; this.stockPoint = new StockPoint(this.Data["Date"][currentLocationIndex].Value.ToString(), string.Format("{0}:{1}", (time/100).ToString("00"), (time%100).ToString("00")).ToString(), this.Data["Open"][currentLocationIndex].Value, this.Data["High"][currentLocationIndex].Value, this.Data["Low"][currentLocationIndex].Value, this.Data["Close"][currentLocationIndex].Value, ((long)(int)this.Data["Volume"][currentLocationIndex].Value)); //this.stockPoint.Open = this.Data["Date"][currentLocationIndex]; //this.stockPoint = new StockPoint() // this.indicators[this.IndicatorLocations["Date"]], // this.indicators[this.IndicatorLocations["Time"]], // double.Parse(this.indicators[this.IndicatorLocations["Open"]]), // double.Parse(this.indicators[this.IndicatorLocations["High"]]), // double.Parse(this.indicators[this.IndicatorLocations["Low"]]), // double.Parse(this.indicators[this.IndicatorLocations["Close"]]), // long.Parse(this.indicators[this.IndicatorLocations["Volume"]])); } return this.stockPoint; }
/// <summary> /// Advance the indicator with a new StockPoint /// </summary> /// <param name="point">The StockPoint to add</param> public void AddPoint(StockPoint point) { this.valuesReceived++; this.sma.AddValue(point.Close); if (this.IsReady) { this.Values[0] = this.sma.MovingAverage(); } }
/// <summary> /// Advance the indicator with a new StockPoint /// </summary> /// <param name="point">The StockPoint to add</param> public void AddPoint(StockPoint point) { this.valuesReceived++; if (this.previousPoint != null) { if (this.previousPoint.Close > point.Close) { this.losses.AddValue(this.previousPoint.Close - point.Close); this.gains.AddValue(0); } else if (this.previousPoint.Close < point.Close) { this.gains.AddValue(point.Close - this.previousPoint.Close); this.losses.AddValue(0); } if (this.IsReady) { this.Values[0] = 100 - (100 / (1 + (this.gains.MovingAverage / this.losses.MovingAverage))); } } this.previousPoint = point; }
/// <summary> /// Manually exit the trade. /// </summary> /// <param name="stockPoint">The stock point to sell the contract at.</param> /// <param name="stopped">Is thw exit stopped out</param> public void Exit(StockPoint stockPoint, int periodDecrement, bool stopped = true) { this.sell = new Sell(stockPoint, periodDecrement, stopped); /// ************************************************ /// GDBCup - Need to code /// ************************************************ }
/// <summary> /// Manually enter the trade. /// </summary> /// <param name="stockPoint">The stock point to purchase the contract at.</param> /// <param name="tradeTypes">The type of trade: long or short.</param> /// <param name="shares">The number of shares to buy.</param> public void Enter(StockPoint stockPoint, TradeType tradeType, int shares) { this.buy = new Buy(stockPoint, tradeType, shares); /// ************************************************ /// GDBCup - Need to code /// ************************************************ }
/// <summary> /// Checks to make sure the trade hasn't stopped out. If it has it sells. /// </summary> /// <param name="stockPoint">The stock point to check the trade against.</param> /// <returns>True if it stopped the trade.</returns> public bool CheckStop(StockPoint stockPoint) { return this.Status == TradeStatus.complete; /// ************************************************ /// GDBCup - Need to code /// ************************************************ }