← กลับหน้ารายการ

7/19 EMA Crypto strategy

Strategy ผู้เขียน: iamqamarali Profit Factor: 1.347

ลิงก์ TradingView

เปิดใน TradingView

Equity Chart

Equity chart

เปิดรูปเต็มขนาด

คำอธิบาย

asfuil i adfs uioadfyufdas yuadfuio ayu dfouiafo asd ufo u afioasdfo

รูป Preview

Preview

Pine Script Source

//@version=5
strategy("7/19 EMA Crypto", overlay=true, initial_capital=1000, currency=currency.USD, default_qty_type=strategy.fixed, default_qty_value=0, calc_on_every_tick=false, process_orders_on_close=true, commission_type=strategy.commission.percent, commission_value=0.04, max_lines_count=500, max_labels_count=500)

// ==================== INPUTS ====================
fastLen = input.int(7, "Fast EMA")
slowLen = input.int(19, "Slow EMA")
riskType = input.string("% of Account", "Risk Type", options=["Fixed $", "% of Account"])
riskAmount = input.float(50.0, "Risk Per Trade $")
riskPercent = input.float(5.0, "Risk Per Trade %", minval=0.1, maxval=100)
useStopLoss = input.bool(true, "Use Stop Loss")
slCandles = input.int(4, "SL Candles Lookback", options=[3, 4])
useTP1 = input.bool(false, "Use TP1")
tp1RR = input.float(1.0, "TP1 R:R", minval=0.1, step=0.1)
tp1Size = input.float(50.0, "TP1 Close %", minval=1, maxval=100)
skipSaturday = input.bool(true, "Skip Saturday")

// ==================== EMAs ====================
ema7 = ta.ema(close, fastLen)
ema19 = ta.ema(close, slowLen)

// ==================== SIGNALS ====================
bullishCross = ta.crossover(ema7, ema19)
bearishCross = ta.crossunder(ema7, ema19)

// ==================== DAY FILTER ====================
canTrade = skipSaturday ? dayofweek != dayofweek.saturday : true

// ==================== POSITION STATE ====================
inLong = strategy.position_size > 0
inShort = strategy.position_size < 0
flat = strategy.position_size == 0

// ==================== STOP LOSS CALCULATIONS ====================
lowestLow3 = math.min(low, math.min(low[1], low[2]))
lowestLow4 = math.min(low, math.min(low[1], math.min(low[2], low[3])))
highestHigh3 = math.max(high, math.max(high[1], high[2]))
highestHigh4 = math.max(high, math.max(high[1], math.max(high[2], high[3])))

// ==================== RISK CALCULATION ====================
riskDollar = riskType == "Fixed $" ? riskAmount : strategy.equity * (riskPercent / 100)

// ==================== ENTRY CONDITIONS ====================
longEntry = bullishCross and canTrade
shortEntry = bearishCross and canTrade

// ==================== VISUAL SL/ENTRY TRACKING ====================
var float currentSL = na
var float currentEntry = na
var float currentTP1 = na
var line slLine = na
var line entryLine = na
var line tp1Line = na
var label slLabel = na
var label entryLabel = na

// ==================== EXECUTE LONG ====================
if longEntry
    sl = slCandles == 3 ? lowestLow3 : lowestLow4
    risk = close - sl
    
    if risk > 0
        posSize = riskDollar / risk
        tp1 = close + (risk * tp1RR)
        
        // Store values for drawing
        currentSL := sl
        currentEntry := close
        currentTP1 := tp1
        
        strategy.entry("Long", strategy.long, qty=posSize)
        
        if useTP1 and useStopLoss
            strategy.exit("Long TP1", "Long", qty_percent=tp1Size, limit=tp1, stop=sl)
            strategy.exit("Long SL", "Long", stop=sl)
        else if useTP1
            strategy.exit("Long TP1", "Long", qty_percent=tp1Size, limit=tp1)
        else if useStopLoss
            strategy.exit("Long SL", "Long", stop=sl)
        
        // Draw entry line
        entryLine := line.new(bar_index, close, bar_index, close, color=color.green, width=2, style=line.style_solid)
        label.new(bar_index, close, "▶ LONG: " + str.tostring(close, "#.##"), style=label.style_label_right, color=color.green, textcolor=color.white, size=size.small)
        
        // Draw SL line
        if useStopLoss
            slLine := line.new(bar_index, sl, bar_index, sl, color=color.red, width=2, style=line.style_dashed)
            slLabel := label.new(bar_index, sl, "SL: " + str.tostring(sl, "#.##"), style=label.style_label_right, color=color.red, textcolor=color.white, size=size.small)
        
        // Draw TP1 line
        if useTP1
            tp1Line := line.new(bar_index, tp1, bar_index, tp1, color=color.lime, width=1, style=line.style_dotted)
            label.new(bar_index, tp1, "TP1: " + str.tostring(tp1, "#.##") + " (" + str.tostring(tp1RR) + "R)", style=label.style_label_right, color=color.lime, textcolor=color.black, size=size.tiny)

