Code to exit position after 30 bars if long still in profit

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #234631 quote
    Kovit
    Participant
    Senior

    Hi guys

    Please can someone help with this bit of coding I’ve used something similar for breakeven but wanted to try something that would exit long position if the price started to retrace after so many bars but whilst still being in profit.

    Thanks in advance.

    #234645 quote
    Iván González
    Moderator
    Master

    Hi!
    with this code you can control the number of bars inside the trade:

    once n=0
    
    if not onmarket and close crosses over average[20](close) then
    buy 1 contract at market
    endif
    
    if onmarket then
    n=n+1
    if n>=30 then
    sell at market
    n=0
    endif
    endif

    If you want also be in profit>0 you can add this:

    if n>=30 and positionperf>0 then
    sell at market
    n=0
    endif
    Kovit thanked this post
    #234650 quote
    Kovit
    Participant
    Senior

    Thank you Ivan. 🙂

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

Code to exit position after 30 bars if long still in profit


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Kovit @kovit Participant
Summary

This topic contains 2 replies,
has 2 voices, and was last updated by Kovit
1 year, 7 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 07/01/2024
Status: Active
Attachments: No files
Logo Logo
Loading...