mrsic8
 ( 18.59% )
- ago
Hello,
no drawing levels on building Blocks.

Can someone help me with this code (something is wrong), adding drawing levels. See image.
Error: RSI.Series

Thanks.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript2 {    public class RSIDrawingLevels : UserStrategyBase    {       public RSIDrawingLevels() : base()       {          AddParameter("RSILevels", ParameterType.Int32, 10, 10, 100, 5); // Index start at 0          AddParameter("RSIlong", ParameterType.Int32, 70, 10, 100, 5);          StartIndex = 20;       }       public override void Initialize(BarHistory bars)       {          _levelLong = Parameters[0].AsInt;          _RSIindicatorLong = RSI.Series(bars, Parameters[0].AsInt);          PlotIndicator(_RSIindicatorLong, new WLColor(0, 0, 0));          DrawHorzLine(_levelLong, WLColor.Green, 3, LineStyle.Dotted, _RSIindicatorLong.PaneTag);       }       public override void Execute(BarHistory bars, int idx)       {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);          bool condition0;          if (foundPosition0 == null)          {             condition0 = false;             {                if (_RSIindicatorLong[idx] < Parameters[0].AsInt)                {                   condition0 = true;                }             }             if (condition0)             {                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)");             }          }          else          {             condition0 = false;             {                condition0 = true;             }             if (condition0)             {                Backtester.CancelationCode = 351;                ClosePosition(foundPosition0, OrderType.Market, 0, "Sell At Market (1)");             }          }       }       public override void NewWFOInterval(BarHistory bars)       {          _RSIindicatorLong = new RSI(bars.Close, 20);       }       private IndicatorBase _RSIindicatorLong;       private int _levelLong;       private Transaction _transaction;    } }

0
632
Solved
11 Replies

Reply

Bookmark

Sort
mrsic8
 ( 18.59% )
- ago
#1
I found the error.
0
- ago
#2
The error is that _levelLong isn't assigned.

QUOTE:
no drawing levels on building Blocks

No, the Blocks generated code is modified - the error comes from here.

Also, code will not compile:
CODE:
//_RSIindicatorLong = RSI.Series(bars, Parameters[0].AsInt); _RSIindicatorLong = RSI.Series(bars.Close, Parameters[0].AsInt);
0
mrsic8
 ( 18.59% )
- ago
#3
Okay. I did again. Modified Block, adding Levels.
Assigned Levels. But no I am getting short positions only and no drawing levels.
I thought it couldn't be that difficult to build in the levels.
Someone an idea?

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript10 { public class MyStrategy : UserStrategyBase {     public MyStrategy() : base() {          AddParameter("RSILevelsLong", ParameterType.Double, 30, 10, 100, 5); // Index start at 0          AddParameter("RSILevelsShort", ParameterType.Double, 80, 10, 100, 5);           StartIndex = 20; } public override void Initialize(BarHistory bars) {          _levelLong = Parameters[0].AsDouble;          _levelShort = Parameters[1].AsDouble;                    indicator = new RSI(bars.Close,20);          PlotIndicator(indicator,new WLColor(0,0,0));          indicator2 = new RSI(bars.Close,20);          DrawHorzLine(_levelLong, WLColor.Green, 2, LineStyle.Solid, indicator.PaneTag ); DrawHorzLine(_levelShort, WLColor.DarkRed, 2, LineStyle.Solid, indicator2.PaneTag); } public override void Execute(BarHistory bars, int idx) {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);          bool condition0;          if (foundPosition0 == null)          {             condition0 = false;             {                if (indicator[index] < _levelLong)                {                   condition0 = true;                }             }             if (condition0)             {                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)");             }          }          else          {             condition0 = false;             {                condition0 = true;             }             if (condition0)             {                Backtester.CancelationCode = 484;                if (idx - foundPosition0.EntryBar + 1 >= 10)                {                   ClosePosition(foundPosition0, OrderType.Market, 0,"Sell after 10 bars");                }             }          }          Position foundPosition1 = FindOpenPosition(1);          bool condition1;          if (foundPosition1 == null)          {             condition1 = false;             {                if (indicator2[index] > _levelShort)                {                   condition1 = true;                }             }             if (condition1)             {                _transaction = PlaceTrade(bars, TransactionType.Short, OrderType.Market, 0, 1, "Short At Market (2)");             }          }          else          {             condition1 = false;             {                condition1 = true;             }             if (condition1)             {                Backtester.CancelationCode = 486;                if (idx - foundPosition1.EntryBar + 1 >= 5)                {                   ClosePosition(foundPosition1, OrderType.Market, 0,"Cover after 5 bars");                }             }          } } public override void NewWFOInterval(BarHistory bars) {          indicator = new RSI(bars.Close,20);          indicator2 = new RSI(bars.Close,20); }       private double _levelLong;       private double _levelShort;       private IndicatorBase indicator;       private IndicatorBase indicator2;       private Transaction _transaction; } }
0
- ago
#4
QUOTE:
But no I am getting short positions only

Bet you can make a complete system in Blocks first and then export it before adding the thresholds (by the way, using Parameters[...].AsInt).
0
- ago
#5
I ran your code using WL NASDAQ 100, daily, for 6 years, $100K capital, 5% equity per position. There were many long and short positions generated. However, the horizontal lines did not appear in the RSI pane. Using WriteToDebugLog($"{indicator2.PaneTag}, {_levelLong}"); shows the correct values (RSI and 30).
0
mrsic8
 ( 18.59% )
- ago
#6
Thanks.
Will test it.
0
- ago
#7
The RSI thresholds are plotted with either .AsInt or .AsDouble, there's no error.
0
- ago
#8
@mrsic - I stated that the horizontal lines did not show up. I was not correct. The mistake I made was that I initially did not look at a dataset's symbol after a back test run. I was looking at an existing chart of a symbol that wasn't part of the back test dataset. Ooops. I double-clicked on a position, scrolled back and forth, and the horizontal lines appear.
0
mrsic8
 ( 18.59% )
- ago
#9
Thanks to both.

It's works after first clicking on another portfolio and then back on the previous one.
0
Cone8
 ( 4.98% )
- ago
#10
fwiw, you can use the indicator's Overbought/Oversold fields to store those values. Here's the code with that change and with the verbosity of the blocks removed -

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript10 {    public class MyStrategy : UserStrategyBase    {       public MyStrategy() : base()       {          AddParameter("RSILevelsLong", ParameterType.Double, 30, 10, 100, 5);          AddParameter("RSILevelsShort", ParameterType.Double, 80, 10, 100, 5);          StartIndex = 20;       }       public override void Initialize(BarHistory bars)       {          _rsi = new RSI(bars.Close, 20);                   _rsi.OversoldLevel = Parameters[0].AsDouble;         //was _levelLong          _rsi.OverboughtLevel = Parameters[1].AsDouble; //was _levelShort          PlotIndicator(_rsi);          DrawHorzLine(_rsi.OversoldLevel, WLColor.Green, 2, LineStyle.Solid, _rsi.PaneTag);          DrawHorzLine(_rsi.OverboughtLevel, WLColor.DarkRed, 2, LineStyle.Solid, _rsi.PaneTag);       }       public override void Execute(BarHistory bars, int idx)       {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);                   if (foundPosition0 == null)          {             if (_rsi[index] < _rsi.OversoldLevel)             {                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)");             }          }          else          {             Backtester.CancelationCode = 484;             if (idx - foundPosition0.EntryBar + 1 >= 10)             {                ClosePosition(foundPosition0, OrderType.Market, 0, "Sell after 10 bars");             }          }                    Position foundPosition1 = FindOpenPosition(1);                   if (foundPosition1 == null)          {             if (_rsi[index] > _rsi.OverboughtLevel)             {                _transaction = PlaceTrade(bars, TransactionType.Short, OrderType.Market, 0, 1, "Short At Market (2)");             }          }          else          {             Backtester.CancelationCode = 486;             if (idx - foundPosition1.EntryBar + 1 >= 5)             {                ClosePosition(foundPosition1, OrderType.Market, 0, "Cover after 5 bars");             }          }       }       public override void NewWFOInterval(BarHistory bars)       {                   _rsi = new RSI(bars.Close, 20);       }       private IndicatorBase _rsi;       private Transaction _transaction;    } }
0
Best Answer
mrsic8
 ( 18.59% )
- ago
#11
Great Cone. Many Thanks :-)
0

Reply

Bookmark

Sort