This sets the stop to a certain multiple (stopOffset) of the 10-bar Average True Range (ATR) (atrValue) above the current price (close). That looks like this. See my TradingView programming services, Have a programming question? From the btcusd chart here, we can see, after a nice uptrend and consolidation around 8k level, price started to break down around july end period and it broke below the 20MA — giving sell signal. Losing trades are closed at a predefined stop-loss level. Check out my TradingView programming help, See all TradingView tutorials to learn about a lot of Pine Script features, // Highlight when the RSI crosses over its EMA, // Plot values and show overbought and oversold levels, // Highlight the chart when RSI moves above 50, // Colour the background when %K crosses under 20 or 80, Focus@Will: Scientifically Optimised Music That Gets You, See if values cross each other in TradingView Pine, See if a value crosses over another with crossover(), Example: check if a series of values crosses over another, Example: see if a variable series crosses over a fixed level, Check if a value crosses under another with crossunder(), Example: see if a series of values dropped below another series, Example: check if a variable series fell below a fixed value, Monitor for a cross in any direction with TradingView's cross() function, Example: check if a series of values crosses some other series, Example: see if a series of values crosses a single value, How historical and real-time data influences crosses, https://www.tradingview.com/study-script-reference/, https://www.tradingview.com/wiki/Type_System. First we implement the enter short condition: There are two requirements before enterShort becomes true. strategy("MA_strategy" , shorttitle="MA_strategy", overlay=true, initial_capital=100000), plot(s, color=yellow,linewidth=2) // Plots the MA. Press J to jump to the feed.
Else the second conditional operator runs.
A short period MA crossing below a larger period MA (10–20MA in this case) indicates that bears are in control, gaining momentum and a big move is around the corner. Then we calculate the Stochastics %K and %D values: Then we colour the background when one of two cross below happened: Which, if any, background colour we use depends on two comparisons. strategy.entry("long",strategy.long,ordersize,when=buy), strategy.exit( "exit long", from_entry="long" , profit=p,loss=sl,trail_points=tl,trail_offset=os), “long” will be the id of the position to close, p,sl, tl and os can be defined as input variables with some default values.
And when there's no crossover or crossunder, cross() returns false. Fortunately Pine Script has an built-in function to detect crossovers, crossunders or just crosses. For that we multiply the input variable with 0.01. We get that value when we multiply the ‘Margin %’ input option with the total contract value, which we get when we multiply the current price (close) with syminfo.pointvalue.
If this second comparison also turns up false, the conditional operator returns na. 100 – the number of shares we want to trade; when = rsi > 50 – this is an additional parameter that tells pine script to only execute the trade if the RSI is higher than 50. And neither do they exit near the top. First we make the shortStop floating-point variable. :) return the long stop value (longStop).
This strategy uses two moving averages: one that follows the short-term trend and another that reflects the market's long-term trend. Without such a cross, crossover() returns false instead (and we don't get a coloured background).
If you are building a strategy then you will have to use the strategy function, which specifies the script name and some other script properties. That makes cross() return true when the 15-bar SMA crosses the 35-bar average. Now when the %K line drops below %K, crossunder() returns true. The World is Data Rich, But Information Poor! try using plotchar to see what's really happening. TradingView charts have around 5k or 10k recent price bars. Selecting the data you for you script to calculate with is arguably one of the harder parts of algorithmic scripting, but I’ll be sure to dedicate one article to it in the series. Strategy.entry with parameters order id, strategy.long, ordersize and buy: Opens a long position with defined ordersize at our buy condition. That means losses are made worse because it takes some time before the exit signal happens. The indicator's last statement uses the plot() function to show the 35-bar SMA as a regular line plot on the chart: The three cross functions (crossover(), crossunder(), and cross()) always compare the current bar's value with that of the previous bar. If you are testing your script on Bitcoin for example, it will have trouble testing it at the time $BTC was at $20.000 a piece. When the bar is a historical bar, the cross functions compare the bar's value at the close with the closing value of the previous bar. When the script is not long we have that operator return na to disable plotting. Top Talks from Devcon3 Summarized, What India’s Potential Crypto Ban Means for Startups, The Libertarian Fantasies of Cryptocurrencies.
Then we use the sma() function to calculate a 30-bar average of closing prices: Next we colour the chart's background for those bars where a cross happened: We colour the chart's background from top to bottom with the bgcolor() function. Tradingview has inbuilt pine scripting language where anybody, even free account holders, can develop their strategy and back test accordingly. At the bottom of the screen you will see “Pine Editor,” clicking there will bring up the Pine Script Editor, where we will be creating our indicator.