Untitled

 avatar
unknown
plain_text
9 months ago
10 kB
12
Indexable
//@version=5
strategy("5-8-13 Dynamic Lot System", overlay=true, pyramiding=0)

// === INPUT PARAMETERS ===
ema5_period = input.int(5, "EMA 5 Period")
ema8_period = input.int(8, "EMA 8 Period") 
ema13_period = input.int(13, "EMA 13 Period")

// Lot Parameters
initial_lot = input.float(10, "Initial Lot")
lot_reduction_amount = input.float(3, "Lot Reduction Amount")

// Strategy Options
additional_purchase_allowed = input.bool(false, "Allow Additional Purchases?", tooltip="On: Can buy back after reduction\nOff: Only initial purchase")
cancel_intermediate_steps = input.bool(false, "Cancel Intermediate Steps", tooltip="On: Only use CONFIRMED and EXIT status\nOff: Use all status levels")

// === CALCULATIONS ===
// Calculate EMAs
ema5 = ta.ema(close, ema5_period)
ema8 = ta.ema(close, ema8_period)
ema13 = ta.ema(close, ema13_period)

// EMA Sorting Check
emas_sorted = ema5 > ema8 and ema8 > ema13
emas_reverse_sorted = ema5 < ema8 and ema8 < ema13

// === STATUS DETECTION ===
status_confirmed = close > ema5 and emas_sorted
status_cautious = close < ema5 and close > ema8 and close > ema13 and emas_sorted
status_risk = close < ema5 and close < ema8 and close > ema13 and emas_sorted
status_exit = close < ema5 and close < ema8 and close < ema13 and emas_reverse_sorted

// === DYNAMIC LOT CALCULATION ===
var float target_lot = 0.0
var float current_lot = 0.0
var bool initial_purchase_done = false

// Calculate target lot for each status
if cancel_intermediate_steps
    // Only use CONFIRMED and EXIT status
    if status_confirmed
        if not initial_purchase_done or additional_purchase_allowed
            target_lot := initial_lot
        else
            target_lot := current_lot
    else if status_exit
        target_lot := 0.0
    else
        // Maintain current position for intermediate status
        target_lot := current_lot
else
    // Use all status levels
    if status_confirmed
        if not initial_purchase_done or additional_purchase_allowed
            target_lot := initial_lot
        else
            target_lot := current_lot
    else if status_cautious
        target_lot := math.max(1, initial_lot - lot_reduction_amount)
    else if status_risk
        target_lot := math.max(1, initial_lot - (2 * lot_reduction_amount))
    else if status_exit
        target_lot := 0.0

// Calculate lot difference
lot_difference = target_lot - current_lot

// POSITION MANAGEMENT - using strategy.order
if strategy.position_size == 0
    // Open new position in CONFIRMED status
    if status_confirmed
        strategy.order("Long_Entry", strategy.long, qty=target_lot)
        current_lot := target_lot
        initial_purchase_done := true
else
    // Position exists
    if status_exit
        // Close all position in EXIT status
        strategy.order("Long_Exit", strategy.short, qty=math.abs(current_lot))
        current_lot := 0.0
        initial_purchase_done := false
    else if lot_difference > 0 and additional_purchase_allowed
        // Add lots - ONLY if additional_purchase_allowed is ON
        strategy.order("Long_Add", strategy.long, qty=lot_difference)
        current_lot := target_lot
    else if lot_difference < 0 and (not cancel_intermediate_steps or status_exit)
        // Reduce lots - always allowed unless cancel_intermediate_steps is ON
        strategy.order("Long_Reduce", strategy.short, qty=math.abs(lot_difference))
        current_lot := target_lot

// Determine active status
var string active_status = "NO POSITION"

if status_confirmed
    active_status := "CONFIRMED"
else if status_cautious and not cancel_intermediate_steps
    active_status := "CAUTIOUS"
else if status_risk and not cancel_intermediate_steps
    active_status := "RISK"
else if status_exit
    active_status := "EXIT"
else
    active_status := "NO POSITION"

// === VISUALIZATION ===
// Plot EMAs
plot(ema5, color=color.new(color.blue, 0), linewidth=2, title="EMA 5")
plot(ema8, color=color.new(color.orange, 0), linewidth=2, title="EMA 8") 
plot(ema13, color=color.new(color.red, 0), linewidth=2, title="EMA 13")

// Current price line
plot(close, color=color.white, linewidth=1, title="Price")

// === DYNAMIC TABLE DISPLAY ===
var table status_table = table.new(position.top_right, 4, 7, bgcolor=color.black, border_width=1)

