Milvetti_Pineconnector_LibraryLibrary "Milvetti_Pineconnector_Library"
This library has methods that provide practical signal transmission for Pineconnector.Developed By Milvetti
buy(licenseId, symbol, risk, sl, tp, beTrigger, beOffset, trailTrig, trailDist, trailStep, atrTimeframe, atrTrigger, atrPeriod, atrMultiplier, atrShift, spread, accFilter, secret, comment)
Create a buy order message
Parameters:
licenseId (string) : License Id. This is a unique identifier found in the Pineconnector Licensing Dashboard.
symbol (string) : Symbol. Default is syminfo.ticker
risk (float) : Risk. Function depends on the “Volume Type” selected in the EA
sl (float) : StopLoss. Place stop-loss. Computation is based on the Target Type selected in the EA. Default is 0(inactive)
tp (float) : TakeProfit. Place take-profit. Computation is based on the Target Type selected in the EA. Default is 0(inactive)
beTrigger (float) : Breakeven will be activated after the position gains this number of pips. Ensure > 0
beOffset (float) : Offset from entry price. 0 means the SL will be placed exactly at entry price. 1 means 1 pip above the entry price for buy trades and 1 pip below for sell trades.
trailTrig (int) : Trailing stop-loss will be activated after a trade gains this number of pips. Default is 0(inactive)
trailDist (int) : SL will be opened at traildist after trailtrig is met, even if you do not have a SL placed.. Default is 0(inactive)
trailStep (int) : Moves trailing stop-loss once price moves to favourable by a specified number of pips. Default is 0(inactive)
atrTimeframe (string) : ATR Trailing Stop will be based on the specified timeframe in minutes and will only update once per bar close. Default is Timeframe.Period
atrTrigger (float) : Activate the trigger of ATR Trailing after market moves favourably by a number of pips. Default is 0(inactive)
atrPeriod (int) : ATR averaging period. Default is 0
atrMultiplier (float) : Multiple of ATR to utilise in the new SL computation. Default is 1
atrShift (float) : Relative shift of price information, 0 uses latest candle, 1 uses second last, etc. Default is 0
spread (float) : Enter the position only if the spread is equal or less than the specified value in pips. Default is 0(inactive)
accFilter (float) : Enter the position only if the account requirement is met. Default is 0(inactive)
secret (string)
comment (string) : Comment. Add a string into the order’s comment section. Default is "Symbol+Timeframe"
sell(licenseId, symbol, risk, sl, tp, beTrigger, beOffset, trailTrig, trailDist, trailStep, atrTimeframe, atrTrigger, atrPeriod, atrMultiplier, atrShift, spread, accFilter, secret, comment)
Create a buy order message
Parameters:
licenseId (string) : License Id. This is a unique identifier found in the Pineconnector Licensing Dashboard.
symbol (string) : Symbol. Default is syminfo.ticker
risk (float) : Risk. Function depends on the “Volume Type” selected in the EA
sl (float) : StopLoss. Place stop-loss. Computation is based on the Target Type selected in the EA. Default is 0(inactive)
tp (float) : TakeProfit. Place take-profit. Computation is based on the Target Type selected in the EA. Default is 0(inactive)
beTrigger (float) : Breakeven will be activated after the position gains this number of pips. Ensure > 0
beOffset (float) : Offset from entry price. 0 means the SL will be placed exactly at entry price. 1 means 1 pip above the entry price for buy trades and 1 pip below for sell trades.
trailTrig (int) : Trailing stop-loss will be activated after a trade gains this number of pips. Default is 0(inactive)
trailDist (int) : SL will be opened at traildist after trailtrig is met, even if you do not have a SL placed.. Default is 0(inactive)
trailStep (int) : Moves trailing stop-loss once price moves to favourable by a specified number of pips. Default is 0(inactive)
atrTimeframe (string) : ATR Trailing Stop will be based on the specified timeframe in minutes and will only update once per bar close. Default is Timeframe.Period
atrTrigger (float) : Activate the trigger of ATR Trailing after market moves favourably by a number of pips. Default is 0(inactive)
atrPeriod (int) : ATR averaging period. Default is 0
atrMultiplier (float) : Multiple of ATR to utilise in the new SL computation. Default is 1
atrShift (float) : Relative shift of price information, 0 uses latest candle, 1 uses second last, etc. Default is 0
spread (float) : Enter the position only if the spread is equal or less than the specified value in pips. Default is 0(inactive)
accFilter (float) : Enter the position only if the account requirement is met. Default is 0(inactive)
secret (string)
comment (string) : Comment. Add a string into the order’s comment section. Default is "Symbol+Timeframe"
buyLimit(licenseId, symbol, pending, risk, sl, tp, beTrigger, beOffset, trailTrig, trailDist, trailStep, atrTimeframe, atrTrigger, atrPeriod, atrMultiplier, atrShift, spread, accFilter, secret, comment)
Create a buy limit order message
Parameters:
licenseId (string) : License Id. This is a unique identifier found in the Pineconnector Licensing Dashboard.
symbol (string) : Symbol. Default is syminfo.ticker
pending (float) : Computing pending order entry price. EA Options: Pips, Specified Price, Percentage
risk (float) : Risk. Function depends on the “Volume Type” selected in the EA
sl (float) : StopLoss. Place stop-loss. Computation is based on the Target Type selected in the EA. Default is 0(inactive)
tp (float) : TakeProfit. Place take-profit. Computation is based on the Target Type selected in the EA. Default is 0(inactive)
beTrigger (float) : Breakeven will be activated after the position gains this number of pips. Ensure > 0
beOffset (float) : Offset from entry price. 0 means the SL will be placed exactly at entry price. 1 means 1 pip above the entry price for buy trades and 1 pip below for sell trades.
trailTrig (int) : Trailing stop-loss will be activated after a trade gains this number of pips. Default is 0(inactive)
trailDist (int) : SL will be opened at traildist after trailtrig is met, even if you do not have a SL placed.. Default is 0(inactive)
trailStep (int) : Moves trailing stop-loss once price moves to favourable by a specified number of pips. Default is 0(inactive)
atrTimeframe (string) : ATR Trailing Stop will be based on the specified timeframe in minutes and will only update once per bar close. Default is Timeframe.Period
atrTrigger (float) : Activate the trigger of ATR Trailing after market moves favourably by a number of pips. Default is 0(inactive)
atrPeriod (int) : ATR averaging period. Default is 0
atrMultiplier (float) : Multiple of ATR to utilise in the new SL computation. Default is 1
atrShift (float) : Relative shift of price information, 0 uses latest candle, 1 uses second last, etc. Default is 0
spread (float) : Enter the position only if the spread is equal or less than the specified value in pips. Default is 0(inactive)
accFilter (float) : Enter the position only if the account requirement is met. Default is 0(inactive)
secret (string)
comment (string) : Comment. Add a string into the order’s comment section. Default is "Symbol+Timeframe"
buyStop(licenseId, symbol, pending, risk, sl, tp, beTrigger, beOffset, trailTrig, trailDist, trailStep, atrTimeframe, atrTrigger, atrPeriod, atrMultiplier, atrShift, spread, accFilter, secret, comment)
Create a buy stop order message
Parameters:
licenseId (string) : License Id. This is a unique identifier found in the Pineconnector Licensing Dashboard.
symbol (string) : Symbol. Default is syminfo.ticker
pending (float) : Computing pending order entry price. EA Options: Pips, Specified Price, Percentage
risk (float) : Risk. Function depends on the “Volume Type” selected in the EA
sl (float) : StopLoss. Place stop-loss. Computation is based on the Target Type selected in the EA. Default is 0(inactive)
tp (float) : TakeProfit. Place take-profit. Computation is based on the Target Type selected in the EA. Default is 0(inactive)
beTrigger (float) : Breakeven will be activated after the position gains this number of pips. Ensure > 0
beOffset (float) : Offset from entry price. 0 means the SL will be placed exactly at entry price. 1 means 1 pip above the entry price for buy trades and 1 pip below for sell trades.
trailTrig (int) : Trailing stop-loss will be activated after a trade gains this number of pips. Default is 0(inactive)
trailDist (int) : SL will be opened at traildist after trailtrig is met, even if you do not have a SL placed.. Default is 0(inactive)
trailStep (int) : Moves trailing stop-loss once price moves to favourable by a specified number of pips. Default is 0(inactive)
atrTimeframe (string) : ATR Trailing Stop will be based on the specified timeframe in minutes and will only update once per bar close. Default is Timeframe.Period
atrTrigger (float) : Activate the trigger of ATR Trailing after market moves favourably by a number of pips. Default is 0(inactive)
atrPeriod (int) : ATR averaging period. Default is 0
atrMultiplier (float) : Multiple of ATR to utilise in the new SL computation. Default is 1
atrShift (float) : Relative shift of price information, 0 uses latest candle, 1 uses second last, etc. Default is 0
spread (float) : Enter the position only if the spread is equal or less than the specified value in pips. Default is 0(inactive)
accFilter (float) : Enter the position only if the account requirement is met. Default is 0(inactive)
secret (string)
comment (string) : Comment. Add a string into the order’s comment section. Default is "Symbol+Timeframe"
sellLimit(licenseId, symbol, pending, risk, sl, tp, beTrigger, beOffset, trailTrig, trailDist, trailStep, atrTimeframe, atrTrigger, atrPeriod, atrMultiplier, atrShift, spread, accFilter, secret, comment)
Create a sell limit order message
Parameters:
licenseId (string) : License Id. This is a unique identifier found in the Pineconnector Licensing Dashboard.
symbol (string) : Symbol. Default is syminfo.ticker
pending (float) : Computing pending order entry price. EA Options: Pips, Specified Price, Percentage
risk (float) : Risk. Function depends on the “Volume Type” selected in the EA
sl (float) : StopLoss. Place stop-loss. Computation is based on the Target Type selected in the EA. Default is 0(inactive)
tp (float) : TakeProfit. Place take-profit. Computation is based on the Target Type selected in the EA. Default is 0(inactive)
beTrigger (float) : Breakeven will be activated after the position gains this number of pips. Ensure > 0
beOffset (float) : Offset from entry price. 0 means the SL will be placed exactly at entry price. 1 means 1 pip above the entry price for buy trades and 1 pip below for sell trades.
trailTrig (int) : Trailing stop-loss will be activated after a trade gains this number of pips. Default is 0(inactive)
trailDist (int) : SL will be opened at traildist after trailtrig is met, even if you do not have a SL placed.. Default is 0(inactive)
trailStep (int) : Moves trailing stop-loss once price moves to favourable by a specified number of pips. Default is 0(inactive)
atrTimeframe (string) : ATR Trailing Stop will be based on the specified timeframe in minutes and will only update once per bar close. Default is Timeframe.Period
atrTrigger (float) : Activate the trigger of ATR Trailing after market moves favourably by a number of pips. Default is 0(inactive)
atrPeriod (int) : ATR averaging period. Default is 0
atrMultiplier (float) : Multiple of ATR to utilise in the new SL computation. Default is 1
atrShift (float) : Relative shift of price information, 0 uses latest candle, 1 uses second last, etc. Default is 0
spread (float) : Enter the position only if the spread is equal or less than the specified value in pips. Default is 0(inactive)
accFilter (float) : Enter the position only if the account requirement is met. Default is 0(inactive)
secret (string)
comment (string) : Comment. Add a string into the order’s comment section. Default is "Symbol+Timeframe"
sellStop(licenseId, symbol, pending, risk, sl, tp, beTrigger, beOffset, trailTrig, trailDist, trailStep, atrTimeframe, atrTrigger, atrPeriod, atrMultiplier, atrShift, spread, accFilter, secret, comment)
Create a sell stop order message
Parameters:
licenseId (string) : License Id. This is a unique identifier found in the Pineconnector Licensing Dashboard.
symbol (string) : Symbol. Default is syminfo.ticker
pending (float) : Computing pending order entry price. EA Options: Pips, Specified Price, Percentage
risk (float) : Risk. Function depends on the “Volume Type” selected in the EA
sl (float) : StopLoss. Place stop-loss. Computation is based on the Target Type selected in the EA. Default is 0(inactive)
tp (float) : TakeProfit. Place take-profit. Computation is based on the Target Type selected in the EA. Default is 0(inactive)
beTrigger (float) : Breakeven will be activated after the position gains this number of pips. Ensure > 0
beOffset (float) : Offset from entry price. 0 means the SL will be placed exactly at entry price. 1 means 1 pip above the entry price for buy trades and 1 pip below for sell trades.
trailTrig (int) : Trailing stop-loss will be activated after a trade gains this number of pips. Default is 0(inactive)
trailDist (int) : SL will be opened at traildist after trailtrig is met, even if you do not have a SL placed.. Default is 0(inactive)
trailStep (int) : Moves trailing stop-loss once price moves to favourable by a specified number of pips. Default is 0(inactive)
atrTimeframe (string) : ATR Trailing Stop will be based on the specified timeframe in minutes and will only update once per bar close. Default is Timeframe.Period
atrTrigger (float) : Activate the trigger of ATR Trailing after market moves favourably by a number of pips. Default is 0(inactive)
atrPeriod (int) : ATR averaging period. Default is 0
atrMultiplier (float) : Multiple of ATR to utilise in the new SL computation. Default is 1
atrShift (float) : Relative shift of price information, 0 uses latest candle, 1 uses second last, etc. Default is 0
spread (float) : Enter the position only if the spread is equal or less than the specified value in pips. Default is 0(inactive)
accFilter (float) : Enter the position only if the account requirement is met. Default is 0(inactive)
secret (string)
comment (string) : Comment. Add a string into the order’s comment section. Default is "Symbol+Timeframe"
Metatrader
How to force strategies fire exit alerts not reversalsPineScript has gone a long way, from very simple and little-capable scripting language to a robust coding platform with reliable execution endpoints. However, this one small intuitivity glitch is still there and is likely to stay, because it is traditionally justified and quite intuitive for significant group of traders. I'm sharing this workaround in response to frequent inquiries about it.
What's the glitch? When setting alerts on strategies to be synchronized with TradingView's Strategy Tester events, using simple alert messages such as "buy" or "sell" based on entry direction seems straightforward by inserting {{strategy.order.action}} into the Create Alert's "Message" field. Because "buy" or "sell" are exactly the strings produced by {{strategy.order.action}} placeholder. However, complications arise when attempting to EXIT positions without reversing, whether triggered by price levels like Stop Loss or Take Profit, or logical conditions to close trades. Those bricks fall apart, because on such events {{strategy.order.action}} sends the same "sell" for exiting buy positions and "buy" for exiting sell positions, instead of something more differentiating like "closebuy" or "closesell". As a result reversal trades are opened, instead of simply closing the open ones.
This convention harkens back to traditional stock market practices, where traders either bought shares to enter positions or sold them to exit. However, modern trading encompasses diverse instruments like CFDs, indices, and Forex, alongside advanced features such as Stop Loss, reshaping the landscape. Despite these advancements, the traditional nomenclature persists.
And is poised to stay on TradingView as well, so we need a workaround to get a simple strategy going. Luckily it is here and is called alert_message . It is a parameter, which needs to be added into each strategy.entry() / strategy.exit() / strategy.close() function call - each call, which causes Strategy Tester to produce entry or exit orders. As in this example script:
line 12: strategy.entry(... alert_message ="buy")
line 14: strategy.entry(... alert_message ="sell")
line 19: strategy.exit(... alert_message ="closebuy")
line 20: strategy.exit(... alert_message ="closesell")
line 24: strategy.close(... alert_message ="closebuy")
line 26: strategy.close(... alert_message ="closesell")
These alert messages are compatible with the Alerts Syntax of TradingConnector - a tool facilitating auto-execution of TradingView alerts in MetaTrader 4 or 5. Yes, simple alert messages like "buy" / "sell" / "closebuy" / "closesell" suffice to carry the execution of simple strategy, without complex JSON files with multiple ids and such. Other parameters can be added (actually plenty), but they are only option and that's not a part of this story :)
Last thing left to do is to replace "Message" in Create Alert popup with {{strategy.order.alert_message}} . This placeholder transmits the string defined in the PineScript alert_message= parameter, as outlined in this publication. With this workaround, executing closing alerts becomes seamless within PineScript strategies on TradingView.
Disclaimer: this content is purely educational, especially please don't pay attention to backtest results on any timeframe/ticker.
Moving Stop-Loss mechanism + alerts to MT4/MT5"How to code moving stop-loss mechanism", is one of the most often repeating questions in private messages I receive, so just to focus on this mechanism, I made a spin-off from my previous script: TradingView-Alerts-to-MT4-MT5-dynamic-variables-NON-REPAINTING .
The logic here moves the stop-loss each time a trade is running and a new pivot high/low is detected. When such event occurs (UpdateLongStopLoss or UpdateShortStopLoss), stoploss_long or stoploss_short mutable variable is modified. And it needs to be coded inside strategy.exit() line as "stop=stoploss_long" or "stop=stoploss_short". Entries are pretty straightforward - on Stoch crosses.
Last lines of the script show how to wrap information about such updates and send send alerts to MetaTrader via TradingConnector for execution in Forex/indices/commodities/crypto markets via MetaTrader. Please note that "tradeid=" variable must be passed with each alert, to let MetaTrader know which trade to modify. SLMOD, TPMOD are recently added commands, along with BE (as in "move stop-loss to breakeven" - but that's another topic).
Please disregard strategy backtest results, as this script is for coding education purposes only. However, it seems with the stop-loss mechanism enabled, the results are even better, than in original version of the script :)