Help with loop code: bollinger bands touch down then up

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #142984 quote
    Monochrome
    Participant
    Senior

    Im a complete novice and am learn from the videos. Even bought and watched the advanced videos. Im having diffuculty excuting a loop code.

    Im trying to have the code that detects when the prices closes below the bollinger and then closes above the bollinger – in just this two part sequence.

    Can someone help me with the template of the code and if it is even a loop code or not?

    Thank you in advance

    #142989 quote
    robertogozzi
    Moderator
    Master

    There you go:

    Found = -1
    BollUP = BollingerUp[20](close)
    For j = BarIndex downto 0
       i = BarIndex - j
       If close[i+1] <  BollUP and close[i] > BollUP Then
          Found = i
          Break
       Endif
    Next
    //     or
    Found = -1
    BollUP = BollingerUp[20](close)
    For j = BarIndex downto 0
       i = BarIndex - j
       If close[i] Crosses Over BollUP Then
          Found = i
          Break
       Endif
    Next

    The variable FOUND, if <> -1 retains the bar ID where the event most recently occurred.

    Monochrome thanked this post
    #143001 quote
    Monochrome
    Participant
    Senior

    Thank you for your reply.

    It is my fault, i meant to say when prices closes below the lower bollinger and then (any amount of periods later) closes above the upper bollinger – buy.

    I will try to use the IF NEXT structure you provided to try to make it work.

    #143003 quote
    robertogozzi
    Moderator
    Master

    There you go:

    DEFPARAM CumulateOrders = false
    ONCE Trigger = 0
    BollUP   = BollingerUp[20](close)
    BollDOWN = BollingerDown[20](close)
    IF close < BollDOWN Then
       Trigger = 1
    ENDIF
    IF close > BollUP AND Trigger = 1 THEN
       Trigger = 2
    ENDIF
    IF Trigger = 2 AND Not OnMarket THEN
       BUY 1 Contract AT Market
       SET TARGET pPROFIT 100
       SET STOP   pLOSS   50
       Trigger = 0
    ENDIF
    Nicolas and Monochrome thanked this post
    #143004 quote
    robertogozzi
    Moderator
    Master

    There’s no need to use LOOPS, just wait close < BollDOWN then start triggering, as soon as it closes above BollUP you can enter Long.

    Monochrome thanked this post
    #144829 quote
    Monochrome
    Participant
    Senior

    Hi again, i cant seem to get the code working perfectly. Most of the trades are working then some odd trades happens that goes against my code. It makes trades below the bollinger.

    Im trying to make it work by the code you have given me plus –  bollinger above 200ema as core.   (Experimenting with the other indicators.)

    This is my code:

    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    // Conditions to enter long positions
    indicator1 = ExponentialAverage[200](close)
    c1 = (close > indicator1)
    BollUP = CALL “Example2 : Bollinger(1)upup”[0.66, 10]
    BollDOWN = CALL “Example2 : Bollinger(1)down”[0.66, 10]
    indicator3 = ExponentialAverage[200](close)
    c2 = (bolldown > indicator3)
    indicator4 = CCI[20]
    c3 = (indicator4 < 0)indicator6 = AverageTrueRange[14](close)
    c5 = (indicator6 > 3)
    
    IF high < BollDOWN Then
    Trigger = 1
    ENDIF
    IF close> BollUP AND Trigger = 1 THEN
    Trigger = 2
    ENDIF
    
    if trigger= 2 and c1 and c2 and c3 and c5 then
    trigger = 3
    endif
    
    IF trigger = 3 THEN
    BUY 1 PERPOINT AT MARKET
    trigger = 0
    ENDIF
    
    // Stops and targets
    SET STOP pTRAILING 30
    SET TARGET pPROFIT 60

    Any advice of what im doing wrong? Much appreciated.

    thank you

    #144830 quote
    robertogozzi
    Moderator
    Master

    Always use the ‘Insert PRT Code’ button when putting code in your posts to make it easier for others to read.

    Thank you 🙂

    #144831 quote
    robertogozzi
    Moderator
    Master

    To be able to replicate your trades you should post the complete code, not just the strategy.

    Moreover we need to know:

    • instrument traded
    • TF used
    • date and time of at leasr one bar where a traded was entered incorrectly.
    #144832 quote
    Monochrome
    Participant
    Senior

    Sorry i thought i had attached a screenshot before. ill try again now

    5min tf

    eur/usd

    Examples in the screenshot

    Screenshot-9.jpg Screenshot-9.jpg
    #144852 quote
    Nicolas
    Keymaster
    Master

    You are never resetting the triggers variables, so they last forever in their actual states until “trigger3”, then the order is executed at next bar open.

    Monochrome thanked this post
    #144927 quote
    Monochrome
    Participant
    Senior

    How do i reset the variables?

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

Help with loop code: bollinger bands touch down then up


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Monochrome @monochrome Participant
Summary

This topic contains 10 replies,
has 3 voices, and was last updated by Monochrome
5 years, 4 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/30/2020
Status: Active
Attachments: 1 files
Logo Logo
Loading...