Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.5 kB
12
Indexable
Never
//@version=5
strategy('MNQ - test', overlay=true)
import HeWhoMustNotBeNamed/RecursiveAlerts/2 as ra

//Step 1. What all parameters(keys) need to be sent in alerts.
keys = array.from("{side}","{entry}", "{stop}", "{target1}")

//Step 2. Create a default alert template
template = '{\n

         \t"entry" : {entry},\n
         \t"stop" : {stop},\n
         \t"target1" : {target1},\n
         \t"side" : {side}\n
     }'

//Step 3. Create a user input where users can [symbol=alter]alter[/symbol] the default alert template
inputTemplate = input.text_area(template, 'Alert Template')

// Calculate Moving Averages
ma20s = ta.sma(close,50)
ma200s = ta.ema(close, 100)

ma20l = ta.sma(close,120)
ma200l = ta.ema(close, 200)

firstCandle = hour(time, 'UTC-4') == 07 and minute(time, 'UTC-4') == 30

// Determine if it's before 10:00
beforeTenAM = hour(time, 'UTC-4') < 22

// Conditions for entry LONG
greenCandle = close[0] > low[0]
aboveMA20l = close[0] > ma20l[0]
aboveMA200l = close[0] > ma200l[0]

// Conditions for entry SHORT
redCandle = open[0] < high[0]
belowMA20s = close[0] < ma20s[0]
belowMA200s = close[0] < ma200s[0]

// Initialize stop loss, risk, and take profit levels for LONG and SHORT
var float longStopLevel = na
var float longRisk = na
var float longTakeProfitLevel = na
var float shortStopLevel = na
var float shortRisk = na
var float shortTakeProfitLevel = na
isFriday = dayofweek == dayofweek.friday
isMonday = dayofweek == dayofweek.monday

if firstCandle and greenCandle and aboveMA20l and aboveMA200l and strategy.position_size == 0
    strategy.entry('Buy', strategy.long)
    if close[0] - low[0] >=25
        longStopLevel := close -40
    else
        longStopLevel := low[0] -30

    longRisk := close[0] - low[0]
    longTakeProfitLevel := close[0] + 7 * longRisk
    values = array.from("buy", str.tostring(close[0]), str.tostring(longStopLevel), str.tostring(longTakeProfitLevel))
    alertMessage = ra.updateAlertTemplate(inputTemplate, keys, values)
    if (strategy.position_size == 0)
        alert(alertMessage, alert.freq_once_per_bar_close)

if firstCandle and redCandle and belowMA20s and belowMA200s and strategy.position_size == 0
    strategy.entry('Sell', strategy.short)
    if high[0] - close[0] >=50
        shortStopLevel:= open +34
    else
        shortStopLevel:= open +100
    shortRisk := high[0] - close[0]
    shortTakeProfitLevel := close[0] - 7 * shortRisk
    values = array.from("sell",str.tostring(close[0]), str.tostring(shortStopLevel), str.tostring(shortTakeProfitLevel))
    alertMessage = ra.updateAlertTemplate(inputTemplate, keys, values)
    if (strategy.position_size == 0)
        alert(alertMessage, alert.freq_once_per_bar_close)

// Check if the price has reached 2R and update stop loss accordingly
if (strategy.position_size > 0)
    if (close[0] >= (strategy.position_avg_price + 5 * longRisk))
        longStopLevel := strategy.position_avg_price + longRisk

if (strategy.position_size < 0)
    if (close[0] <= (strategy.position_avg_price - 5 * shortRisk))
        shortStopLevel := strategy.position_avg_price - shortRisk

// Set exit orders
strategy.exit('Long Exit', 'Buy', stop=longStopLevel, limit=longTakeProfitLevel)
strategy.exit('Short Exit', 'Sell', stop=shortStopLevel, limit=shortTakeProfitLevel)



// Exit condition
if (isFriday and beforeTenAM)
    strategy.close_all()

plot(ma20l, color=color.new(color.red, 0), linewidth=2)
plot(ma200l, color=color.new(color.blue, 0), linewidth=2)