Skip to content

Instantly share code, notes, and snippets.

@18182324
Created March 30, 2020 14:32
Show Gist options
  • Save 18182324/83605f58473496cbd49c78398aecd89a to your computer and use it in GitHub Desktop.
Save 18182324/83605f58473496cbd49c78398aecd89a to your computer and use it in GitHub Desktop.
Range Bound Strategy [EasyLanguage Code]
INPUT:
MAXFLDUR(15), //Max Flag Duration
FLAGMIN(2.5), // Max Atr in lowest point in flag
PX(23), //Max Pole Duration.
UPT1BARS(70), // Bars for Uptrend leading to flag
POLEMIN(5.5), //Min ATR Height of the pole
LBF(50), // Min distance between flags
ATRmin(5),// Min volatility change
K(1.2), //Profit Target constant
timeexit(100), //Time exit bars
ATRLL(3),BSEMIN(5), // Stop loss below flag
ATRTRAIL(3),TRAILBARS(5), // Trailing stop parameters
BSEINACT(70),ATRINACT(4); // No change exit parameter
vars:X1(0),X2(0),LRSX2(0),LRSX1(0),POLE(0),ptarget(0),BSE(0),TOP(0),BOTTOM(0),X3(0),L3(0),Y23(0),FLAGBOT(0),
UPT1(0),LF(0),TARGETPER(0);
// FLAG CALCULATION
X1=HighestBar(C,MAXFLDUR)[2];//FLAG DURATION EX POLE TOP
X2=X1+1; // FLAG DURATION INCLUDING POLE TOP
LF=LOWEST(C,X2); TOP=Highest(C,X2)[2];
X2=IFF(LINEARREGSLOPE(C,X1)[1]<0 AND TOP-LF<flagmin*AvgTrueRange(40),X1+1,100);
IF X2>2 AND X2<=MAXFLDUR THEN begin //LIMITS FLAG DURATION
Y23=LOWestBar(C,PX+X2); // POLE BOTTOM BAR
BOTTOM=LOWEST(C,(PX+X2));// POLE BOTTOM
POLE=TOP-BOTTOM;
IF TOP-BOTTOM>POLEMIN*AvgTrueRange(40) AND Y23>X2 THEN BEGIN
TOP=Highest(C,X2)[2];
FLAGBOT=LOWEST(C,X2);
UPT1=BOTTOM-LOWEST(L,UPT1BARS); // UPTREND LEADING TO FLAG
LRSX1=LINEARREGSLOPE(C,X1)*100;//SLOPE IN FLAG
LRSX2=LINEARREGSLOPE(C,X1-1)[2]*100; //SLOPE IN FLAG BEFORE BREAKOUT
Condition1 =TOP-LF<flagmin*AvgTrueRange(40) AND (LRSX1<0 OR LRSX2<0); // LIMITS FLAG SLOPE BETWEEN 0 AND -3 ATR
Condition2=POLE>POLEMIN*AvgTrueRange(40) ; // LIMITS MIN POLE HEIGHT
Condition3= UPT1>0; // UPTREND LEADING TO FLAG
Condition4=(barssinceexit(1)=0 or barssinceexit(1)>LBF) ; // LIMITS DISTANCE BETWEEN SUCCESIVE FLAGS
Condition5= (AvgTrueRange(40)/AvgTrueRange(40)[Y23]-1)*100>ATRmin; // VOLATILITY
If MARKETPOSITION=0 AND CONDITION1 AND CONDITION2 AND CONDITION3 and condition4 and condition5
THEN BEGIN
Buy("Flag") NEXT BAR AT HIGHEST(C,X1) STOP;
END;END;END;
{EXIT CONDITIONS}
if MARKETPOSITION =1 THEN BEGIN
BSE=BARSSINCEENTRY;
X3=HighestBar(C,MAXFLDUR)[BSE+2]-BSE+1;
TOP=Highest(C,X3)[BSE+1];
BOTTOM=LOWEST(C,(PX+X3))[BSE+1];
POLE=(TOP-BOTTOM)/(BOTTOM+.0001)*100;
targetPER=K*POLE;
ptarget=(1+TARGETPER/100)*ENTRYPRICE;
L3=LOWEST(L,X3)[BSE];
// PROFIT TARGET
If C>=ptarget then SELL ("pTARGET") THIS bar at CLOSE;
// STOP
IF BSE>BSEMIN THEN SELL ("UNDER FLAG") NEXT bar at L3-ATRLL*AvgTrueRange(40) STOP;
//TRAILING STOP
IF C<HIGHEST(C,TRAILBARS)-ATRTRAIL*AvgTrueRange(40)THEN SELL("TRAIL") NEXT BAR AT MARKET;
// NO CHANGE EXIT
IF BSE>BSEINACT AND C<ENTRYPRICE+ATRINACT*AvgTrueRange(40)THEN SELL("INACTIVITY") NEXT BAR AT MARKET;
// TIME EXIT
If BSE>timeexit
then SELL("TIME") next bar at open;
END;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment