Error in strategy coding: open and closes signals are late one bar

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

    Hi everyone,

    I tried to code a very simple str in a simplified way. And when I pro backtest, I can see, that the open and closes signals are late one bar. Anyone experienced something similar? Any tips?

     

    The strategy as follows (long):

    Entry:

    Price must be at or above 50 SMA (close ›= Average [50](close)

    The setup bar’s close is lower, than the close of the bar two bar before (close[1]‹close[3])

    entry bar’s high is higher than the setup’s bar high (high›high[1])

     

    Exit:

    when a bar’s high is lower than the previous bar’s high (high‹high[1])

     

    And I can see the entry arrow not below the entry bar, but one bar later, and I can see the exit signal also one bar later than expected

     

    Kind regards

    George

    #58699 quote
    robertogozzi
    Moderator
    Master

    For anyone to be of some help you should post your complete code.

    Roberto

    #58701 quote
    George65
    Participant
    Senior

    Sorry, Roberto. Here it is! Thank you!

    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    // Conditions to enter long positions
    indicator1 = Average[50](close)
    c1 = (close >= indicator1)
    c2 = (close[1] < close[3])
    c3 = (high > high[1])
    
    IF c1 AND c2 AND c3 THEN
    BUY 2 CONTRACT AT MARKET
    ENDIF
    
    // Conditions to exit long positions
    c4 = (high < high[1])
    
    IF c4 THEN
    SELL AT MARKET
    ENDIF
    
    // Stops and targets
    SET STOP pLOSS 6
    #58703 quote
    Nicolas
    Keymaster
    Master

    Topic moved to ProOrder support forum and code edited, please respect forums rules.

    #58707 quote
    George65
    Participant
    Senior

    Sorry, I did not intend to break any rules…

    #58770 quote
    robertogozzi
    Moderator
    Master

    I tested it on DAX, both 2-hour and 4-hour TF and it works perfectly as you can see from the attached pic.

    I outlines the 4 setup bars:

    • 0 = the current one (just closed, when the strategy is executed)
    • 1 = previous bar
    • 2 = second previous bar
    • 3 = third previous bar

    Next to them, on the right side, you’ll see the cyan arrow pointin yo the entry bar, yet

    Your code means:

    c1 = (close >= indicator1) //Closing price of current bar > Average
    c2 = (close[1] < close[3]) //Closing price of the previous bar < closing price of the third previous bar
    c3 = (high > high[1])      //high current price > previous bar's high

    All is fine.

    You probably think the current bar (setup bar)= entry bar.  NO, the setup bar is the current (identified by 0) bar, just closed, while the entry bar is the next one which will open afterwards and you cannot reference it until it closes.

    Strategy are ALWAYS (no matter what TF) executed when each bar closes, BEFORE the next one is opened. So the current bar cannot be the next one (the entry bar) because it has not yet started.

    #58777 quote
    George65
    Participant
    Senior

    Thank you Roberto!

    #58786 quote
    George65
    Participant
    Senior

    Before your reply I noticed the “next bar open” in the “assisted ProBacktest creation” window. And it made clear, that its logic different from indicators. I created several indicators in the program, and it draws the arrow when the condition is met (on the entry bar and not one bar later). The strategy draws the arrow next bar open, so not below the entry bar. It is confusing, and may turn the profitability of the strategy upside down (not necessarily, but it can happen) .

    An example (dax long):

    The entry bar gives you the signal at 13.300. But it closes at 13.400.  The next bar opens with a 20 point gap up. So, the strategy enters at 13.420. Your stop is 20 points. It pulls back 40 points, and you are stopped out, and realise a loss. If your trade triggered at 13.300 you are not stopped out, and you realise profit.

    The higher high is such a condition which can be realised immediately, and activate the trade. If you trade 4 hour timeframe…a lot can happen till the next bar opens…

    So, when I noticed the next bar open condition in the window, I understood why the signals are late one bar. So from this point of view, the program works fine. But from trading, or risk point of view it has to be considered. If you have the trigger now, but if your trade is activated on the next bar open, it is an unforeseen risk. It can also happen that the trade should be negated, and the program just activates it instead.

    Is it possible to define the stop level below the setup bar? So with not a number, or %.

    Thank you for your time, and patience.

    Kind regards

    George

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

Error in strategy coding: open and closes signals are late one bar


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
George65 @george65 Participant
Summary

This topic contains 7 replies,
has 3 voices, and was last updated by George65
8 years, 1 month ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 01/09/2018
Status: Active
Attachments: No files
Logo Logo
Loading...