Helping with a code

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #10962 quote
    Jon88
    Participant
    Junior

    I need this idea translated to code if possible please?

    Short: 50 EMA crosses under 200 EMA,

    Price crosses over 50 and 200 ema

    short entry on pivot or R1 or R2 or S1 or S2….

    Also could I get for the long position too (see the second screenshot)

    I have attached the screen shot..

    Much appreciated

    Thanks

    Xavimetralla thanked this post
    Screen-Shot-2016-07-28-at-18.45.34.png Screen-Shot-2016-07-28-at-18.45.34.png Screen-Shot-2016-07-28-at-18.49.02.png Screen-Shot-2016-07-28-at-18.49.02.png
    #10988 quote
    Jon88
    Participant
    Junior

    COULD SOMEONE HELP, Please?

    #10996 quote
    Elsborgtrading
    Participant
    Veteran

    Hi Jon. There are some missing infos here. when do you want to exit your positions?

    Pivot what do you want , go short or long? It doesn’t make sense to say I want to go short and Long on Pivots. you need to be more specific. Anyway I made a simple code on the EMA200 and 50. it exits if the High or Low  on ema 50 touches the price again,  SL is set to 65. it should be easy to change the numbers. Unfortunately it doesn’t make any real profit- I tested it on 4H DAX.

    Cheers

    Kasper

    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    // Conditions to enter long positions
    indicator1 = Average[50](close)
    indicator2 = ExponentialAverage[200](close)
    c1 = (indicator1 CROSSES OVER indicator2)
    
    IF c1 THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF
    
    // Conditions to exit long positions
    indicator3 = Average[20](close)
    c2 = (low CROSSES UNDER indicator3)
    
    IF c2 THEN
    SELL  AT MARKET
    ENDIF
    
    // Conditions to enter short positions
    indicator4 = Average[50](close)
    indicator5 = ExponentialAverage[200](close)
    c3 = (indicator4 CROSSES UNDER indicator5)
    
    IF c3 THEN
    SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    
    // Conditions to exit short positions
    indicator6 = Average[20](close)
    c4 = (high CROSSES OVER indicator6)
    
    IF c4 THEN
    EXITSHORT  AT MARKET
    ENDIF
    
    // Stops and targets
    SET STOP pLOSS 20
    //SET TARGET pPROFIT 65
    Nicolas thanked this post
    #11010 quote
    Jon88
    Participant
    Junior

    Hi Kasper,

    Thank you for making the code, much appreciated. Sorry, I didn’t make the idea more clear. Here is more thought-out idea…..

    Long position,

    50 Ema is over 200 EMA, then price crossed under EMA 50 and 200 and price equals pivot (this could be any of the R1, R2, S1 or S2 not just pivot level) (see the screen shot attached on first post) so entry is at Pivot.

    Stop loss can be set to 10 points, trailing stop 10 points, and target can be till price touches 50 EMA.

     

    Short Position,

    200 Ema is over 50 EMA, then price crossed under EMA 200 and 50 and price equals pivot (this could be any of the R1, R2, S1 or S2 not just pivot level) (see the screen shot attached on first post) so entry is at Pivot.

    Stop loss can be set to 10 points, trailing stop 10 points, and target can be till price touches 50 EMA.

     

    Thank you again for the help

    #11014 quote
    Elsborgtrading
    Participant
    Veteran

    Hi Jon, I’m sorry I cant make this code. My attempts to code this, don’t shows no orders. this means that either I’m doing something wrong or the conditions are never met.

    here is a the code for a simple test only for the Pivot line

    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    // Conditions to enter long positions
    indicator1 = Average[50](close)
    indicator2 = Average[200](close)
    c1 = (indicator1 > indicator2)
    
    indicator3 = close
    indicator4 = Average[50](close)
    c2 = (indicator3 CROSSES UNDER indicator4)
    
    Pivot1Long = (DHigh(1) + DLow(1) + DClose(1))/3
    c3 = (high[1] = Pivot1Long)
    c33= Pivot1Long[1]<close
    
    IF c1 AND c2 AND c3 and c33 THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF
    
    // Conditions to exit long positions
    indicator6 = Average[50](close)
    indicator7 = close
    c4 = (indicator6 CROSSES OVER indicator7)
    
    IF c4 THEN
    SELL  AT MARKET
    ENDIF
    
    // Conditions to enter short positions
    indicator8 = Average[200](close)
    indicator9 = Average[50](close)
    c5 = (indicator8 > indicator9)
    
    indicator10 = close
    indicator11 = Average[200](close)
    c6 = (indicator10 CROSSES UNDER indicator11)
    
    Pivot1short = (DHigh(1) + DLow(1) + DClose(1))/3
    c7 = (low[1] CROSSES UNDER Pivot1Short)
    c77=Pivot1Short[1]>close
    IF c5 AND c6 AND c7 and c77 THEN
    SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    
    // Conditions to exit short positions
    indicator13 = close
    indicator14 = Average[50](close)
    c8 = (indicator13 = indicator14)
    
    IF c8 THEN
    EXITSHORT  AT MARKET
    ENDIF
    
    // Stops and targets
    //SET STOP pLOSS 10 pTRAILING 10
    #11017 quote
    Nicolas
    Keymaster
    Master

    The c3 condition seems a lot restrictive. You try to test if a high is absolutely equal to a pivot point. I didn’t read the strategy description though so I’m maybe wrong 😊

    #11020 quote
    Elsborgtrading
    Participant
    Veteran

    Hi Nicolas, and Thanks. I also found other errors, and I have some time to think it over, between diaper change and gardening 😊

    I will get back a bit later with something.

    #11022 quote
    Elsborgtrading
    Participant
    Veteran

    Hi Jon. This is what I came up with. Seems like there is not to many trades in the 2 years I backtested it on DAX 30 min chart. that is maybe a subject to discuss as I didn’t look further into this. does the number of trades seems legit, should there be more?

    It generates a small profit, but it closes the trades on the same bar. that means that we cant be sure if you were stopped out by SL due to limitation in backtesting with ProOrder. ProOrder only calculate profits on the same bar.

    Anyway it generates a small profit over the last 2 years.

    Cheers Kasper

     

    // WORKING test 2.0 Pivot strategy
    
    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    // Conditions to enter long positions
    indicator1 = Average[200](close)
    indicator2 = Average[50](close)
    c1 = (indicator1 < indicator2)
    
    indicator3 = close
    indicator4 = Average[200](close)
    c2 = (indicator3 CROSSES OVER indicator4)
    
    Pivot1Long = (DHigh(1) + DLow(1) + DClose(1))/3
    S1DLong = 2*((DHigh(1) + DLow(1) + DClose(1))/3) - DHigh(1)
    S2DLong = (DHigh(1) + DLow(1) + DClose(1))/3-(DHigh(1)-DLow(1))
    S3DLong = DHigh(1)+2*(((DHigh(1) + DLow(1) + DClose(1))/3)-DLow(1))
    R1DLong = 2*((DHigh(1) + DLow(1) + DClose(1))/3) - DLow(1)
    R2DLong = (DHigh(1) + DLow(1) + DClose(1))/3+(DHigh(1)-DLow(1))
    R3DLong = DHigh(1)+2*((DHigh(1) + DLow(1) + DClose(1))/3-DLow(1))
    
    
    c3 = (close[1] crosses under Pivot1Long)
    c3S1D=(close[1] crosses under S1DLong)
    c3S2D=(close[1] crosses under S2DLong)
    c3S3D=(close[1] crosses under S3DLong)
    c3R1D=(close[1] crosses under R1DLong)
    c3R2D=(close[1] crosses under R2DLong)
    c3R3D=(close[1] crosses under R3DLong)
    
    c33= close[1]<close
    
    IF (c1 AND c2 and c33 AND c3)then
    BUY 1 CONTRACT AT MARKET 
    endif
    if (c1 AND c2 and c33 AND c3S1D) then
    BUY 1 CONTRACT AT MARKET 
    endif
     if (c1 AND c2 and c33 AND c3S2D)then
    BUY 1 CONTRACT AT MARKET
    endif
    if (c1 AND c2 and c33 AND c3S3D) then
    BUY 1 CONTRACT AT MARKET
    endif
    if (c1 AND c2 and c33 AND c3R1D) then
    BUY 1 CONTRACT AT MARKET
    endif
    if (c1 AND c2 and c33 AND c3R2D) then
    BUY 1 CONTRACT AT MARKET
    endif
    if (c1 AND c2 and c33 AND c3R3D) THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF
    
    
    
    // Conditions to exit long positions
    
    indicator6 = close
    indicator7 = Average[50](close)
    c4 = (indicator6 crosses under indicator7)
    
    IF c4 THEN
    SELL  AT MARKET
    ENDIF
    
    // Conditions to enter short positions
    indicator8 = Average[200](close)
    indicator9 = Average[50](close)
    c5 = (indicator8 > indicator9)
    
    indicator10 = close
    indicator11 = Average[200](close)
    c6 = (indicator10 CROSSES UNDER indicator11)
    
    Pivot1short = (DHigh(1) + DLow(1) + DClose(1))/3
    S1DShort = 2*((DHigh(1) + DLow(1) + DClose(1))/3) - DHigh(1)
    S2DShort = (DHigh(1) + DLow(1) + DClose(1))/3-(DHigh(1)-DLow(1))
    S3DShort = DHigh(1)+2*(((DHigh(1) + DLow(1) + DClose(1))/3)-DLow(1))
    R1DShort = 2*((DHigh(1) + DLow(1) + DClose(1))/3) - DLow(1)
    R2DShort = (DHigh(1) + DLow(1) + DClose(1))/3+(DHigh(1)-DLow(1))
    R3DShort = DHigh(1)+2*((DHigh(1) + DLow(1) + DClose(1))/3-DLow(1))
    
    
    c7 = (low[1] CROSSES over Pivot1Short)
    c7S1D= (low[1] CROSSES over S1DShort)
    c7S2D=(low[1] CROSSES over S2DShort)
    c7S3D=(low[1] CROSSES over S3DShort)
    c7R1D= (low[1] CROSSES over R1DShort)
    c7R2D= (low[1] CROSSES over R2DShort)
    c7R3D= (low[1] CROSSES over R3DShort)
    
    c77=close[1]>close
    IF (c5 AND c6 AND c77 and c7)then
    SELLSHORT 1 CONTRACT AT MARKET
    endif
    if (C5 and c6 and c77 and c7S1D) then
    SELLSHORT 1 CONTRACT AT MARKET
    endif
    if (c5 and c6 and c77 and c7S2D) then
    SELLSHORT 1 CONTRACT AT MARKET
    endif
    if (c5 and c6 and c77 and c7S3D) then
    SELLSHORT 1 CONTRACT AT MARKET
    endif
    if (c5 and c6 and c77 and c7R1D)then
    SELLSHORT 1 CONTRACT AT MARKET
    endif
    if (c5 and c6 and c77 and c7R2D)then
    SELLSHORT 1 CONTRACT AT MARKET
    endif
    if (c5 and c6 and c77 and c7R3D) then
    SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    
    
    
    // Conditions to exit short positions
    indicator13 = close
    indicator14 = Average[50](close)
    c8 = (indicator13 crosses over indicator14)
    
    IF c8 THEN
    EXITSHORT  AT MARKET
    ENDIF
    
    // Stops and targets
    SET STOP pLOSS 10 Ptrailing 5
    
    Screenshot_139.png Screenshot_139.png Screenshot_138.png Screenshot_138.png
    #11026 quote
    Elsborgtrading
    Participant
    Veteran
    #11042 quote
    Jon88
    Participant
    Junior

    Kasper, thank you very much for taking out your time to provide me with the code.

    I think it should have provided more than 44 trades in few years but this probably to do with limitation PRT rather than your code. But how easy would it be to transfer this idea and code you provided me into screener, which prehaps I manually enter position if the setup is correct?

    thanks

    #11047 quote
    Elsborgtrading
    Participant
    Veteran

    Hi Jon.

    Well It should be no problem to convert the code to both an indicator and a Screener. I think it also will be a good time to test if all S and R lines will be used. I changed your strategy a bit so when you have a retracement towards the Pivot, S and R lines, I test for the next candle actually bounces off and not going through. I think this gives a better strategy in terms of automatic trading. I will see how it turns out in a screener. But I’ve just returned to work from summertime so I can’t say when it will be finish.

    Cheers Kasper

    #11050 quote
    Jon88
    Participant
    Junior

    Kasper, no problem any time it suitable to you! thank you!

    #11064 quote
    Elsborgtrading
    Participant
    Veteran

    Hi Jon. I need to solve this problem. When I add Pivot indicators on price it’s only the current day it will display. If I need to verify your strategy with the actual code, I need the history of those lines in the chart. Otherwise I can’t see if I’m on the spot or not. It may be a simple issue, but in fact I started coding just a few weeks ago so many things are still new to me 🙂  I will try and ask in the community.

    #11065 quote
    Elsborgtrading
    Participant
    Veteran

    Ahh never mind, I think I found another Indicator that does what I want 🙂

    #11072 quote
    Elsborgtrading
    Participant
    Veteran

    Hi Jon. It was actually easier that expected. I have made both the screener and indicator. The code has change a bit in the structure, but they are the same. The indicator together with the backtest of the code will give you an idea when conditions are met. I still can’t verify if the code is in consistent with you requirements, so I guess you have to verify it, by looking at the results.

    Further what the backtest shows, I wouldn’t put my money on as the issue with PRT and profit on zero bars is still an issue. PRT will only count the winnings and not the losses, unless there are no winners within the one bar of testing.

    //SCREENER
    
    // WORKING test 2.5 Pivot strategy INDICTOR
    INDICATEUR = 0
    //INDICATEUR1 = 0
    
    // Conditions to enter long positions
    
    
    indicator1 = Average[200](close)
    indicator2 = Average[50](close)
    c1 = (indicator1 < indicator2)
    indicator3 = close
    indicator4 = Average[200](close)
    c2 = (indicator3 CROSSES OVER indicator4)
    
    Rule1 = 1//(BarIndex-TradeIndex)>=1
    
    Pivot1Long = (DHigh(1) + DLow(1) + DClose(1))/3
    S1DLong = 2*((DHigh(1) + DLow(1) + DClose(1))/3) - DHigh(1)
    S2DLong = (DHigh(1) + DLow(1) + DClose(1))/3-(DHigh(1)-DLow(1))
    S3DLong = DHigh(1)+2*(((DHigh(1) + DLow(1) + DClose(1))/3)-DLow(1))
    R1DLong = 2*((DHigh(1) + DLow(1) + DClose(1))/3) - DLow(1)
    R2DLong = (DHigh(1) + DLow(1) + DClose(1))/3+(DHigh(1)-DLow(1))
    R3DLong = DHigh(1)+2*((DHigh(1) + DLow(1) + DClose(1))/3-DLow(1))
    
    c3PvD = (close[1] crosses under Pivot1Long)
    c3S1D=(close[1] crosses under S1DLong)
    c3S2D=(close[1] crosses under S2DLong)
    c3S3D=(close[1] crosses under S3DLong)
    c3R1D=(close[1] crosses under R1DLong)
    c3R2D=(close[1] crosses under R2DLong)
    c3R3D=(close[1] crosses under R3DLong)
    c33= close[1]<close
    
    
    PivotLong = c1 AND c2 and c33 AND c3PvD and Rule1
    Sup1Long = c1 AND c2 and c33 AND c3S1D and Rule1
    Sup2Long = c1 AND c2 and c33 AND c3S2D and Rule1
    Sup3Long = c1 AND c2 and c33 AND c3S3D and Rule1
    Res1Long = c1 AND c2 and c33 AND c3R1D and Rule1
    Res2Long = c1 AND c2 and c33 AND c3R2D and Rule1
    Res3Long = c1 AND c2 and c33 AND c3R3D and Rule1
    
    If PivotLong or Sup1Long or Sup2Long or Sup3Long or Res1Long or Res2Long or Res3Long then
    INDICATEUR =1
    else
    INDICATEUR=0
    Endif
    
    
    // Conditions to enter short positions
    indicator8 = Average[200](close)
    indicator9 = Average[50](close)
    c5 = (indicator8 > indicator9)
    indicator10 = close
    indicator11 = Average[200](close)
    c6 = (indicator10 CROSSES UNDER indicator11)
    
    Pivot1short = (DHigh(1) + DLow(1) + DClose(1))/3
    S1DShort = 2*((DHigh(1) + DLow(1) + DClose(1))/3) - DHigh(1)
    S2DShort = (DHigh(1) + DLow(1) + DClose(1))/3-(DHigh(1)-DLow(1))
    S3DShort = DHigh(1)+2*(((DHigh(1) + DLow(1) + DClose(1))/3)-DLow(1))
    R1DShort = 2*((DHigh(1) + DLow(1) + DClose(1))/3) - DLow(1)
    R2DShort = (DHigh(1) + DLow(1) + DClose(1))/3+(DHigh(1)-DLow(1))
    R3DShort = DHigh(1)+2*((DHigh(1) + DLow(1) + DClose(1))/3-DLow(1))
    
    c7PvD = (low[1] CROSSES over Pivot1Short)
    c7S1D= (low[1] CROSSES over S1DShort)
    c7S2D=(low[1] CROSSES over S2DShort)
    c7S3D=(low[1] CROSSES over S3DShort)
    c7R1D= (low[1] CROSSES over R1DShort)
    c7R2D= (low[1] CROSSES over R2DShort)
    c7R3D= (low[1] CROSSES over R3DShort)
    c77=close[1]>close
    
    PivotShort = c5 AND c6 AND c77 and c7PvD and Rule1
    Sup1Short = c5 AND c6 AND c77 and c7S1D and Rule1
    Sup2Short = c5 AND c6 AND c77 and c7S2D and Rule1
    Sup3Short = c5 AND c6 AND c77 and c7S3D and Rule1
    Res1Short = c5 AND c6 AND c77 and c7R1D and Rule1
    Res2Short = c5 AND c6 AND c77 and c7R2D and Rule1
    Res3Short = c5 AND c6 AND c77 and c7R3D and Rule1
    
    
    IF PivotShort or Sup1Short or Sup2Short or Sup3Short or Res1Short or Res2Short or Res3Short then
    INDICATEUR =-1
    else
    INDICATEUR=0
    ENDIF
    
    screener[INDICATEUR]
Viewing 15 posts - 1 through 15 (of 18 total)
  • You must be logged in to reply to this topic.

Helping with a code


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Jon88 @jon88 Participant
Summary

This topic contains 17 replies,
has 3 voices, and was last updated by Elsborgtrading
9 years, 7 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 07/28/2016
Status: Active
Attachments: No files
Logo Logo
Loading...