Dear everyone,
I wrote a Long / Short code (into backtest mode to test its effectiveness), pretty simple but it works and I’m happy with it.
// Conditions to enter long positions
c1= a>0
c2= b>0
c3= b>c
IF c1 AND c2 AND c3 THEN
BUY 1 SHARES AT MARKET
ENDIF
// Conditions to exit long positions
c4= a<0
c5= b<0
c6= b<c
IF c4 AND c5 AND c6 THEN
SELL AT MARKET
ENDIF
Of course there is the short side as well based on the same but opposite conditions mentioned above:
//conditions to enter short positions
IF c4 AND c5 AND c6 THEN
SELLSHORT 1 SHARES AT MARKET
ENDIF
// Conditions to exit short positions
IF c1 AND c2 AND c3 THEN
EXITSHORT AT MARKET
ENDIF
As mentioned, I#m happy with that and I would like to “translate” thy simple system into a boolean +1 (when Long condition is met I would like the system to keep sowing +1 until the short condition is met), -1(when the short condition is met and keep showing -1 until the long condition is met again; then boolean flips to +1 until short condition is met and then flips to -1, etc..)
So far I coded the following:
LongSetup=c1 AND c2 AND c3
ShortSetup=c4 AND c5 AND c6
IF LongSetup THEN
TrendUP=1
ENDIF
IF Short Setup THEN
TrendDown=-1
ENDIF
However, the system test for the validity of condition LongSetup or ShortSetup at any bar , and if it happen that neither LongSetup nor Short Setup are met, then boolean return zero. This is not what I want, as I would like the Boolean to go 1 when LongSetup is met AND stay 1 until ShortSetup is met.
Does anyone know how I can write that ?
Thank you in advance for your help,
Emanuele
Welcome to the forums. I see that this is your first topic. Please use the ‘Insert PRT Code’ button when posting code in future posts as it makes it far easier for others to read.
I have tidied up your post for you. 🙂
The code you have provided would just set the values at + 1 and -1 and then never change them so I’m confused why you think they return to zero. Try this:
LongSetup=c1 AND c2 AND c3
ShortSetup=c4 AND c5 AND c6
IF LongSetup THEN
TrendUP = 1
trenddown = 0
ENDIF
IF ShortSetup THEN
TrendDown = -1
trendup = 0
ENDIF
Thank you, for the advice, however those conditions return 0 more often than you think.
system returns trendUP only when ALL c1 AND c2 AND c3 are met .
the fact that one variable among c1 or c2 or c3 doesnot meet its condition, it s nto by itself enough to negate the trend.
for example:
c1= a>0
c2= b>0
c3= b>c
LongSetup=c1 AND c2 AND c3
Let’s assume:
a= +0.018
b= +0.25
c= +0.10
Long setup condition is met. and system returns 1. that s fine
now let’s go on with the example and after few bars, the values are now
a= -0.011
b= +0.22
c=+0.08
c1 is not met anymore, as a<0
while c2 and c3 are still valid.
system would return zero in this case (as the Short setup conditions are not met either)
so, what I need is to keep plotting 1 until all c1 and c2 and c3 flips to make c4 and c5 and c6 true and trigger system to go -1
it complicated to say by words, but it is very straightforward in practice (think about the trading system. when longsetup is true, system buy at market, and system stays long the market until shortsetup becomes true, then system sells its holding and also sells short. then system stays short until longsetup becomes true again)
I hope I clarified the issue I’m facing, but if you still have doubts, pls let me know, cheers
this is how the indicator would like like when applying the code you kindly suggested:
dark green= trendUP condition is met
Dark red= TrendDOWN condition is met
as you can see, there are tons of points were trend is zero and no bar shows up.
In the panel above, I shows how the trading system looks like applying the same conditions TrendUP and TrendDOWN
In Blue, the trendUP condition is met and triggers the buy order at market, and system stays long until proven otherwise
In orange, the trendDOWN condition is met and the system sells to close the long and open a new short at market, system then stays short until proven otherwise
I hope this further clarifies what I’m trying to translate from the trading system into the indicator
thanks again,
emanuele
//I believe I can input something like this:
IF LongSetup THEN
TrendUP=1 "until ShortSetup is triggered"
ENDIF
IF ShortSetup THEN
TrendDOWN=-1 "until LongSetup is triggered"
ENDIF
//how can I code something like this ?
//thanks
As it is just an indicator then why have two variables – just use one?
LongSetup=c1 AND c2 AND c3
ShortSetup=c4 AND c5 AND c6
IF LongSetup THEN
Trend = 1
r = 0
g = 128
ENDIF
IF ShortSetup THEN
Trend= -1
r = 128
g = 0
ENDIF
return trend coloured(r,g,0) style(histogram, 2)
bingo. it looks perfect
thank you very much for your help