Storing a met condition in a previous bar

Viewing 15 posts - 1 through 15 (of 25 total)
  • Author
    Posts
  • #115850 quote
    Mike Boorman
    Participant
    Average

    Hello people.

    I want my bots to enter a long trade when there has been a previous cross on the green moving average lines, and the moving average lines are above the central black bollingers.

    As per the screengrab, I’d like to be able to enter on August 4, i.e. “The cross condition was met on August 2. Then on August 3 the averages have eventually got higher than the bollinger. Now I want to enter on August 4”.

 So I’d like the first entry rule to be committed to memory until such time as the second entry rule is passed in a later bar.

    How would I do this?

    Thank you for your help.

    Averagecrosslong= ExponentialAverage[8](close) crosses over ExponentialAverage[13](close)
    
    Abovebollinger= ExponentialAverage[8](close) and ExponentialAverage[13](close) > Average[21](close)
    
    Averagecrossshort= ExponentialAverage[8](close) crosses under ExponentialAverage[13](close)
    
    Belowbollinger= ExponentialAverage[8](close) and ExponentialAverage[13](close) < Average[21](close)
    
    If Averagecrosslong and Abovebollinger THEN
    BUY 1 PERPOINT AT MARKET
    ENDIF
    
    If Averagecrossshort and Belowbollinger THEN
    SELLSHORT 1 PERPOINT AT MARKET
    ENDIF
    
    SET STOP pTRAILING VLSTOP
    Bitcoin-grab.png Bitcoin-grab.png
    #115852 quote
    deletedaccount100622
    Participant
    New

    Do you need to cross would the one average being above the other in the current period not give the same entry condition?

    #115853 quote
    Vonasi
    Moderator
    Master

    Forget crosses over and crosses under and us > and < and also adjust the way you think AND works in your bollinger conditions and you have what you want if I understand your question right.

    Averagecrosslong= ExponentialAverage[8](close) > ExponentialAverage[13](close)
    
    Abovebollinger = (ExponentialAverage[8](close) > Average[21](close)) and (ExponentialAverage[13](close) > Average[21](close))
    
    Averagecrossshort= ExponentialAverage[8](close) < ExponentialAverage[13](close)
    
    Belowbollinger= (ExponentialAverage[8](close < Average[21](close)) and (ExponentialAverage[13](close) < Average[21](close))
    
    If Averagecrosslong and Abovebollinger THEN
    BUY 1 PERPOINT AT MARKET
    ENDIF
    
    If Averagecrossshort and Belowbollinger THEN
    SELLSHORT 1 PERPOINT AT MARKET
    ENDIF
    
    SET STOP pTRAILING VLSTOP
    Mike Boorman thanked this post
    #115872 quote
    Mike Boorman
    Participant
    Average

    Do you need to cross would the one average being above the other in the current period not give the same entry condition?

    The one average simply being above or below the other would give an entry on pretty much every bar, which isn’t the objective. I simply want to react to the instance of a cross and enter one trade, and only re enter once the cross occurs again.

    #115873 quote
    Mike Boorman
    Participant
    Average

    Forget crosses over and crosses under and us > and < and also adjust the way you think AND works in your bollinger conditions and you have what you want if I understand your question right.


    Thanks for the suggestion Vonasi, but this code enters on almost every bar (as you can see from the screengrab). I only want this to enter after the line cross on August 3. There should be no other entries in that time period. The next time it should enter should only be whenever the line crosses again. It’s about me trying to enter at the beginning of a trend, not during a trend.

    Does this make sense?

    Screen-Shot-2020-01-04-at-13.05.37.png Screen-Shot-2020-01-04-at-13.05.37.png
    #115890 quote
    Vonasi
    Moderator
    Master

    Just add to the top of the code:

    defparam cumulateorders = false
    #115891 quote
    Mike Boorman
    Participant
    Average

    Yes, I’m using that piece of code in the bot that generated the above results. It must be something else that is needed 🙁

    #115909 quote
    Vonasi
    Moderator
    Master

    My guess is that your trailing stop is too close as trades are opening and closing in the same bar then the conditions are met again at the next bar.

    Try this (not tested):

    defparam cumulateorders = false
    
    Averagecrosslong= ExponentialAverage[8](close) > ExponentialAverage[13](close)
    Abovebollinger = (ExponentialAverage[8](close) > Average[21](close)) and (ExponentialAverage[13](close) > Average[21](close))
    Averagecrossshort= ExponentialAverage[8](close) < ExponentialAverage[13](close)
    Belowbollinger= (ExponentialAverage[8](close < Average[21](close)) and (ExponentialAverage[13](close) < Average[21](close))
    
    if Averagecrosslong and not averagecrosslong[1] then
    crossflag = 1
    endif
    
    if crossflag = 1 and Abovebollinger THEN
    BUY 1 PERPOINT AT MARKET
    crossflag = 0
    ENDIF
    
    if Averagecrossshort and not averagecrossshort[1] then
    crossflag = -1
    endif
    
    If crossflag = -1 and Belowbollinger THEN
    SELLSHORT 1 PERPOINT AT MARKET
    crossflag = 0
    ENDIF
    
    SET STOP pTRAILING VLSTOP
    Mike Boorman thanked this post
    #115917 quote
    Mike Boorman
    Participant
    Average

    My guess is that your trailing stop is too close as trades are opening and closing in the same bar then the conditions are met again at the next bar.

    Try this (not tested):

    The problem now is the delay on entry while it processes the Bollinger variable. As you can see from the screengrabs, one shows an entry on August 6 and one shows an entry on August 5 (this is when I removed the Bollinger criteria). How can we enter on August 5 but include the “above bollinger” criteria in the code?
    Screen-Shot-2020-01-05-at-11.50.05.png Screen-Shot-2020-01-05-at-11.50.05.png
    #115925 quote
    Vonasi
    Moderator
    Master

    There shouldn’t be any delay. All the code does is check for the average cross condition and only checks the bollinger situation if this has occurred. This can happen in the same bar so there should be no delay. It is difficult for me to see in your image what the exact state of the averages is – are you sure they are actually how you think they are?

    Add the following to the bottom of the code so that you can see when each condition is true:

    graph crossflag
    graph averagecrosslong
    graph averagecrossshort
    graph abovebollinger
    graph belowbollinger
    #115927 quote
    Mike Boorman
    Participant
    Average

    I’ve done that with the code. Now the screengrab shows the black X for the Bollinger occurring on Aug 5 (this is the right-hand X of the two) but if you look at the lines on the chart, the purple moving average is above the black Bollinger line on Aug 3 and Aug 4. How can it be that PRT says it was only above the Bollinger on Aug 5?

    Screen-Shot-2020-01-05-at-14.20.52.png Screen-Shot-2020-01-05-at-14.20.52.png
    #115937 quote
    Vonasi
    Moderator
    Master

    It’s easier for others to understand if when you take a screenshot it includes dates and possibly hovering over the candle so that we have a data window to see what each line is.

    #115952 quote
    Mike Boorman
    Participant
    Average

    Sorry, I missed the date from the first grab. Here are three screenshots with a hover over Aug 3, Aug 4 and Aug 5.

    All criteria are met on Aug 3 and yet the graph cross says “abovebollinger” is 0. It’s even more obvious that all criteria are met on Aug 4, but it’s only on Aug 5 that the graph acknowledges that “abovebollinger” has been met.

    Aug-3.png Aug-3.png Aug-4.png Aug-4.png Aug-5.png Aug-5.png
    #115957 quote
    Vonasi
    Moderator
    Master

    I see no issue. At the close of the candle on the 5th all conditions to go long are true. At this point the crossflag is set to zero and an order placed to open a trade at the open of the next candle. This is what happens in your images.

    #115958 quote
    Mike Boorman
    Participant
    Average

    But all the conditions were met on the 3rd and again on the 4th. The averages are above the black bollinger on the 3rd and 4th – it’s clear to see by looking at the lines. Why did it not enter on the 4th or the 5th?

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

Storing a met condition in a previous bar


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 24 replies,
has 3 voices, and was last updated by Mike Boorman
6 years, 1 month ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 01/03/2020
Status: Active
Attachments: 14 files
Logo Logo
Loading...