Pinescript v4 - The Holy Grail (Trailing Stop)

After studying several other scripts, I believe I have found the Holy Grail! (Or perhaps I've just found a bug with Tradingview's Pinescript v4 language) Anyhow, I'm publishing this script in the hope that someone smarter than myself could shed some light on the fact that adding a trailing stop to any strategy seems to make it miraculously...no that's an understatement...incredulously, stupendously, mind-bendingly profitable. I'm talking about INSANE profit factors, higher than 200x, with drawdowns of <10%. Sounds too good to be true? Maybe it is...or you could hook it up to your LIVE broker, and pray it doesn't explode. This is an upgraded version of my original Pin Bar Strategy.

Recommended Chart Settings:

Asset Class: Forex
Time Frame: H1

Long Entry Conditions:

a) Exponential Moving Average Fan up trend
b) Presence of a Bullish Pin Bar
c) Pin Bar pierces the Exponential Moving Average Fan

Short Entry Conditions:

a) Exponential Moving Average down trend
b) Presence of a Bearish Pin Bar
c) Pin Bar pierces the Exponential Moving Average Fan

Exit Conditions:
a) Trailing stop is hit
b) Moving Averages cross-back (optional)
c) It's the weekend

Default Robot Settings:

Equity Risk (%): 3 //how much account balance to risk per trade
Stop Loss (x*ATR, Float): 0.5 //stoploss = x * ATR, you can change x
Stop Loss Trail Points (Pips): 1 //the magic sauce , not sure how this works
Stop Loss Trail Offset (Pips): 1 //the magic sauce , not sure how this works
Slow SMA (Period): 50 //slow moving average period
Medium EMA (Period): 18 //medium exponential moving average period
Fast EMA (Period): 6 //fast exponential moving average period
ATR (Period): 14 // average true range period
Cancel Entry After X Bars (Period): 3 //cancel the order after x bars not triggered, you can change x

Backtest Results (2019 to 2020, H1, Default Settings):

AUDUSD - 1604% profit, 239.6 profit factor, 4.9% drawdown (INSANE)
NZDUSD - 1688.7% profit, 100.3 profit factor, 2.5% drawdown
GBPUSD - 1168.8% profit, 98.7 profit factor, 0% drawdown
USDJPY - 900.7% profit, 93.7 profit factor, 4.9% drawdown
USDCAD - 819% profit, 31.7 profit factor, 8.1% drawdown
EURUSD - 685.6% profit, 26.8 profit factor, 5.9% drawdown
USDCHF - 1008% profit, 18.7 profit factor, 8.6% drawdown
GBPJPY - 1173.4% profit, 16.1 profit factor, 7.9% drawdown
EURAUD - 613.3% profit, 14.4 profit factor, 9.8% drawdown
AUDJPY - 1619% profit, 11.26 profit factor, 9.1% drawdown
EURJPY - 897.2% profit, 6 profit factor, 13.8% drawdown
EURGBP - 608.9% profit, 5.3 profit factor, 9.8% drawdown (NOT TOO SHABBY)

As you can clearly see above, this forex robot is projected by the Tradingview backtester to be INSANELY profitable for all common forex pairs. So what was the difference between this strategy and my previous strategies? Check my code and look for "trail_points" and "trail_offset"; you can even look them up in the PineScript v4 documentation. They specify a trailing stop as the exit condition, which automatically closes the trade if price reverses against you.

I however suspect that the backtester is not properly calculating intra-bar price movement, and is using a simplified model. With this simplfied approach, the trailing stop code becomes some sort of "holy grail" generator, making every trade entered profitable.

Risk Warning:

This is a forex trading strategy that involves high risk of equity loss, and backtest performance will not equal future results. You agree to use this script at your own risk.


To get more realistic results, and *maybe* overcome the intrabar simulation error, change the settings to: "Stop Loss Trail Points (pips)": 100
I am not sure if this eradicates the bug, but the entries and exits look more proper, and the profit factors are more believable.
Script de código aberto

No verdadeiro espírito TradingView, o autor deste script o publicou com código aberto, para que os traders possam compreendê-lo e verificá-lo. Um brinde ao autor! Você pode usá-lo gratuitamente, mas a reutilização deste código em uma publicação é regida pelas Regras da Casa. Você pode favoritá-lo para utilizá-lo em um gráfico.

Quer usar esse script no gráfico?


almost any strategy where average bar is 1 will give profit. did you check for this issue?
+16 Resposta
TsangYouJun RafaelZioni
@RafaelZioni, sorry don't understand what you mean by "average bar = 1".
+6 Resposta
Elvano12 RafaelZioni
+9 Resposta
As @RafaelZioni points out. You are only in a given trade for one bar. So, if you can identify a high probability opportunity for a bull bar to follow the current bar and then you buy its open and sell its close, that is a profitable strategy. On that 1h chart, you would be in a trade for 1 hour or less. Nothing wrong with that at all. But you don't really have a trailing stop here in a conventional sense. I think what's happening is your function is forcing a entry and exit on the same bar.
Try to add commissions to this strategy and watch how the results change.
+6 Resposta
TsangYouJun kurtsmock
@kurtsmock, still I think its rather strange that my script can be so "prescient". It seems all the trades (a very large majority), are in the correct direction; which is unusual.
add your commissions and you will lose 99.9%
+4 Resposta
@http777, what's the best way to add commissions in the settings window?
Say for example 1.3 spread. Do I add this as 0.00013 'per contract' ? Cheers.
+1 Resposta
ifclause002 citizentraderx
@citizentraderx, in your strategy(title=""......) function, you can include "commission_type = strategy.commission.percent, commission_value = 0.015" to set commission. This will set up your bot to apply 0.015% of trading volume be deducted to commissions per trade executed. Hope that helps!
This is called Scalping and most brokers are actually against it. In some countries you might even get a fine if you do it.
+2 Resposta
TsangYouJun dMLTquant
@dMLTquant, if you set "Stop Loss Trail Points (pips)": 100, you'll notice that trades close after several bars. Yet still this strategy remains profitable, albeit to a less "insane" extent.

I am boggled that this works, because I've written many simple strategies (even the original pin-bar), and they are never this reliable.
+3 Resposta