Hello everybody,
I am fairly new at programming my own trading codes. I recently finished the advanced programming course and i am starting to program my first (simple) codes.
I have run into a problem with setting my stop and target at de previous bar and low.
What I want to achieve is that when a insidebar appears in a strong trend a order is put in.
The entrypoint is the previous bar high(high[1] (this i already accomplished!)
The stoploss is at the previous bar low (low[1]). (this also now works in the following code thanks to this forum)
The target I want to set at two times the difference between the bar high and bar low so I get a R:R of 1:2.
once mystop = 1
//once mytarget = 1
var1 = ExponentialAverage[20](close)>= ExponentialAverage[100](close)
var2 = High[0] < High[1]
var3 = Low[0] > Low[1]
// Conditions to enter long positions
IF var1 AND var2 AND var3 AND not daysForbiddenEntry THEN
BUY 1 CONTRACT AT HIGH[1] STOP
mystop = low[1]
//mytarget = high[1] + (2*(high[1]-low[1]))
ENDIF
sell at mystopbull stop
//sell at mytarget stop
Hope someone can help me.
Kind Regards,
Jaldidee
Change the last line from STOP to LIMIT
var1 = ExponentialAverage[20](close)>= ExponentialAverage[100](close)
var2 = High[0] < High[1]
var3 = Low[0] > Low[1]
// Conditions to enter long positions
IF not onmarket and var1 AND var2 AND var3 AND not daysForbiddenEntry THEN
BUY 1 CONTRACT AT HIGH[1] STOP
mystop = low[1]
mytarget = high[1] + (2*(high[1]-low[1]))
sell at mystopbull stop
sell at mytarget limit
ENDIF
if onmarket then
sell at mystopbull stop
sell at mytarget limit
endif
I’ve added a IF NOT ONMARKET condition as otherwise the stop and limit values could change after you have opened a position. This does however mean that you can not have cumulating positions.
Hello Vonasi,
Thanks for your reply. I tested your changes and it works perfectly now.
Thanks alot.
Hmmm.
When i tested the code it look like this?
Yeah I noted that … it needs below as Line 9?
Vonasi must not have had his coffees! 🙂
Oh no it was 6:52 PM … he must have started on the beers early … well it is Sunday and he had been travelling for days! 🙂
9 mystopbull = low[1]
Hello Guys,
Grahal is right. The inconsistency was in difference between mystop and mystopbull (has to be the same offcourse!).
Underneath I complemented the code now with also the conditions for short positions.
var1 = ExponentialAverage[20](close)>= ExponentialAverage[100](close)
var2 = High[0] < High[1]
var3 = Low[0] > Low[1]
var4 = ExponentialAverage[20](close)<= ExponentialAverage[100](close)
// Conditions to enter long positions
IF not onmarket and var1 AND var2 AND var3 AND not daysForbiddenEntry THEN
BUY 1 CONTRACT AT HIGH[1] STOP
mystoplong = low[1]
mytargetlong = high[1] + (2*(high[1]-low[1]))
ENDIF
if onmarket then
sell at mystoplong stop
sell at mytargetlong limit
endif
//conditions to enter short positions
IF not onmarket and var4 AND var2 AND var3 AND not daysForbiddenEntry THEN
SELLSHORT 1 CONTRACT AT LOW[1] STOP
mystopshort = high[1]
mytargetshort = low[1]-(2*(high[1]-low[1]))
ENDIF
if onmarket then
exitshort at mystopshort stop
exitshort at mytargetshort limit
endif