Close position when new candle

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #147902 quote
    umebon
    Participant
    Average

    Hi again

    I have watched tutorials but I cant figure out how the code is for closing a position after a candle when a new candle begins. Please can someone help me with that.

    #147907 quote
    robertogozzi
    Moderator
    Master

    Simply add these lines at the beginning of your code, just after DEFPARAM’s:

    Sell at Market
    Exitshort at Market

    when you are not OnMarket they are ignored, otherwise they will close any trade opened the previous candle.

    umebon thanked this post
    #147910 quote
    umebon
    Participant
    Average

    Is it possible too close position after X sec or X min?

    #147913 quote
    robertogozzi
    Moderator
    Master

    You can only count bars, read https://www.prorealcode.com/topic/define-a-time-stop/#post-147903.

    If you are using a minute or second TF, yes it’s possible.

    #147916 quote
    umebon
    Participant
    Average

    You are the best!

    But in a one minute TF I cant close a postion after 50 s? In that case i need to work with multiple timeframes?

    #147918 quote
    MAKSIDE
    Participant
    Veteran

    Yes, “Second” is the smallest TF (and tick isn’t possible yet..)

    umebon thanked this post
    #147921 quote
    umebon
    Participant
    Average

    But if I am in a 1 minute TF I cant define it too stop after x secs?

    #147922 quote
    MAKSIDE
    Participant
    Veteran

    But if I am in a 1 minute TF I cant define it too stop after x secs?

    Yes, you can do it

    umebon thanked this post
    #147923 quote
    umebon
    Participant
    Average

    How do I write that code? I am in 1m TF and want it to stop after 50s.

    #147936 quote
    robertogozzi
    Moderator
    Master

    There you go (run it from a 1-second TF):

    TIMEFRAME(1 minute,UpdateOnClose)
    IF MyLongConditions THEN
       BUY 1 Contract at Market
    ENDIF
    TIMEFRAME(1 second,UpdateOnclose)
    MySecond = opentime - (round((opentime / 100) - 0.5) * 100)
    TIMEFRAME(default)
    IF MySecond >= 50 AND LongOnMarket THEN
       SELL at Market
    ENDIF

    line 8 checks for MySecond being 50 or greater, since it may happen not to have that candle on such a small TF.

    Searching this forum for the words MTF and Multi Time Frame will return many links to examples, documentations, blogs and many topics.

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

Close position when new candle


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
umebon @umebon Participant
Summary

This topic contains 9 replies,
has 3 voices, and was last updated by robertogozzi
5 years, 3 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 10/20/2020
Status: Active
Attachments: No files
Logo Logo
Loading...