Timed Exiting when in the Red

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #229597 quote
    Brad
    Participant
    Junior

    Good day all

    Can someone please let me know if I have coded this correctly?

     

    • Exit on a Friday if an open trade shows a value of -30 points or less at 21h20 UTC.
    • Exit any open trade with a value of -80 points or less from bar 15 or more.

     

    Regards

    Brad

    FloatingPoints = ((Close - POSITIONPRICE) * POINTVALUE) / POINTSIZE
    
    FridayPoints = -30
    NumberOfBars = 15
    CutTheTradePoints = -80
    
    //Exit Friday evening trades at 21h20 UCT if current points are less than -30
    IF (DayOfWeek = 5 AND Time >= 212000) AND (FloatingPoints < FridayPoints) THEN
    CloseFridayTrade = 1
    ELSE
    CloseFridayTrade = 0
    ENDIF
    
    //Exit the trade if after 15 bars the current points are less than -80
    IF (BARINDEX - TRADEINDEX(1) >= NumberOfBars) AND (FloatingPoints < CutTheTradePoints) THEN
    CuttingMyLosses = 1
    ELSE
    CuttingMyLosses = 0
    ENDIF
    #229629 quote
    JS
    Participant
    Senior
    Hi @Brad

    Your code looks good… It’s just that it’s not an indicator or a trading system right now, so you can’t test the code…

    Customize the code to make it a trading system or an indicator so that you can actually test and assess the code…

    Brad thanked this post
    #229632 quote
    PeterSt
    Participant
    Master
    Hi Brad, JS is right that it is not easy to see whether a real trading system will work out. However, in advance I think I can say this :
    //Exit the trade if after 15 bars the current points are less than -80
    IF (BARINDEX - TRADEINDEX(1) >= NumberOfBars) AND (FloatingPoints < CutTheTradePoints) THEN
      CuttingMyLosses = 1
    ELSE
      CuttingMyLosses = 0
    ENDIF
    This may initially not exit when you would have a Pending Stop for this. Thus, at an initial bar which complies to above, the exit may not happen because the price is not met (again, when you’d use a Pending Stop), while at a next bar the condition may not be true any more because the loss is less than 80 again (e.g. 79). And because the construction of your code, CuttingMyLosses will be 0 again and no exit will happen. This might be what you want. If that is not what you want, you must introduce something like CuttingMyLossesSet. For example :
    //Exit the trade if after 15 bars the current points are less than -80
    
    // Don't forget the logic to reset CuttingMyLossesSet at the beginning of a new trade.
    
    If Not CuttingMyLossesSet then
    
      IF (BARINDEX - TRADEINDEX(1) >= NumberOfBars) AND (FloatingPoints < CutTheTradePoints) THEN
        CuttingMyLosses = 1
        CuttingMyLossesSet = 1
      ELSE
        CuttingMyLosses = 0
      ENDIF
    
    Endif
    
    This may look overdone too some, but I know how decently you are trying to master (PRT) coding. Peter
    Brad thanked this post
    #229722 quote
    Brad
    Participant
    Junior
    Thanks, JS and Peter, for your feedback. Looking at the code you supplied, Peter, does the logic remain the same if I write the code like this?
    //Exit the trade if after 15 bars the current points are less than -80
    
    IF NOT LongOnMarket THEN
    CuttingMyLosses = 0
    ENDIF
    
    IF (BARINDEX - TRADEINDEX(1) >= NumberOfBars) AND (FloatingPoints < CutTheTradePoints) THEN
    CuttingMyLosses = 1
    ELSE
    CuttingMyLosses = 0
    ENDIF
    Am I understanding your coding correctly? (You are correct; I’m definitely trying to master/understand code logic to improve my coding).   Regards Brad
    #229737 quote
    PeterSt
    Participant
    Master
    To me the introduction of lines 3,4,5 looks more decent than without it. But I would have the same remarks as in my previous post. Please remember, those remarks are only valid when you’d use Pending Stops. Compare with this :
    IF (BARINDEX - TRADEINDEX(1) >= NumberOfBars) AND (FloatingPoints < CutTheTradePoints) THEN
      CuttingMyLosses = 1
      Sell at Market
    ELSE
      CuttingMyLosses = 0
    ENDIF
    Now all is fine, and/but your CuttingMyLosses variable would do nothing. So that variable urges (in my mind) for something like
    If CuttingMyLosses then
      // Pending Stop logic here
    Endif
    And my previous post would be valid because the order may not be filled and your code will be called again with the position still there. What remains is that it is difficult to brainstorm without seeing the full code. But I am confident you got the gist already. 🙂
    Brad thanked this post
    #229829 quote
    Brad
    Participant
    Junior
    Great, thanks Peter!
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

Timed Exiting when in the Red


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Brad @bradkinsey-2 Participant
Summary

This topic contains 5 replies,
has 3 voices, and was last updated by Brad
1 year, 11 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/11/2024
Status: Active
Attachments: No files
Logo Logo
Loading...