Trading strategy with 3 SMA, how to code it?

Forums ProRealTime English forum ProOrder support Trading strategy with 3 SMA, how to code it?

Viewing 15 posts - 1 through 15 (of 15 total)
  • #41556

    Good morning, I need help writing a code because I’m not so practical. I wrote to the Prorealtime customer care and they told me to refer my issues here.

     

    so here we are..

     

    I want to write a code with these characteristics:

    I want to create this for the EUR/USD and the GBP/USD cross.

    • we have 3 simple MAs (180;100;80), ATR  (average true range set with period of 25)
    • every day at 00:01 (cest/cet) we check the value of these MAs, and we compare the values of this day (just concluded) with the values of the day before. In this way we only use the end of day values. For example: if today is 21/7/17 00:01 we will check the end of day values of 20/7/17 (already closed) and we will compare these with the end of day values of 19/7/17.
    •  during the check, if all MAs are increasing we will go long and if they are all decreasing we will go short on market.
    • the value of the position that will be opened is:  (2% of our capital available in our account)/ [the value of the ATR (of the day already closed) x 10.000].
    • if we are on market we exit only if:

    a) the 80 MA changes direction and in this case we would exit with 60% of our position

    b) the 100 MA changes  direction and in this case we would exit with everything we have opened ( either the remaining 40% or the entire position) .

    • we will not enter the market again until all current positions have been closed.

    Can you please help me?

     

    Thank you for your time.

     

    #41571

    Have you considered putting the 3 MAs on a Chart and using the Simplified Creation Tool (GUI click select parameters click – see attached) to pull together the basic coded strategy,  then get familiar with syntax etc and then edit / improve using the Creation Tool?

    Also there are the PRT Mauals in pdf for Guidance.

    Let us know how you get on please?

    #41586

    Yes I tried but I had some difficulties and after hours I didn’t solve anything. I was wondering if there is someone that could help me or if there is someone that I can pay to write it for me.

     

    thanks.

    #41612

    Its would be easier / quicker if you were to put on here any code you have so far and then we may be able to spot any Issues.

    One big Issue is this  … we would exit with 60% of our position …  PRT does not allow partial exit, you would have to exit 100% of the position (sounds so daft I’m doubting myself now … somebody will correct me if I’m wrong?).

    #41631

    OK THANK YOU

     

    here there is the code.

    I tried my best to figure it out but it’s not running and I don’t know why.

    // Definizione dei parametri del codice
    DEFPARAM CumulateOrders = TRUE // Posizioni cumulate sattivate

    //ATR SETTINGS
    p = 22

    // Average True Range STOPLOSS
    ATRx = AverageTrueRange[p](1) * 10000

    //MONEY MANAGMENT
    Capital = 20000
    Risk = 0.02
    StopLoss = ATRx
    equity = Capital + StrategyProfit
    maxrisk = round(equity*Risk)

    PositionSize = (abs(round((maxrisk)*POINTSIZE)/abs(round(StopLoss)*PointValue)))

    PositionSize60 = PositionSize/100*60

    PositionSize40 = PositionSize/100*40

    // Condizioni per entrare su posizioni long
    indicator1 = Average[180](close)
    indicator2 = Average[180](close)
    c1 = (indicator1[0] > indicator2[1])
    indicator3 = Average[100](close)
    indicator4 = Average[100](close)
    c2 = (indicator3[0] > indicator4[1])
    indicator5 = Average[80](close)
    indicator6 = Average[80](close)
    c3 = (indicator5[0] > indicator6[1])

    IF c1 AND c2 AND c3 THEN
    BUY (PositionSize60 AND PositionSize40) CONTRACTS AT MARKET
    ENDIF

    // Condizioni per uscire da posizioni long
    indicator7 = Average[80](close)
    indicator8 = Average[80](close)
    c4 = (indicator7[0] < indicator8[1])

    indicator17 = Average[100](close)
    indicator18 = Average[100](close)
    c9 = (indicator17[0] < indicator18[1])

    IF c4 THEN
    SELL PositionSize60 CONTRACTS AT MARKET
    ELSIF C4 AND c9 THEN
    SELL PositionSize40 CONTRACTS AT MARKET
    ELSIF c9 THEN
    SELL (PositionSize60 AND PositionSize40) CONTRACTS AT MARKET

    ENDIF

    // Condizioni per entrare su posizioni short
    indicator9 = Average[180](close)
    indicator10 = Average[180](close)
    c5 = (indicator9[0] < indicator10[1])
    indicator11 = Average[100](close)
    indicator12 = Average[100](close)
    c6 = (indicator11[0] < indicator12[1])
    indicator13 = Average[80](close)
    indicator14 = Average[80](close)
    c7 = (indicator13[0] < indicator14[1])

    IF c5 AND c6 AND c7 THEN
    SELLSHORT (PositionSize60 AND PositionSize40) CONTRACTS AT MARKET
    ENDIF

    // Condizioni per uscire da posizioni short
    indicator15 = Average[80](close)
    indicator16 = Average[80](close)
    c8 = (indicator15[0] > indicator16[1])

    indicator19 = Average[100](close)
    indicator20 = Average[100](close)
    c10 = (indicator19[0] > indicator20[1])
    IF c8 THEN
    EXITSHORT PositionSize60 CONTRACTS AT MARKET
    ELSIF C8 AND c10 THEN
    EXITSHORT PositionSize40 CONTRACTS AT MARKET
    ELSIF c10 THEN
    EXITSHORT (PositionSize60 AND PositionSize40) CONTRACTS AT MARKET
    ENDIF

     

    that you for your time

    #41643

    Wow … your first post gave me the impression you wanted loads of help and couldn’t code … not the case at all, you are much better at coding than me!

    Anyway to keep the momentum up, I got it going! I stripped out all references to exit trades at partial position size (not allowed), but it still came back with errors. Then I kept stripping out bits which I felt might be causing errors (but I may be wrong cos I’m far from expert at coding).

    Below is the code that produced results attached. You can now insert code back in and try and run each time and then you know where the errors are?

    If you want to directly compare your original code to below then enter both into the Difference Checker below. It’s good I use it often.

    https://www.diffchecker.com/

    Keep up the Good Work
    GraHal

     

     

    #41650

    Here’s some info from ‘Our Master’ 🙂 re partial exits not allowed …

    https://www.prorealcode.com/topic/moving-stop-losses-and-closing-partial-positions/

    #41651

    WOWWW!!

    thank you so much for the prompt response and for the kind words, I don’t understand anything about coding but I tried to steal pieces of code here and there.

    by the way, I tried to run your code but it didn’t work on EUR/USD so I suppose it was an error due to the ATR value and I restore the multiplication x 10.000 and now it works!!

    I think it happened because you used the code on the DAX INDEX so the ATR value is different because the pip size and values are different.

    I don’t understand this because I dunno what it means: DEFPARAM PRELOADBARS = 1000, so I tried to delete it and it works in the same way.

    now I have another problem the entries and the exits happen on bar late instead the next bar after the conditions occur.

    I tried to re-establish the parameters that I changed from the standard code but it didn’t work, do you know how to solve it?

     

    #41653

    // Definizione dei parametri del codice
    DEFPARAM CumulateOrders = False // Posizioni cumulate sattivate

    //ATR SETTINGS
    p = 22

    // Average True Range STOPLOSS
    ATRx = AverageTrueRange[p](close)*10000

    //MONEY MANAGMENT
    Capital = 20000
    Risk = 0.02
    StopLoss = ATRx
    equity = Capital + StrategyProfit
    maxrisk = round(equity*Risk)

    PositionSize = (abs(round((maxrisk))/abs(round(StopLoss))))

    // Condizioni per entrare su posizioni long
    indicator1 = Average[180](close)
    indicator2 = Average[180](close)
    c1 = (indicator1[1] > indicator2[2])
    indicator3 = Average[100](close)
    indicator4 = Average[100](close)
    c2 = (indicator3[1] > indicator4[2])
    indicator5 = Average[80](close)
    indicator6 = Average[80](close)
    c3 = (indicator5[1] > indicator6[2])

    IF c1 AND c2 AND c3 THEN
    BUY PositionSize CONTRACTS AT MARKET
    ENDIF

    // Condizioni per uscire da posizioni long
    indicator7 = Average[80](close)
    indicator8 = Average[80](close)
    c4 = (indicator7[1] < indicator8[2])

    indicator17 = Average[100](close)
    indicator18 = Average[100](close)
    c9 = (indicator17[1] < indicator18[2])

    IF c4 OR c9 OR c9 THEN
    SELL AT MARKET

    ENDIF

    // Condizioni per entrare su posizioni short
    indicator9 = Average[180](close)
    indicator10 = Average[180](close)
    c5 = (indicator9[1] < indicator10[2])
    indicator11 = Average[100](close)
    indicator12 = Average[100](close)
    c6 = (indicator11[1] < indicator12[2])
    indicator13 = Average[80](close)
    indicator14 = Average[80](close)
    c7 = (indicator13[1] < indicator14[2])

    IF c5 AND c6 AND c7 THEN
    SELLSHORT PositionSize CONTRACTS AT MARKET
    ENDIF

    // Condizioni per uscire da posizioni short
    indicator15 = Average[80](close)
    indicator16 = Average[80](close)
    c8 = (indicator15[1] > indicator16[2])

    indicator19 = Average[100](close)
    indicator20 = Average[100](close)
    c10 = (indicator19[1] > indicator20[2])
    IF c8 OR c10 OR c10 THEN
    EXITSHORT AT MARKET
    ENDIF

    #41670

    For clarity of messages on ProRealCode’s forums, please use the “insert code PRT” button to separate the text of the code part! Thank you! <<

    PLEASE! 👿

    1 user thanked author for this post.
    #41671

    SORRY..

    #41685

    DEFPARAM PRELOADBARS = 1000 is to get 1000 bars before the strategy launches in case of Long moving averages etc, e.g. MA[1000]. As you say, you don’t need it .. I was trying anything to get your code to spring into life! 🙂

    Is this the cause of your latest issue …

    I changed below on your original code …

    to this

    and you maybe want this or similar?

     

     

    #41691

    thank you, I saw something change but it’s still not working . as you seen in the pic, the circle is where the conditions happen so fro the next one (the arrow) we have to enter but there is a sort of delay that I can’t understand.

     

    really really thank you for your efforts.

    #41694

    You should GRAPH your conditions to see where they happen. Also, keep in mind that code is read at Close and orders are launched at next Open.

    #41695

    Of course I graphed it, but probably the misters answer is here.. when he read the conditions he starts from the next candle. So it’s impossible to change it.

     

    THANK YOU A LOT FOR HELPING ME!!!

     

    it has been so useful for me … if I will have some good results with this strategy I will post it.

     

    have a good week.

Viewing 15 posts - 1 through 15 (of 15 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login