Strategy Tester EMA-SMA-RSI-MACD

On Tradingview I never saw a custom adjustable strategy script yet, so this is it,
you can change different things and see if you'll get a good strategy or not


First choose the source, you can choose out of:
close, open, high, low, ohlc4, hlc3, hl2

Then choose you strategy: Long & Short, Long only or Short only

Next, choose your entry "Buy/Long" (which is the "close Short position" when "Short"):
- (E)MA 1 > (E)MA 2 (Each can be made ema or sma )
- close above (E)MA 1
- RSI strategy
- macd > signal
- macd > 0
- signal > 0

Then choose your RSI values if needed (for example you want a trigger when EMA 1 > SMA 2
but only if RSI > 60, then change "IF RSI >" from 0 to 60

Next you can choose an extra argument
and even a second argument with Higher Time Frame settings

Under this you can change your (E)MA values as desired (HTF values, MACD and RSI length can be found lower)

All the same with the exit/close (or if "Short", this is your entry)
Again, change everything as you wish

Then comes the RSI length setting, MACD settings and HTF settings, followed by SL/TP settings
(you also can enable/disable SL/TP), and TIME settings (for example you want to know the profit only from this year)

Alerts are provided in next script

Have fun!

Sürüm Notları: Updated the RSI arguments
Sürüm Notları: Improved SL/TP, thank you finetime1000 for spotting !
Sürüm Notları: Added debugging

Also added symbols when it triggers (at close),
the strategy order will then happen at open (next bar),
just to help seeing how it works
Favori Komutlardan Çıkart Favori Komutlara Ekle


I'm not sure if this is a tradingview pine script bug or not.
If you try to reuse this code in a v4 script in this way:

L_S = "Long & Short" , _L_ = "Long Only" , _S_ = "Short Only"
Type = input(L_S, "Type Strategy", options=)
if (Type == L_S)

The Dosomething part is never going to be run.

How did I fix it?

L_S = "Long and Short" , _L_ = "Long Only" , _S_ = "Short Only"
Type = input(L_S, "Type Strategy", options=)
if (Type == L_S)

As you can see I have replaced the "&" character to "and".

So, apparently the L_S variable is just parsed or replaced without keeping its string nature.

The original code from fikira nevers checks for the L_S value in an if statement. That's why he never found this issue.
Cevap Gönder
fikira ruckard
@ruckard, If I understand correctly, the question is what the purpose is of "L_S",
the inclusions in the strat only beholds the very selective ones, so

if BUY
if (Type == _S_)
strategy.entry("", strategy.long)

means: a BUY trigger will give a "close" trigger ONLY when going "Short", with the other options ("only long" or "long & short") the BUY trigger
will trigger an "entry"
The same with the SELL trigger

So the L_S is used, but only as an "else" trigger

So, no bug, just purpose :)
Cevap Gönder
ruckard fikira
@fikira, I'm sorry to confuse you.
I understand the logic you are trying to explain.
You do not check the L_S value because you do not need to. So, as you say, the L_S is used but as an else trigger.

It's just that your code helped me to find this bug so here there is a better way to understand it.

So, your code right now uses the "_L_" strategy by default and that gives me: 44.08% net benefit on the strategy simulator summary.
But, at the same time, a slightly modified version of your script gives me: 48.26% net benefit.

The problem is about the minimal change I have done.

Your original line was:

L_S = "Long & Short" , _L_ = "Long Only" , _S_ = "Short Only"

My modified line is:

L_S = "Long & Short" , _L_ = "Long & Only" , _S_ = "Short Only"


Adding '&' char there mangles everything.
I suspect that, under the hood, the two strings that are being compared are:
"Long & Only" and "Long & Only" because the char '&' gets sanitized.

Either it is a bug or something that should be better documented.
Cevap Gönder
fikira ruckard
@ruckard, Ah! Now I understand! Well that is interesting! Will check it out! ;)
Normally though it wouldn't make a difference since the script uses the "_L_",... and not the string
Have you tried to change it again and see if you get the original outcome?
Cevap Gönder
fikira fikira
@fikira, No need to respond previous question, I tried it myself and just adding the "&" gives me a $20-ish difference, the program sees "Long & Only" as "Long & Short"... going back and forth proves indeed that this slight change actually changes the outcome, which shouldn't be happening... Are you going to ask TV (since it is your discovery) or shall I? Thanks!
Cevap Gönder
ruckard fikira
@fikira, I have sent a private message to PineCoders user pointing them to this conversation.

Feel free to raise their attention to this issue.

What you say about going back and forth it's just that the:

if (Type == _L_)

statement is returning the correct value if the _L_ variable does not contain the ampersand character.

However if the _L_ variable contains the ampersand character then that if statement always returns false.
Cevap Gönder
fikira ruckard
@ruckard, Indeed, it is true that if option _L_ is chosen it will return false when there is an & added in the string, and will return true
when no & is added (original), I informed PineCoders, very curious about it!


For future readers (and the ones who are wondering what the hell we're talking about :)


It is just that changing a simple symbol in the script on a specific place gives unexpected outcomes,
this now will be investigated further along, thank you!

Cevap Gönder
there is no sl/tp for short entry,
in line 129-133

"SL_B = strategy.position_avg_price * (1 - SL)
TP_B = strategy.position_avg_price * (1 + TP)

SL_S = strategy.position_avg_price * (1 + SL)
TP_S = strategy.position_avg_price * (1 - TP)"

so it is better to entry in line 292

strategy.exit("", "", stop= sl ? SL_B : na, limit= tp ? TP_B : na)
strategy.exit("", "", stop= sl ? SL_S : na, limit= tp ? TP_S : na)
Cevap Gönder
fikira finetime1000
@finetime1000, Indeed, you're right!
Improved the SL/TP, thank you for spotting !
Cevap Gönder
Ana Sayfa Hisse Senedi Takipçisi Forex Takipçisi Kripto Takipçisi Ekonomik Takvim Nasıl Çalışır Grafik Özellikleri Ücretlendirme Tanıdık yönlendirme Kurallarımız Destek Merkezi Web Sitesi & Aracı Kurum Çözümleri Görsel Bileşenler(Widget) Grafik Çözümleri Hafif Grafik Kitaplığı Blog ve Haberler Twitter
Profil Profil Ayarları Hesap ve Ödemeler Tanıdık yönlendirme Destek Kayıtlarım Destek Merkezi Yayınlanmış Fikirler Takip Edenler Takip ediliyor Özel Mesajlar Sohbet Çıkış