# Forex Master

### Top FREE TradingView strategy this week!

To make our community more profitable we decided to start a series in which we will review open-source TradingView strategies. The aim of the review is to select a strategy (or a number of strategies) from TradingView and run a reality check on it, optimise them and convert into alerts, so you can set it up as a bot on Wunderbit Trading platform.

![Forex Master - free TradingView strategy](https://1298640921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lxzdw8maMUh98DLsWjS%2F-M9wTCNe113hVZ5t15-L%2F-M9wYcuucT-XE_gxKQZZ%2FForex%20Master.png?alt=media\&token=4c814364-30bb-465c-9c2c-76c80d30685a)

### What we will provide

* Open-strategy source
* Strategy adjustment for particular exchange, pair and timeframe
* Backtest&#x20;
* Revised study script that you can apply in Wunderbit Trading platform for bot trading.

{% embed url="<https://www.youtube.com/watch?v=YjCnjeVTh9E>" %}

This week we found a nice strategy for a **30 min timeframe**. The original idea was taken from: [Koi\_Capital](https://www.tradingview.com/u/Koi_Capital/). Originally, this strategy was applied to a forex pair EUR/USD. Our team made some adjustment and improvements and we are happy to present this work to you.&#x20;

You will find the strategy code here: [Forex Master v4.0](https://www.tradingview.com/script/AIZqyS45-Forex-Master-v4-0-EUR-USD-Mean-Reversion-Algorithm/).

{% hint style="info" %}
**IMPORTANT!**

* This is a trend strategy and works better in the uptrend&#x20;
* This strategy is only working for long positions
* We added the trend identifier using the EMA and SMA interaction
* Take profit and stop loss levels were converted into percentage
* We added inputs for the period selection, so you could see how the strategy is performing on a monthly basis.
  {% endhint %}

### Settings – Applicable to [FTX](https://ftx.com/#a=2594970): BTC-PERP 30 min

| **Variable**       | Value |
| ------------------ | ----- |
| Period             | 33    |
| Length             | 17    |
| Mult               | 1.7   |
| ADX Smoothing      | 1     |
| DI Length          | 77    |
| Smoothed ADX1      | 7     |
| Smoothed ADX2      | 14    |
| Long Take Profit % | 6.4   |
| Long Stop Loss%    | 4.2   |

### **Revised strategy script code**

You can copy this code and paste it into your TradingVie&#x77;**.**

```
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Wunderbit Trading

//@version=4

strategy("Forex Master v4.0", overlay=true, pyramiding=0, commission_type=strategy.commission.percent, commission_value=0.07, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)

Price = close

ribbon_period = input(33, "Period", step=1)

leadLine1 = ema(close, ribbon_period)
leadLine2 = sma(close, ribbon_period)

p1 = plot(leadLine1, color= #53b987, title="EMA", transp = 50, linewidth = 1)
p2 = plot(leadLine2, color= #eb4d5c, title="SMA", transp = 50, linewidth = 1)
fill(p1, p2, transp = 60, color = leadLine1 > leadLine2 ? #53b987 : #eb4d5c)

Length = input(17, step=1)
Mult = input(1.7, step=0.1)
Basis = sma(Price, Length)
StdDev = Mult * stdev(Price, Length)
Upper = Basis + StdDev
Lower = Basis - StdDev

adxlen = input(1, title="ADX Smoothing")
dilen = input(77, title="DI Length")
dirmov(len) =>
	up = change(high)
	down = -change(low)
	truerange = rma(tr, len)
	plus = fixnan(100 * rma(up > down and up > 0 ? up : 0, len) / truerange)
	minus = fixnan(100 * rma(down > up and down > 0 ? down : 0, len) / truerange)
	[plus, minus]

adx(dilen, adxlen) =>
	[plus, minus] = dirmov(dilen)
	sum = plus + minus
	adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
	[adx, plus, minus]

[sig, up, down] = adx(dilen, adxlen)

// plot(sig, color=color.red, title="ADX")
// plot(up, color=color.blue, title="+DI")
// plot(down, color=color.orange, title="-DI")

SmoothedADX1 = ema(sig, input(7))
SmoothedADX2 = ema(sig, input(14))


long_tp_inp = input(6.4, title='Long Take Profit %', step=0.1)/100
long_sl_inp = input(4.2, title='Long Stop Loss %', step=0.1)/100

long_take_level = strategy.position_avg_price * (1 + long_tp_inp)
long_stop_level = strategy.position_avg_price * (1 - long_sl_inp)


entry_long = crossover(Price, Lower) and SmoothedADX1 < SmoothedADX2 and leadLine2<leadLine1
exit_long =  crossunder(Price, Upper) and SmoothedADX1 < SmoothedADX2 and leadLine2>leadLine1


////// BACKTEST PERIOD ///////
testStartYear = input(2019, "Backtest Start Year")
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(1, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)

testStopYear = input(9999, "Backtest Stop Year")
testStopMonth = input(12, "Backtest Stop Month")
testStopDay = input(31, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)

testPeriod() =>
    time >= testPeriodStart and time <= testPeriodStop ? true : false


if testPeriod()
    strategy.entry("LongEntry", true, when = entry_long)
    strategy.exit("LongExit", "LongEntry", limit = long_take_level, stop = long_stop_level)
    strategy.close("LongEntry", when=exit_long)
```

### **Revised study script code**

You can copy this code and paste it into your TradingVie&#x77;**.**

```
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Wunderbit Trading

//@version=4

study("Forex Master v4.0", overlay=true)

Price = close

ribbon_period = input(33, "Period", step=1)

leadLine1 = ema(close, ribbon_period)
leadLine2 = sma(close, ribbon_period)

p1 = plot(leadLine1, color= #53b987, title="EMA", transp = 50, linewidth = 1)
p2 = plot(leadLine2, color= #eb4d5c, title="SMA", transp = 50, linewidth = 1)
fill(p1, p2, transp = 60, color = leadLine1 > leadLine2 ? #53b987 : #eb4d5c)

Length = input(17, step=1)
Mult = input(1.7, step=0.1)
Basis = sma(Price, Length)
StdDev = Mult * stdev(Price, Length)
Upper = Basis + StdDev
Lower = Basis - StdDev

adxlen = input(1, title="ADX Smoothing")
dilen = input(77, title="DI Length")
dirmov(len) =>
	up = change(high)
	down = -change(low)
	truerange = rma(tr, len)
	plus = fixnan(100 * rma(up > down and up > 0 ? up : 0, len) / truerange)
	minus = fixnan(100 * rma(down > up and down > 0 ? down : 0, len) / truerange)
	[plus, minus]

adx(dilen, adxlen) =>
	[plus, minus] = dirmov(dilen)
	sum = plus + minus
	adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
	[adx, plus, minus]

[sig, up, down] = adx(dilen, adxlen)

// plot(sig, color=color.red, title="ADX")
// plot(up, color=color.blue, title="+DI")
// plot(down, color=color.orange, title="-DI")

SmoothedADX1 = ema(sig, input(7))
SmoothedADX2 = ema(sig, input(14))

long_tp_inp = input(6.4, title='Long Take Profit %', step=0.1)/100
long_sl_inp = input(4.2, title='Long Stop Loss %', step=0.1)/100

isEntry_Long = false
isEntry_Long := nz(isEntry_Long[1], false)

isExit_Long = false
isExit_Long := nz(isExit_Long[1], false)

entry_long =not isEntry_Long and (crossover(Price, Lower) and SmoothedADX1 < SmoothedADX2 and leadLine2<leadLine1)
entry_price_long=valuewhen(entry_long,close,0)
TP_long = entry_price_long * (1 + long_tp_inp)
SL_long = entry_price_long * (1 - long_sl_inp)
exit_long = not isExit_Long and ((crossunder(Price, Upper) and SmoothedADX1 < SmoothedADX2 and leadLine2>leadLine1) or high > TP_long or low < SL_long )

if (entry_long)
    isEntry_Long := true
    isExit_Long := false

if (exit_long)
    isEntry_Long := false
    isExit_Long := true

alertcondition(entry_long, title="Enter Long")
alertcondition(exit_long, title="Exit Long")
plotshape(series=entry_long, text="BUY", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(series=exit_long, text="EXIT BUY",style=shape.triangledown, location=location.abovebar, color=color.purple, size=size.small)
```
