Hello.
I am looking to create a watchlist based on the parabolicsar. If the index I am looking at is /ES, I want to plot the parabolicsar of the VIX. If the index I am looking at is the /NQ, I want to plot the parabolicsar of the VXN. Is there a way to program this so that the script identifies the current symbol, and plot the PSAR of the corresponding index?
Current code posted below:
input ticker1 = "VIX";
input TimeFrame1 = AggregationPeriod.TWO_MIN;
def close = Close(Period = TimeFrame1, symbol = ticker1);
def low = Low(Period = TimeFrame1, symbol = ticker1);
def high = High(Period = TimeFrame1, symbol = ticker1);
input accelerationFactor = 0.02;
input accelerationLimit = 0.2;
assert(accelerationFactor > 0, "'acceleration factor' must be positive: " + accelerationFactor);
assert(accelerationLimit >= accelerationFactor, "'acceleration limit' (" + accelerationLimit + ") must be greater than or equal to 'acceleration factor' (" + accelerationFactor + ")");
def state = {default init, long, short};
def extreme;
def SAR;
def acc;
switch (state[1]) {
case init:
state = state.long;
acc = accelerationFactor;
extreme = high;
SAR = low;
case short:
if (SAR[1] < high)
then {
state = state.long;
acc = accelerationFactor;
extreme = high;
SAR = extreme[1];
} else {
state = state.short;
if (low < extreme[1])
then {
acc = min(acc[1] + accelerationFactor, accelerationLimit);
extreme = low;
} else {
acc = acc[1];
extreme = extreme[1];
}
SAR = max(max(high, high[1]), SAR[1] + acc * (extreme - SAR[1]));
}
case long:
if (SAR[1] > low)
then {
state = state.short;
acc = accelerationFactor;
extreme = low;
SAR = extreme[1];
} else {
state = state.long;
if (high > extreme[1])
then {
acc = min(acc[1] + accelerationFactor, accelerationLimit);
extreme = high;
} else {
acc = acc[1];
extreme = extreme[1];
}
SAR = min(min(low, low[1]), SAR[1] + acc * (extreme - SAR[1]));
}
}
def DotsBuy = if high > SAR then 1 else 0;
def Dot_Buy = DotsBuy;
plot buy = Dot_Buy is true;
AddLabel(yes,
if buy >= 0 then "-"
else "-" );
plot data = double.nan;
assignbackgroundColor(if buy == 1 then color.green else color.red);