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:
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
RSI14 = RSI[14](close)
MA50 = Average[50](close)
RSI10 = RSI[10](close)
MA100 = Average[100](close)
MA20 = Average[20](close)
Percentchange = Variation(close)
//Bull Setup and Exit
Bullsetup = (RSI14 < 20)
Bulltrend = (close > MA50)
Bullexit = (RSI10 > 80)
//Bear Setup and Exit
Bearsetup1 = Close < Close[1] AND Close [1] < Close [2]
Bearsetup2 = (Percentchange[0] = Percentchange[4])
Beartrend = (close < MA100)
Bearexit = (close CROSSES UNDER MA20)
// Conditions to enter long positions
IF bulltrend AND bullsetup THEN
BUY 1 CONTRACT AT MARKET
ENDIF
// Conditions to exit long positions
IF bullexit THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
IF bearsetup1 AND bearsetup2 AND beartrend THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
// Conditions to exit short positions
IF bearexit THEN
EXITSHORT AT MARKET
ENDIF
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
I haven’t studied the full code but you could try something like this for Bearsetup2:
closepercentage = abs((close - close[1])/close)
bearsetup2 = summation[4](closepercentage <= closepercentage[1]) = 4
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:
closepercentage = abs((close - close[1])/close)
bearsetup2 = summation[2](closepercentage = closepercentage[1]) = 2
Yes – but it includes today and yesterday only and is very unlikely to happen!
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.
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:
onmarket
not onmarket
longonmarket
not longonmarket
shortonmarket
not shortonmarket
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:
IF bulltrend AND bullsetup and not bullexit THEN
IF bearsetup1 AND bearsetup2 AND beartrend and not bearexit THEN
Do you have some examples on how to use those conditions? Or maybe a link to somewhere where they describe them?
defparam cumulateorders = false
if (my long entry conditions) then
buy 1 contract at market
endif
if (my short entry condition) then
sellshort 1 contract at market
endif
if longonmarket and close < positionprice and (my second long entry conditions) then
buy 1 contract at market
endif
if shortonmarket and close > positionprice and (my second short entry conditions) then
sellshort 1 contract at market
endif
if longonmarket and close > positionprice then
sell at market
endif
if shortonmarket and close < positionprice then
exitshort at market
endif
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!
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:
closepercentage = abs((close - close[1])/close)
bearsetup2 = summation[4](closepercentage <= closepercentage[1]) = 4
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?
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?
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!
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.
closepercentage = abs((close - close[1])/close[1])
bearsetup2 = (closepercentage = highest[2](closepercentage))