TradingView
Peter_O
18 ноя 2019 г., 00:42

TradingView Alerts to MT4 MT5 + dynamic variables NON-REPAINTING 

Euro Fx/U.S. DollarFXCM

Описание

Accidentally, I’m sharing open-source profitable Forex strategy. Accidentally, because this was aimed to be purely educational material. A few days ago TradingView released a very powerful feature of dynamic values from PineScript now being allowed to be passed in Alerts. And thanks to TradingConnector, they could be instantly executed in MT4 or MT5 platform of any broker in the world. So yeah - TradingConnector works with indices and commodities, too.

The logic of this EURUSD 6h strategy is very simple - it is based on Stochastic crossovers with stop-loss set under most recent pivot point. Setting stop-loss with surgical precision is possible exactly thanks to allowance of dynamic values in alerts. TradingConnector has been also upgraded to take advantage of these dynamic values and it now enables executing trades with pre-calculated stop-loss, take-profit, as well as stop and limit orders.

Another fresh feature of TradingConnector, is closing positions only partly - provided that the broker allows it, of course. A position needs to have trade_id specified at entry, referred to in further alerts with partial closing. Detailed spec of alerts syntax and functionalities can be found at TradingConnector website. How to include dynamic variables in alert messages can be seen at the very end of the script in alertcondition() calls.

The strategy also takes commission into consideration.

Slippage is intentionally left at 0. Due to shorter than 1 second delivery time of TradingConnector, slippage is practically non-existing. This can be achieved especially if you’re using VPS server, hosted in the same datacenter as your brokers’ servers. I am using such setup, it is doable. Small slippage and spread is already included in commission value.

This strategy is NON-REPAINTING and uses NO TRAILING-STOP or any other feature known to be faulty in TradingView backtester. Does it make this strategy bulletproof and 100% success-guaranteed? Hell no! Remember the no.1 rule of backtesting - no matter how profitable and good looking a script is, it only tells about the past. There is zero guarantee the same strategy will get similar results in the future.

To turn this script into study so that alerts can be produced, do 2 things:
1. comment “strategy” line at the beginning and uncomment “study” line
2. comment lines 54-59 and uncomment lines 62-65.
Then add script to the chart and configure alerts.

This script was build for educational purposes only.

Certainly this is not financial advice. Anybody using this script or any of its parts in any way, must be aware of high risks connected with trading.

Thanks @LucF and @a.tesla2018 for helping me with code fixes :)

Информация о релизе

Stoch default K value was changed.

Информация о релизе

Modified alertcondition() syntax.

Информация о релизе

Added plot definitions and plot names to fix passing dynamic variables to alerts.

Note! when converting to study for alerts, you need to uncomment lines 62-71, not 62-65, as explained in the original description.

By they way - previous update took place November 20, when net profit was at $56336.34 and profit factor 1.208. 40 days later these numbers have improved to over 57k and 1.21 profit factor. So it continues to grow :)

Информация о релизе

Fixed trade_id variable. Not all dynamic values were passed correctly to alerts.

Aaaaand - another 2 weeks, another check. Account appears to continue growing, +$500 since December 29 :)

Информация о релизе

With the fresh update to TradingConnector to version 2.5, I have decided to take a look at my previous scripts whether they are still using latest developments by TradingView dev team and guess what - this script definitely needed an update, too. It didn't include alerts-straight-from-strategy feature, so I have added the necessary lines at the end. Enjoy!

Информация о релизе

Updated to PineScript version 5.

Информация о релизе

With TradingConnector upgrade to version 1.35, a lot has been simplified, therefore this script update:
- Multiple Take-Profit feature has been introduced and example code can be found in this update.
- when dealing with single entries, tradeid= parameter is not needed (however still operational) for features like partial closing, slmod and tpmod. So tradeid= has been removed form this script.

Other new features are: 'delay=' parameter, separating TradingConnector alerts from all others, breakeven exit.

Информация о релизе

With the biggest TradingConnector's upgrade since its inception - including "Search&Replace Alerts Modifier", details can be found here:
- it was highest time to update this script, to showcase the ability of setting exit conditions as percentage move from the entry price.

Moreover, alerts examples are now being displayed in an overlay.
Комментарии
scarfacexxx
Hi, Peter.

You know if is possible to put an alert into a strategy?

I dont know how to code my signals, but all my signals can be convert in a alert.
asterix1967
@scarfacexxx,

