When that kind of cross occurs, TradingView's conditional operator (? We name that exit long order ‘XL’ (id="XL") and have it close the ‘EL’ entry order (from_entry="EL"). When we highlight those instances, this is what the chart looks like: The indicator's code that makes this happen is: We first define the indicator properties with the study() function. Since we want to close all orders after that point in time, we need a true value for strategy.close_all() once that time and date passes. 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. Or perhaps use a trailing stop-loss so that always a portion of open profit is ‘protected’. Taking the example of bitcoin to test this strategy. We enable the setting by default (defval=true), which makes the script use its position sizing algorithm. First we display the simple moving averages: We plot the SMAs with TradingView's plot() function. The second sma() call calculates the 100-bar SMA of closing prices with help of the slowMALen input variable. And it makes it easier to start with a smaller part of the strategy instead of being confronted with an empty script. This makes it for instance possible to fetch the previous bar's Average True Range (ATR) value. Now that we’ve defined the scope, we can continue creating two Moving Averages, a fast one and a slow one. Let’s see why we are taking the 20 SMA for formulating our strategy. It should be open < ema9, A place to discuss Tradingview's pinescript, Press J to jump to the feed. If you have a bag of $BNB, the commission/trading fees are 0.075% (discount of 0.025%). Earlier we set the tradeWindow variable to a true/false value. Then we calculate two Simple Moving Averages (SMAs): We calculate the moving averages with the sma() function, which we run here on closing prices (close) with a length of 15 and 35 bars. The function can check if some series of values crossed below another. For the risk equity computation we first turn the maxRisk input variable to a percentage expressed as a floating-point value (so 10% becomes 0.10). However any trading strategy need to be tested under varying market conditions to measure consistency and accuracy. Not only that, I do not want any entries triggered if the 9EMA is below the 18EMA at any point on the chart, hence why I am intending to only have entry upon and during crossover combined with the candlestick condition. That makes cross() return true when the 15-bar SMA crosses the 35-bar average. However, the TradingView team reviews everything and takes your many great suggestions into account. That means losses are made worse because it takes some time before the exit signal happens. ... Price crossovers are used to identify shifts in momentum and can be used as a basic entry or exit strategy. We calculate those extreme values with TradingView's highest() and lowest() functions. See my TradingView programming services, Have a programming question? We'll have to determine the moving averages and Average True Range, figure out the strategy's trade window, and calculate the position size. 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. Else we turn off the background with na. Next Line: input function with default value as 10 and min value as 1. That line appears in the teal colour. The World is Data Rich, But Information Poor! So if our strategy is long and the 50-bar SMA crosses below the 100-bar SMA, strategy.entry() turns that long position into a short trade. strategy("MA_strategy" , shorttitle="MA_strategy", overlay=true, initial_capital=100000), plot(s, color=yellow,linewidth=2) // Plots the MA. The maximum exposure (that is, the margin to equity ratio) for a single position is 10% of equity. Many thanks. Should enterShort be false, we use TradingView's history referencing operator ([]) to fetch the previous bar value of shortStop. 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. Its value depends on the combination of two true/false expressions. This is how that looks as code in your editor: As you can see, i gave the plot function some extra details, such as the color of the MA, the width of the line and a title.