diff --git a/strategies/Hammer_Candle_Strategy.pine b/strategies/Hammer_Candle_Strategy.pine new file mode 100755 index 0000000..5d1133a --- /dev/null +++ b/strategies/Hammer_Candle_Strategy.pine @@ -0,0 +1,83 @@ +//@version=5 +strategy("Hammer Candle Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100) + +// Inputs for customization +hammertBodyRatio = input.float(0.3, title="Max Body Size Ratio", minval=0.1, maxval=0.5, step=0.1) +hammerWickRatio = input.float(2.0, title="Min Wick to Body Ratio", minval=1.0, maxval=5.0, step=0.1) + +// Function to determine if a candle is bullish or bearish +is_candle_bullish(index) => + close[index] > open[index] + +is_candle_bearish(index) => + close[index] < open[index] + +// Hammer Candle Detection Function +//isHammer() => +string dbg = "" +bodySize = math.abs(close - open) +totalRange = high - low + +// Calculate upper and lower wicks +upperWick = high - math.max(close, open) +lowerWick = math.min(close, open) - low + +// Check if body is small (less than 30% of total candle range) +isSmallBody = bodySize <= (totalRange * hammertBodyRatio) +//plotchar(isSmallBody, title="lw", char='-', location=location.abovebar, color=#40fbf5, size=size.small) + +// Lower/Upper wick should be at least 2x the body size and significantly long +bool isLongUpperWick = upperWick >= (totalRange * 0.5) and upperWick >= (bodySize * hammerWickRatio) +bool isLongLowerWick = lowerWick >= (totalRange * 0.5) and lowerWick >= (bodySize * hammerWickRatio) +//plotchar(isLongUpperWick, title="LW", char='L', location=location.abovebar, color=color.fuchsia, size=size.small) +//plotchar(isLongLowerWick, title="lw", char='l', location=location.belowbar, color=color.fuchsia, size=size.small) + +// Minimal lower/upper wick +bool isShortUpperWick = upperWick <= (bodySize * 0.5) or upperWick <= (totalRange * 0.2) +bool isShortLowerWick = lowerWick <= (bodySize * 0.5) or lowerWick <= (totalRange * 0.2) +//plotchar(isShortUpperWick, title="Bullish Candle", char='S', location=location.abovebar, color=color.fuchsia, size=size.small) +//plotchar(isShortLowerWick, title="Bullish Candle", char='s', location=location.belowbar, color=color.fuchsia, size=size.small) + +// Bullish Hammer (Green/White) +isBullishHammer = isSmallBody and isShortUpperWick and isLongLowerWick //and is_candle_bearish(1) and is_candle_bearish(2) // and close > open + +// Bearish Hammer (Red/Black) +isBearishHammer = isSmallBody and isLongUpperWick and isShortLowerWick // and is_candle_bullish(1) and is_candle_bullish(2) // and close < open +if (isBearishHammer) + dbg := "TEST: " + str.tostring(isShortUpperWick) + " | "+ str.tostring(upperWick) + " | "+ str.tostring(bodySize) + " | "+ str.tostring(totalRange) + " | " + +// [isBullishHammer, isBearishHammer, dbg] + +//[bullish, bearish, deb] = isHammer() +bullish = isBullishHammer +bearish = isBearishHammer +// Plot a '*' when close is above open (bullish candle) +plotchar(bullish, title="Bullish Candle", char='*', location=location.belowbar, color=color.green, size=size.small) + +// Plot an '*' when close is below open (bearish candle) +plotchar(bearish, title="Bearish Candle", char='*', location=location.abovebar, color=color.red, size=size.small) + +bullish := isBullishHammer and is_candle_bullish(1) and is_candle_bullish(2) +bearish := isBearishHammer and is_candle_bullish(1) and is_candle_bullish(2) + +// Plot hammer candle markers +plotshape(bullish, + title="Bullish Hammer", + location=location.belowbar, + style=shape.triangleup, + size=size.small, + color=color.green) + +plotshape(bearish, + title="Bearish Hammer", + location=location.abovebar, + style=shape.triangledown, + size=size.small, + color=color.red) + +// Trading logic +if (bullish) + strategy.entry("Long", strategy.long) + +if (bearish) + strategy.entry("Short", strategy.short) \ No newline at end of file diff --git a/strategies/Strategy_EveryBar_LimitOrders_and_Dates.pine b/strategies/Strategy_EveryBar_LimitOrders_and_Dates.pine new file mode 100755 index 0000000..d8178ac --- /dev/null +++ b/strategies/Strategy_EveryBar_LimitOrders_and_Dates.pine @@ -0,0 +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)