code to close a position when in profit

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #213606 quote
    borderlineJim
    Participant
    Senior

    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 quote
    GraHal
    Participant
    Master

    How about …

    If abs(TradePrice - Close) > 0 Then
    Sell at Market
    ExitShort at Market
    Endif
    robertogozzi and borderlineJim thanked this post
    #213615 quote
    robertogozzi
    Moderator
    Master

    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:

    IF PositionPerf > 0 THEN
    GraHal and borderlineJim thanked this post
    #213620 quote
    borderlineJim
    Participant
    Senior

    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.

    GraHal thanked this post
    #213660 quote
    smp
    Participant
    Average

    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 quote
    robertogozzi
    Moderator
    Master

    Be warned that

    InProfit = (close – tradeprice) * pipsize

    should read

    InProfit = (close – tradeprice) / pipsize

    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:

    timeCloseTradeInProfit = 163000
    if time = timeCloseTradeInProfit Then
       InProfit = (tradeprice - close) * pipsize     //SHORT trades
       if LongOnMarket THEN
          InProfit = (close – tradeprice) * pipsize  //LONG  trades
       endif
       If InProfit > 0 THEN
          Sell at market
          ExitShort at Market
       Endif
    endif
    borderlineJim thanked this post
    #213671 quote
    smp
    Participant
    Average

    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 quote
    robertogozzi
    Moderator
    Master

    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)
  • You must be logged in to reply to this topic.

code to close a position when in profit


General Trading: Market Analysis & Manual Trading

New Reply
Author
Summary

This topic contains 7 replies,
has 4 voices, and was last updated by robertogozzi
2 years, 9 months ago.

Topic Details
Forum: General Trading: Market Analysis & Manual Trading
Language: English
Started: 04/22/2023
Status: Active
Attachments: No files
Logo Logo
Loading...