Exit trade if entry bar is against the trend

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #44160 quote
    mikenew
    Participant
    Junior

    Hi

    I’m writing code

    to exit the trade if the entry bar is against the trend. I’ve simplified it into a price crosses SMA indicates the trend. Long side only.

    So if on entry it is a down bar then the strategy will exit the trade on the open of the next bar.

    Here’s the code:

    ONCE exitEntryBarDown = 0
    
    IF NOT OnMarket AND open > Average[20](close) THEN
      BUY 1 CONTRACTS AT MARKET
      // Exit if candle OF the entry is against the trade - Market goes down
      IF open > close THEN
        // Market moves down
        exitEntryBarDown = 1
        SELL AT MARKET NextBarOpen
      ENDIF
      // Set the stop
      SELL AT Average[20](close) STOP
    ENDIF
    
    IF OnMarket THEN
      SELL AT Average[20](close) STOP
    ENDIF
    
    GRAPH exitEntryBarDown as "Exit on Entry bar is down"
    GRAPH OnMarket COLOURED (255,0,255) AS "On Market"
    
    exitEntryBarDown = 0

    It’s behaving in two unexpected ways:

    1. It exits on the open of the bar of entry (not the next bar)
    2. It enters a trade even if the open is less than the SMA

    See attached image.

    Am I misunderstanding something about the code here?

    Broker is IG

    Market is XAUUSD Spot Gold A1 Contract

    Dates in the image are 14th Jan 2016 to 22nd Jan 2016

    Screen-Shot-2017-08-18-at-3.39.41-pm.png Screen-Shot-2017-08-18-at-3.39.41-pm.png
    #44171 quote
    Despair
    Blocked
    Master

    Cool! You just learned me a new commando. I wasn’t aware of nextbaropen. Nice! Thanks.

    Have a look at your line 6. Should you have there one < close? Now you are exiting on an upbar.

    #44196 quote
    Nicolas
    Keymaster
    Master

    NEXTBAROPEN is depreciated keyword that does nothing in the recent probuilder language version. So at line 9, you are exiting your BUY order at the same moment than its open.

    For your second problem, please consider that at line 3, you are comparing the Open of the bar of when the code is read (at Close).

    #44199 quote
    mikenew
    Participant
    Junior

    Thanks Nicholas

    The NextBarOpen is in the current user manual for probacktest I down loaded last week v5.1.2-20170404 .. no mention of it being deprecated. How do we feed this back to the PRT team?

     

    I’ll have a look at the second issue and comment.

    #44200 quote
    mikenew
    Participant
    Junior

    Nicholas,

    Your comment:

    For your second problem, please consider that at line 3, you are comparing the Open of the bar of when the code is read (at Close).

     

    OK so I get it …. my code says this bar’s open>AV after the close and it will enter on the open of the next bar….

    I don’t see any way to implement what I want  “IF open crosses moving average then buy on this open”

    Unless there’s a openNextBar variable which I can’t find …. or a BUY 1 THISOPEN which seems just as rare 🙂

    Screen-Shot-2017-08-19-at-7.22.45-am.png Screen-Shot-2017-08-19-at-7.22.45-am.png
    #44208 quote
    Nicolas
    Keymaster
    Master

    https://www.prorealtime.com/en/changelog_probacktest_10_1

    The variable “OpenOfNextBar” contained the opening price of the bar following the current one. This variable was removed because it is impossible to know this price in an automatic trading situation. This instruction should not be confused with “NextBarOpen” which is an instruction that can be used to place an order at the opening price of the next bar and is still available as shown in the last section.

    But “NextBarOpen” doesn’t help in your situation and it is useless in code since orders are always launch at the next bar open.

    #44210 quote
    mikenew
    Participant
    Junior

    Thanks Again Nicolas. I’ll need to look at a different backtesting tool for this strategy … I’m thinking Python Backtrader https://github.com/mementum/backtrader combined with Pyfolio for basket testing.

    #44211 quote
    robertogozzi
    Moderator
    Master

    You order is placed at some bar, which is the entry bar (conditions were met when the previous bar closed), so you’ll be able to tell the entry bar went the wrong way only when it closes.

    So if you place this code at the very beginning of your strategy it should do:

    IF LongOnMarket THEN
       IF open > close THEN   //check if BEARish
          SELL AT MARKET      //and close any open trade
       ENDIF
    ENDIF
    

    Roberto

    Nicolas thanked this post
    #44212 quote
    mikenew
    Participant
    Junior

    Thanks Roberto

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

Exit trade if entry bar is against the trend


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
mikenew @mikenew Participant
Summary

This topic contains 8 replies,
has 4 voices, and was last updated by mikenew
8 years, 6 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/18/2017
Status: Active
Attachments: 2 files
Logo Logo
Loading...