Skip to content

Instantly share code, notes, and snippets.

@sherwind
Created April 29, 2018 06:04
Show Gist options
  • Save sherwind/7c76d11e144692124a9d6e69aa4e9b33 to your computer and use it in GitHub Desktop.
Save sherwind/7c76d11e144692124a9d6e69aa4e9b33 to your computer and use it in GitHub Desktop.
Dynamic Zones
//@version=3
//
// A port of the MT4 indicator of Mladen Rakic found at https://www.mql5.com/en/forum/179876
// which is based on Dynamic Zones that was originally published in Stocks & Commodities 1996 issue.
//
// Dynamic Zones is meant to be applied to oscillators to get dynamic overbought and oversold levels
// quantified using statistical methods.
//
//
// -----------------------------------------------------------------------------
// Copyright 2018 sherwind
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// The GNU General Public License can be found here
// <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
//
study("Dynamic Zones")
input_src = input(close, title="Price Source", type=source)
input_lookbackbars = input(70, title="Lookback Bars", minval=1)
input_startbuyprobability = input(0.12, title="Start Buy Probability")
input_startsellprobability = input(0.12, title="Start Sell Probability")
dzsell(src, initvalue, lookbackbars) =>
left = -10000.0
right = 10000.0
eps = 0.001
yval = (left + right)/2.0
delta = yval - left
maxsteps = 0
for i = 0 to 99999
if not (delta > 0.005 and maxsteps < 50)
break
maxsteps := maxsteps + 1
count = 0
for k = 0 to lookbackbars - 1
if src[k] > yval
count := count + 1
prob = count / lookbackbars
if prob > (initvalue + eps)
left := yval
yval := (yval + right)/2.0
if prob < (initvalue - eps)
right := yval
yval := (yval + left)/2.0
if prob < (initvalue + eps) and prob > (initvalue - eps)
left := yval
yval := (yval + right)/2.0
delta := yval - left
[yval, delta, maxsteps]
dzbuy(src, initvalue, lookbackbars) =>
left = -10000.0
right = 10000.0
eps = 0.001
yval = (left + right)/2.0
delta = yval - left
maxsteps = 0
for i = 0 to 99999
if not (delta > 0.005 and maxsteps < 50)
break
maxsteps := maxsteps + 1
count = 0
for k = 0 to lookbackbars - 1
if src[k] < yval
count := count + 1
prob = count / lookbackbars
if prob > (initvalue + eps)
right := yval
yval := (yval + left)/2.0
if prob < (initvalue - eps)
left := yval
yval := (yval + right)/2.0
if prob < (initvalue + eps) and prob > (initvalue - eps)
right := yval
yval := (yval + left)/2.0
delta := yval - left
[yval, delta, maxsteps]
[sell_zone, sell_delta, sell_maxsteps] = dzsell(input_src, input_startsellprobability, input_lookbackbars)
[buy_zone, buy_delta, buy_maxsteps] = dzbuy(input_src, input_startbuyprobability, input_lookbackbars)
upper_band = plot(sell_zone, title="Sell Zone", color=gray)
lower_band = plot(buy_zone, title="Buy Zone", color=gray)
fill(upper_band, lower_band, color=purple, transp=90)
plot(input_src, color=purple, transp=0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment