-1

I have a small riddle that I can not manage to solve for a good while now!

It is possible to assign colours to values when coding a TV indicator but is it possible to do the opposite - to assign values to colours taken from an other indicator?

In other words, I have an indicator that plots a line and assigns colours to it (the colours can either be #2196f3 or #FF0000). What I would like to do is to have an other indicator below it taking the #colour value and plotting a new line based on that colour. For example if indicator A has turned from #2196f3 to #FF0000, the needed indicator B, changes from 1 to -1.

I have looked throughout the web to find a similar question with an answer but so far no luck ;( I am learning pine script so I apologize if its a silly question with an easy solution.

Thank you for you time!

YLR
  • 1,503
  • 4
  • 21
  • 28

1 Answers1

0

We can use the == operator to check if a value is equal to something, rather than assigning it with just one = sign. *(I edited the original ema here to align with your indicator)We check the variables color and assign a number conditionally.

col1 = #2196f3 
col2 = #FF0000

bValue = css == col1 ? 1 : css  == col2 ? -1 : 0 

plot(bValue)

bullalert = bValue ==  1 and (bValue == -1)[1]
bearalert = bValue == -1 and (bValue ==  1)[1]

alertcondition(bullalert, title='Mod filter Bull', message='Mod filter Bull on {{interval}} chart. Price is {{close}}')
alertcondition(bearalert, title='Mod filter Bear', message='Mod filter Bear on {{interval}} chart. Price is {{close}}')

if our color variable is equal to one color, we assign 1, if the other color we assign -1, else it will be 0. We can then even go on to make an alert condition based on a positive or negative change in that number in reference to the bar prior.

Bjorgum
  • 2,054
  • 2
  • 4
  • 11
  • Hey Bjorgum! What an honour to have an answer from you - love your indicators and scripts :)!!! Unfortunately I would need some more help... you see, you have supposed a "close over and ema" but instead I would need something like: – the.mathew Sep 22 '21 at 20:51
  • col1 = #2196f3 col2 = #FF0000 aColor = "modular filter color=col1" ? col1 : col2 bValue = aColor == col1 ? 1 : aColor == col2 ? -1 : 0 plot(bValue) – the.mathew Sep 22 '21 at 20:52
  • So the "aColor" would respond to the statement is the "modular filter" (here is the indicator I am trying to use: https://www.tradingview.com/script/06pNDfyd-Modular-Filter-Spot-Trends-And-Smooth-Price/) "colored like col1" and if yes then or if no then... Also I would need to plot this value afterwards (I think I managed to do this with the "plot(bValue)" but please do let me know if this is incorrect) Sorry to bother again, and thanks again for your time! – the.mathew Sep 22 '21 at 20:52
  • Cheers my friend!! I edited the original to align with your indicator subbing in css (the color variable for the mod filter). I went on to show you how to make an alert with our number after plotting the number to the chart. Take care and best of luck with your trading and coding! – Bjorgum Sep 23 '21 at 06:33
  • Thats exactly what I needed! A huge thanks for your time and answer! Everything works flawlessly!!! Have an amazing day :) – the.mathew Sep 23 '21 at 07:24