From 57c18b78a801dc776a4745fb5518242da427e95b Mon Sep 17 00:00:00 2001 From: LucF <50338286+LucFF@users.noreply.github.com> Date: Fri, 13 Sep 2019 17:28:11 -0400 Subject: [PATCH 1/2] Update README.md --- coding_conventions/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/coding_conventions/README.md b/coding_conventions/README.md index 049492a..5dff20a 100644 --- a/coding_conventions/README.md +++ b/coding_conventions/README.md @@ -36,7 +36,7 @@ The Pine compiler is not very strict on exact positioning of specific statements Here is an example of a complete script: -``` +```js //@version=4 // MACD indicator, a Gerald Appel concept. // Author: TradingView, mods by PineCoders, v1.0, 2019.07.31 @@ -59,8 +59,8 @@ macd = fastMa - slowMa signal = sma(macd, 9) // ————— Plots -plot(macd, color=color.blue) -plot(signal, color=color.orange) +plot(macd, color = color.blue) +plot(signal, color = color.orange) ``` **[Back to top](#table-of-contents)** From dbfd20557e1fefedefed5a6f7475fa61dc5567a4 Mon Sep 17 00:00:00 2001 From: LucF <50338286+LucFF@users.noreply.github.com> Date: Sat, 14 Sep 2019 13:35:40 -0400 Subject: [PATCH 2/2] Update README.md --- faq_and_code/README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/faq_and_code/README.md b/faq_and_code/README.md index 3a0bf33..d7801ac 100644 --- a/faq_and_code/README.md +++ b/faq_and_code/README.md @@ -635,7 +635,7 @@ plot(initOnEachBar2, "initOnEachBar2", color.orange, 3, transp = 0) See [here](https://www.tradingview.com/pine-script-docs/en/v4/language/Expressions_declarations_and_statements.html#variable-declaration) for more information. This is another example by vitvlkv: [Holding a state in a variable](https://www.tradingview.com/script/llcoIPKG-Pine-Example-Holding-a-state-in-a-variable/). ### How do I calculate averages? -1. If you just want the average between two values, you can use `avg(val1, val2)` or `(val1 + val2)/2`. Note that the [`avg()`](https://www.tradingview.com/pine-script-reference/v4/#fun_avg) accepts up to 6 values. +1. If you just want the average between two values, you can use `avg(val1, val2)` or `(val1 + val2)/2`. Note that [`avg()`](https://www.tradingview.com/pine-script-reference/v4/#fun_avg) accepts up to 6 values. 1. To average the last x values in a series, you can use `sma(series, x)`. ### How can I calculate an average only when a certain condition is true? @@ -818,6 +818,24 @@ plot(v3, "3. f_verboseAndINEFFICIENT_TimesInLast") bgcolor(v1 != v2 or v2 != v3 ? color.red : na, transp = 80) ``` +### How can implement and On/Off switch? +```js +//@version=4 +study("On/Off condition", "", true) +upBar = close > open +// On/off conditions. +triggerOn = upBar and upBar[1] and upBar[2] +triggerOff = not upBar and not upBar[1] +// Switch state is implicitly saved across bars thanks to initialize-only-once keyword "var". +var onOffSwitch = false +// Turn the switch on when triggerOn is true. If it is already on, +// keep it on unless triggerOff occurs. +onOffSwitch := triggerOn or (onOffSwitch and not triggerOff) +bgcolor(onOffSwitch ? color.green : na) +plotchar(triggerOn, "triggerOn", "▲", location.belowbar, color.lime, 0, size = size.tiny, text = "On") +plotchar(triggerOff, "triggerOff", "▼", location.abovebar, color.red, 0, size = size.tiny, text = "Off") +``` + **[Back to top](#table-of-contents)**