- ago
I am trying to find lower wicks that are larger in size than the candle body. To facilitate this, I created custom indicators that measure the LowerWickLength and the CandleBodyLength. Each indicator used on its own seems to work correctly.

However, when I try to compare these two custom indicators using "Indicator % Above/Below Indicator", it does not work. I run the strategy and it never enters positions. So I click "Open as C# Coded Strategy" and run that, and it gives the following error:

CODE:
Execute Exception (BIP-20DEC30-CDE,0) Line 46 - Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') at WealthLab.Core.DateSynchedList`1.get_Item(Int32 idx) at WealthScript2.MyStrategy.Execute(BarHistory bars, Int32 idx) in :line 46 at WealthLab.Backtest.UserStrategyExecutor.CallWatcher(List`1 lst, DateTime dt)


I have not modified the coded strategy in any way from what was auto-generated. This is what the coded version of the strategy looks like:
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript2 { public class MyStrategy : UserStrategyBase {     public MyStrategy() : base() { StartIndex = 0; } public override void Initialize(BarHistory bars) {          indicator1 = new WickLowerLength(bars);          PlotIndicator(indicator1,new WLColor(0,0,0));          _startIndexList.Add(indicator1);          indicator2 = new CandleBodyLength(bars);          PlotIndicator(indicator2,new WLColor(0,0,255));          _startIndexList.Add(indicator2); foreach(IndicatorBase ib in _startIndexList) if (ib.FirstValidIndex > StartIndex) StartIndex = ib.FirstValidIndex; } public override void Execute(BarHistory bars, int idx) {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);          bool condition0;          if (foundPosition0 == null)          {             condition0 = false;             {                if (index - 0 >= 0)                {                   outcome = false;                   if (indicator2[index - 1] >= 0)                   {                      if (indicator1[index] > indicator2[index - 0] * (1.0 + (10.00 / 100.0)))                         outcome = true;                   }                   else                   {                      if (indicator1[index] < indicator2[index - 0] * (1.0 + (10.00 / 100.0)))                         outcome = true;                   }                }                if (outcome)                {                   condition0 = true;                }             }             if (condition0)             {                Backtester.CancelationCode = 1;                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)");             }          }          else          {             {                Backtester.CancelationCode = 1;                ClosePosition(foundPosition0, OrderType.Market, 0, "Sell At Market (1)");             }          } } public override void NewWFOInterval(BarHistory bars) {          indicator1 = new WickLowerLength(bars);          indicator2 = new CandleBodyLength(bars); }       private IndicatorBase indicator1;       private IndicatorBase indicator2;       private bool outcome;       private Transaction _transaction; private List<IndicatorBase> _startIndexList = new List<IndicatorBase>(); } }


If it's helpful to see the code for the custom indicators, let me know. It's very straightforward, but I didn't want to make this post unnecessarily long and overwhelming.

UPDATE: It seems that the StartIndex just needs to be set to 1 instead of 0. Is there any way I can accomplish that without having to switch to a coded strategy?
0
87
Solved
1 Replies

Reply

Bookmark

Sort
- ago
#1
Okay, I made a change in the Populate() of my custom indicators so that they start at bar = 1 instead of bar = 0, and this seems to result in the StartIndex being handled correctly.
0
Best Answer

Reply

Bookmark

Sort