if barstate.islast
    // Header row
    table.cell(status_table, 0, 0, "SYSTEM", text_color=color.white, bgcolor=color.gray)
    table.cell(status_table, 1, 0, "STATUS", text_color=color.white, bgcolor=color.gray)
    table.cell(status_table, 2, 0, "LOT", text_color=color.white, bgcolor=color.gray)
    table.cell(status_table, 3, 0, "ACTION", text_color=color.white, bgcolor=color.gray)
    
    // Current Position row
    var status_bg_color = color.navy
    var status_text_color = color.white
    
    if active_status == "CONFIRMED"
        status_bg_color := color.green
    else if active_status == "CAUTIOUS"
        status_bg_color := color.orange
    else if active_status == "RISK"
        status_bg_color := color.red
    else if active_status == "EXIT"
        status_bg_color := color.gray
    else
        status_bg_color := color.navy
    
    position_status = strategy.position_size > 0 ? "OPEN" : "CLOSED"
    purchase_status = initial_purchase_done ? "DONE" : "NOT DONE"
    
    table.cell(status_table, 0, 2, "CURRENT", text_color=status_text_color, bgcolor=status_bg_color)
    table.cell(status_table, 1, 2, active_status, text_color=status_text_color, bgcolor=status_bg_color)
    table.cell(status_table, 2, 2, str.tostring(current_lot, "#.##"), text_color=status_text_color, bgcolor=status_bg_color)
    table.cell(status_table, 3, 2, position_status, text_color=status_text_color, bgcolor=status_bg_color)
    
    // CONFIRMED row
    confirmed_bg = active_status == "CONFIRMED" ? color.green : color.new(color.green, 80)
    confirmed_text = active_status == "CONFIRMED" ? "ACTIVE" : "PASSIVE"
    table.cell(status_table, 0, 3, "CONFIRMED", text_color=color.white, bgcolor=confirmed_bg)
    table.cell(status_table, 1, 3, confirmed_text, text_color=color.white, bgcolor=confirmed_bg)
    table.cell(status_table, 2, 3, str.tostring(initial_lot, "#.##"), text_color=color.white, bgcolor=confirmed_bg)
    table.cell(status_table, 3, 3, "BUY", text_color=color.white, bgcolor=confirmed_bg)
    
    // CAUTIOUS row
    cautious_bg = active_status == "CAUTIOUS" ? color.orange : color.new(color.orange, 80)
    cautious_text = active_status == "CAUTIOUS" ? "ACTIVE" : "PASSIVE"
    table.cell(status_table, 0, 4, "CAUTIOUS", text_color=color.white, bgcolor=cautious_bg)
    table.cell(status_table, 1, 4, cautious_text, text_color=color.white, bgcolor=cautious_bg)
    table.cell(status_table, 2, 4, str.tostring(math.max(1, initial_lot - lot_reduction_amount), "#.##"), text_color=color.white, bgcolor=cautious_bg)
    table.cell(status_table, 3, 4, "REDUCE", text_color=color.white, bgcolor=cautious_bg)
    
    // RISK row
    risk_bg = active_status == "RISK" ? color.red : color.new(color.red, 80)
    risk_text = active_status == "RISK" ? "ACTIVE" : "PASSIVE"
    table.cell(status_table, 0, 5, "RISK", text_color=color.white, bgcolor=risk_bg)
    table.cell(status_table, 1, 5, risk_text, text_color=color.white, bgcolor=risk_bg)
    table.cell(status_table, 2, 5, str.tostring(math.max(1, initial_lot - (2 * lot_reduction_amount)), "#.##"), text_color=color.white, bgcolor=risk_bg)
    table.cell(status_table, 3, 5, "REDUCE", text_color=color.white, bgcolor=risk_bg)
    
    // EXIT row
    exit_bg = active_status == "EXIT" ? color.gray : color.new(color.gray, 80)
    exit_text = active_status == "EXIT" ? "ACTIVE" : "PASSIVE"
    table.cell(status_table, 0, 6, "EXIT", text_color=color.white, bgcolor=exit_bg)
    table.cell(status_table, 1, 6, exit_text, text_color=color.white, bgcolor=exit_bg)
    table.cell(status_table, 2, 6, "0", text_color=color.white, bgcolor=exit_bg)
    table.cell(status_table, 3, 6, "CLOSE ALL", text_color=color.white, bgcolor=exit_bg)

// === PRICE & EMA VALUES TABLE ===
var table values_table = table.new(position.bottom_right, 3, 5, bgcolor=color.black, border_width=1)

if barstate.islast
    // Header row
    table.cell(values_table, 0, 0, "INDICATOR", text_color=color.white, bgcolor=color.gray)
    table.cell(values_table, 1, 0, "VALUE", text_color=color.white, bgcolor=color.gray)
    
    // Price row
    price_color = close > ema5 and close > ema8 and close > ema13 ? color.green : color.red
    table.cell(values_table, 0, 1, "PRICE", text_color=color.white, bgcolor=price_color)
    table.cell(values_table, 1, 1, str.tostring(close, "#.##"), text_color=color.white, bgcolor=price_color)
    
    // EMA 5 row
    ema5_color = close > ema5 ? color.green : color.red
    table.cell(values_table, 0, 2, "EMA 5", text_color=color.white, bgcolor=ema5_color)
    table.cell(values_table, 1, 2, str.tostring(ema5, "#.##"), text_color=color.white, bgcolor=ema5_color)
    
    // EMA 8 row
    ema8_color = close > ema8 ? color.green : color.red
    table.cell(values_table, 0, 3, "EMA 8", text_color=color.white, bgcolor=ema8_color)
    table.cell(values_table, 1, 3, str.tostring(ema8, "#.##"), text_color=color.white, bgcolor=ema8_color)
    
    // EMA 13 row
    ema13_color = close > ema13 ? color.green : color.red
    table.cell(values_table, 0, 4, "EMA 13", text_color=color.white, bgcolor=ema13_color)
    table.cell(values_table, 1, 4, str.tostring(ema13, "#.##"), text_color=color.white, bgcolor=ema13_color)
// === STATUS CHANGE ALERTS ===
var string last_status = ""

if active_status != last_status
    last_status := active_status
    if active_status == "CONFIRMED" and strategy.position_size == 0
        alert("NEW CONFIRMED BUY - " + str.tostring(target_lot) + " lot", alert.freq_once_per_bar)
    else if active_status == "CAUTIOUS"
        alert("CAUTIOUS - " + str.tostring(target_lot) + " lot", alert.freq_once_per_bar)
    else if active_status == "RISK"
        alert("RISK - " + str.tostring(target_lot) + " lot", alert.freq_once_per_bar)
    else if active_status == "EXIT" and strategy.position_size > 0
        alert("EXIT ALL - Selling All Lots (" + str.tostring(strategy.position_size) + " lot)", alert.freq_once_per_bar)
Editor is loading...
Leave a Comment