this is my polished version of the addition to pSAR:
#============ PLOT OF INFORMATION REGARDING pSAR AND PRICE RELATION ============================
input ShowLabel = yes;
# PAST STATES pSAR LEVELS AND PERCENTAGE DISTANCE OF CLOSE TO THEM AT STATE CHANGE TIME*******************
def StateChange = if state == state.long and state[1] == state.short then 1 else if state == state.short and state[1] == state.long then -1 else 0;
def sar_level = if statechange <> 0 then sar else sar_level[1];
plot level = if ShowLabel and !IsNaN(close) then sar_level else Double.Nan;
AddChartBubble(StateChange == 1 and ShowLabel, sar*1.000, round(-(level-close)/level*100,2) +"% $"+ round(level,2), Color.UPTICK, no);
AddChartBubble(StateChange == -1 and ShowLabel,sar*1.000, round(-(level-close)/level*100,2) +"% $"+round(level,2),Color.DOWNTICK ,yes);
# COUNT OF PERIODS AT EACH OF PAST STATES ***************************************************************
def longcount = if StateChange == 1 then 1 else if state == state.long then longcount[1] + 1 else 0 ;
AddChartBubble(state == state.long and state[-1] == state.short, level, longcount+" "+round((close - level)/level*100,2)+"%", Color.UPTICK,yes);
def shortcount = if StateChange == -1 then 1 else if state == state.short then shortcount[1] + 1 else 0 ;
AddChartBubble(state == state.short and state[-1] == state.long, level, shortcount+" "+round((close - level)/level*100,2)+"%", Color.DOWNTICK,no);
# COUNT OF PERIODS AT CURRENT STATE ***************************************************************
AddChartBubble(state == state.long and isnan(close[-1]), level, longcount, Color.CYAN,yes);
AddChartBubble(state == state.short and isnan(close[-1]), level, shortcount, Color.YELLOW,no);
# CURRENT pSAR LEVEL AND PERCENTAGE DISTANCE OF CLOSE TO IT ****************************************
AddChartBubble(state == state.long and isnan(close[-1]), parsar, round(-(sar-close)/sar*100,2) +"% $"+round(parsar,2), Color.CYAN,no);
AddChartBubble(state == state.short and isnan(close[-1]), parsar, round(-(sar-close)/sar*100,2) +"% $"+round(parsar,2), Color.YELLOW,yes);
View attachment 19969
View attachment 19970
this code added to the built in Parabolic SAR, which I did for my own use, will provide more detailed information to, not only past pSAR behavior related to price as well as more current information, including a simple projection for next period pSAR. you can create a new indicator using the original built-in code for the pSAR and add at the end the code below:
########################################################################################
###################### BELOW IS ADDITIONAL CODE TO BUILT-IN parabolicSAR ###############
########################################################################################
parSAR.SetPaintingStrategy(PaintingStrategy.DASHES);
#parSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
parSAR.SetLineWeight(1);
parSAR.DefineColor("Up", Color.CYAN);
parSAR.DefineColor("Down", Color.YELLOW);
parSAR.DefineColor("Flat", Color.GRAY);
parSAR.AssignValueColor(if (SAR < low) then parSAR.Color("Up") else parSAR.Color("Down") );
# =================== COLOR TREND IN PRICE GRAPH====================================
def up = SAR < low ;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
DefineGlobalColor("dgreen" , CreateColor(0,59,0));
DefineGlobalColor("dred" , CreateColor(79,0,0));
AddCloud(if up then pos else neg, if up then neg else pos, GlobalColor("dgreen"), GlobalColor("dred"));
#============ PLOT OF INFORMATION REGARDING pSAR AND PRICE RELATION ============================
input ShowLabel = yes;
# PAST STATES pSAR LEVELS AND PERCENTAGE DISTANCE OF CLOSE TO THEM AT STATE CHANGE TIME*******************
def StateChange = if state == state.long and state[1] == state.short then 1 else if state == state.short and state[1] == state.long then -1 else 0;
def sar_level = if statechange <> 0 then sar else sar_level[1];
plot level = if ShowLabel and !IsNaN(close) then sar_level else Double.Nan;
level.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
level.SetLineWeight(1);
level.AssignValueColor(color.WHITE);
AddChartBubble(StateChange == 1 and ShowLabel and !isnan(close[-1]), sar*1.000, round(-(level-close)/level*100,2) +"% $"+ round(level,2), Color.UPTICK, no);
AddChartBubble(StateChange == -1 and ShowLabel and !isnan(close[-1]),sar*1.000, round(-(level-close)/level*100,2) +"% $"+round(level,2),Color.DOWNTICK ,yes);
# COUNT OF PERIODS AT EACH OF PAST STATES ***************************************************************
def longcount = if StateChange == 1 then 1 else if state == state.long then longcount[1] + 1 else 0 ;
AddChartBubble(state == state.long and state[-1] == state.short, level, longcount+" "+round((close - level)/level*100,2)+"% sar=> " + round((sar - level)/level*100,2)+"%" , Color.UPTICK,yes);
def shortcount = if StateChange == -1 then 1 else if state == state.short then shortcount[1] + 1 else 0 ;
AddChartBubble(state == state.short and state[-1] == state.long, level, shortcount+" "+round((close - level)/level*100,2)+"% sar=> " + round((sar - level)/level*100,2)+"%", Color.DOWNTICK,no);
# COUNT OF PERIODS AT CURRENT STATE ***************************************************************
AddChartBubble(state == state.long and isnan(close[-1]) and ShowLabel, level, longcount + " close => $" + round(-(level-close)/level*100,2) +"%", Color.CYAN,yes);
AddChartBubble(state == state.short and isnan(close[-1]) and ShowLabel, level, shortcount + " close => $" + round(-(level-close)/level*100,2) +"%", Color.YELLOW,no);
# CURRENTR pSAR LEVEL AND PERCENTAGE DISTANCE OF CLOSE TO IT ****************************************
AddChartBubble(state == state.long and isnan(close[-1]) and ShowLabel, parsar, round(-(sar-close)/sar*100,2) +"% $"+round(parsar,2) + " <= pSAR level", Color.CYAN,no);
AddChartBubble(state == state.short and isnan(close[-1]) and ShowLabel, parsar, round(-(sar-close)/sar*100,2) +"% $"+round(parsar,2) + " <= pSAR level", Color.YELLOW,yes);
# Simple Linear Projection estimate for next future period ********************************************
def angle = (parsar - parsar[1]);
def proj = angle+parsar;
def ook = parSAR == level ;
def angle_percent = ((proj-parSAR) /parSAR)*100;
plot projection = if isnan(close[-1]) and isnan(close) and !ook[1] and ShowLabel then proj[1] else Double.Nan;
AddChartBubble(isnan(close[-1]) and isnan(close) and ShowLabel , projection,"$"+ round(projection,2)+" "+ round(angle_percent[1],2)+"%"+" <= next stop ", Color.white,no);
projection.SetPaintingStrategy(PaintingStrategy.SQUARES);
projection.SetLineWeight(4);
projection.AssignValueColor(color.WHITE);
#######################################################################
Last edited: