set stoploss from tradeprice to the lowest of 5 candles plus spread

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #162911 quote
    kevin12345
    Participant
    Average

    hi,

    i am trying to add a stop loss when going long when the candle crosses the 200ma. the stop loss is meant to be under the lowest of the last 5 candles when i have gone long. somehow it is not placing any stop loss.

    indicator1 = Average[200](close)
    longc1 = (low CROSSES UNDER indicator1)
    longc2 = (close CROSSES UNDER indicator1)
    bullcandle = (open<close)
    period = 5
    
    for a = 0 to period -1
    IF  (longc1[a] or longc2[a]) and not onmarket[a] then
    if bullcandle and close crosses over indicator1 then
    buy 1 contracts at market
    longstoploss = (tradeprice - lowest[5](low) + 0.002)*pipsize
    set stop ploss longstoploss
    endif
    break
    ENDIF
    next
    #162912 quote
    robertogozzi
    Moderator
    Master

    Replace TRADEPRICE with CLOSE in line 11, since it takes a whole candle for it to be known. Using CLOSE may not exactly the same, due to slippage (if any), but it’s thfe only known entry price at that moment. TRADEPRICE is either 0 (if it’s the first trade) or the exit price of the prior trade.

    Next candle, if you want to be more accurate, you can recalculate your SL. But this is usually not necessary.

    #162913 quote
    kevin12345
    Participant
    Average

    i changed the tradeprice to close and whenever i open a position it is closing immediately

    #162914 quote
    kevin12345
    Participant
    Average

    longstoploss is always zero but when i check the individual values of close, and lowest[5](low) and do the math myself, its not equal to zero.

    #162915 quote
    kevin12345
    Participant
    Average

    i solve it. line 11 replace with

    longstoploss = (close - lowest[5](low)+0.0002)*10000

    it was pipsize command. i thought it converts the number in the bracket to pips.

    #162918 quote
    robertogozzi
    Moderator
    Master

    Yes, you are right. You have to divide by PipSize:

    longstoploss = (tradeprice - lowest[5](low) + 0.002)/pipsize
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

set stoploss from tradeprice to the lowest of 5 candles plus spread


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
kevin12345 @kevin12345 Participant
Summary

This topic contains 5 replies,
has 2 voices, and was last updated by robertogozzi
4 years, 11 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/02/2021
Status: Active
Attachments: No files
Logo Logo
Loading...