Tutorial 204 | looking for conditions to occur within specified numbers of bars for each other

  • 2 months ago
A Gold Pass asked for a tutorial about how to write a program that looked for three conditions to occur, in order. The first condition should occur, then the second condition should occur within a specified number of bars of the first. Finally a third condition would occur within a certain number of bars of the second.

See the tutorial page: https://markplex.com/free-tutorials/tutorial-204-looking-for-conditions-to-occur-within-specified-numbers-of-bars-for-each-other/
Transcript
00:00Hello and welcome to Tutorial 204.
00:03I was asked by a GoldPass member
00:05how you'd go about programming a situation
00:07where you wanted something to happen
00:09and then within a certain number of bars
00:11something else had to happen
00:12and then within a certain number of bars
00:14something else had to happen
00:16and then you could plot
00:18or you could enter a trade, et cetera.
00:20So that's what we're doing in this example.
00:22And the first thing we're going to look at
00:24is a Show Me Study.
00:26And what that Show Me Study is doing
00:27it's looking for a cross of the moving average
00:31and then within so many bars
00:32it's looking for a doji.
00:35And in fact, you can see
00:37there's a doji immediately afterwards
00:39and then one after that.
00:41And then within a certain number of bars
00:43it's looking for a bearish harami
00:45and you'll see we've got one of those here
00:48and that is indicated by the red dot.
00:50So if we just look at the
00:53what's going on with the program
00:55I'm just going to go to the program
00:58and in fact, we've got two programs here.
01:01We've got the Show Me Study
01:05and we've also got a strategy.
01:07So let's just look at the Show Me Study
01:10very, very basic, simple stuff
01:13using standard moving average functions
01:16and standard doji functions.
01:18But the guts of the program
01:20are that we have some counters,
01:22we have two counters
01:23and these counters are incremented by one every bar
01:28come what may they're incremented.
01:31But what happens, what I've done in this program
01:34is said, if we get a fast average cross
01:37over a slow average, then we reset counter one to zero.
01:42We also plot the green dot.
01:45Then if a doji occurs
01:48and what the counter has to be less than or equal to five
01:54then if doji occurs,
01:55which we're using the standard function here
01:58then we reset counter two.
02:01We also do a dot.
02:03So it's green dot for the cross moving average
02:05cyan dot for the doji.
02:08And then we're looking for our bearish harami.
02:10So we're now looking at counter two.
02:12Counter two of course is incrementing every bar.
02:15And we simply say if counter two is less than 25
02:18and it's a bullish harami
02:20then plot the plot,
02:24draw a plot with red color.
02:27And that is as simple as the program is.
02:30So any other bullish harami
02:32is not going to appear on the chart.
02:35So that is the first part of the program.
02:38What I also did is said, okay,
02:40well, if you get the moving average cross,
02:42the doji and the bearish harami,
02:44then we want to get into a trade the next bar.
02:48So what I've done is created
02:50a very simple strategy version of the program,
02:53which is exactly the same,
02:55apart from obviously we're not drawing the dots anymore.
02:59But what we do is we say,
03:01if counter two is less than 25 and it's bullish harami,
03:04then we buy a contract next bar at market.
03:09So there's no particular reason I've used
03:11moving average cross doji and bullish harami,
03:16just really to demonstrate this technique,
03:18which can be quite useful in a number of circumstances.
03:23In the strategy,
03:24what I've also done is set the profit target
03:26and set the stop loss that enables us
03:29to get into some trades and then get out of them.
03:32So if I just go to the studies,
03:34to the edit strategies and turn on the strategy,
03:40set the status to on,
03:42then you'll see that we don't get the dots there
03:45done by the show me study,
03:46but you'll see that we get after the bearish harami red dot,
03:50we then get into a trade
03:53and that's stopped out a few bars later.
03:57So that is the technique.
03:59I hope it might be useful for you.
04:01Incidentally, what I've done is included
04:03these very simple programs,
04:05just for you to copy and paste
04:08into your own easy language editor.
04:11And of course you use these programs
04:13at your own risk as always.
04:15Anyway, thank you for watching.
04:17If you find this stuff useful,
04:18then please subscribe to markplex.com
04:21and also join the YouTube channel.
04:25Thank you.

Recommended