Eoin_Coffey
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: February 12th, 2022

Translating sl and tp orders into mt4 using pineconnector

Hey people of the pinescript community. I need some help please! I've set up my code, and sometimes my stop loss and take profit orders go thru, and sometimes they dont. This is in regard to pineconnector. if anyone has any idea of how to change this or make it consistent I'd love it. I'll even cash app 5$ if you can figure it out lol but seriously. ill send in my code here.

//@version=4
strategy(title="MACD MASTER STRATEGY", shorttitle="MACBIGD", overlay = true, calc_on_order_fills=true, initial_capital=10000, default_qty_value=1)

trend = input(title="Display 200 EA", type=input.bool, defval=false)
trendma = ema(close, 200)
labeled = input(title="Display strategy name with signals", type=input.bool, defval=true)
indicators = input(title="Display indicators", type=input.bool, defval=false)
plot(trend ? trendma : na, title = "200 EA", color=color.black, linewidth = 3)


////////

// MACD
macdenabler = input(title="Enable MACD ? 🎯", type=input.bool, defval=true)

fast_length = 12 //input(title="Fast Length", type=input.integer, defval=12)
slow_length = 26 //input(title="Slow Length", type=input.integer, defval=26)
srcmacd = close //input(title="Source", type=input.source, defval=close)
signal_length = 9 //input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
// Calculation
fast_ma = ema(srcmacd, fast_length)
slow_ma = ema(srcmacd, slow_length)
macd = fast_ma - slow_ma
signal = ema(macd, signal_length)
hist = macd - signal
//Strategy
buymacd = crossover (macd, signal) and low > trendma and macd[1]<0
sellmacd = crossunder (macd, signal) and high < trendma and macd [1]> 0

plotshape(labeled and macdenabler ? buymacd :na, style = shape.triangleup, size = size.small, location = location.belowbar, color = color.red, title = "MACD Buy L", text = "MACD")
plotshape(labeled and macdenabler ? sellmacd :na, style = shape.triangledown, size = size.small, location = location.abovebar, color = color.red, title = "MACD Sell L", text = "MACD")

plotshape(labeled or not macdenabler ? na : buymacd, style = shape.triangleup, size = size.small, location = location.belowbar, color = color.red, title = "MACD Buy")
plotshape(labeled or not macdenabler ? na : sellmacd, style = shape.triangledown, size = size.small, location = location.abovebar, color = color.red, title = "MACD Sell")


//Donchian Channels
donenabler = input(title="Enable Donchian Channels ? 🎯", type=input.bool, defval=true)
lendon = 20 //input(20, minval=1)
lowerdon = lowest(lendon)
upperdon = highest(lendon)
basisdon = avg(upperdon, lowerdon)

//Tady by to chtělo ošetřit případ, kdy ani jedno ještě nenastalo.
gorilla = barssince (lowerdon[1] > lowerdon)
stegosaurus = barssince (upperdon[1] < upperdon)

buydon = low > trendma and gorilla[1] <= stegosaurus[1] and high > upperdon[1]
selldon = high < trendma and gorilla[1] >= stegosaurus[1] and low < lowerdon[1]

plotshape(labeled and donenabler ? buydon :na, style = shape.triangleup, size = size.small, location = location.belowbar, color = color.blue, title = "Donchian Buy L", text = "Donchian")
plotshape(labeled and donenabler ? selldon :na, style = shape.triangledown, size = size.small, location = location.abovebar, color = color.blue, title = "Donchian Sell L", text = "Donchian")

plotshape(labeled or not donenabler ? na : buydon, style = shape.triangleup, size = size.small, location = location.belowbar, color = color.blue, title = "Donchian Buy")
plotshape(labeled or not donenabler ? na : selldon, style = shape.triangledown, size = size.small, location = location.abovebar, color = color.blue, title = "Donchian Sell")

donalertshard = (upperdon-lowerdon) *0.07
donalertshardlong = high > (upperdon[1] - donalertshard)
donalertshardshort = low < (lowerdon[1] + donalertshard)
donalert = (low > trendma and gorilla[1] <= stegosaurus[1] and donalertshardlong) or (high < trendma and gorilla[1] >= stegosaurus[1] and donalertshardshort)


