- ago
Hi,
i am having trouble again transfering a strategy from WL m6.9 to 8. This strategy pulls fundamental data from a csv and initializes trades based on these values. If i run this code i am getting this error message:
" Execute Exception (AAPL,1666) Line 84 - Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
at WealthScript1.MyStrategy.Execute(BarHistory bars, Int32 idx) in :line 84
at WealthLab.Backtest.UserStrategyExecutor.DeleteConsumer(List`1 lst, DateTime dt)
"
I cant find the problem. Could you please help me with this ?

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; using WealthLab.TASC; using System.IO; using System.Globalization; namespace WealthScript1 { public class MyStrategy : UserStrategyBase {     public MyStrategy() : base() {          AddParameter("ROC Length", ParameterType.Int32, 210, 10, 250, 10);           } public override void Initialize(BarHistory bars) {          rocser = new ROC(bars.Close, Parameters[0].AsInt);          PlotIndicator(rocser,new WLColor(0,0,0));          StartIndex = 300; } public override void Execute(BarHistory bars, int idx) {          //Exit          if (HasOpenPosition(bars, PositionType.Long))          {             foreach (Position p in OpenPositions)             {                if (bars.DateTimes[idx-1].IsLastTradingDayOfMonth(bars) &&                   (bars.DateTimes[idx].Month == 1 || bars.DateTimes[idx].Month == 4 || bars.DateTimes[idx].Month == 7 || bars.DateTimes[idx].Month == 10)                   )                {                   ClosePosition(p, OrderType.Market, 0.0, "Timeout");                }                if (!p.IsOpen) continue;                             }          }          //Entry          string datenFile = "C:\\Users\\FundamentalData\\" + bars.Symbol + ".csv";          if (!File.Exists(datenFile))          {             WriteToDebugLog("File not found: " + datenFile.ToString() + ".\r\n");             return;          }          string[] zeile;          string[] lines = File.ReadAllLines(datenFile);          double epsgrowth = 0; double epsq1= 0; double epsq5= 0;                                DateTime merkdatumv = bars.DateTimes[bars.Count-1].AddYears(-30);          DateTime merkdatumv2 = bars.DateTimes[bars.Count-1].AddYears(-30);          DateTime merkdatumq = bars.DateTimes[bars.Count-1].AddYears(-30);          for (int i = 0; i < lines.Length; i++)          {             zeile = lines[i].Split(',');             DateTime quarterdate = DateTime.ParseExact(zeile[2], "yyyy-MM-dd", CultureInfo.InvariantCulture);             DateTime veroffentlichdate = DateTime.ParseExact(zeile[3], "yyyy-MM-dd", CultureInfo.InvariantCulture);             if (zeile[1] == "ARQ" && veroffentlichdate <= bars.DateTimes[idx] && veroffentlichdate > merkdatumv && quarterdate > bars.DateTimes[idx].AddMonths(-6) && zeile[92] != "" )             {                merkdatumv = veroffentlichdate;                merkdatumq = quarterdate;                epsq1= double.Parse(zeile[92]);             }          }          for (int i = 0; i < lines.Length; i++)          {             zeile = lines[i].Split(',');             DateTime quarterdate = DateTime.ParseExact(zeile[2], "yyyy-MM-dd", CultureInfo.InvariantCulture);             DateTime veroffentlichdate = DateTime.ParseExact(zeile[3], "yyyy-MM-dd", CultureInfo.InvariantCulture);             if (zeile[1] == "ARQ" && zeile[92] != "" && quarterdate == merkdatumq.AddYears(-1) && veroffentlichdate > merkdatumv2)             {                merkdatumv2 = veroffentlichdate;                   epsq5 = double.Parse(zeile[92]);             }                       }          if (epsq1 > 0 && epsq5 > 0) epsgrowth = epsq1 / epsq5 ;                    if ( epsgrowth > 0)          {             _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market " + epsgrowth .ToString());             _transaction.Weight = rocser[idx];          }       }       private IndicatorBase rocser;       private Transaction _transaction; } }
0
364
2 Replies

Reply

Bookmark

Sort
- ago
#1
So there's nothing (null) at 93th position of the "zeile" array:
CODE:
epsq1= double.Parse(zeile[92]);
0
- ago
#2
Also, don't do this...

zeile[92] != ""

instead do this...

!string.IsNullOrWhiteSpace(zeile[92])
0

Reply

Bookmark

Sort