// ==================== EXECUTE SHORT ====================
if shortEntry
    sl = slCandles == 3 ? highestHigh3 : highestHigh4
    risk = sl - close
    
    if risk > 0
        posSize = riskDollar / risk
        tp1 = close - (risk * tp1RR)
        
        // Store values for drawing
        currentSL := sl
        currentEntry := close
        currentTP1 := tp1
        
        strategy.entry("Short", strategy.short, qty=posSize)
        
        if useTP1 and useStopLoss
            strategy.exit("Short TP1", "Short", qty_percent=tp1Size, limit=tp1, stop=sl)
            strategy.exit("Short SL", "Short", stop=sl)
        else if useTP1
            strategy.exit("Short TP1", "Short", qty_percent=tp1Size, limit=tp1)
        else if useStopLoss
            strategy.exit("Short SL", "Short", stop=sl)
        
        // Draw entry line
        entryLine := line.new(bar_index, close, bar_index, close, color=color.orange, width=2, style=line.style_solid)
        label.new(bar_index, close, "▼ SHORT: " + str.tostring(close, "#.##"), style=label.style_label_right, color=color.orange, textcolor=color.white, size=size.small)
        
        // Draw SL line
        if useStopLoss
            slLine := line.new(bar_index, sl, bar_index, sl, color=color.red, width=2, style=line.style_dashed)
            slLabel := label.new(bar_index, sl, "SL: " + str.tostring(sl, "#.##"), style=label.style_label_right, color=color.red, textcolor=color.white, size=size.small)
        
        // Draw TP1 line
        if useTP1
            tp1Line := line.new(bar_index, tp1, bar_index, tp1, color=color.lime, width=1, style=line.style_dotted)
            label.new(bar_index, tp1, "TP1: " + str.tostring(tp1, "#.##") + " (" + str.tostring(tp1RR) + "R)", style=label.style_label_right, color=color.lime, textcolor=color.black, size=size.tiny)

// ==================== EXTEND LINES WHILE IN POSITION ====================
if strategy.position_size != 0
    if not na(slLine)
        line.set_x2(slLine, bar_index)
    if not na(entryLine)
        line.set_x2(entryLine, bar_index)
    if not na(tp1Line)
        line.set_x2(tp1Line, bar_index)

// ==================== CLEAR LINES WHEN POSITION CLOSES ====================
if strategy.position_size == 0 and strategy.position_size[1] != 0
    // Position just closed - add exit marker
    if not na(currentSL)
        if (strategy.position_size[1] > 0 and low <= currentSL) or (strategy.position_size[1] < 0 and high >= currentSL)
            label.new(bar_index, currentSL, "⛔ STOPPED", style=label.style_label_left, color=color.red, textcolor=color.white, size=size.small)
        else
            label.new(bar_index, close, "✕ EXIT", style=label.style_label_left, color=color.gray, textcolor=color.white, size=size.small)
    
    slLine := na
    entryLine := na
    tp1Line := na
    currentSL := na
    currentEntry := na
    currentTP1 := na

// ==================== PLOTS ====================
plot(ema7, "7 EMA", color.lime, 2)
plot(ema19, "19 EMA", color.red, 2)

plotshape(longEntry, "Long", shape.triangleup, location.belowbar, color.lime, size=size.normal, text="LONG")
plotshape(shortEntry, "Short", shape.triangledown, location.abovebar, color.red, size=size.normal, text="SHORT")
plotshape(bullishCross and not canTrade, "Skip Long", shape.xcross, location.belowbar, color.gray, size=size.small, text="SKIP")
plotshape(bearishCross and not canTrade, "Skip Short", shape.xcross, location.abovebar, color.gray, size=size.small, text="SKIP")

bgcolor(ema7 > ema19 ? color.new(color.green, 93) : color.new(color.red, 93))

// ==================== INFO TABLE ====================
var table t = table.new(position.top_right, 2, 8, bgcolor=color.new(color.black, 80))

if barstate.islast
    table.cell(t, 0, 0, "7/19 EMA CRYPTO", text_color=color.white, bgcolor=color.blue)
    table.cell(t, 1, 0, "", bgcolor=color.blue)
    table.cell(t, 0, 1, "Trend:", text_color=color.white, text_size=size.small)
    table.cell(t, 1, 1, ema7 > ema19 ? "BULLISH" : "BEARISH", text_color=ema7 > ema19 ? color.lime : color.red, text_size=size.small)
    table.cell(t, 0, 2, "Position:", text_color=color.white, text_size=size.small)
    pos = strategy.position_size > 0 ? "LONG" : strategy.position_size < 0 ? "SHORT" : "FLAT"
    posCol = strategy.position_size > 0 ? color.lime : strategy.position_size < 0 ? color.red : color.gray
    table.cell(t, 1, 2, pos, text_color=posCol, text_size=size.small)
    table.cell(t, 0, 3, "Risk/Trade:", text_color=color.white, text_size=size.small)
    riskText = riskType == "Fixed $" ? "$" + str.tostring(riskAmount) : str.tostring(riskPercent) + "% ($" + str.tostring(riskDollar, "#.##") + ")"
    table.cell(t, 1, 3, riskText, text_color=color.yellow, text_size=size.small)
    table.cell(t, 0, 4, "Equity:", text_color=color.white, text_size=size.small)
    table.cell(t, 1, 4, "$" + str.tostring(strategy.equity, "#.##"), text_color=color.aqua, text_size=size.small)
    table.cell(t, 0, 5, "TP1:", text_color=color.white, text_size=size.small)
    tp1Text = useTP1 ? str.tostring(tp1RR) + "R (" + str.tostring(tp1Size) + "% close)" : "OFF"
    table.cell(t, 1, 5, tp1Text, text_color=useTP1 ? color.green : color.gray, text_size=size.small)
    table.cell(t, 0, 6, "TP2:", text_color=color.white, text_size=size.small)
    table.cell(t, 1, 6, "Crossover", text_color=color.green, text_size=size.small)
    table.cell(t, 0, 7, "SL:", text_color=color.white, text_size=size.small)
    slText = not useStopLoss ? "OFF" : str.tostring(slCandles) + " Candles"
    table.cell(t, 1, 7, slText, text_color=useStopLoss ? color.orange : color.gray, text_size=size.small)

// ==================== ALERTS ====================
alertcondition(longEntry, "Long Entry", "7/19 EMA: Go LONG")
alertcondition(shortEntry, "Short Entry", "7/19 EMA: Go SHORT")