//plot Donchian
plot(indicators and donenabler ? basisdon : na, "Basis Donchian", color=color.blue, linewidth = 1)
udon = plot(indicators and donenabler ? upperdon : na, "Upper Donchian", color=color.blue, linewidth = 2)
ldon = plot(indicators and donenabler ? lowerdon : na, "Lower Donchian", color=color.blue, linewidth = 2)
fill(udon, ldon, color=color.new(color.blue,94), title="Background Donchian")

//Ichimoku strategy
ichimokuenabler = input(title="Enable Ichimoku Cloud ? 🎯", type=input.bool, defval=true)
conversionPeriods = 9//input(9, minval=1, title="Conversion Line Periods")
basePeriods = 26//input(26, minval=1, title="Base Line Periods")
laggingSpan2Periods = 52//input(52, minval=1, title="Lagging Span 2 Periods")
displacement = 26//input(26, minval=1, title="Displacement")
donchian(len) => avg(lowest(len), highest(len))
conversionLine = donchian(conversionPeriods)
baseLine = donchian(basePeriods)
leadLine1 = avg(conversionLine, baseLine)
leadLine2 = donchian(laggingSpan2Periods)

buyichi = low > trendma and low > leadLine1[displacement] and low > leadLine2[displacement] and crossover(conversionLine,baseLine)
sellichi = high < trendma and high < leadLine1[displacement] and high < leadLine2[displacement] and crossunder(conversionLine,baseLine)
//

