OPEN-SOURCE SCRIPT

PineScript v4 - Forex Pin-Bar Trading Strategy

PineScript v4, forex trading robot based on the commonly used bullish / bearish pin-bar piercing the moving averages strategy.

I coded this robot to stress-test the PineScript v4 language to see how advanced it is, and whether I could port a forex trading strategy from MT4 to TradingView.

In my opinion, PineScript v4 is still not a professional coding language; for example you cannot use IF-statements to modify the contents of global variables; this makes complex robot behaviour difficult to implement. In addition, it is unclear if the programmer can use nested IF-ELSE, or nested FOR within IF.

The sequence of program execution is also unclear, and although complex order entry and exit appears to function properly, I am not completely comfortable with it.

Recommended Chart Settings:

Asset Class: Forex
Time Frame: H1

Long Entry Conditions:

a) Moving Average up trend, fast crosses above slow
b) Presence of a Bullish Pin Bar
c) Pin Bar pierces either Moving Average
d) Moving Averages must be sloping up, angle threshold (optional)

Short Entry Conditions:

a) Moving Average down trend, fast crosses below slow
b) Presence of a Bearish Pin Bar
c) Pin Bar pierces either Moving Average
d) Moving Averages must be sloping down, angle threshold (optional)

Exit Conditions:
a) Stoploss level is hit
b) Takeprofit level is hit
c) Moving Averages cross-back (optional)

Default Robot Settings:

Equity Risk (%): 3 //how much account balance to risk per trade
Stop Loss (x*ATR, Float): 2.1 //stoploss = x * ATR, you can change x
Risk : Reward (1 : x*SL, Float): 3.1 //takeprofit = x * stop_loss_distance, you can change x
Fast MA (Period): 20 //fast moving average period
Slow MA (Period): 50 //slow moving average period
ATR (Period): 14 //average true range period
Use MA Slope (Boolean): true //toggle the requirement of the moving average slope
Bull Slope Angle (Deg): 1 //angle above which, moving average is considered to be sloping up
Bear Slope Angle (Deg): -1 //angle below which, moving average is considered to be sloping down
Exit When MA Re-Cross (Boolean): true //toggle, close trade if moving average crosses back
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):

EURJPY - 111% profit, 2.631 profit factor, 16.43% drawdown
EURUSD - 103% profit, 2.899 profit factor, 14.95% drawdown
EURAUD - 76.75% profit, 1.8 profit factor, 17.99% drawdown
NZDUSD - 64.62% profit, 1.727 profit factor, 19.14% drawdown
GBPUSD - 58.73% profit, 1.663 profit factor, 15.44% downdown
AUDJPY - 48.71% profit, 1.635 profit factor, 11.81% drawdown
USDCHF - 30.72% profit, 1.36 profit factor, 22.63% drawdown
AUDUSD - 8.54% profit, 1.092 profit factor, 19.86% drawdown
EURGBP - 0.03% profit, 1.0 profit factor, 29.66% drawdown
USDJPY - 1.96% loss, 0.972 profit factor, 28.37% drawdown
USDCAD - 6.36% loss, 0.891 profit factor, 21.14% drawdown
GBPJPY - 28.27% loss, 0.461 profit factor, 39.13% drawdown

To reduce the possibility of curve-fitting, this robot was backtested on 12 popular forex currencies, as shown above. The robot was profitable on 8 out of 12 currencies, breakeven on 1, and made a loss on 3.

The default robot settings could be over-fitting for the EUR, as we can see out-sized performance for the EUR pairs, with the exception of the EURGBP. We can see that GBPJPY made the largest loss, so these two pairs could be related.

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.
Simple Moving Average (SMA)

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