Back to links
This code is for 3-4 Bar Pattern Strategy with filters - use 3-4 Bar Trades PDF for visually identifying these trade patterns. I have not yet reversed each of these trades to accomodate long vs. short triggers. Trade #s 7 & 7.2 need additional work.

ALERT posted 4/15/2014: Although I cannot confirm this because I can no longer test these codes, I have been informed that the above 3-4 Bar Strategies requires that anyone using this code should add a "Look-inside bar" function to avoid issues with Intra-bar Price Movement Assumptions. This is not included in the code below. This link is provided as a reference.

Anyone using information or codes on these pages does so at their own risk - no guarantees of stability or profitability are claimed. These codes are all for testing purposes only. Copy everything in the bottom panel and paste into a new strategy.

//////////////////////////// 3-4 BAR PATTERN TRADES ////////////////////////////////
// Set the TradeType based on the patterns drawings in the 3-Bar Pattern PDF
// No inputs declared here - All settings are made in this code
// Each trade entry (1-9) has filters below it that can be turned ON or OFF

///// SET TRADE TYPE 1-9 including 2.1 and 7.2 Here -see PDF for pattern explanation /////
// Trades 1-4, 6, 8 and 9 are long trades and should be tested on up-trending markets
// Trades 5, 7, and 7,2 are short trades and should be tested on down-trending markets
Variables: TradeType (0);{<<--Leave this alone} TradeType=8; {<<--- (see PDF) -set Trade Type Here}

Variables: MultiX (true), {Multi-exits -turn on here or in "MultiExits" & adjust at bottom -switch - True=ON, False=OFF}
HdStop (false), {Turn on Hard Stop & Hard Target here - settings (bottom) -switch - True=ON, False=OFF}
CCILn(20), LinReg(11), XMA(233),{<-CCI,Linear Regression & Exponential MA -adjust as desired}
FastLength(13), SlowLength(21), MACDLength(34),MacDif(-.0000),{<-MACD settings -adjust as desired}
dnMACD(0), MACDAvg(0), MACDDiff(0);{<-MACD declarations-leave as is}

dnMACD=MACD(Close,FastLength,SlowLength);MACDAvg=XAverage(dnMACD,MACDLength);MACDDiff=dnMACD-MACDAvg;{<-defines MACD-leave alone}

/////////////////////////
///// TRADE ENTRIES /////
/////////////////////////

