Close a trade buy/sell based on number of hours

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #112148 quote
    JohnLeeJYJohnLeeJY
    Participant
    New
    Hi there, Does anyone know how to close a position based on number of hours? Thank you.
    #112154 quote
    Put this, example with 10 hours for both long and short.
    
    If longonmarket and (barindex-tradeindex)>=10 then
    
    SELL AT MARKET
    
    ENDIF
    
    If shortonmarket and (barindex-tradeindex)>=10 then
    
    BUY AT MARKET
    
    Endif
    
    
    
    #112162 quote
    robertogozzirobertogozzi
    Moderator
    Legend
    Mauro‘s code is correct if you are using 1-hour TF, if you are using lower TF’s you’ll have to write:
    ONCE MaxHours = 10
    IF Not OnMarket THEN
       Count = 0
    ELSE
       IF OpenHour <> OpenHour[1] THEN
          Count = Count + 1
          IF Count >= MaxHours THEN    //exit any trade, be it Long or Short
             SELL      AT MARKET
             EXITSHORT AT MARKET
          ENDIF
       ENDIF
    ENDIF
    #112176 quote
    Yes Roberto you are right I answered for number of bars.
    #112177 quote
    JohnLeeJYJohnLeeJY
    Participant
    New

    Thank you both Mauro and Roberto. Very much appreciate your assistance.

    #112201 quote
    VonasiVonasi
    Moderator
    Master
    There is a small issue with your code Roberto as it does not exit exactly ten hours after a trade is opened when used on a faster time frame than hourly. You would need to record the minute and/or second when a trade is opened and then count the hours and then check for the same minute and/or second in the tenth hour to work out when to precisely close the trade.
    #112207 quote
    robertogozzirobertogozzi
    Moderator
    Legend
    I think he doesn’t need an exact number of hours, but if he does I can do it.
Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Close a trade buy/sell based on number of hours


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
JohnLeeJY @johnleejy Participant
Summary

This topic contains 6 replies,
has 4 voices, and was last updated by robertogozzirobertogozzi
6 years, 11 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 11/05/2019
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...