Time-based Stop Loss

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #133206 quote
    Sascha
    Participant
    Average

    Hi,

    How can I code a time-based stop loss, meaning if, for example,  a short trade is going on for more than a specified length of time (ie. 20 hours), then it should move the stop loss to breakeven minus 2 pips?

    Thanks.

    Sascha

    #133214 quote
    robertogozzi
    Moderator
    Master

    You can use the number of elapsed bars:

    IF Not OnMarket THEN
        MyProfit = 0
        NewSL    = 0
    ENDIF
    IF LongOnMartket THEN
       MyProfit = close - TradePrice
    ELSIF ShortOnMartket THEN
       MyProfit = TradePrice - close
    ENDIF
    IF Barindex >= (TradeIndex + 10) AND MyProft > 0 AND NewSL = 0 THEN //Count 10 bars before setting Break Even
       NewSL = TradePrice
    ENDIF
    IF NewSL THEN
       IF LongOnMarket THEN
          SELL AT NewSL STOP
       ELSIF ShortOnMarket THEN
          EXITSHORT AT NewSL STOP
       ENDIF
    ENDIF

    or use the MTF support to exactly count hours:

    TIMEFRAME(????,UpDateOnClose)
     .
     .
    TIMEFRAME(1 hour,UpdateOnClose)
    IF Not OnMarket THEN
       MyProfit = 0
       NewSL    = 0
       Count    = 0
    ELSE
       Count = Count + 1
    ENDIF
    IF LongOnMartket THEN
       MyProfit = close - TradePrice
    ELSIF ShortOnMartket THEN
       MyProfit = TradePrice - close
    ENDIF
    IF Count >= 20 AND MyProfit > 0 AND NewSL = 0 THEN //Count 20 hours before setting Break Even
       NewSL = TradePrice
    ENDIF
    IF NewSL THEN
       IF LongOnMarket THEN
          SELL AT NewSL STOP
       ELSIF ShortOnMarket THEN
          EXITSHORT AT NewSL STOP
       ENDIF
    ENDIF
     .
     .
    TIMEFRAME(default)
     .
     .
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Time-based Stop Loss


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Sascha @goedelsa Participant
Summary

This topic contains 1 reply,
has 2 voices, and was last updated by robertogozzi
5 years, 9 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 05/25/2020
Status: Active
Attachments: No files
Logo Logo
Loading...