Stop loss at x% of low of entry bar

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #78045 quote
    petitdudu
    Participant
    Junior

    Hello !

    I want to sell a long position if at any time after the buy, the stock reaches a price 2% below the low of the candle of entry. I thought about using (0,98*Tradeindex(low)) but that doesn’t work.

    Can anyone tell me how to code this ?

    Thank you very much !

    #78047 quote
    Vonasi
    Moderator
    Master
    if not onmarket and (your conditions) then
    buy 1 contract at market
    sl = low * 0.98
    endif
    
    sell at sl stop
    #78048 quote
    robertogozzi
    Moderator
    Master

    Tradeindex is the bar when the trade was opened. Tradeprice is the keyword you should use, instead.

    But first of all you have to save the price of the LOW when the trade is opened, like:

    IF My_Conditions AND Not Onmarket THEN
       BUY 1 ONTRACT AT MARKET
       MyLow = LOW * 0.98
    ENDIF

    Then you can test the exit conditions:

    A) comparing the current price each bar

    IF LongOnMarket AND Close <= MyLow THEN
       SELL AT MARKET
    ENDIF

    or

    B) using a pending order each bar

    IF LongOnMarket THEN
       SELL AT MyLow STOP
    ENDIF
    #78049 quote
    robertogozzi
    Moderator
    Master

    You don’t need to use TRADEPRICE either.

    #78057 quote
    Vonasi
    Moderator
    Master

    I think we have both got it slightly wrong Robert where the pending orders are concerned as mine only lasts one bar and yours it not placed on the first bar!

    These two options might work better:

    if not onmarket and (your conditions) then
    buy 1 contract at market
    sl = low * 0.98
    sell at sl stop
    endif
     
    if longonmarket then
    sell at sl stop
    endif

    or

    if not onmarket and (your conditions) then
    buy 1 contract at market
    sl = low * 0.98
    set stop ploss close - sl
    endif

    If it is a market with lots of gaps then the last option might not work so well.

    Nicolas56 thanked this post
    #78084 quote
    petitdudu
    Participant
    Junior

    Ok it works. Thank you !!

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

Stop loss at x% of low of entry bar


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
petitdudu @petitdudu Participant
Summary

This topic contains 5 replies,
has 3 voices, and was last updated by petitdudu
7 years, 6 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/12/2018
Status: Active
Attachments: No files
Logo Logo
Loading...