Untitled
unknown
plain_text
2 years ago
13 kB
16
Indexable
//@version=5
strategy('NDX100 Momentum - live', overlay=true,default_qty_type=strategy.fixed, currency=currency.USD)
import HeWhoMustNotBeNamed/RecursiveAlerts/2 as ra
//Step 1. What all parameters(keys) need to be sent in alerts.
keys = array.from("{side}","{amount}", "{stop}", "{final_target}")
keys2 = array.from("{close}")
//Step 2. Create a default alert template
template = '{side} NDX100 Q={amount} \nupdate NDX100 SL={stop}, TP={final_target}\n'
template2='
{close}'
//Step 3. Create a user input where users can [symbol=alter]alter[/symbol] the default alert template
inputTemplate = input.text_area(template, 'Alert Template')
inputTemplateCLOSE = input.text_area(template2, 'Alert Template CLOSE')
// Calculate Moving Averages
ma20s = ta.sma(close,10)
ma200s = ta.ema(close, 30)
ma20l = ta.sma(close,4)
ma200l = ta.ema(close, 200)
// Determine if it's the first 2 min candle after market open\
var bool firstCandle = na
var bool secondCandle = na
var bool thirdCandle = na
var bool beforeTenAM = na
//if(hour(time, 'UTC-5') == 09 and minute(time, 'UTC-5') == 0)
//firstCandle :=hour(time, 'UTC-5') == 09 and minute(time, 'UTC-5') == 0
if(hour(time, 'UTC-5') == 10 and minute(time, 'UTC-5') == 30)
firstCandle := hour(time, 'UTC-5') == 10 and minute(time, 'UTC-5') == 30
secondCandle:=hour(time, 'UTC-5') == 09 and minute(time, 'UTC-5') == 00
thirdCandle:=hour(time, 'UTC-5') == 12 and minute(time, 'UTC-5') == 00
beforeTenAM := hour(time, 'UTC-5') ==14 and minute(time, 'UTC-5') == 30
else
firstCandle := hour(time, 'UTC-4') == 10 and minute(time, 'UTC-4') == 30
secondCandle:= hour(time, 'UTC-4') == 09 and minute(time, 'UTC-4') == 00
beforeTenAM := hour(time, 'UTC-4') ==14 and minute(time, 'UTC-4') == 30
thirdCandle:=hour(time, 'UTC-4') == 12 and minute(time, 'UTC-4') == 00
// Determine if it's before 10:00
//beforeTenAM = hour(time, 'UTC-4') ==15
currentBalance = strategy.initial_capital + strategy.netprofit
// Conditions for entry LONG
greenCandle = close[0] > open[0]
aboveMA20l = close[0] > ma20l[0]
aboveMA200l = close[0] > ma200l[0]
// Conditions for entry SHORT
redCandle = close[0] < open[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
isTuesday = dayofweek == dayofweek.tuesday
isMonday = dayofweek == dayofweek.monday
balanceInContracts = math.abs(currentBalance/close)
equity = strategy.equity
risk_percentage = 0.008
dollar_risk = currentBalance * risk_percentage
//or hour(time, 'UTC-4') == 09 and minute(time, 'UTC-4') == 00)
if (year >= 202 and month>= 0 )
if (firstCandle and aboveMA20l and aboveMA200l)
// for testing
//if (open<close)
if strategy.position_size <=-1
strategy.close_all()
values = array.from("close NDX100")
alertMessage = ra.updateAlertTemplate(inputTemplateCLOSE, keys2, values)
alert(alertMessage, alert.freq_once_per_bar_close)
longStopLevel := low[0]-10
longRisk := close[0] - low[0]
longTakeProfitLevel := close[0] + 3 * longRisk
entry_quantity = (equity * 3) / (close-longStopLevel * 0.02)
longPositionSize = dollar_risk / (close[0] - low[0])
long_risk_per_share = math.abs(close[0] - longStopLevel)
long_balanceInContracts = long_risk_per_share > 0 ? dollar_risk / long_risk_per_share : 0
strategy.entry('Buy', strategy.long, qty=long_balanceInContracts)
values = array.from("buy",str.tostring(long_balanceInContracts/10), str.tostring(longStopLevel), str.tostring(longTakeProfitLevel))
alertMessage = ra.updateAlertTemplate(inputTemplate, keys, values)
alert(alertMessage, alert.freq_once_per_bar_close)
//strategy.exit('tp1', 'Buy',limit=close[0] + 3 * longRisk, qty_percent = 50)
// strategy.exit('Long Exit', 'Buy', stop=longStopLevel, limit=longTakeProfitLevel,qty_percent = 100)
strategy.exit('Long Exit', 'Buy', stop=longStopLevel, limit=longTakeProfitLevel)
if (secondCandle and aboveMA20l and aboveMA200l )
// for testing
//if (open<close)
if strategy.position_size <=-1
strategy.close_all()
values = array.from("close NDX100")
alertMessage = ra.updateAlertTemplate(inputTemplateCLOSE, keys2, values)
alert(alertMessage, alert.freq_once_per_bar_close)
longStopLevel := low[0]-10
longRisk := close[0] - low[0]
longTakeProfitLevel := close[0] + 1 * longRisk
entry_quantity = (equity * 3) / (close-longStopLevel * 0.02)
longPositionSize = dollar_risk / (close[0] - low[0])
long_risk_per_share = math.abs(close[0] - longStopLevel)
long_balanceInContracts = long_risk_per_share > 0 ? dollar_risk / long_risk_per_share : 0
strategy.entry('Buy', strategy.long, qty=long_balanceInContracts)
values = array.from("buy",str.tostring(long_balanceInContracts/10), str.tostring(longStopLevel), str.tostring(longTakeProfitLevel))
alertMessage = ra.updateAlertTemplate(inputTemplate, keys, values)
alert(alertMessage, alert.freq_once_per_bar_close)
//strategy.exit('tp1', 'Buy',limit=close[0] + 3 * longRisk, qty_percent = 50)
// strategy.exit('Long Exit', 'Buy', stop=longStopLevel, limit=longTakeProfitLevel,qty_percent = 100)
strategy.exit('Long Exit', 'Buy', stop=longStopLevel, limit=longTakeProfitLevel)
strategy.exit('Long Exit', 'Buy', stop=longStopLevel, limit=longTakeProfitLevel,qty_percent = 100)
// if (secondCandle and greenCandle and aboveMA20l and aboveMA200l)
// // for testing
// //if (open<close)
// if strategy.position_size <=-1
// strategy.close_all()
// values = array.from("close NDX100")
// alertMessage = ra.updateAlertTemplate(inputTemplateCLOSE, keys2, values)
// alert(alertMessage, alert.freq_once_per_bar_close)
// strategy.entry('Buy', strategy.long,qty=balanceInContracts)
// longStopLevel := low[0]-10
// longRisk := close[0] - low[0]
// longTakeProfitLevel := close[0] + 2 * longRisk
// values = array.from("buy",str.tostring(balanceInContracts/10), str.tostring(longStopLevel), str.tostring(longTakeProfitLevel))
// alertMessage = ra.updateAlertTemplate(inputTemplate, keys, values)
// alert(alertMessage, alert.freq_once_per_bar_close)
// //strategy.exit('Long Exit', 'Buy', stop=longStopLevel, limit=longTakeProfitLevel)
// if (secondCandle and greenCandle and aboveMA20l and aboveMA200l)
// // for testing
// //if (open<close)
// if strategy.position_size <=-1
// strategy.close_all()
// values = array.from("close NDX100")
// alertMessage = ra.updateAlertTemplate(inputTemplateCLOSE, keys2, values)
// alert(alertMessage, alert.freq_once_per_bar_close)
// longStopLevel := low[0]-10
// longRisk := close[0] - low[0]
// longTakeProfitLevel := close[0] + 1.5 * longRisk
// entry_quantity = (equity * 3) / (close-longStopLevel * 0.02)
// longPositionSize = dollar_risk / (close[0] - low[0])
// long_risk_per_share = math.abs(close[0] - longStopLevel)
// long_balanceInContracts = long_risk_per_share > 0 ? dollar_risk / long_risk_per_share : 0
// strategy.entry('Buy', strategy.long, qty=long_balanceInContracts)
// values = array.from("buy",str.tostring(long_balanceInContracts/10), str.tostring(longStopLevel), str.tostring(longTakeProfitLevel))
// alertMessage = ra.updateAlertTemplate(inputTemplate, keys, values)
// alert(alertMessage, alert.freq_once_per_bar_close)
// //strategy.exit('tp1', 'Buy',limit=close[0] + 3 * longRisk, qty_percent = 50)
// // strategy.exit('Long Exit', 'Buy', stop=longStopLevel, limit=longTakeProfitLevel,qty_percent = 100)
// strategy.exit('Long Exit', 'Buy', stop=longStopLevel, limit=longTakeProfitLevel)
if firstCandle and redCandle and belowMA20s and belowMA200s
//for testing
//if (close<open)
if strategy.position_size >=1
strategy.close_all()
values = array.from("close NDX100")
alertMessage = ra.updateAlertTemplate(inputTemplateCLOSE, keys2, values)
alert(alertMessage, alert.freq_once_per_bar_close)
shortPositionSize = dollar_risk / (high[0] - close[0])
// when we get to 10K+ we can adjust the sl
shortStopLevel:= high+10
shortRisk := high[0] - close[0]
shortTakeProfitLevel := close[0] - 1 * shortRisk
entry_quantity = (equity * 3) / (shortStopLevel-close * 0.0)
short_risk_per_share = math.abs(shortStopLevel - close[0])
short_balanceInContracts = short_risk_per_share > 0 ? dollar_risk / short_risk_per_share : 0
strategy.entry('Sell', strategy.short,qty=short_balanceInContracts)
values = array.from("sell",str.tostring(short_balanceInContracts/10), str.tostring(shortStopLevel), str.tostring(shortTakeProfitLevel))
alertMessage = ra.updateAlertTemplate(inputTemplate, keys, values)
alert(alertMessage, alert.freq_once_per_bar_close)
//strategy.exit('tp1', 'Sell', limit=close[0] - 1 * shortRisk, qty_percent = 75)
strategy.exit('Short Exit', 'Sell', stop=shortStopLevel, limit=shortTakeProfitLevel)
// if secondCandle and redCandle and belowMA20s and belowMA200s
// //for testing
// //if (close<open)
// if strategy.position_size >=1
// strategy.close_all()
// values = array.from("close NDX100")
// alertMessage = ra.updateAlertTemplate(inputTemplateCLOSE, keys2, values)
// alert(alertMessage, alert.freq_once_per_bar_close)
// shortPositionSize = dollar_risk / (high[0] - close[0])
// // when we get to 10K+ we can adjust the sl
// shortStopLevel:= high+10
// shortRisk := high[0] - close[0]
// shortTakeProfitLevel := close[0] - 1 * shortRisk
// entry_quantity = (equity * 3) / (shortStopLevel-close * 0.0)
// short_risk_per_share = math.abs(shortStopLevel - close[0])
// short_balanceInContracts = short_risk_per_share > 0 ? dollar_risk / short_risk_per_share : 0
// strategy.entry('Sell', strategy.short,qty=short_balanceInContracts)
// values = array.from("sell",str.tostring(short_balanceInContracts/10), str.tostring(shortStopLevel), str.tostring(shortTakeProfitLevel))
// alertMessage = ra.updateAlertTemplate(inputTemplate, keys, values)
// alert(alertMessage, alert.freq_once_per_bar_close)
// //strategy.exit('tp1', 'Sell', limit=close[0] - 1 * shortRisk, qty_percent = 75)
// strategy.exit('Short Exit', 'Sell', stop=shortStopLevel, limit=shortTakeProfitLevel)
//strategy.exit('Short Exit', 'Sell', stop=shortStopLevel, limit=shortTakeProfitLevel)
// // Check if the price has reached 2R and update stop loss accordingly
// if (strategy.position_size > 0)
// if (close[0] >= (strategy.position_avg_price + 2.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)
// if ( beforeTenAM and isFriday )
// strategy.close_all()
// 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.green, 0), linewidth=2)
plot( ma20l, color=color.new(color.green, 0), linewidth=2)
plot(ma200s, color=color.new(color.red, 0), linewidth=2)
plot( ma20s, color=color.new(color.red, 0), linewidth=2)
Editor is loading...