I'm trying to create a strategy which shorts when the prices reaches or touches the upper boundary of pre-defined range. The other condition that should be True is that the price for that day should have opened between the pre-defined range mentioned earlier.
I've tried the following logic:
I evaluated each condition separately, so I have two arrays as condition1 and condition2. Now, I set a loop where-in I check when condition1 is true, only then check if codition2 is true. If condition2 is set to be true, then generate the signal on top of condition2 candle.
Below is the snippet of the code:
//This checks if the day opened within L3 and H3
//Condition1 will only be true if opening candle does not touch or cross H3 and L3.
tn = TimeNum();
startTime = 091500;
endTime = 151000;
fixtime = 091500;
timeOkay = (tn >= startTime) AND (tn <= endTime);
condition1 = (H3 > High) AND (High > L3) AND (H3 > Low) AND (Low > L3) AND (startTime == TimeNum());
//condition 2
crossHappened1 = (((open < H3) AND (open > (H3 - (H3*0.001)))) OR ( (open > (H3 + (H3*0.005))) AND (close < H3) )) OR (((high < H3) AND (high > (H3 - (H3*0.0005)))) OR ( (high > (H3 + (H3*0.005))) AND (low < H3) ));
crossHappened2 = ((close < h3) and (close <open));
condition2 = crossHappened1 AND crossHappened2 AND timeOkay;
//Condition2 will check if any candle touched H3
//Check for Condition2 only when Condition1 is true
tradeOpen = False;
for(i=0; i<BarCount; i++){
if(condition1[i] == TRUE){
for(j=i; j<BarCount; j++){
crossHappened1 = (((open < H3) AND (open > (H3 - (H3*0.001)))) OR ( (open > (H3 + (H3*0.005))) AND (close < H3) )) OR (((high < H3) AND (high > (H3 - (H3*0.0005)))) OR ( (high > (H3 + (H3*0.005))) AND (low < H3) ));
crossHappened2 = ((close < h3) and (close <open));
condition2 = crossHappened1 AND crossHappened2;
if(condition2[j] == True){
_TRACE("H3 was labelled at J:" + J);
PlotText("H3", j, High[j] + 2, colorWhite);
Short = True;
break;
}
}
}
}
Cover = (Open < L3) OR (Low < L3);
The problems:
- The code makes chart slow. Is there a better way/logic to achieve this? I tried looking in history as in checking for condition1 only if condition2 is true, but that gave me troubles as I was unable to figure the how to check for condition1 to be true in the same day.
- Short and Cover do not work as intended. Short keeps on taking trades. As per_TRACEF, both the conditions were satisfied 38 times within my dataset, but the amount of time trades taken were more than 500.
Please guide me.