- ago
Hi,

Below is my version of Hello World for WL7. It touches on a few basic things that will hopefully help people out. The most complicated thing on it is the coloring of the background for position profitability that is in a procedure separate from the Execute procedure. It also includes a debugging example. The strategy is not meant to be profitable.

MPO

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace HelloWorld { public class HelloWorldStrategy : UserStrategyBase {              //the base procedure must have the same name as the class       //add parameters here       public HelloWorldStrategy() : base()       {          AddParameter("Days", ParameterTypes.Int32, 2, 2, 5, 1);          AddParameter("SMA Days", ParameterTypes.Int32, 90, 30, 180, 30);       }                     //create indicators and other objects here, this is executed prior to the main trading loop     //also do plotting here       public override void Initialize(BarHistory bars) {          //assign parameters to variables          days = (int)Parameters[0].Value;          sma_days = (int)Parameters[1].Value;                    //create new series          sma_values = SMA.Series(bars.Close, 14);          //ignore data before the StartIndex          StartIndex = sma_days + 1;          //do all the plotting here          //label the line          DrawHeaderText("SMA " + sma_days + " day", Color.Red, 12);          PlotTimeSeries(sma_values, sma_values.Description, "Price", Color.Red, PlotStyles.Line);       }              public void WinLossBackground(BarHistory bars, int idx)       {          int profit_level = 0;          System.Drawing.Color color;          double profit = 0.0, code_s = 0.0;          // Series of color codes          // use code_s to find the worst trade for overlapping positions                    // First determine the worst profit level at each bar based on the positions          List<Position> positions = GetPositions();          foreach (Position p in positions)          {             //add line to the Debug Log window             WriteToDebugLog("Bar: " + idx + " Pos Open: " + p.IsOpen + " ExitBar: " + p.ExitBar);             // foreach (Position p in OpenPositions) won't work because it skips the ExitBar             if ( !(p.IsOpen || idx == p.ExitBar) )                continue;                          //can't use p.Profit because the profit isn't calculated yet so it just gives open cost             profit = p.ProfitPctAsOf(idx);             // Profit/Loss in percent             if ((profit >= 0))             {                profit_level = (int)-1;             }             else                // loss                profit_level = (int)-2;             double old_code = 0.0;             // keep most pessimistic in overlapping trades             old_code = (double)code_s;             if ((profit_level < old_code))             {                code_s = profit_level;             }          } // foreach position          // Now set the background color for the bar          int color_code = 0;          color_code = (int)Math.Floor(code_s);          switch (color_code)          {             case -1:                color = Color.LightGreen;                break;             case -2:                color = Color.MistyRose;                break;             default:                color = Color.White;                break;          } // switch   color_code          SetBackgroundColor(bars, idx, color);       } // WinLossBackground              //execute the strategy rules here, this is executed once for each bar in the backtest history       public override void Execute(BarHistory bars, int idx) {          //cycle through all the open positions and put in the sell signals          foreach (Position p in OpenPositions) {     //End trade after a fixed number of days             if (idx + 1 - p.EntryBar > days)                ClosePosition(p, OrderType.Market, 0, "Exit Day");          }          //put the buy orders here          if ((OpenPositions.Count == 0) && (bars.Close[idx] < sma_values[idx]))             PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, "Initial");          //change background colors here          WinLossBackground(bars, idx);           }       //declare private variables below       int days, sma_days;       TimeSeries sma_values;    } }
2
475
0 Replies

Reply

Bookmark

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