Hello,
My objective is to set a stop loss which equals the buying price – 1,5*Average Daily Range and a profit target which equals the buying price + 3*Average Daily Range.
I’m considering an Average Daily Range of 7 trading days.
1 candle = 1 trading day
I also want to sell at stop if the opening price equals the closing price, or if nothing happened after 20 days.
This is my code
ADR = Average[7](Dhigh(1)-Dlow(1))
// Target and Stop Loss
c6 = 1.5*ADR
c7 = 3*ADR
IF Dclose(0)-c6 THEN
SELL AT close STOP
ENDIF
IF Dclose(0)+c7 THEN
SELL AT close LIMIT
ENDIF
IF close = open THEN
SELL AT close STOP
ENDIF
I don’t know if the above code will do exactly what I expect. Also, I don’t know how to code “If nothing happens, sell at limit after 20 days”.
Can anybody help with this one? Thanks!
Frank
Have you read through all the instruction documentation that can be found here?
https://www.prorealcode.com/prorealtime-documentation/
STOP and LIMIT orders are pending orders that you place on the market at each bar to do something at an inferior price or at a better price so you are using them all wrong.
If you calculate your take profit and stop loss levels then you can simply do this:
ADR = Average[7](Dhigh(1)-Dlow(1))
sl = 1.5*ADR
tp = 3*ADR
set stop ploss sl
set target pprofit tp
Obviously the sl and tp values will be updated at every new bar so if you want them fixed then set them at the time of entering the market and include them with in a IF NOT ONMARKET condition.
Selling after 20 days is easy. Selling if close equals open is also easy but I can’t imagine this happens very often on a daily chart.
if onmarket and ((barindex - tradeindex = 20) or (close = open)) then
sell at market
exitshort at market
endif
Hi Vonasi,
Thank you very much for the link to the online documentation, I only had the PDF version, this is very handy.
I’m still in doubt with one part of the code. I confirm that I do want to keep the stop loss and the take profit fixed, but it’s still not clear to me how to do it, when you say “set them at the time of entering the market and include them with in a IF NOT ONMARKET condition”, How am I supposed to change the code to make that happen?
ADR = Average[7](Dhigh(1)-Dlow(1))
sl = 1.5*ADR
tp = 3*ADR
set stop ploss sl
set target pprofit tp
Many thanks in advance.
Frank
if (your entry conditions) then
buy 1 contract at market
if not onmarket then
ADR = Average[7](Dhigh(1)-Dlow(1))
sl = 1.5*ADR
tp = 3*ADR
endif
endif
set stop ploss sl
set target pprofit tp
Thank you very much! I stll have one more question (I’m learning…). I’ve tried to incorporate the “not onmarket” condition in the buy conditions rather than in a separate one, and I’m getting different results. I don’t understand why, and which of the 2 cases would be more appropriate for my objective:
IF (c1 AND c2 AND c3 AND c4 AND c5) AND not daysForbiddenEntry THEN
BUY 100 SHARES AT MARKET
IF NOT ONMARKET THEN
ADR = Average[7](Dhigh(1)-Dlow(1))
sl = 1.5*ADR
tp = 3*ADR
ENDIF
ENDIF
set stop ploss sl
set target pprofit tp
IF (c1 AND c2 AND c3 AND c4 AND c5) AND not daysForbiddenEntry AND NOT ONMARKET THEN
BUY 100 SHARES AT MARKET
ENDIF
ADR = Average[7](Dhigh(1)-Dlow(1))
sl = 1.5*ADR
tp = 3*ADR
set stop ploss sl
set target pprofit tp
Could you please shed a light on this one? I’m surely missing something here.
Frank
All conditions are checked at the close of a bar. So if you are on market at the close of that bar then that condition is true even if you place an order to close a trade. That stops an entry on the next bar or stops the orders cancelling each other out.
Use DEFPARAM CUMULATEORDERS = FALSE at the top of your first code shown in your last post and that will only ever have one position open at a time but not miss alternate bars due to the ONMARKET condition.