plotshape(labeled and ichimokuenabler ? buyichi :na, style = shape.triangleup, size = size.small, location = location.belowbar, color = #00FF93, title = "Ichimoku Buy L", text = "Ichimoku")
plotshape(labeled and ichimokuenabler ? sellichi :na, style = shape.triangledown, size = size.small, location = location.abovebar, color = #00FF93, title = "Ichimoku Sell L", text = "Ichimoku")

plotshape(labeled or not ichimokuenabler ? na : buyichi, style = shape.triangleup, size = size.small, location = location.belowbar, color = #00FF93, title = "Ichimoku Buy")
plotshape(labeled or not ichimokuenabler ? na : sellichi, style = shape.triangledown, size = size.small, location = location.abovebar, color = #00FF93, title = "Ichimoku Sell")


//Ichimoku plotting

plot(ichimokuenabler and indicators ? conversionLine : na, color=#0496ff, title="Conversion Line")
plot(ichimokuenabler and indicators ? baseLine : na, color=#991515, title="Base Line")
plot(ichimokuenabler and indicators ? close : na, offset = -displacement + 1, color=#459915, title="Lagging Span")
p1 = plot(ichimokuenabler and indicators ? leadLine1 : na, offset = displacement - 1, color=color.green, title="Lead 1")
p2 = plot(ichimokuenabler and indicators ? leadLine2 : na, offset = displacement - 1, color=color.red, title="Lead 2")
fill(p1, p2, color = leadLine1 > leadLine2 ? color.green : color.red)

//Schaff Trend Cycle
// Copyright (c) 2018-present, Alex Orekhov (everget)
// Schaff Trend Cycle script may be freely distributed under the MIT license.
//Schaff was modified by greenmask9 for purpose of this multiscript study

schaffenabler = input(title="Enable Schaff Trend Cycle ? 🎯", type=input.bool, defval=false)

schffastLength = 23//input(title="MACD Fast Length", type=integer, defval=23)
schfslowLength = 50//input(title="MACD Slow Length", type=integer, defval=50)
schfcycleLength = 10//input(title="Cycle Length", type=integer, defval=10)
d1Length = 3//input(title="1st %D Length", type=integer, defval=3)
d2Length = 3//input(title="2nd %D Length", type=integer, defval=3)
srcschaff = close//input(title="Source", type=source, defval=close)

schfmacd = ema(srcschaff, schffastLength) - ema(srcschaff, schfslowLength)
k = nz(fixnan(stoch(schfmacd, schfmacd, schfmacd, schfcycleLength)))
d = ema(k, d1Length)
kd = nz(fixnan(stoch(d, d, d, schfcycleLength)))
stc = ema(kd, d2Length)
stc := stc > 100 ? 100 : stc < 0 ? 0 : stc
upper = 75
lower = 25

Schaff_conditions_up = crossover(stc, lower) and low > trendma
Schaff_conditions_down = crossunder(stc, upper) and high < trendma

plotshape(labeled and schaffenabler ? Schaff_conditions_up :na, style = shape.triangleup, size = size.small, location = location.belowbar, color = color.green, title = "Schaff Buy L", text = "Schaff")
plotshape(labeled and schaffenabler ? Schaff_conditions_down :na, style = shape.triangledown, size = size.small, location = location.abovebar, color = color.green, title = "Schaff Sell L", text = "Schaff")

plotshape(labeled or not schaffenabler ? na : Schaff_conditions_up, style = shape.triangleup, size = size.small, location = location.belowbar, color = color.green, title = "Schaff Buy")
plotshape(labeled or not schaffenabler ? na : Schaff_conditions_down, style = shape.triangledown, size = size.small, location = location.abovebar, color = color.green, title = "Schaff Sell")





//All Enabled Alerts


/////////system run



/////Risk settings
var g_risk = "Risk Settings"
pips = input(title="Stop Pips", type=input.float, defval=2.0, group=g_risk, tooltip="How many pips above high to put stop loss")
rr = input(title="Risk:Reward", type=input.float, defval=1.0, group=g_risk, tooltip="This determines the risk:reward profile of the setup")

/// Calculating stops and targets
shortStopPrice = high + (syminfo.mintick * pips * 100)
shortStopDistance = shortStopPrice - close
shortTargetPrice = close - (shortStopDistance * rr)

longStopPrice = low - (syminfo.mintick * pips * 100)
longStopDistance = close - longStopPrice
longTargetPrice = close + (longStopDistance * rr)

var tradeStopPrice = 0.0
var tradeTargetPrice = 0.0

long = crossover (macd, signal) and low > trendma and macd[1]<0
short = crossunder (macd, signal) and high < trendma and macd [1]> 0


///
// PineConnector Settings
var g_pc = "pineconnector settings"
pc_id = input(title="License ID", defval="6070518874325", type=input.string, group=g_pc, tooltip="This is your PineConnector license ID")
pc_risk = input(title="Risk Per Trade", defval=1, step=0.5, type=input.float, group=g_pc, tooltip="This is how much to risk per trade (if set to %, use whole numbers)")
pc_prefix = input(title="MetaTrader Prefix", defval="", type=input.string, group=g_pc, tooltip="This is your broker's MetaTrader symbol prefix")
pc_suffix = input(title="MetaTrader Suffix", defval="", type=input.string, group=g_pc, tooltip="This is your broker's MetaTrader symbol suffix")
pc_limit = input(title="Use Limit Order?", defval=false, type=input.bool, group=g_pc, tooltip="If true a limit order will be used, if false a market order will be used")

// Generate PineConnector alert string
var symbol = pc_prefix + syminfo.ticker + pc_suffix
var limit = pc_limit ? "limit" : ""
price = pc_limit ? "price=" + tostring(close) + "," : ""
pc_entry_alert(direction, price, sl, tp) =>
pc_id + "," + direction + "," + symbol + "," + price + "sl=" + tostring(sl) + ",tp=" + tostring(tp) + ",risk=" + tostring(pc_risk)

// See if this bar's time happened on/after start date (to stop script taking too many trades)
afterStartDateFilter = time >= timestamp(syminfo.timezone, 2021, 12, 1, 0, 0)

// Test long command
longCondition = crossover (macd, signal) and low > trendma and macd[1]<0

if longCondition and barstate.isconfirmed
tradeStopPrice := longStopPrice
tradeTargetPrice := longTargetPrice
alert_string = pc_entry_alert("buy", price, tradeStopPrice, tradeTargetPrice)
alert(alert_string, alert.freq_all)
strategy.entry("Long", strategy.long, comment=alert_string)

// Test short command
shortCondition = crossunder (macd, signal) and high < trendma and macd[1]> 0

if shortCondition and barstate.isconfirmed
tradeStopPrice := shortStopPrice
tradeTargetPrice := shortTargetPrice
alert_string = pc_entry_alert("sell", price, tradeStopPrice, tradeTargetPrice)
alert(alert_string, alert.freq_all)
strategy.entry("Short", strategy.short, comment=alert_string)

// Exit mock trades
strategy.exit(id="Long Exit", from_entry="Long", limit=tradeTargetPrice, stop=tradeStopPrice, when=strategy.position_size != 0)
strategy.exit(id="Short Exit", from_entry="Short", limit=tradeTargetPrice, stop=tradeStopPrice, when=strategy.position_size != 0)

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Translating sl and tp orders into mt4 using pineconnector

Hey , just to clarify , are you setting the alerts on the strategy or the alerts.

i.e. setting alerts on "Order Fills Only" or on "alert() function calls only"

Return to “Pine Script Q&A”