How to create a breakout box between 2 hours, Code please?

Viewing 15 posts - 1 through 15 (of 57 total)
  • Author
    Posts
  • #71015 quote
    abacus23
    Participant
    Average

    I have searched this site, gone through the replies by Nicolas, watched the video various times, checked youtube comments and sections, but I can’t seem to find the code for the “How to create a breakout box between 2 hours” ?

    I would then apply the code I was trying to develop, into an automated system.

    I was looking at the 5 minute chart, I was trying to build a breakout box for 11pm-6am. It would reset each day, and show high and low between the set times each day, see indicator below that somebody else wrote, but I have amended;

    if time = 061000 then //
    x1= barindex[0] //
    x2 = barindex[86] //
    yh = highest[86](high)
    yL=lowest[86](low)
    drawsegment(x1,yh,x2,yh) coloured (0,0,255)
    drawsegment(x1,yL,x2,yL) coloured (255,0,0)
    // set text offset from line
    os = 4*pipsize
    drawtext("#yh#",barindex,yh+os,SansSerif,bold,20) coloured(0,0,0)
    drawtext("#yL#",barindex,yL-os,SansSerif,bold,20) coloured(0,0,0)
    endif
    
    return yh as "hi" ,yL as "Lo"

     

    The code would work out the high and low between these times, 11pm-6am, and only trade between 8am and 8pm, it would close all trades at 9pm.

    It would Buy, if the price crosses over the high, by 2 points, or sell, if the price crosses under the low, by 2 points.

    Only 1 buy allowed per day and 1 sell allowed per day.

    Stop loss 15, target 15.

    I have read various topics and codes, it seems fairly straight forward, and is covered in various posts by various people, but not all together, I have tried to piece the codes together, but I can’t seem to make it all work. 🙁

    Any help would be much appreaciated. 🙂

    #71043 quote
    Nicolas
    Keymaster
    Master

    This is basically, an Open Range Break Out strategy, this could be coded as follows:

    defparam flatafter = 210000
    
    if longonmarket then 
    alreadybuy=1
    endif
    if shortonmarket then 
    alreadysell=1
    endif
    
    if intradaybarindex=0 then 
    alreadybuy=0
    alreadysell=0
    endif
    
    
    if time = 061000 then 
    yh = highest[86](high)
    yL=lowest[86](low)
    os = 2*pipsize
    endif
    
    tcondition = time>080000 and time<200000
    
    if tcondition then 
    if close<yh+os and not alreadybuy then 
    buy 1 contract at yh+os stop 
    endif
    if close>yl+os and not alreadysell then 
    sellshort 1 contract at yl-os stop
    endif
    endif
    
    
    set stop ploss 15
    set target pprofit 15
    
    abacus23 thanked this post
    #71076 quote
    abacus23
    Participant
    Average

    Nicholas,

    Many thanks for that. 😉

    I knew it was basic for you, just couldn’t write the correct information to tie it all together.

    Thanks again.

    #75269 quote
    abacus23
    Participant
    Average

    Nicholas,

    I have been using the code as above and it’s working well. 😉

    I have been through the pdf’s below, but can’ find what I am looking for.

    I only want, 1 buy allowed per day and 1 sell allowed per day which is working, but is it possible to insert another piece of code, to amend that slightly.

    As this is a breakout code, would it be possible to code in, that if the buy won, (say hit target) that the sell trade can’t happen and the other way, if the sell won, (say hit target) that the buy trade can’t happen.?

    I can’t see anywhere in the guides anything about this, perhaps it’s not possible?

    Thanks in advance. 😉

    #75275 quote
    Nicolas
    Keymaster
    Master

    You can try this modified code: (not tested)

    defparam flatafter = 210000
    
    if longonmarket then 
    alreadybuy=1
    endif
    if shortonmarket then 
    alreadysell=1
    endif
    
    if intradaybarindex=0 then 
    alreadybuy=0
    alreadysell=0
    profit=strategyprofit
    endif
    
    
    if time = 061000 then 
    yh = highest[86](high)
    yL=lowest[86](low)
    os = 2*pipsize
    endif
    
    tcondition = time>080000 and time<200000
    
    if tcondition and strategyprofit<=profit then 
    if close<yh+os and not alreadybuy  then 
    buy 1 contract at yh+os stop 
    endif
    if close>yl+os and not alreadysell then 
    sellshort 1 contract at yl-os stop
    endif
    endif
    
    
    set stop ploss 15
    set target pprofit 15
    abacus23 thanked this post
    #75281 quote
    GraHal
    Participant
    Master

    not trying to be clever 🙂 but lines 13 and 25 need to be changed to something like below  …

    myprofit=strategyprofit
    
    if tcondition and strategyprofit<=myprofit then
    abacus23 thanked this post
    #75293 quote
    Nicolas
    Keymaster
    Master

    Good point GraHal!! You deserve a glass of fresh Wine .. or do you prefer green tea? 😉

    #75300 quote
    GraHal
    Participant
    Master

    Wine after nine, so coffee be fine … to keep my brain going! 🙂

    Any ideas why I get attached message when I  run the code?

    box.jpg box.jpg
    #75302 quote
    Nicolas
    Keymaster
    Master

    Because yl and yh are only calculated one time at 06h10 AM

    To avoid the problem, try to change the code with:

    defparam flatafter = 210000
    
    if longonmarket then 
    alreadybuy=1
    endif
    if shortonmarket then 
    alreadysell=1
    endif
    
    if intradaybarindex=0 then 
    alreadybuy=0
    alreadysell=0
    myprofit=strategyprofit
    endif
    
    
    if time = 061000 then 
    yh = highest[86](high)
    yL=lowest[86](low)
    os = 2*pipsize
    endif
    
    tcondition = time>080000 and time<200000
    
    if tcondition and strategyprofit<=myprofit and yh>0 and yl>0 then 
    if close<yh+os and not alreadybuy  then 
    buy 1 contract at yh+os stop 
    endif
    if close>yl+os and not alreadysell then 
    sellshort 1 contract at yl-os stop
    endif
    endif
    
    
    set stop ploss 15
    set target pprofit 15
    GraHal and abacus23 thanked this post
    #75324 quote
    abacus23
    Participant
    Average

    Nicholas and GraHal,

    Many thanks for your quick help. 😉

    I will now try and backtest.

    Thanks again.

    #75840 quote
    abacus23
    Participant
    Average

    Nicholas / GraHal

     

    Many thanks for the previous codes, they are working well, I am just a little lost with a trailing stop loss method. 😉

     

    I have seen a stop loss method, as described below, and wondered if this was possible?

     

    I looked through the https://www.prorealcode.com/blog/learning/kinds-trailing-stop-proorder/ but there are so many, I am confused as to which it could be. L

    Original Target Profit in Pips = 100p
    Projected Risk in Pips = -30p

    In this scenario I would place a Hard Stop Loss @ Entry – 30p

    Once the trade has acquired 50% of my Projected Profit {50p}, I will move my SL to risk 20% of those 50p.
    50 x 20% = 10. Thereby Locking in a 40p Profit.

    And continue to Trail my SL @ Max Trade Profit Pips – (Max Trade Profit Pips x 20%)

    I will continue to do this even through my Original Projected Profit in Pips Target of 100p

    eg.
    Up 50p = Risk 10p Locking in 40p Prft.
    Up 80p = Risk 16p Locking in 64p Prft.
    Up 100p = Risk 20p Locking in 80p Prft.
    Up 135p = Risk 27p Locking in 108p Prft.
    Up 200p = Risk 40p Locking in 160p Prft.

     

    Lastly, if I want to select which days of the week for a back-test, where do I insert the code please?

    Say I would like to exclude Tuesday’s, but when I insert the code below in line 2, it doesn’t work.

     

    DayOfWeek[1] DayOfWeek[3] DayOfWeek[4] DayOfWeek[5]

     

    Monday =1

    Tuesday =2

    Wednesday =3

    Thursday =4

    Friday =5

     

    Many thanks in advance.

    #75848 quote
    robertogozzi
    Moderator
    Master

    As for SL, I think this should work fine (line 20 of the Snippet Library, modified to accomodate both Long & Short trades https://docs.google.com/spreadsheets/d/1rgboqj7sVwsP9ZRhOduOefye48QMWC07jWVXCl-KJPU/edit#gid=0)

    //////////////////////////////////////////////////////////////////////////////////////////////////////////
    ONCE TrailStart    = 30        //30     Start trailing profits from this point
    ONCE ProfitPerCent = 0.666     //66.6%  Profit to keep
    IF Not OnMarket THEN
       y1 = 0
       y2 = 0
    ELSIF LongOnMarket AND close > (TradePrice + (y1 * pipsize)) THEN                         //LONG
       x1 = (close - tradeprice) / pipsize           //convert price to pips
       IF x1 >= TrailStart THEN                      //go ahead only if 30+ pips
          y1 = max(x1 * ProfitPerCent, y1)           //y = % of max profit
       ENDIF
       IF y1 THEN                                       //Place pending STOP order when y>0
          SELL AT Tradeprice + (y1 * pipsize) STOP      //convert pips to price
       ENDIF
    ELSIF ShortOnMarket AND close < (TradePrice - (y2 * pipsize)) THEN                        //SHORT
       x2 = (tradeprice - close) / pipsize           //convert price to pips
       IF x2 >= TrailStart THEN                      //go ahead only if 30+ pips
          y2 = max(x2 * ProfitPerCent, y2)           //y = % of max profit
       ENDIF
       IF y2 THEN                                       //Place pending STOP order when y>0
          EXITSHORT AT Tradeprice - (y2 * pipsize) STOP //convert pips to price
       ENDIF
    ENDIF
    //////////////////////////////////////////////////////////////////////////////////////////////////////////

    As for DayOfWeek, lines 17 and 24 at https://www.prorealcode.com/topic/something-is-missing-in-my-friday-sell-off/ will be great of help.

    abacus23, Meta Signals Pro and ZeroCafeine thanked this post
    #75859 quote
    abacus23
    Participant
    Average

    robertogozzi,

    Many thanks.

    I have inserted the hard stop at: set stop ploss 30 and it works great. 🙂

    I have looked at: (As for DayOfWeek, lines 17 and 24 at https://www.prorealcode.com/topic/something-is-missing-in-my-friday-sell-off/) but not being code minded, i’ll admit to being totally lost and all I get is an error message;

    Syntax error:

    This variable is not used in the code:daysforbiddenentry.

    I tried to insert it here, I would like Tuesdays removed.

    defparam flatafter = 210000
    
    daysForbiddenEntry = OpenDayOfWeek = 1 OR OpenDayOfWeek = 2 OR OpenDayOfWeek = 3 OR OpenDayOfWeek = 4 OR OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
    
    if longonmarket then
    alreadybuy=1
    endif
    if shortonmarket then
    alreadysell=1
    endif
    
    if intradaybarindex=0 then
    alreadybuy=0
    alreadysell=0
    endif
    
    if time = 061000 then
    yh = highest[86](high)
    yL=lowest[86](low)
    os = 2*pipsize
    endif
    
    tcondition = time>080000 and time<200000
    
    if tcondition then
    if close<yh+os and not alreadybuy then
    buy 1 contract at yh+os stop
    endif
    if close>yl+os and not alreadysell then
    sellshort 1 contract at yl-os stop
    endif
    endif
    
    set stop ploss 30
    ONCE TrailStart = 30 //30 Start trailing profits from this point
    ONCE ProfitPerCent = 0.666 //66.6% Profit to keep
    IF Not OnMarket THEN
    y1 = 0
    y2 = 0
    ELSIF LongOnMarket AND close > (TradePrice + (y1 * pipsize)) THEN //LONG
    x1 = (close – tradeprice) / pipsize //convert price to pips
    IF x1 >= TrailStart THEN //go ahead only if 30+ pips
    y1 = max(x1 * ProfitPerCent, y1) //y = % of max profit
    ENDIF
    IF y1 THEN //Place pending STOP order when y>0
    SELL AT Tradeprice + (y1 * pipsize) STOP //convert pips to price
    ENDIF
    ELSIF ShortOnMarket AND close < (TradePrice – (y2 * pipsize)) THEN //SHORT
    x2 = (tradeprice – close) / pipsize //convert price to pips
    IF x2 >= TrailStart THEN //go ahead only if 30+ pips
    y2 = max(x2 * ProfitPerCent, y2) //y = % of max profit
    ENDIF
    IF y2 THEN //Place pending STOP order when y>0
    EXITSHORT AT Tradeprice – (y2 * pipsize) STOP //convert pips to price
    ENDIF
    ENDIF

    Many thanks in advance

    #75860 quote
    abacus23
    Participant
    Average

    I tried again, incorperating the daysForbiddenEntry into the tcondition, but still I get the error.

    IF tcondition = time>060500 and time<193000 and daysForbiddenEntry then
    
    if tcondition and strategyprofit<=myprofit and yh>0 and yl>0 then
    if close<yh+os and not alreadybuy then
    buy 1 contract at yh+os stop
    endif
    if close>yl+os and not alreadysell then
    sellshort 1 contract at yl-os stop
    endif
    endif
    #75861 quote
    robertogozzi
    Moderator
    Master

    To write code, please use the <> “insert PRT code” button to make code easier to read and understand. Thank you.

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

How to create a breakout box between 2 hours, Code please?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
abacus23 @abacus23 Participant
Summary

This topic contains 56 replies,
has 5 voices, and was last updated by robertogozzi
7 years, 4 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 05/21/2018
Status: Active
Attachments: 7 files
Logo Logo
Loading...