Update strategies/Strategy_Limit_DATE_TIME.pine

This commit is contained in:
2024-12-13 15:13:25 -08:00
parent 03a3ce0757
commit 173efa4604

View File

@@ -1,96 +1,96 @@
//@version=5
strategy("Test Candle Close Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1, slippage=3)
// Input parameters for additional customization
riskPerTrade = input.float(1, title="Risk % per Trade", minval=0.1, maxval=5, step=0.1)
stopLossPercent = input.float(0.5, title="Stop Loss %", minval=0.1, maxval=2, step=0.1)
takeProfitPercent = input.float(1.5, title="Take Profit %", minval=0.5, maxval=5, step=0.1)
var string userKey = input.string("Xbwln5mblaaEBPDnFpVuTmbUqNax51CmjOEmdvuXSjc", "API Key")
var string userAccount = input.string("Sim101", "Account ID")
var string atmName = input.string("EMA_CROSS_50", "ATM Strategy Name")
var int numContracts = input.int(1, "Number of Contracts")
// Precise Start Date and Time inputs
startDateTimeX = input.time(timestamp("2024-11-21 00:01"), title="Start Date and Time")
var startDateTime = startDateTimeX + (6 * 60 * 60 * 1000) // Adjust to match 6h diff >> UTC to Chicago
startTime = input.string("00:15", title="Start Time (HH:MM)")
endTime = input.string("00:30", title="End Time (HH:MM)")
// Parse the input times
startHour = str.tonumber(str.substring(startTime, 0, 2))
startMinute = str.tonumber(str.substring(startTime, 3, 5))
endHour = str.tonumber(str.substring(endTime, 0, 2))
endMinute = str.tonumber(str.substring(endTime, 3, 5))
maxOrderCount = input.int(150, "MAX number of orders in the strategy")
// Order counting
var order_count = 0
// Time conditions and trade conditions
isWithinTimeWindow = (hour(time_close) > startHour or
(hour(time_close) == startHour and minute(time_close) >= startMinute)) and
(hour(time_close) < endHour or
(hour(time_close) == endHour and minute(time_close) <= endMinute))
isAfterStartDateTime = time_close >= startDateTime
within_trading_period = order_count <= maxOrderCount and isWithinTimeWindow and isAfterStartDateTime
// Determine candle direction
candleGreen = close > open
candleRed = close < open
// Build alerts
alertLong = "key=" + userKey + ";command=PLACE;account=" + userAccount + ";instrument=MNQ 12-24; action=BUY" + ";qty=" + str.tostring(numContracts) + ";order_type=MARKET;tif=DAY;strategy=" + atmName + ";flatten_first=true;"
alertShort = "key=" + userKey + ";command=PLACE;account=" + userAccount + ";instrument=MNQ 12-24; action=SELL" + ";qty=" + str.tostring(numContracts) + ";order_type=MARKET;tif=DAY;strategy=" + atmName + ";flatten_first=true;"
// Entry conditions - using barstate.isconfirmed to ensure we act on closed candles only
if (barstate.isconfirmed and within_trading_period)
// Close any existing positions and enter new ones based on candle color
if (candleGreen)
alert(alertLong)
order_count := order_count + 1
strategy.close_all()
strategy.entry("long", strategy.long, 1)
if (candleRed)
alert(alertShort)
order_count := order_count + 1
strategy.close_all()
strategy.entry("short", strategy.short, 1)
// Calculate stop loss and take profit levels for open positions
if (strategy.position_size != 0)
longStopPrice = strategy.position_avg_price * (1 - stopLossPercent/100)
longTakeProfit = strategy.position_avg_price * (1 + takeProfitPercent/100)
shortStopPrice = strategy.position_avg_price * (1 + stopLossPercent/100)
shortTakeProfit = strategy.position_avg_price * (1 - takeProfitPercent/100)
if (strategy.position_size > 0)
strategy.exit("Long TP/SL", "Long",
stop=longStopPrice,
limit=longTakeProfit)
if (strategy.position_size < 0)
strategy.exit("Short TP/SL", "Short",
stop=shortStopPrice,
limit=shortTakeProfit)
// Close all positions after 5000 candles
if (order_count == maxOrderCount)
strategy.close_all()
// Plotting for visual reference
plotshape(candleGreen and barstate.isconfirmed and within_trading_period, title="Long Entry", location=location.belowbar, style=shape.triangleup, size=size.small, color=color.green)
plotshape(candleRed and barstate.isconfirmed and within_trading_period, title="Short Entry", location=location.abovebar, style=shape.triangledown, size=size.small, color=color.red)
// Plot line showing where trading stops
plot(order_count == maxOrderCount ? high + (high * 0.001) : na, color=color.red, linewidth=2, style=plot.style_circles, title="Trading End Point")
// Plot line showing trading status
bgcolor(isAfterStartDateTime ? (isWithinTimeWindow ? color.new(color.green, 80) : color.new(color.red, 80)) : na)
//@version=5
strategy("Test Candle Close Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1, slippage=3)
// Input parameters for additional customization
riskPerTrade = input.float(1, title="Risk % per Trade", minval=0.1, maxval=5, step=0.1)
stopLossPercent = input.float(0.5, title="Stop Loss %", minval=0.1, maxval=2, step=0.1)
takeProfitPercent = input.float(1.5, title="Take Profit %", minval=0.5, maxval=5, step=0.1)
var string userKey = input.string("Xbwln5mblaaEBPDnFpVuTmbUqNax51CmjOEmdvuXSjc", "API Key")
var string userAccount = input.string("Sim101", "Account ID")
var string atmName = input.string("EMA_CROSS_50", "ATM Strategy Name")
var int numContracts = input.int(1, "Number of Contracts")
// Precise Start Date and Time inputs
startDateTimeX = input.time(timestamp("2024-11-21 00:01"), title="Start Date and Time")
var startDateTime = startDateTimeX + (6 * 60 * 60 * 1000) // Adjust to match 6h diff >> UTC to Chicago
startTime = input.string("00:15", title="Start Time (HH:MM)")
endTime = input.string("00:30", title="End Time (HH:MM)")
// Parse the input times
startHour = str.tonumber(str.substring(startTime, 0, 2))
startMinute = str.tonumber(str.substring(startTime, 3, 5))
endHour = str.tonumber(str.substring(endTime, 0, 2))
endMinute = str.tonumber(str.substring(endTime, 3, 5))
maxOrderCount = input.int(150, "MAX number of orders in the strategy")
// Order counting
var order_count = 0
// Time conditions and trade conditions
isWithinTimeWindow = (hour(time_close) > startHour or
(hour(time_close) == startHour and minute(time_close) >= startMinute)) and
(hour(time_close) < endHour or
(hour(time_close) == endHour and minute(time_close) <= endMinute))
isAfterStartDateTime = time_close >= startDateTime
within_trading_period = order_count <= maxOrderCount and isWithinTimeWindow and isAfterStartDateTime
// Determine candle direction
candleGreen = close > open
candleRed = close < open
// Build alerts
alertLong = "key=" + userKey + ";command=PLACE;account=" + userAccount + ";instrument=MNQ 12-24; action=BUY" + ";qty=" + str.tostring(numContracts) + ";order_type=MARKET;tif=DAY;strategy=" + atmName + ";flatten_first=true;"
alertShort = "key=" + userKey + ";command=PLACE;account=" + userAccount + ";instrument=MNQ 12-24; action=SELL" + ";qty=" + str.tostring(numContracts) + ";order_type=MARKET;tif=DAY;strategy=" + atmName + ";flatten_first=true;"
// Entry conditions - using barstate.isconfirmed to ensure we act on closed candles only
if (barstate.isconfirmed and within_trading_period)
// Close any existing positions and enter new ones based on candle color
if (candleGreen)
alert(alertLong)
order_count := order_count + 1
strategy.close_all()
strategy.entry("long", strategy.long, 1)
if (candleRed)
alert(alertShort)
order_count := order_count + 1
strategy.close_all()
strategy.entry("short", strategy.short, 1)
// Calculate stop loss and take profit levels for open positions
if (strategy.position_size != 0)
longStopPrice = strategy.position_avg_price * (1 - stopLossPercent/100)
longTakeProfit = strategy.position_avg_price * (1 + takeProfitPercent/100)
shortStopPrice = strategy.position_avg_price * (1 + stopLossPercent/100)
shortTakeProfit = strategy.position_avg_price * (1 - takeProfitPercent/100)
if (strategy.position_size > 0)
strategy.exit("Long TP/SL", "Long",
stop=longStopPrice,
limit=longTakeProfit)
if (strategy.position_size < 0)
strategy.exit("Short TP/SL", "Short",
stop=shortStopPrice,
limit=shortTakeProfit)
// Close all positions after 5000 candles
if (order_count == maxOrderCount)
strategy.close_all()
// Plotting for visual reference
plotshape(candleGreen and barstate.isconfirmed and within_trading_period, title="Long Entry", location=location.belowbar, style=shape.triangleup, size=size.small, color=color.green)
plotshape(candleRed and barstate.isconfirmed and within_trading_period, title="Short Entry", location=location.abovebar, style=shape.triangledown, size=size.small, color=color.red)
// Plot line showing where trading stops
plot(order_count == maxOrderCount ? high + (high * 0.001) : na, color=color.red, linewidth=2, style=plot.style_circles, title="Trading End Point")
// Plot line showing trading status
bgcolor(isAfterStartDateTime ? (isWithinTimeWindow ? color.new(color.green, 80) : color.new(color.red, 80)) : na)