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

ssaeed73

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

Administrative
Staff member
Staff
VIP
@ssaeed73 How about something like this?
6mEiBsB.png

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

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

Well-known 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

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);
 

pine

New member
@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);

Thank you for a great method!

I used your code for something similar that I am trying to accomplish. I can see the arrows on desktop but not mobile. Do you (or anyone) know what could be the issue? Thanks!

Code:
input showBreakoutSignals = yes;
input aggPeriod = AggregationPeriod.THIRTY_MIN;

# buy sell

def buysignals = buy condition;

def sellsignals = sell condition;

def buycondition = if buysignals then 1 else if !sellsignals then buycondition[1] else 0;
def sellcondition = if sellsignals then 1 else if !buysignals then sellcondition[1] else 0;
 
plot UpSignal = if buysignals and sellcondition[1] == 1 then low else double.nan;
plot DownSignal = if sellsignals and buycondition[1] == 1 then high else double.nan;

UpSignal.SetDefaultColor(Color.WHITE);
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
UpSignal.SetHiding(!showBreakoutSignals);


DownSignal.SetDefaultColor(Color.CYAN);
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
DownSignal.SetHiding(!showBreakoutSignals);
 

rad14733

Well-known member
VIP
@pine The mobile version doesn't allow multiple aggregation periods so that is most likely your problem... The code should only work for you on a 30m chart, unless it gags on any reference to AggregationPeriod...
 

pine

New member
@pine The mobile version doesn't allow multiple aggregation periods so that is most likely your problem... The code should only work for you on a 30m chart, unless it gags on any reference to AggregationPeriod...

Ok, that makes sense. The condition is based on the 30 min aggregation, while chart is on a lower time frame. Thank you for your response.
 

Jman831

New member
I'm new to thinkscript but I think I've found a much simpler solution to this issue. In this case I'm going to use "close > ExpAverage(close, 9)" as a buy signal as it would normally cause a buy signal every time the close is above the 9EMA. In any case, you should be able to replace the condition with whatever your condition for a buy signal is. See below:

Code:
def BuySig = close > ExpAverage(close, 9);

plot BuySignal = BuySig is True and BuySig[1] is False;

BuySignal.setPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);

Plot can also be written like so:

Code:
plot BuySignal = BuySig and !BuySig[1];

What this code is doing is plotting an arrow when BuySig of the current bar is true and BuySig of the bar before the current bar is false. This way if BuySig is both true for the current bar and the bar before the current bar then it reads it as "false" and doesn't plot an arrow. Hope that helps.
 
Last edited:

Pensar

Well-known member
VIP
I'm new to thinkscript but I think I've found a much simpler solution to this issue. In this case I'm going to use "close > ExpAverage(close, 9)" as a buy signal as it would normally cause a buy signal every time the close is above the 9EMA. In any case, you should be able to replace the condition with whatever your condition for a buy signal is. See below:

Code:
def BuySig = close > ExpAverage(close, 9);

plot BuySignal = BuySig is True and BuySig[1] is False;

BuySignal.setPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);

Plot can also be written like so:

Code:
plot BuySignal = BuySig and !BuySig[1];

What this code is doing is plotting an arrow when BuySig of the current bar is true and BuySig of the bar before the current bar is false. This way if BuySig is both true for the current bar and the bar before the current bar then it reads it as "false" and doesn't plot an arrow. Hope that helps.
@Jman831 I'm surprised. I actually didn't think this would work until I tried to prove it wrong. Nice!
 

Similar threads

Top