//TRADE 1 Long-------------------------------------------------------------------------------------------------------------------
// {Set the filters for entry of Trade #1 with these Variables - except MACD, set at top of page}
Variables: LnReg1(11){Linear Reg Length},SMA1(144){SimpleMA},CCILn1(20){CCI Length}, XMA1(144){ExpMA}; {Adjust Variables except MACD for trade #1 here}
If TradeType=1
and H[3]>H[2] and C[2]<C[1] and L[1]>L[2] and H[1]>H[2] and H[0]>H[3] // 3Bar PricePattern entry
{FILTERS BELOW - turned on or off with the slashes - adjust settings in the variables above and MACD at top of page}
// and L+(L*.0004)<LinearRegValue(L, LnReg1, 0) // Linear Regression filter //LinearRegValue(Price, Length, TgtBar)
and Close > Average(Close, SMA1) {Simple Moving Average filter }
// and CCI(CCILn1)[1]>0 and CCI(CCILn1)[2]>0 {CCI above 0}
and CCI(CCILn1)[1]>CCI(CCILn1)[2] and CCI(CCILn1)[2]>CCI(CCILn1)[3] //and CCI(CCILn1)[3]>CCI(CCILn1)[4] {rising CCI}
and Close > XAverage(Close, XMA1) //Exponential Moving Average
// and MACDDiff crosses over 0+MacDif {MACD difference - set variables on top of page}
then Buy ("T1 Long") Next Bar at H[3]+ .0001{<-BuyTrigger#} stop; {BuyTrigger# controls amount above breakout to buy}

//TRADE 2 Long-------------------------------------------------------------------------------------------------------------------
// {Set the filters for entry of Trade #2 with these Variables - except MACD, set at top of page}
Variables: LnReg2(11){Linear Reg Length},SMA2(89), CCILn2(20){CCI Length}, XMA2(233){ExpMA}; {Adjust Variables except MACD for trade #2 here}
If TradeType=2// similar to TradeType=2.1
and H[3]>H[2] and C[2]<C[1] and L[2]<L[3] and O[1]<C[2] and C[1]>C[2] and L[1]<L[2]
{FILTERS BELOW - turned on or off with the slashes - adjust settings in the variables above and MACD at top of page}
// and L-(L*.0003)<LinearRegValue(L, LnReg2, 0) // Linear Regression filter //LinearRegValue(Price, Length, TgtBar)
// and Close > Average(Close, SMA2) {Simple Moving Average filter }
and CCI(CCILn2)[1]>0 and CCI(CCILn2)[2]>0 {CCI above 0}
// and CCI(CCILn2)[1]>CCI(CCILn2)[2] and CCI(CCILn2)[2]>CCI(CCILn2)[3] //and CCI(CCILn2)[3]>CCI(CCILn2)[4] {rising CCI}
and Close > XAverage(Close, XMA2) //Exponential Moving Average
// and MACDDiff crosses over 0+MacDif {MACD difference - set variables on top of page}
Then Buy ("T2 Long") Next Bar at H[2]+ .0001 stop;

//TRADE 2.1 Long--------------------------------------------------------------------------------------------------------------------
// {Set the filters for entry of Trade #2.1 with these Variables - except MACD, set at top of page}
Variables: LnReg2.1(11){Linear Reg Lenghth},SMA2.1(89),CCILn2.1(20){CCI Length}, XMA2.1(233){ExpMA}; {Adjust Variables except MACD for trade #2.1 here}
If TradeType=2.1 // this matches the ShowMe
and H[3]>H[2] and C[2]<C[1] and L[2]<L[3] and O[1]<C[2] and C[1]>C[2] and L[1]<L[2] and H[0]>H[2]{breakout}
{FILTERS BELOW - turned on or off with the slashes - adjust settings in the variables above and MACD at top of page}
// and L+(L*.0004)<LinearRegValue(L, LnReg2.1, 0) // Linear Regression filter //LinearRegValue(Price, Length, TgtBar)
// and Close > Average(Close, SMA2.1) {Simple Moving Average filter }
and CCI(CCILn2.1)[1]>0 and CCI(CCILn2.1)[2]>0 {CCI above 0}
// and CCI(CCILn2.1)[1]>CCI(CCILn2.1)[2] and CCI(CCILn2.1)[2]>CCI(CCILn2.1)[3] //and CCI(CCILn2.1)[3]>CCI(CCILn2.1)[4] {rising CCI}
// and Close > XAverage(Close, XMA2.1) //Exponential Moving Average
// and MACDDiff crosses over 0+MacDif {MACD difference - set variables on top of page}
Then Buy ("T2.1 Long") Next Bar at market;

//TRADE 3 Long--------------------------------------------------------------------------------------------------------------------
// {Set the filters for entry of Trade #3 with these Variables - except MACD, set at top of page}
Variables: LnReg3(11){Linear Reg Lenghth},SMA3(89), CCILn3(20){CCI Length}, XMA3(233){ExpMA}; {Adjust Variables except MACD for trade #3 here}
If TradeType=3
and H[3]>H[2] and C[2]<C[3] and O[1]>C[2] and H[1]>H[3] and H[0]>H[1]
{FILTERS BELOW - turned on or off with the slashes - adjust settings in the variables above and MACD at top of page}
// and L+(L*.0004)<LinearRegValue(L, LnReg3, 0) // Linear Regression filter //LinearRegValue(Price, Length, TgtBar)
// and Close > Average(Close, SMA3) {Simple Moving Average filter }
// and CCI(CCILn3)[1]>0 and CCI(CCILn3)[2]>0 {CCI above 0}
// and CCI(CCILn3)[1]>CCI(CCILn3)[2] and CCI(CCILn3)[2]>CCI(CCILn3)[3] //and CCI(CCILn3)[3]>CCI(CCILn3)[4] {rising CCI}
and Close > XAverage(Close, XMA3) //Exponential Moving Average
// and MACDDiff crosses over 0+MacDif {MACD difference - set variables on top of page}
Then Buy ("T3 Long") Next Bar at H[1]+ .0001 stop;

//TRADE 4 Long-------------------------------------------------------------------------------------------------------------------
// {Set the filters for entry of Trade #4 with these Variables - except MACD, set at top of page}
Variables: LnReg4(11){Linear Reg Lenghth},SMA4(144), CCILn4(20){CCI Length}, XMA4(144){ExpMA}; {Adjust Variables except MACD for trade #4 here}
If TradeType=4
and H[3]>H[2] and H[2]>H[1] and L[3]<L[2] and L[2]<L[1]
{FILTERS BELOW - turned on or off with the slashes - adjust settings in the variables above and MACD at top of page}
// and L+(L*.0004)<LinearRegValue(L, LnReg4, 0) // Linear Regression filter //LinearRegValue(Price, Length, TgtBar)
// and Close > Average(Close, SMA4) {Simple Moving Average filter }
// and CCI(CCILn4)[1]>0 and CCI(CCILn4)[2]>0 {CCI above 0}
// and CCI(CCILn4)[1]>CCI(CCILn4)[2] and CCI(CCILn4)[2]>CCI(CCILn4)[3] //and CCI(CCILn4)[3]>CCI(CCILn4)[4] {rising CCI}
and Close > XAverage(Close, XMA4) //Exponential Moving Average
// and MACDDiff crosses over 0+MacDif {MACD difference - set variables on top of page}
Then Buy ("T4 Long") Next Bar at H[3]+.0001 stop;

//TRADE 5 Short----use in a downtrending market for best results------------------------------------------------------------------
// {Set the filters for entry of Trade #5 with these Variables - except MACD, set at top of page}
Variables: LnReg5(5){Linear Reg Lenghth},SMA5(144), CCILn5(20){CCI Length}, XMA5(55){ExpMA}; {Adjust Variables except MACD for trade #5 here}
If TradeType=5
and H[3]>H[2] and H[2]>H[1] and L[3]<L[2] and L[2]<L[1]
{FILTERS BELOW - turned on or off with the slashes - adjust settings in the variables above and MACD at top of page}
and H+(H*.0001)>LinearRegValue(H, LnReg5, 0) // Linear Regression filter //LinearRegValue(Price, Length, TgtBar)
// and Close < Average(Close, SMA5) {Simple Moving Average filter }
// and CCI(CCILn5)[1]<0 and CCI(CCILn5)[2]<0 {CCI below 0}
// and CCI(CCILn5)[1]<CCI(CCILn5)[2] and CCI(CCILn5)[2]<CCI(CCILn5)[3] //and CCI(CCILn5)[3]<CCI(CCILn5)[4] {falling CCI}
and Close < XAverage(Close, XMA5) and XAverage(Close, 144)< XAverage(Close, 377)//Exponential Moving Average
// and MACDDiff crosses under 0-MacDif {MACD difference - set variables on top of page}
Then sell short ("T5 Short") next bar at L[3]-.0001 stop;

//TRADE 6 Long-------------------------------------------------------------------------------------------------------------------
// {Set the filters for entry of Trade #6 with these Variables - except MACD, set at top of page}
Variables: LnReg6(11){Linear Reg Lenghth},SMA6(144), CCILn6(17){CCI Length}, XMA6(377){ExpMA}; {Adjust Variables except MACD for trade #6 here}
If TradeType=6
and H[3]>h[2] and C[2]<C[3] and O[1]>C[2] and H[1]>H[3]
{FILTERS BELOW - turned on or off with the slashes - adjust settings in the variables above and MACD at top of page}
// and L+(L*.0004)<LinearRegValue(L, LnReg6, 0) // Linear Regression filter //LinearRegValue(Price, Length, TgtBar)
// and Close > Average(Close, SMA6) {Simple Moving Average filter }
and CCI(CCILn6)[1]>0 and CCI(CCILn6)[2]>0 {CCI above 0}
// and CCI(CCILn6)[1]>CCI(CCILn6)[2] and CCI(CCILn6)[2]>CCI(CCILn6)[3] //and CCI(CCILn6)[3]>CCI(CCILn6)[4] {rising CCI}
and Close > XAverage(Close, XMA6) //Exponential Moving Average
// and MACDDiff crosses over 0+MacDif {MACD difference - set variables on top of page}
Then Buy ("T6 Long") Next Bar at H[1]+ .0001 stop;

//TRADE 7 Short----use in a downtrending market Like USDCHF for positive results-----------------------------------------------------
// {Set the filters for entry of Trade #7 with these Variables above each trade - except MACD, set at top of page}
Variables: LnReg7(11){Linear Reg Lenghth},SMA7(13), CCILn7(34){CCI Length}, XMA7(144){ExpMA}; {Adjust Variables except MACD for trade #7 here}
If TradeType=7
and L[4]<L[5] and L[3]<L[4] and H[5]>H[4] and H[4]>H[3]
and H[2]<H[3] and H[1]<h[3] and L[2]>L[3] and L[1]>L[3] //and a series of 2 small inside bars
{FILTERS BELOW - turned on or off with the slashes - adjust settings in the variables above and MACD at top of page}
// and H+(H*.0004)>LinearRegValue(H, LnReg7, 0) // Linear Regression filter //LinearRegValue(Price, Length, TgtBar)
and Close < Average(Close, SMA7) {Simple Moving Average filter }
and CCI(CCILn7)[1]<0 and CCI(CCILn7)[2]<0 {CCI below 0}
// and CCI(CCILn7)[1]<CCI(CCILn7)[2] //and CCI(CCILn7)[2]<CCI(CCILn7)[3] //and CCI(CCILn7)[3]<CCI(CCILn7)[4] {falling CCI}
// and Close < XAverage(Close, XMA7) //Exponential Moving Average
// and MACDDiff crosses under 0+MacDif {MACD difference - set variables on top of page}
Then Sell Short ("T7 Short") Next Bar at H[1]- .001 stop;

//TRADE 7.2 Short----use in a downtrending market Like USDCHF for positive results----------------------------------------------------
// {Set the filters for entry of Trade #7.2 with these Variables above each trade - except- MACD, set at top of page}
Variables: LnReg7.2(11){Linear Reg Lenghth},SMA7.2(13), CCILn7.2(34){CCI Length}, XMA7.2(144){ExpMA}; {Adjust Variables except MACD for trade #7.2 here}
If TradeType=7.2 //$PF 3.77
and L[5]<L[6] and L[4]<L[5] and H[6]>H[5] and H[5]>H[4]
and H[3]<H[4] and H[2]<H[4] and H[1]<h[4] and L[3]>L[4] and L[2]>L[4] and L[1]>L[4] //and a series of 3 small inside bars
{FILTERS BELOW - turned on or off with the slashes - adjust settings in the variables above and MACD at top of page}
// and H+(H*.0004)>LinearRegValue(H, LnReg7, 0) // Linear Regression filter //LinearRegValue(Price, Length, TgtBar)
and Close < Average(Close, SMA7.2) {Simple Moving Average filter }
and CCI(CCILn7.2)[1]<0 and CCI(CCILn7.2)[2]<0 {CCI below 0}
// and CCI(CCILn7.2)[1]<CCI(CCILn7.2)[2] //and CCI(CCILn7.2)[2]<CCI(CCILn7.2)[3] //and CCI(CCILn7.2)[3]<CCI(CCILn7.2)[4] {falling CCI}
// and Close < XAverage(Close, XMA7.2) //Exponential Moving Average
// and MACDDiff crosses under 0+MacDif {MACD difference - set variables on top of page}
Then Sell Short ("T7.2 Short") Next Bar at H[1]- .001 stop;

//TRADE 8 Long------------------------------------------------------------------------------------------------------------------
// {Set the filters for entry of Trade #8 with these Variables - except MACD, set at top of page}
Variables: LnReg8(11){Linear Reg Length},SMA8(34){SimpleMA},CCILn8(21){CCI Length}, XMA8(233){ExpMA}; {Adjust Variables except MACD for trade #8 here}
If TradeType=8
and H[4]<H[3] and C[3]>C[2] and C[2]<C[3] and O[1]<C[2] and C[1]>C[2]
{FILTERS BELOW - turned on or off with the slashes - adjust settings in the variables above and MACD at top of page}
// and L+(L*.0000)<LinearRegValue(L, LnReg8, 0) // Linear Regression filter //LinearRegValue(Price, Length, TgtBar)
// and Close < Average(Close, SMA8) {Simple Moving Average filter }
and CCI(CCILn8)[1]>0 and CCI(CCILn8)[2]>0 {CCI above 0}
// and CCI(CCILn8)[1]>CCI(CCILn8)[2] and CCI(CCILn8)[2]>CCI(CCILn8)[3] //and CCI(CCILn8)[3]>CCI(CCILn8)[4] {rising CCI}
and Close > XAverage(Close, XMA8) //Exponential Moving Average
// and MACDDiff crosses over 0+MacDif {MACD difference - set variables on top of page}
Then Buy ("T8 Long") Next Bar at H[3]+ .001 stop;

//TRADE 9 Long-------------------------------------------------------------------------------------------------------------------
// {Set the filters for entry of Trade #9 with these Variables - except MACD, set at top of page}
Variables: LnReg9(11){Linear Reg Length},SMA9(144){SimpleMA},CCILn9(20){CCI Length}, XMA9(144){ExpMA}; {Adjust Variables except MACD for trade #9 here}
If TradeType=9
and H[3]<H[4] and C[3]>C[4] and H[2]>H[3] and C[2]<C[3] and L[1]<L[3] and C[1]>C[3]
{FILTERS BELOW - turned on or off with the slashes - adjust settings in the variables above and MACD at top of page}
// and L+(L*.0004)<LinearRegValue(L, LnReg9, 0) // Linear Regression filter //LinearRegValue(Price, Length, TgtBar)
// and Close < Average(Close, SMA9) {Simple Moving Average filter }
// and CCI(CCILn9)[1]>0 and CCI(CCILn9)[2]>0 {CCI above 0}
// and CCI(CCILn9)[1]>CCI(CCILn9)[2] and CCI(CCILn9)[2]>CCI(CCILn9)[3] //and CCI(CCILn9)[3]>CCI(CCILn9)[4] {rising CCI}
// and Close > XAverage(Close, XMA9) //Exponential Moving Average
// and MACDDiff crosses over 0+MacDif {MACD difference - set variables on top of page}
Then Buy ("T9 Long") next bar at H[2]+.0001 stop;

///////////////////////////
////////// Exits //////////
///////////////////////////
//This exit group (declared as MultiExits below) can be turned here or at "MultiX" top of page. Set to "false" in BOTH places to turn it off.
{This exit strategy group includes stops and targets. It's set for divergence from the entry price instead of a specific price point.
There is a stop, a profit target, a breakeven stop, 2 different trailing stops, and an end-of-day exit.
This will exit your entire position, either long or short.
IntrabarOrderGeneration is set to false because these commands evaluate intrabar even when intrabar order generation is disabled.}
[IntrabarOrderGeneration = false]
var:{these are exit inputs: Suggested Starting Points (SSPs) are for EUR and similar denominated Forex instruments}
MultiExits(false),
ShareOrPosition( 1 ), //1 = per share basis, 2 = position basis
ProfitTargetAmt( 0 ), //SSP .005 or 0 if you don't want a specific profit target
StopLossAmt( .005 ), // SSP .007 or 0 if you don't want a stop loss
BreakevenFloorAmt( 0 ), //SSP .006 or 0 if you don't want a breakeven stop
DollarTrailingAmt( .007 ), //SSP .007 or 0 if you don't want a dollar trailing stop
PctTrailingFloorAmt( .005 ), //SSP .005 or 0 here and/or in next input if you don't want a percent trailing stop or XX for XX percent
PctTrailingPct( 0 );//,// SSP .01. Enter 0 here and/or in previous input if you don't want percent trailing stop or XX for XX percent
{ ExitOnClose( false ); Turned off, not needed - Set to TRUE only for back-testing, if at all; in automated execution, the exit order will NOT be filled
at the close of the day; instead, the order can be sent into the extended session as a limit order. }

if MultiExits=true or MultiX=true then begin
if ShareOrPosition = 1 then SetStopShare else SetStopPosition;
if ProfitTargetAmt > 0 then SetProfitTarget( ProfitTargetAmt );
if StopLossAmt > 0 then SetStopLoss( StopLossAmt );
if BreakevenFloorAmt > 0 then SetBreakeven( BreakevenFloorAmt );
if DollarTrailingAmt > 0 then SetDollarTrailing( DollarTrailingAmt );
if PctTrailingFloorAmt > 0 and PctTrailingPct > 0 then SetPercentTrailing( PctTrailingFloorAmt, PctTrailingPct );
//if ExitOnClose = true then SetExitOnClose;
end;

//////////////////////////////////
////// HARD STOP & TARGET ////////
//////////////////////////////////
Var: HardStop (HdStop), HardStopPrice(.0005); //true to turn on, false to turn off
If HardStop = true then begin
SetStopContract;
Sell Next Bar at EntryPrice - HardStopPrice stop;
SetProfitTarget (.03);
end;

//Daily cost of carry = (qty of units traded X interest rate differential) / 360