OPEN-SOURCE SCRIPT

Investments/swing trading strategy for different assets

Stop worrying about catching the lowest price, it's almost impossible!: with this trend-following strategy and protection from bearish phases, you will know how to enter the market properly to obtain benefits in the long term.

Backtesting context: 1899-11-01 to 2023-02-16 of SPX by Tvc. Commissions: 0.05% for each entry, 0.05% for each exit. Risk per trade: 2.5% of the total account

For this strategy, 5 indicators are used:
  • One Ema of 200 periods
  • Atr Stop loss indicator from Gatherio
  • Squeeze momentum indicator from LazyBear
  • Moving average convergence/divergence or Macd
  • Relative strength index or Rsi


Trade conditions:

There are three type of entries, one of them depends if we want to trade against a bearish trend or not.
---If we keep Against trend option deactivated, the rules for two type of entries are:---

First type of entry:

With the next rules, we will be able to entry in a pull back situation:
  • Squeeze momentum is under 0 line (red)
  • Close is above 200 Ema and close is higher than the past close
  • Histogram from macd is under 0 line and is higher than the past one


Once these rules are met, we enter into a buy position. Stop loss will be determined by atr stop loss (white point) and break even(blue point) by a risk/reward ratio of 1:1.
For closing this position: Squeeze momentum crosses over 0 and, until squeeze momentum crosses under 0, we close the position. Otherwise, we would have closed the position due to break even or stop loss.

Second type of entry:

With the next rules, we will not lose a possible bullish movement:

  • Close is above 200 Ema
  • Squeeze momentum crosses under 0 line


Once these rules are met, we enter into a buy position. Stop loss will be determined by atr stop loss (white point) and break even(blue point) by a risk/reward ratio of 1:1.
Like in the past type of entry, for closing this position: Squeeze momentum crosses over 0 and, until squeeze momentum crosses under 0, we close the position. Otherwise, we would have closed the position due to break even or stop loss.

---If we keep Against trend option activated, the rules are the same as the ones above, but with one more type of entry. This is more useful in weekly timeframes, but could also be used in daily time frame:---

Third type of entry:

  • Close is under 200 Ema
  • Squeeze momentum crosses under 0 line


Once these rules are met, we enter into a buy position. Stop loss will be determined by atr stop loss (white point) and break even(blue point) by a risk/reward ratio of 1:1.
Like in the past type of entries, for closing this position: Squeeze momentum crosses over 0 and, until squeeze momentum crosses under 0, we close the position. Otherwise, we would have closed the position due to break even or stop loss.

Risk management

For calculating the amount of the position you will use just a small percent of your initial capital for the strategy and you will use the atr stop loss for this.
Example: You have 1000 usd and you just want to risk 2,5% of your account, there is a buy signal at price of 4,000 usd. The stop loss price from atr stop loss is 3,900. You calculate the distance in percent between 4,000 and 3,900. In this case, that distance would be of 2.50%. Then, you calculate your position by this way: (initial or current capital * risk per trade of your account) / (stop loss distance).
Using these values on the formula: (1000*2,5%)/(2,5%) = 1000usd. It means, you have to use 1000 usd for risking 2.5% of your account.
We will use this risk management for applying compound interest.

In settings, with position amount calculator, you can enter the amount in usd of your account and the amount in percentage for risking per trade of the account. You will see this value in green color in the upper left corner that shows the amount in usd to use for risking the specific percentage of your account.

Script functions

Inside of settings, you will find some utilities for display atr stop loss, break evens, positions, signals, indicators, etc.
You will find the settings for risk management at the end of the script if you want to change something. But rebember, do not change values from indicators, the idea is to not over optimize the strategy.
If you want to change the initial capital for backtest the strategy, go to properties, and also enter the commisions of your exchange and slippage for more realistic results.
If you activate break even using rsi, when rsi crosses under overbought zone break even will be activated. This can work in some assets.
---Important: In risk managment you can find an option called "Use leverage ?", activate this if you want to backtest using leverage, which means that in case of not having enough money for risking the % determined by you of your account using your initial capital, you will use leverage for using the enough amount for risking that % of your acount in a buy position. Otherwise, the amount will be limited by your initial/current capital---

Some things to consider

USE UNDER YOUR OWN RISK. PAST RESULTS DO NOT REPRESENT THE FUTURE.
DEPENDING OF % ACCOUNT RISK PER TRADE, YOU COULD REQUIRE LEVERAGE FOR OPEN SOME POSITIONS, SO PLEASE, BE CAREFULL AND USE CORRECTLY THE RISK MANAGEMENT

Do not forget to change commissions and other parameters related with back testing results!

Some assets and timeframes where the strategy has also worked:

  • BTCUSD : 4H, 1D, W
  • SPX (US500) : 4H, 1D, W
  • GOLD : 1D, W
  • SILVER : 1D, W
  • ETHUSD : 4H, 1D
  • DXY : 1D
  • AAPL : 4H, 1D, W
  • AMZN : 4H, 1D, W
  • META : 4H, 1D, W
    (and others stocks)
  • BANKNIFTY : 4H, 1D, W
  • DAX : 1D, W
  • RUT : 1D, W
  • HSI : 1D, W
  • NI225 : 1D, W
  • USDCOP : 1D, W



algotradingbtcusdstrategyChart patternsgoldstrategylongterminvestingprofitablestrategyregressionsSPX (S&P 500 Index)stocksignalsswingtradingTrend Analysistrendfollowing

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer