Glitch8
 ( 11.43% )
- ago
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript1 { public class MyStrategy : UserStrategyBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) {          rates = Fred.Series(bars, "FEDFUNDS", false, "FFR");          PlotIndicator(rates);          StartIndex = 1; } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) {          if (idx == bars.Count - 1)             return;          double rtn = ROC.Value(idx + 1, bars.Close, 1);          if (rates[idx] > rates[idx - 1])          {             isRising = true;          }          else if (rates[idx] < rates[idx - 1])          {             isRising = false;          }          if (isRising)          {             countUp++;             sumUp += rtn;             SetBackgroundColor(bars, idx, colorRise);          }          else          {             countDown++;             sumDown += rtn;             SetBackgroundColor(bars, idx, colorFall);          } } public override void Cleanup(BarHistory bars) {          string s = countUp + " bars with Rising Rates";          DrawHeaderText(s, WLColor.Aqua, 20);          double avg = sumUp / (double)countUp;          DrawHeaderText("Avg Return: " + avg.ToString("N2"), WLColor.Aqua, 20);          s = countDown + " bars with Falling Rates";          DrawHeaderText(s, WLColor.Aqua, 20);          avg = sumDown / (double)countDown;          DrawHeaderText("Avg Return: " + avg.ToString("N2"), WLColor.Aqua, 20); }       //declare private variables below       bool isRising = true; private IndicatorBase rates;       private int countUp = 0;       private int countDown = 0;       private double sumUp = 0.0;       private double sumDown = 0.0;       private WLColor colorRise = new WLColor(32, 255, 0, 0);       private WLColor colorFall = new WLColor(32, 0, 128, 0); } }
0
473
0 Replies

Reply

Bookmark

Sort
Currently there are no replies yet. Please check back later.