/// <summary> /// Same as Tuple<decimal, TickList > SimpleHoldStrategy(..) but this /// method only outputs the end result. /// </summary> /// <returns><Remaining value after using the specified strategy/returns> public static decimal SimpleHoldStrategyResult(TickList valuedata, TickList webdata, bool relative, decimal change, int holdtime) { decimal value = 1; TickList absticklist = webdata.AbsoluteDifferences(); List <Tick> absdif = absticklist.getList(); TickList relticklist = webdata.RelativeDifferences(); List <Tick> reldif = relticklist.getList(); for (int i = 0; i < reldif.Count && i != -1;) { if ((relative && reldif[i].getValue() > change) || (!relative && absdif[i].getValue() > change)) { DateTime buydate = reldif[i].getDate(); DateTime selldate = buydate.AddHours(holdtime); decimal buytimevalue = valuedata.valueAt(buydate); decimal selltimevalue = valuedata.valueAt(selldate); if ((buytimevalue != -1) && (selltimevalue != -1)) { value *= selltimevalue / buytimevalue; } i = relticklist.findDate(selldate); } else { i++; } } return(value); }
///// <summary> ///// Test a custom simple hold strategy. ///// </summary> //private void btnTestCustomSimpleHold_Click(object sender, EventArgs e) //{ // Tuple<decimal, TickList> result; // decimal change; // int holdtime; // bool changeerror = !decimal.TryParse(txtChange.Text, out change); // bool holderror = !int.TryParse(txtHoldFor.Text, out holdtime); // bool relative = chkRelative.Checked; // String name = "SimpleH(" + txtChange.Text + ","; // if (relative) // { // name += "rel,"; // change *= (decimal)0.01; // } // else { name += "abs,"; } // name += txtHoldFor.Text + "," + cmbDayYearMonth.Text + ")"; // switch (cmbDayYearMonth.Text) // { // case "Days": holdtime *= 24; break; // case "Weeks": holdtime *= 24 * 7; break; // // this is not completely correct, not all months are 30 days // case "Months": holdtime *= 24 * 7 * 30; break; // } // if (!changeerror && !holderror) // { // result = BackTest.SimpleHoldStrategy(valuedata, webdata, // relative, change, holdtime); // MessageBox.Show("Remaining value: " + result.Item1, "Finished", // MessageBoxButtons.OK, MessageBoxIcon.Information); // addToChart(result.Item2, name, true, chart1); // lstResults.Items.Add(name + ", " + result.Item1); // } // if (changeerror) // { // MessageBox.Show("Enter a valid change value"); // } // if (holderror) // { // MessageBox.Show("Enter a valid hold time value"); // } //} /// <summary> /// Add some data to the chart. /// </summary> /// <param name="ticklist">A TickList containing the values to be added.</param> /// <param name="legend">The string to be displayed in the legend.</param> /// <param name="piecewise">If true, every two other points will be connected, the /// points in between will be left unconnected: 0---1 2---3 etc.</param> private void addToChart(TickList ticklist, String name, bool piecewise, Chart c) { if (c.Series.Contains(c.Series.FindByName(name))) { name += "1"; } if (piecewise) { Series points = new Series { Name = name, ChartType = SeriesChartType.Point, XValueType = ChartValueType.DateTime }; Series line = new Series { Name = name + "_line", ChartType = SeriesChartType.Line, XValueType = ChartValueType.DateTime, IsVisibleInLegend = false }; c.Series.Add(points); c.Series.Add(line); foreach (Tick t in ticklist.getList()) { points.Points.AddXY(t.getDate(), t.getValue()); } for (int i = 0; i < ticklist.getList().Count; i += 2) { int p0 = line.Points.AddXY(ticklist.getList()[i].getDate(), ticklist.getList()[i].getValue()); int p1 = line.Points.AddXY(ticklist.getList()[i + 1].getDate(), ticklist.getList()[i + 1].getValue()); line.Points[p0].Color = Color.Transparent; } } else { Series line = new Series { Name = name, ChartType = SeriesChartType.Line, XValueType = ChartValueType.DateTime, IsVisibleInLegend = true }; c.Series.Add(line); foreach (Tick t in ticklist.getList()) { line.Points.AddXY(t.getDate(), t.getValue()); } } }
/// <summary> /// Add some data to the chart for previewing. /// </summary> /// <param name="ticklist">A TickList containing the values to be added. /// </param> private void showInPreviewChart(TickList ticklist, String name, Chart c) { c.Series.Clear(); Series line = new Series { Name = name, ChartType = SeriesChartType.Line, XValueType = ChartValueType.DateTime, IsVisibleInLegend = true }; c.Series.Add(line); foreach (Tick t in ticklist.getList()) { line.Points.AddXY(t.getDate(), t.getValue()); } }
/// <summary> /// A simple hold strategy. /// When the value of the web data has (absolutely or relatively), increased /// by a certain number or percentage, the asset is held for the specified holdtime. /// </summary> /// <param name="valuedata">A TickList containing the value data.</param> /// <param name="webdata">A TickList containing the Google search data.</param> /// <param name="relative">If true, the change is interpreted as relative change. /// Otherwise, the change is absolute.</param> /// <param name="change">A number representing the change treshold.</param> /// <param name="holdtime">The time for which to hold the asset in hours. /// (this might change into the future when testing high-frequency strategies).</param> /// <returns>A touple containing: a decimal representing the remaing value /// after using this strategy. A TickList containing the buy- and -sell /// values and prices.</returns> public static Tuple <decimal, TickList> SimpleHoldStrategy(TickList valuedata, TickList webdata, bool relative, decimal change, int holdtime) { decimal value = 1; TickList portfolio = new TickList(); TickList absticklist = webdata.AbsoluteDifferences(); List <Tick> absdif = absticklist.getList(); TickList relticklist = webdata.RelativeDifferences(); List <Tick> reldif = relticklist.getList(); for (int i = 0; i < reldif.Count && i != -1;) { if ((relative && reldif[i].getValue() > change) || (!relative && absdif[i].getValue() > change)) { DateTime buydate = reldif[i].getDate(); DateTime selldate = buydate.AddHours(holdtime); try { decimal buyprice = valuedata.valueAt(buydate); decimal sellprice = valuedata.valueAt(selldate); value *= sellprice / buyprice; portfolio.Add(new Tick(buydate, buyprice)); portfolio.Add(new Tick(selldate, sellprice)); } catch (InvalidOperationException e) { MessageBox.Show(e.Message); } i = relticklist.findDate(selldate); } else { i++; } } return(Tuple.Create(value, portfolio)); }