Set Stop Loss not working

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #237602 quote
    Steffen0101
    Participant
    New

    Hi all,

    I want to define my custom trailing stop loss. Below is my code. What works is the indication of the SL, SLPrice and max Price on the chart. However the SET STOP LOSS command does not trigger. The only exits I have are after market close as defined by Defparam FlatAfter = 215900. Any ideas why this is not working?

    Many thanks in advance

    // Define the moving averages
    DefParam CumulateOrders=false
    Defparam FlatAfter = 215900
    EMA20 = ExponentialAverage[20](close)
    EMA50 = ExponentialAverage[50](close)
    EMA200 = ExponentialAverage[200](close)
    EMAAusstieg = ExponentialAverage[28](close)
    positionSize = 25000
    maxLossPerTrade = 100 // Risk per Trade
    entrySignal = CALL "Signal_Trendhandel4" // Call the custom indicator
    noTradeHours = (Hour >= 19 AND Hour < 19)
    isUptrend = (EMA20 < EMA50) AND (close > EMA20) // Define the conditions for the uptrend
    trailingDistance = 1  // Define your trailing stop distance (in points/USD/EUR)
    
    // Entry condition: Only enter after 3 consecutive correction candles followed by a recovery and outside the noTradeHours period
    IF isUptrend AND entrySignal AND NOT noTradeHours THEN
    maxPrice = close // Variables to track the highest price and trailing stop level
    Buy positionSize CASH ROUNDEDDOWN AT MARKET
    // Calculate and store the approximate number of contracts bought
    numberOfContracts = positionSize / close  // Estimate the number of contracts based on entry price
    ENDIF
    
    // If the position is open, initialize stop loss and trailing stop logic
    IF longonmarket THEN
    // Update the highest price after entry
    maxPrice = max(maxPrice, close)  // Track the highest price reached after entry
    stopLossPrice = maxPrice - (maxLossPerTrade / numberOfContracts)  // Calculate stop loss price based on the max loss
    
    // Calculate the trailing stop based on the highest price reached
    currentTrailingStop = maxPrice - trailingDistance
    
    // Update the stop loss to the trailing stop value, ensuring it does not exceed the original stop loss
    SET STOP LOSS max(currentTrailingStop, stopLossPrice)
    ENDIF
    
    GRAPHONPRICE currentTrailingStop  AS "SL" coloured(255,0,255, 255)
    GRAPHONPRICE stopLossPrice  AS "SLPrice" coloured(0,255,0, 255)
    GRAPHONPRICE maxPrice AS "max Price" COLOURED(0,0,255,200)
    #237638 quote
    Iván González
    Moderator
    Master

    Maybe you mean: Set stop price max(xxx,yyy)

    Steffen0101 thanked this post
    #237655 quote
    Steffen0101
    Participant
    New

    Thanks, thats what I was looking for!

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

Set Stop Loss not working


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 2 replies,
has 2 voices, and was last updated by Steffen0101
1 year, 5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 09/16/2024
Status: Active
Attachments: No files
Logo Logo
Loading...