- ago
I did a graphical analysis and would like to test my strategies only in certain periods, for example: between 04/18/22 at 18:00 until 04/20/22 at 18:00 and also between 05/04/22 at 07:00: 00 until 05/05/22 at 12:00. How to write this in code?
0
406
Solved
8 Replies

Reply

Bookmark

Sort
Glitch8
 ( 10.62% )
- ago
#1
Hi lucas, In WL we placed the data range control in the strategy settings section. You can even save a date range and give it a name, so you can quickly use multiple ranges when backtesting. We felt this was more flexible than making you have to set this in the strategy code. For one reason, it can work on other strategy types as well.

0
- ago
#2
Ok... I see, but it's not what I am looking for...

I have several periods and several strategies to run in the optimize function, so the easist way would be copy and paste this periods in their codes, would that be possible?
0
- ago
#3
Well, you can do it in C# code.

CODE:
public override void Execute(BarHistory bars, int idx) {          if(bars.DateTimes[idx].Date > ...
0
- ago
#4
Returns an error: operator ">" cannot be applied to operands of type "DateTime" and "int"



CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; using WealthLab.MyIndicators; namespace WealthScript2 { public class MyStrategy : UserStrategyBase {     public MyStrategy() : base() {          AddParameter("Media01", ParameterType.Int32, 20, 5, 7, 2);          AddParameter("Media02", ParameterType.Int32, 20, 10, 14, 4);          AddParameter("Media03", ParameterType.Int32, 20, 20, 21, 1);          AddParameter("Media04", ParameterType.Int32, 20, 28, 30, 2); } public override void Initialize(BarHistory bars) {          indicator1 = new CustomSMA(bars.Close,Parameters[0].AsInt);          PlotIndicator(indicator1,new WLColor(0,0,0));          indicator1.MassageColors = true;          indicator2 = new CustomSMA(bars.Close,Parameters[1].AsInt);          indicator12 = new CustomSMA(bars.Close,Parameters[1].AsInt);          indicator22 = new CustomSMA(bars.Close,Parameters[2].AsInt);          indicator13 = new CustomSMA(bars.Close,Parameters[2].AsInt);          indicator23 = new CustomSMA(bars.Close,Parameters[3].AsInt);          indicator14 = new CustomSMA(bars.Close,Parameters[0].AsInt);          indicator24 = new CustomSMA(bars.Close,Parameters[1].AsInt);          indicator15 = new CustomSMA(bars.Close,Parameters[1].AsInt);          indicator25 = new CustomSMA(bars.Close,Parameters[2].AsInt);          indicator16 = new CustomSMA(bars.Close,Parameters[2].AsInt);          indicator26 = new CustomSMA(bars.Close,Parameters[3].AsInt);          StartIndex = 20; } public override void Execute(BarHistory bars, int idx) {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);          bool condition0;          if (foundPosition0 == null && bars.DateTimes[idx] > 20220404)          {             condition0 = false;             {                if (index - 0 >= 0 && indicator1[index] > indicator2[index - 0])                {                   if (index - 0 >= 0 && indicator12[index] > indicator22[index - 0])                   {                      if (index - 0 >= 0 && indicator13[index] > indicator23[index - 0])                      {                         condition0 = true;                      }                   }                }             }             if (condition0)             {                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)");             }          }          else          {             condition0 = false;             {                if (index - 0 >= 0 && indicator14[index] < indicator24[index - 0])                {                   if (index - 0 >= 0 && indicator15[index] < indicator25[index - 0])                   {                      if (index - 0 >= 0 && indicator16[index] < indicator26[index - 0])                      {                         condition0 = true;                      }                   }                }             }             if (condition0)             {                Backtester.CancelationCode = 60;                ClosePosition(foundPosition0, OrderType.Market, 0, "Sell At Market (1)");             }          } } public override void NewWFOInterval(BarHistory bars) {          indicator1 = new CustomSMA(bars.Close,Parameters[0].AsInt);          indicator2 = new CustomSMA(bars.Close,Parameters[1].AsInt);          indicator12 = new CustomSMA(bars.Close,Parameters[1].AsInt);          indicator22 = new CustomSMA(bars.Close,Parameters[2].AsInt);          indicator13 = new CustomSMA(bars.Close,Parameters[2].AsInt);          indicator23 = new CustomSMA(bars.Close,Parameters[3].AsInt);          indicator14 = new CustomSMA(bars.Close,Parameters[0].AsInt);          indicator24 = new CustomSMA(bars.Close,Parameters[1].AsInt);          indicator15 = new CustomSMA(bars.Close,Parameters[1].AsInt);          indicator25 = new CustomSMA(bars.Close,Parameters[2].AsInt);          indicator16 = new CustomSMA(bars.Close,Parameters[2].AsInt);          indicator26 = new CustomSMA(bars.Close,Parameters[3].AsInt); }       private IndicatorBase indicator1;       private IndicatorBase indicator2;       private IndicatorBase indicator12;       private IndicatorBase indicator22;       private IndicatorBase indicator13;       private IndicatorBase indicator23;       private IndicatorBase indicator14;       private IndicatorBase indicator24;       private IndicatorBase indicator15;       private IndicatorBase indicator25;       private IndicatorBase indicator16;       private IndicatorBase indicator26;       private Transaction _transaction; } }
0
Glitch8
 ( 10.62% )
- ago
#5
That’s right. You can’t compare a .Net DateTime to a numeric constant. We’re getting into basic C# here, you’ll need to create another DateTime instance using your numeric constant and then compare the two DateTime instances.
0
- ago
#6
Hope you get the picture.
CODE:
...bars.DateTimes[idx].Date > new DateTime(2022,04,04)...
1
Best Answer
- ago
#7
It worked, I put it this way:

CODE:
if ((bars.DateTimes[idx].Date >= new DateTime(2022, 04, 19) && bars.DateTimes[idx].Date < new DateTime(2022, 04, 21) ) || (bars.DateTimes[idx].Date == new DateTime(2022, 05, 04)) || (bars.DateTimes[idx].Date == new DateTime(2022, 05, 30)))


Sorry for the silly questions, I started studying C# last week, just to make better use of Wealth Lab. An amazing tool and even better support.

Thank you very much guys ;)
1
Glitch8
 ( 10.62% )
- ago
#8
Thank you and glad to help!
1

Reply

Bookmark

Sort