@Hardik I was inspired, by your video on webhooks, am absolute beginner at coding, have written this code using chat GPT,
the orders are being sent to exchange, but they are not exiting,
code
//@version=5
strategy(“2 - EMA NOT Working WEBHOOK”, overlay=true, shorttitle=“2”)
// Define input parameters
shortTermLength = input.int(2, title=“Short-Term EMA Length”, minval=1)
longTermLength = input.int(5, title=“Long-Term EMA Length”, minval=1)
stopLossPercent = input.float(1.5, title=“Stop Loss (%)”, step=0.1)
takeProfitPercent = input.float(3.0, title=“Take Profit (%)”, step=0.1)
startDate = input.time(timestamp(“2024-09-19 09:15”), title=“Start Date”)
endDate = input.time(timestamp(“2024-09-22 23:59”), title=“End Date”)
// Calculate EMAs
shortTermEMA = ta.ema(close, shortTermLength)
longTermEMA = ta.ema(close, longTermLength)
// Plot EMAs on the chart
plot(shortTermEMA, title=“Short-Term EMA”, color=color.blue, linewidth=2)
plot(longTermEMA, title=“Long-Term EMA”, color=color.red, linewidth=2)
// Check if current time is within the specified date range
inDateRange = (time >= startDate and time <= endDate)
// Define entry conditions
longCondition = inDateRange and ta.crossover(shortTermEMA, longTermEMA)
shortCondition = inDateRange and ta.crossunder(shortTermEMA, longTermEMA)
// Execute trades based on conditions
if (inDateRange)
if (longCondition)
strategy.entry(“Long”, strategy.long)
alert(“Long Entry Signal: Short-Term EMA crossed above Long-Term EMA”, alert.freq_once_per_bar_close)
if (strategy.position_size > 0)
stopLossLevel = strategy.position_avg_price * (1 - stopLossPercent / 100)
takeProfitLevel = strategy.position_avg_price * (1 + takeProfitPercent / 100)
// Set stop loss and take profit orders
strategy.exit("Exit Chotu", "Long", limit=takeProfitLevel, stop=stopLossLevel)
// Prepare JSON for webhook alert with dynamic stop loss and take profit prices
stopLossJson = '{"secret":"AQcgp","alertType":"multi_leg_order","order_legs":[{"transactionType":"B","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"CESC","instrument":"EQ","productType":"I","sort_order":"1","price":"0"},{"transactionType":"S","orderType":"SL","quantity":"1","exchange":"NSE","symbol":"CESC","instrument":"EQ","productType":"I","sort_order":"2","triggerPrice":"' + str.tostring(stopLossLevel) + '","price":"0"}]}'
takeProfitJson = '{"secret":"AQcgp","alertType":"multi_leg_order","order_legs":[{"transactionType":"B","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"CESC","instrument":"EQ","productType":"I","sort_order":"1","price":"0"},{"transactionType":"S","orderType":"SL","quantity":"1","exchange":"NSE","symbol":"CESC","instrument":"EQ","productType":"I","sort_order":"2","triggerPrice":"' + str.tostring(stopLossLevel) + '","price":"0"},{"transactionType":"S","orderType":"TP","quantity":"1","exchange":"NSE","symbol":"CESC","instrument":"EQ","productType":"I","sort_order":"3","triggerPrice":"' + str.tostring(takeProfitLevel) + '","price":"0"}]}'
// Trigger alerts for stop loss and take profit
if (close <= stopLossLevel)
alert(stopLossJson, alert.freq_once_per_bar_close)
if (close >= takeProfitLevel)
alert(takeProfitJson, alert.freq_once_per_bar_close)
json
{“secret”:“AQcgp”,“alertType”:“multi_leg_order”,“order_legs”:[{“transactionType”:“B”,“orderType”:“MKT”,“quantity”:“1”,“exchange”:“NSE”,“symbol”:“CESC”,“instrument”:“EQ”,“productType”:“I”,“sort_order”:“1”,“price”:“0”},{“transactionType”:“S”,“orderType”:“SL”,“quantity”:“1”,“exchange”:“NSE”,“symbol”:“CESC”,“instrument”:“EQ”,“productType”:“I”,“sort_order”:“2”,“triggerPrice”:“{{stopLossLevel}}”,“price”:“0”}]}
Sl 0.4% , take profit % .04
This is only for entering a position. You will have to write a similar message for
exit position as well if you wish to create separate conditions for exit.
kindly help what I am a doing wrong