code to close a position when in profit

Forums ProRealTime English forum General trading discussions code to close a position when in profit

Viewing 8 posts - 1 through 8 (of 8 total)
  • #213606

    Hello to the community!

    Could any of the coding wizards write some simple code for me? Here is my request:

    If I have an open position I would like to close this position at the close of the first candle in profit, whether long or short.

    That is all

     

    Best wishes

    Jamed

    #213607

    How about …

     

    2 users thanked author for this post.
    #213615

    To make the code work with both Long and Short trades or in case of accumulation of orders or when partial closing of positions is used, a more accurate line 1 should read:

     

    2 users thanked author for this post.
    #213620

    Thank you kindly, GraHal and Roberto.  This is a great community! I sincerely appreciate how all you guys help with coding for ProRealTime, long may it continue.

    1 user thanked author for this post.
    #213660
    smp

    If you want to try and max out on a profit into a close, for example, FTSE100 and DAX40 close at 16:30hrs you could also use this;

    timeCloseTradeInProfit = 163000
    if time = timeCloseTradeInProfit Then
    InProfit = (close – tradeprice) * pipsize
    If InProfit > 0 THEN
    Sell at market
    ExitShort at Market
    Endif

    #213662

    Be warned that

    should read

    the first one will convert a price difference of, say 0.0040, into 0.00000040 pips (for FX pairs), the latter will convert it into 40 pips.

    In addition it calculates the profit for only LONG trades, while you coded exiting also the SHORT trades and one final ENDIF is missing.

    It should be coded this way:

     

     

     

    1 user thanked author for this post.
    #213671
    smp

    Hi,

    The code was indeed just for long trades.  It’s been working perfectly daily for me in active Algo’s on DAX and FTSE.

     

    If you look at your code and mine, you are saying the same as I!

    #213688

    It works simply because you only trade DAX and FTSE, which have a price to pip ratio of 1:1, but it doesn’t make it a portable code. You may not need it, but many other may!

     

     

Viewing 8 posts - 1 through 8 (of 8 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login