Now it's possible with alert_message in strategy.order
UnknownUnicorn9234952
Your strategy is not realistic. The profit depends on the cash you are using against the initial cash which is 10000, but you have set the default_qty_value to 100000, which is more than supposedly you have. So, first set the size of the operations to be a percentage of what you actually have like default_qty_type=strategy.percent_of_equity. After that we can talk about the other issues in the code. Tradingview strategies do not enforce realistic results so depending on what you do you can fake the profit.
Honestcowboy
@UnknownUnicorn9234952, That is not an issue in the code at all. That is using leverage.
Natthavudh_Suwannawong
@UnknownUnicorn9234952, But that's not the real issue, you can change any equity you want and still end up with the same factor. Don't just look at net profit.
lewis.wren1990
Hey Peter_O,

i'm trying to understand your statement : To turn this script into study so that alerts can be produced, do 2 things:
1. comment “strategy” line at the beginning and uncomment “study” line
2. comment lines 54-59 and uncomment lines 62-65.
Then add script to the chart and configure alerts.

could you post the script with the above changed if possible, finding it difficult to make it work,

thank you in advance,
Peter_O
@Lewis.wren1990,

//@version=4
//strategy(title="TradingView Alerts to MT4 MT5 Strategy example", commission_type=strategy.commission.cash_per_order, commission_value=0.00003, overlay=false, default_qty_value=100000, initial_capital=1000)
study(title="TradingView Alerts to MT4 MT5 Strategy example") //uncomment this line and comment previous one to make it a study producing alerts
//
// This script was created for educational purposes only.
// It is showing how to use dynamic variables in TradingView alerts.
// And how to execute them in Forex, indices and commodities markets

TakeProfitLevel=input(400)
TakePartialProfitLevel=input(150)

// **** Entries logic **** {
periodK = input(13, title="K", minval=1)
periodD = input(3, title="D", minval=1)
smoothK = input(4, title="Smooth", minval=1)
k = sma(stoch(close, high, low, periodK), smoothK)
d = sma(k, periodD)
plot(k, title="%K", color=color.blue)
plot(d, title="%D", color=color.orange)
h0 = hline(80)
h1 = hline(20)
fill(h0, h1, color=color.purple, transp=75)

GoLong=crossover(k,d)// and k<80
GoShort=crossunder(k,d)// and k>20
// } End of entries logic

// **** Pivot-points and stop-loss logic **** {
piv_high = pivothigh(high,1,1)
piv_low = pivotlow(low,1,1)
var float stoploss_long=low
var float stoploss_short=high

pl=valuewhen(piv_low,piv_low,0)
ph=valuewhen(piv_high,piv_high,0)

if GoLong
stoploss_long := low<pl ? low : pl
if GoShort
stoploss_short := high>ph ? high : ph
// } End of Pivot-points and stop-loss logic

// **** Trade counter and partial closing mechanism **** {
var int trade_id=0
if GoLong or GoShort
trade_id:=trade_id[1]+1

TakePartialProfitLong = barssince(GoLong)<barssince(GoShort) and crossover(high,(valuewhen(GoLong,close,0)+TakePartialProfitLevel*syminfo.mintick))
TakePartialProfitShort = barssince(GoLong)>barssince(GoShort) and crossunder(low,(valuewhen(GoShort,close,0)-TakePartialProfitLevel*syminfo.mintick))
// } End of Trade counter and partial closing mechanism

//strategy.entry("Long", strategy.long, when=GoLong)
//strategy.exit("XPartLong", from_entry="Long", qty_percent=50, profit=TakePartialProfitLevel)
//strategy.exit("XLong", from_entry="Long", stop=stoploss_long, profit=TakeProfitLevel)
//strategy.entry("Short", strategy.short, when=GoShort)
//strategy.exit("XPartShort", from_entry="Short", qty_percent=50, profit=TakePartialProfitLevel)
//strategy.exit("XShort", from_entry="Short", stop=stoploss_short, profit=TakeProfitLevel)

alertcondition(GoLong, title='Long', message='long slprice={{stoploss_long}} tradeid={{trade_id}} tp=TakeProfitLevel')
alertcondition(GoShort, title='Short', message='short slprice={{stoploss_short}} tradeid={{trade_id}} tp=TakeProfitLevel')
alertcondition(TakePartialProfitLong, title='ClosePartLong', message='closepart tradeit={{trade_id}} part=0.5')
alertcondition(TakePartialProfitShort, title='ClosePartShort', message='closepart tradeit={{trade_id}} part=0.5')
lewis.wren1990
@Peter_O,

Thank you for your fast response, unfortunately i get the response: Add to Chart operation failed, reason: line 38: mismatched input 'stoploss_long' expecting 'end of line without line continuation'

i am not aware of what this could be?

Thanks, Lewis
Peter_O
@Lewis.wren1990, I tried to do it fst for you, but when posting code in comments, indents got deleted. Add indents at the beginning of line 38 and 40 and it should be fine.
lewis.wren1990
@Peter_O,

Thank you, yes its making alerts now, i'm guessing you are the owner of trading connector also?

Thanks, Lewis
Ещё