Henk8
- ago
0
428
Solved
9 Replies

Reply

Bookmark

Sort
Glitch8
 ( 11.82% )
- ago
#1
It should be an easy Building Block exercise.

Indicator compare to Indicator
Close > BBandUpper
Qualifier 1 bar ago

Indicator compare to Indicator
Close < BbandUpper
Indicator Compare to indicator
Close > BbandLower
0
Best Answer
- ago
#2
I converted it. But I would compare your Block strategy results with the C# coded results to be sure both are the same.

This isn't a trading strategy, so it doesn't behave like a normal trading strategy.

CODE:
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; //using Superticker.Components; /* WLUtil.HideVolume here */ namespace WealthScript2 {    //wl6.wealth-lab.com/Forum/Posts/Scanning-for-pattern-close-outside-Bollinger-band-35552    //Here's the Bollinger Bands scanner. Switch bar scale to Monthly before using    public class BbScanner : UserStrategyBase    {       Parameter period, stdDev;       IndicatorBase bbL, bbU;       int lastBar;       public BbScanner()       {          period = AddParameter("BB period", ParameterType.Int32, 16, 10, 40, 2);          stdDev = AddParameter("BB stdDev", ParameterType.Double, 2.0, 1.0, 3.0, 0.5);          //SetChartDrawingOptions(WLUtil.HideVolume());       }       public override void Initialize(BarHistory bars)       {          bbL = BBLower.Series(bars.Close, period.AsInt, stdDev.AsDouble);          bbU = BBUpper.Series(bars.Close, period.AsInt, stdDev.AsDouble);          PlotIndicatorBands(bbU, bbL, WLColor.Magenta,1,10);          lastBar = bars.Count-1;       }       public override void Execute(BarHistory bars, int idx)       {          if (idx == lastBar && lastBar >= period.AsInt)          {             //The first candle CLOSES outside the upper Bollinger Band.             //After that, the second candle must CLOSE inside the Bollinger Bands AND must be a red candle.             if (bars.Close[lastBar-1] > bbU[lastBar-1]              && bars.Close[lastBar] < bbU[lastBar]              && bars.Close[lastBar] < bars.Open[lastBar])                PlaceTrade(bars,TransactionType.Buy,OrderType.Market);          }       }    } }
0
Henk8
- ago
#3
Thank you.
0
Henk8
- ago
#4
Sorry, i have made a mistake in my question.
Close outside band and then higher low inside the band.
One of the setups mentionned in the Bollinger book.


0
- ago
#5
Well, now we are talking about a trading strategy rather than a scanner. The WL6 code was for a scanner.

And for this setup, how many bars are allowed (bar range) between these two events discussed above? Some bar range needs to be allowed for the TimeSeries.BarsSince(??) argument.

What's the exit rules?
0
- ago
#6
I tried to code something, but the entry rules are too simple. If you look at the BackTest Results > Positions tab, you'll notice many "Bad Buy..." signals, which is telling us the strategy got into the trade only to immediately exit. In other words, the entry rules need to consider more variables than what we are doing here. You need to add many more entry conditions. But you can play with the code.

You might want to convert this to Blocks so you can play with it.

The exit rules I included seem to work okay.

CODE:
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; using WealthLab.TASC; /* DSMA indicator here */ //using Superticker.Components; /* WLUtil.HideVolume here */ namespace WealthScript1 {    public class BbTrader : UserStrategyBase    {       Parameter period, stdDev, dsmaPeriod, barsXitThreshold;       IndicatorBase bbL, bbU, bbUSell, derivativeLows, smoothedMomentum, barsMomentumFalls;       TimeSeries barsAfterCloseDip;       public BbTrader()       {          period = AddParameter("BB period", ParameterType.Int32, 16, 10, 40, 2);          stdDev = AddParameter("BB stdDev", ParameterType.Double, 2.0, 1.0, 3.0, 0.5);          dsmaPeriod = AddParameter("DSMA period", ParameterType.Int32, 12, 5, 25);          barsXitThreshold = AddParameter("#bars Xit threshold", ParameterType.Double, 4.0, 1.0, 6.0);          //SetChartDrawingOptions(WLUtil.HideVolume());       }       public override void Initialize(BarHistory bars)       {          bbL = BBLower.Series(bars.Close, period.AsInt, stdDev.AsDouble);          bbU = BBUpper.Series(bars.Close, period.AsInt, stdDev.AsDouble);          bbUSell = BBUpper.Series(bars.Close, period.AsInt, stdDev.AsDouble*0.75);          PlotIndicatorBands(bbU, bbL, WLColor.Magenta,1,10);          barsAfterCloseDip = TimeSeries.BarsSince(bars.Close < bbL);          PlotTimeSeries(barsAfterCloseDip, "Bars since Close dip", "Bars since Close dip", WLColor.Tan, PlotStyle.Histogram);          derivativeLows = Momentum.Series(bars.Low, 3);          TimeSeries momentum = new Momentum(bars.Close, 1);          smoothedMomentum = DSMA.Series(momentum, dsmaPeriod.AsInt);          smoothedMomentum.Description = "Smoothed momentum oscillator";          barsMomentumFalls = ConsecDown.Series(smoothedMomentum, 1);          barsMomentumFalls.Description = "Consecutive bars momentum falls";       }       public override void Execute(BarHistory bars, int idx)       {          if (HasOpenPosition(bars, PositionType.Long))          {             //sell conditions below             if (bars.Close[idx] < LastOpenPosition.EntryPrice)                PlaceTrade(bars, TransactionType.Sell, OrderType.Market, 0.0, "Bad Buy; immediate Sell");             else if (barsMomentumFalls[idx] >= barsXitThreshold.AsDouble)                PlaceTrade(bars, TransactionType.Sell, OrderType.Market, 0.0, barsMomentumFalls[idx].ToString("#=barsMomentFall Xit"));             else if (bars.Close[idx] > bbUSell[idx])                PlaceTrade(bars, TransactionType.Sell, OrderType.Market, signalName: "BB stdDev*0.75 high Sell");          }          else          {             //buy conditions below             if (derivativeLows[idx] > 0.0 && bars.Close[idx] > bbL[idx]                && barsAfterCloseDip[idx] < 6.0)                PlaceTrade(bars, TransactionType.Buy, OrderType.Market, signalName: "BB increasing Buy");          }       }    } }
0
Henk8
- ago
#7
How many bars? Between 7 and 14.
You see this setup al lot of times, it looks like a 123 bottom.

Thank you for the effort. I will take a look at it.
0
- ago
#8
QUOTE:
Between 7 and 14.

I coded it for <6 bars as you can see. If you get this number too high you won't get any trades.
CODE:
&& barsAfterCloseDip[idx] < 6.0)

QUOTE:
... this setup a lot of times, it looks like a 123 bottom.

I don't know what "123" means. You just need to modify the code.

I would convert it to Blocks (which you're more familiar with), then you can play with it. Converting the momentum exit to Blocks might be involved. You might consider using a simpler exit approach.

I tried adding an atrFactor for the entry, but it didn't help.
CODE:
   if (derivativeLows[idx] > atr22[idx]*atrFactor.AsDouble && bars.Close[idx] > bbL[idx]       && barsAfterCloseDip[idx] < 6.0)       PlaceTrade(bars, TransactionType.Buy, OrderType.Market, signalName: "BB increasing Buy");
1
Cone8
 ( 21.52% )
- ago
#9
Henk, you're going too far with this thread by adding unrelated requests to the starter topic.

... and as soon as I posted that, Eugene already moved it to a new topic :)
3

Reply

Bookmark

Sort