Untitled

 avatar
unknown
plain_text
2 years ago
4.2 kB
7
Indexable
//@version=5
strategy('MNQ Momentum - 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,10)
ma200s = ta.ema(close, 30)

ma20l = ta.sma(close,4)
ma200l = ta.ema(close, 30)


// Determine if it's the first 2 min candle after market open
//firstCandle = hour(time, 'UTC-4') == 10 and minute(time, 'UTC-4') == 30
var bool firstCandle = na

if (month(time) >= 3 and month(time) < 11)
    firstCandle := hour(time, 'UTC-4') == 10 and minute(time, 'UTC-4') == 30
else
    firstCandle := hour(time, 'UTC-5') == 10 and minute(time, 'UTC-5') == 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)
    
    longStopLevel := low[0] -10
    longRisk := close[0] - low[0]
    longTakeProfitLevel := close[0] + 4 * 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)
   
    
    // when we get to 10K+ we can adjust the sl
   
    shortStopLevel:= high
    shortRisk := high[0] - close[0]
    shortTakeProfitLevel := close[0] - 1 * 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
if (strategy.position_size > 0)
    strategy.exit('Long Exit', 'Buy', stop=longStopLevel, limit=longTakeProfitLevel)
    
if (strategy.position_size < 0)
    strategy.exit('Short Exit', 'Sell', stop=shortStopLevel, limit=shortTakeProfitLevel)
  

// Exit condition
//if (isFriday)
// // // // //     //and strategy.openprofit<0)
// // // // //     //and close[0] <= (strategy.position_avg_price)) 
  //  strategy.close_all()

plot(longStopLevel, color=color.new(color.red, 0), linewidth=2)
plot( longTakeProfitLevel, color=color.new(color.green, 0), linewidth=2)



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

plot(ma200s, color=color.new(color.black, 0), linewidth=2)
plot( ma20s, color=color.new(color.black, 0), linewidth=2)



Editor is loading...