H

#### HighBredCloud

##### Well-known member

**VIP**

Code:

```
# Parabolic SAR modified with fractal dimension index
# Mobius
# V01.01.2010 V02.09.2013 V03.07.2014 V04.2014 Altered calculations for Fractal dimensions
#hint:<b>Parabolic Stop and Reverse:</b>\n This version is adjusted using a fractal dimension index.\n When price is trending the index will move lower compressing the PSAR's scaling factor and limit factor so that trend reversals are picked up quicker and when the market becomes more balanced and trading the scaling factor and limit factor will widen to lesson whipsaw.\n This same process could be accomplished with ADX or Implied Volatility.
# User Inputs
input n = 8;
input AscaleLow = .01; #hint AscaleLow: Typical range from .01 to .03
input AscaleHigh = .0618;#hint AscaleHigh: Typical range from .03 to .09
input BscaleLow = .1; #hint BscaleLow: Typical range from .1 to .3
input BscaleHigh = .99; #hint BscaleHigh: Typical range from .3 to .9
input Labels = yes;
# Variables
def o;
def h;
def l;
def c;
def TR;
#def A;
def B;
def Ascaled;
def Bscaled;
# Internal Script
script Scale {
input c = close;
input Min = .01;
input Max = 1;
def hh = HighestAll(c);
def ll = LowestAll(c);
plot Range = (((Max - Min) * (c - ll)) / (hh - ll)) + Min;
}
# Calculations
o = (open[1] + close[1]) / 2;
h = Max(high, close[1]);
l = Min(low, close[1]);
c = (o + h + l + close) / 4;
TR = Max(h, c[1]) - Min(l, c[1]);
# A = Log(Sum(TR, n)) / (Highest(c, n) - Lowest(c, n)) / Log(n);
# Ascaled = Round(Scale(c = A, min = AscaleLow, max = AscaleHigh), 3);
B = (Log(Sum(TR, n) / (Highest(h, n) - Lowest(l, n)))
/ Log(10)) / (Log(n) / Log(10));
Ascaled = Round(Scale(c = B, min = AscaleLow, max = AscaleHigh), 3);
Bscaled = Round(Scale(c = B, min = BscaleLow, max = BscaleHigh), 2);
AddLabel(Labels, "A Factor Scaled: " + Ascaled, Color.WHITE);
AddLabel(Labels, "B Factor Scaled: " + Bscaled, Color.WHITE);
AddLabel(Labels, if Bscaled > Bscaled[1] and Bscaled > .5
then "Trading"
else if Bscaled < Bscaled[1] and Bscaled < .618
then "Trending"
else if Bscaled > .618
then "Trading"
else if Bscaled < .382
then "Trending"
else "",
if Bscaled < Bscaled[1] and Bscaled < .618
then Color.Green
else if Bscaled < .382
then Color.Green
else if Bscaled > .382 and Bscaled > Bscaled[1]
then Color.Red
else if Bscaled > .618
then Color.Red
else Color.Yellow);
def state = {default init, long, short};
def extreme;
def SAR;
def acc;
switch (state[1]) {
case init:
state = state.long;
acc = Ascaled;
extreme = h;
SAR = l;
case short:
if (SAR[1] < h)
then {
state = state.long;
acc = Ascaled;
extreme = h;
SAR = extreme[1];
} else {
state = state.short;
if (l < extreme[1])
then {
acc = Min(acc[1] + Ascaled, Bscaled);
extreme = l;
} else {
acc = acc[1];
extreme = extreme[1];
}
SAR = Max(Max(h, h[1]), SAR[1] + acc * (extreme - SAR[1]));
}
case long:
if (SAR[1] > l)
then {
state = state.short;
acc = Ascaled;
extreme = l;
SAR = extreme[1];
} else {
state = state.long;
if (h > extreme[1])
then {
acc = Min(acc[1] + Ascaled, Bscaled);
extreme = h;
} else {
acc = acc[1];
extreme = extreme[1];
}
SAR = Min(Min(l, l[1]), SAR[1] + acc * (extreme - SAR[1]));
}
}
# Chart Management
AssignPriceColor(if SAR > h then Color.RED else Color.GREEN);
# End Code PSAR Auto-Adjusted
```