First try to code a strategy

Forums ProRealTime English forum ProOrder support First try to code a strategy

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

    Ok so I’ve been trying to learn the coding language in PRT for the last few days and had an idea, which is probably horrible, but I am mainly trying to write it for learning purposes and would like your input in how to write it correctly. I want to run it in ProBacktest.

    I will first write in plain English what it is that I want put down in code.

    My Bull Setup and Exit

    • BullSetup = RSI 14 should be below 20
    • BullTrend= Close should be above MA50
    • BullExit = RSI 10 should be above 80

    My Bear Setup and Exit

    • BearSetup1 = Close today should be lower than close yesterday and the close yesterday should be lower than close 2 days ago.
    • BearSetup2 = The percentagechange (measured close to close) today should be equal to or less than the highest percenagechange in 4 days.
    • BearTrend = close should be less MA100
    • BearExit = when close cross under MA20

    Rules

    If condition ‘’BullTrend’’ And ‘’ BullSetup’’ both are met I want to buy the next bar at market. If this buy statement is not true and the 3 bear conditions (BearsetupA, bearsetupB, beartrend) are all met then then I want to short next bar at market.

    Then I need a code that checks if I have a position or not (either long or short). And if I have a long position and the ‘’BullExit’’ happens then I want to sell at next bar at the market and if I have a short position and ‘’BearExit’’ happens then I want to cover my short

    So what I have coded so far is this:

    I am sure many things are wrong but I have a specially hard time figuring out how to write bearsetup2 (the percentagechange).

    Hope somebody can give me some advice on what is wrong and how to fix it.

    Best regards

    #96278

    I haven’t studied the full code but you could try something like this for Bearsetup2:

     

    #96287

    So lets say I instead want todays percentagechange (measured close to close) to be equal to the highest percenagechange in 2 days.

    Will the code then look something like this:

    #96317

    Yes – but it includes today and yesterday only and is very unlikely to happen!

    #96320

    And otherwise does this code as it is written now follow the rules that I want?:

    If condition ‘’BullTrend’’ And ‘’ BullSetup’’ both are met I want to buy the next bar at market. If this buy statement is not true and the 3 bear conditions (BearsetupA, bearsetupB, beartrend) are all met then then I want to short next bar at market.

    Then I need a code that checks if I have a position or not (either long or short). And if I have a long position and the ‘’BullExit’’ happens then I want to sell at next bar at the market and if I have a short position and ‘’BearExit’’ happens then I want to cover my short.

    #96323

    Yes it does because of the DEFPARAM CUMULATEORDERS = FALSE. This means that once a trade is opened it is impossible to open any more trades in either the same direction to add to your position or in the opposite direction which would close your open trade. So once a trade is opened with this set to false the only things that can close the trades are your exit conditions or stop loss and take profit levels if you have them.

    Remember that when writing a strategy you can also use the following useful conditions:

     

     

     

    #96324

    You might want to add some entry conditions to ensure that you exit conditions are not being met at the time of entry as this could result in a buy and sell order at the same time. I think PRT would just send no orders as they cancel each other out but it is better to be on the safe side when money is at risk. You can do this very simply:

     

    #96325

    Great advice. Thank you!

    #96337

    Do you have some examples on how to use those conditions? Or maybe a link to somewhere where they describe them?

    #96341

    This is an example using them to average up and average down and get out as soon as you are in profit – not recommended unless you have Warren Buffets bank account!

    1 user thanked author for this post.
    #96429

    I saw that there is a built-in indicator called ‘change’ that displays the percentage variation between the close and yesterdays close. When I use the simplified creation in PRT and click the ‘change’ indicator window the code  uses a function called variable. Out of curiosity and for learning purposes I was wondering how I can use this in my code instead of your first suggestion:

    Lets say I want that the percentagechange today should be equal to the highest percenagechange in 2 days. Measured close to close.

    Is it possible to write this using the ‘variation’ function?

    #96430

    This means that once a trade is opened it is impossible to open any more trades in either the same direction to add to your position or in the opposite direction which would close your open trade.

    Isn’t it true to say that conditions being met for entry in the opposite direction  WILL close your open trade if you have DEFPARAM CUMULATEORDERS = FALSE?

    #96431

    Isn’t it true to say that conditions being met for entry in the opposite direction  WILL close your open trade if you have DEFPARAM CUMULATEORDERS = FALSE?

    Absolutely correct and well spotted GraHal. I am so used to having strategies that only trade long or only trade short or that use IF NOT ONMARKET to ensure that a trade can play out and not be reversed by conditions being met for a trade in the opposite direction that my brain has gone a little fuzzy. Proving that if you don’t use it you lose it!

    #96432

    Lets say I want that the percentage change today should be equal to the highest percentage change in 2 days. Measured close to close.

    Very similar to the discussion in your other topic.

     

    #96434

    Yeah absolutely I was just wondering if it’s possible to use the built in funtion called ”variation” instead?

    Because the variation function is basically closepercentage if I’m not misstaken.

    Couldnt the code I stead just be written as:

    Bearsetup2 = (closepercentage = highest[2](closepercentage))

     

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

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