How to show one arrow on plot for the first time condition is met?

ssaeed73

New member
Hi guy,

Hoping someone can help me out. I have a study where I look for a long and a short condition to be met. The long and short conditions can occur multiple times in succession. For example, and I am making this up, I am looking at the 9EMA>13EMA and so every bar if that is true the long condition is met. Now I just want to put an arrow on the plot the first time the condition is met and not show it again until after the short condition occurs. Likewise for the short condition only one arrow and no more until a long condition occurs.

is there a simple way to do this? I use the long and short conditions for a strategy and it correctly plots the entry and exit arrows once but when I use the code as a study and add Up or Down arrows for the same conditions I see that the arrows are repeated multiple times.

Thanks,
 

rad14733

Well-known member
VIP
@ssaeed73 There really is no simple way to do what you are asking... Have you tried thinking through the logic entailed as it pertains to Thinkscript...??? The only way I can imagine tackling this task would require a recursion routine that keeps checking to see if an active long or short crossover has occurred in the past without a negating opposite crossover... And if things get out of sync you could potentially never get another valid indication... That is why you see most indicators showing every occurrence of conditions being met... Perhaps not the answer you wanted to hear but that's the reality of the limitations of coding logic to only show some conditions being met but not all...
 

MerryDay

Well-known member
VIP
@ssaeed73 How about something like this?

Code:
# ########################################################
# TOS Moving Averages
plot ema9  = ExpAverage(close, 9);
plot ema13  = ExpAverage(close, 13);
AddCloud(ema9, ema13, Color.green, Color.orange);

def prange = high – low;
def plotHigh = high + prange * 0.3;
def plotLow  = low  - prange * 0.3;

plot crossUP = if ema9 crosses above ema13 then plotLow else double.NaN ;
plot crossDN = if ema9 crosses below ema13 then plotHigh else double.NaN ;

crossUP.SetDefaultColor(color.cyan);
crossUP.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
crossUP.SetLineWeight(2);

crossDN.SetDefaultColor(Color.magenta);
crossDN.SetLineWeight(2);
crossDN.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
# ########################################################
 

ssaeed73

New member
MeryDay, that would work if my event only happened once, like a crossing of the EMAs as in your script above. In my case though there are several factors that are going into the condition and as a result it hold true over several bars, not a binary one time event like a crossing.

Is there no way to store a state in a variable that would notice that the condition went positive and not to show the arrow until the opposite condition becomes true? This variable would need to know the condition across multiple bars.
 

Pensar

Active member
VIP
@ssaeed73 This is a method that may help you. Assign your conditions to the respective "up" or "down" variables - the arrows should alternate back and forth, with only 1 arrow per direction each time.

Code:
def up = your condition 1;
def down = your condition 2;

def Hold_Up_Value = if up then 1 else if !down then Hold_Up_Value[1] else 0;
def Hold_Dn_Value = if down then 1 else if !up then Hold_Dn_Value[1] else 0;

plot Signal_Up = if up and Hold_Dn_Value[1] == 1 then low else Double.NaN;
Signal_Up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
plot Signal_Dn = if down and Hold_Up_Value[1] == 1 then high else Double.NaN;
Signal_Dn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
 

ssaeed73

New member
@Pensar could you please help me out one more time? I have the following code implemented for my watchlist now. How can I set it up so it doesn't show me NaN in the watchlist window all the time? I would like the window to be black and when SignalUp condition is met it shades the cell green as in the code below. My understanding is your code is setting Signal_UP to the low value of the stock otherwise NaN. How to modify this to be binary 1 or 0? Do I just change Low to "1" and "else Double.NaN" to 0?

Code:
def up = if isopen then Long5 else 0;
def down = FiveMinExit;

def Hold_Up_Value = if up then 1 else if !down then Hold_Up_Value[1] else 0;
def Hold_Dn_Value = if down then 1 else if !up then Hold_Dn_Value[1] else 0;

plot Signal_Up = if up and Hold_Dn_Value[1] == 1 then low else Double.NaN;

AddLabel(Signal_Up, "Buy", color.Black);
AddLabel(!Signal_Up, " ", color.black);

AssignBackgroundColor(if Signal_Up then color.green else color.black);
 

CountVlad

New member
I'm trying to figure out a way of keeping track when I get an enter signal and when i get an exit signal. I want to count or keep track of when i get a signal so i can only have one Exit down arrow. If i dont get an Entry Signal then there should be no Exit signal. I'm trying to avoid having numerous exit down arrows on my chart... So, if you were to look at the chart there should be a pair of Entry and Exit Signals.

Here's what i'm talking about:

Code:
declare upper;

def a = MovAvgTwoLines("fast length" = 35)."fastAvg" crosses above MovAvgTwoLines("slow length" = 200)."slowAvg";
def b = MovAvgTwoLines("fast length" = 35)."fastAvg" crosses above reference VWAP()."VWAP";

def count = If Enter then count or keep track of Enter Signal
                     If Exit then reset count to zero;

plot Enter = a and b within 2 bars and count == 0;

plot Exit = ExitSignal == 1 and count !=0;

Enter.SetPaintingStrategy(PaintingStrategy. BOOLEAN_ARROW_UP); 
Enter.SetDefaultColor(Color.green); 
Enter.SetLineWeight(5); 

Exit.SetPaintingStrategy(PaintingStrategy. BOOLEAN_ARROW_DOWN); 
Exit.SetDefaultColor(Color.red); 
Exit.SetLineWeight(5);
 

Similar threads

Top