# Trouble combining two systems into one system.....little help needed.

#### praveen5344

##### Active Member
I found a very simple formula few days ago over the internet it is able to catch the up and down moves and gives good returns but only when there is a clear trend and when the price is not in trend then a lots and lots of whipsaws.......so i decided to combine with another trading system that I was having........when I tested them side by side then I found that when 1st system is giving whipsaws the 2nd system is filtering them and when the second system is giving whipsaws the 1st system is filtering them most of the time and overall giving satisfactory signals with less whipsaws.......I tried to code them together where a buy signal condition is

Buy = ((1st System Buy) and (2nd System Buy)) or ((2nd System Buy) and (1st System Buy))
Sell = ((1st System Sell) and (2nd System Sell)) or ((2nd System Sell) and (1st System Sell))

But when I tested the code, I was not satisfied cos the signals are not being projected as I wanted.........So can someone please combine these two system and correct me where I went wrong......

I am posting.....
- Trading System 1
- Trading System 2
- Combined code of the above two systems by me

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

n=Param("n",34,1,500,1);
A = Wilders( H,n );
B = Wilders( L,n );

SetPositionSize(1,spsShares);

x= Ref(A,-n);
y= Ref(B,-n);

p=Param("p",0.0009,0.0001,1,0.0001);
D=p;
xt=x+x*D;
xb=x-x*D;

yt=y+y*D;
yb=y-y*D;

/* Buy or Sell Condition */
Sell = Cross(yt,Close);
Short=Sell;

Filter = Buy OR Sell;
/* Exploration Parameters */
AddTextColumn( FullName(), "Company Name" );
AddColumn( Sell, "Sell", 1 );
AddColumn( C, "Close", 1.3 );
AddColumn( H, "High", 1.3 );

Title = EncodeColor(colorWhite)+ "Trading System 1" + " - " + Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
" - " + Date() +" - "+"\n" +EncodeColor(colorRed) +"Op-"+O+" "+"Hi-"+H+" "+"Lo-"+L+" "+
"Cl-"+C+" "+ "Vol= "+ WriteVal(V)+"\n"+
EncodeColor(colorLime)+
WriteIf (Buy , " GO LONG / Reverse Signal at "+C+" ","")+
WriteIf (Sell , " EXIT LONG / Reverse Signal at "+C+" ","")+"\n"+EncodeColor(colorWhite)+
WriteIf(Sell , "Total Profit/Loss for the Last Trade Rs."+(C-BuyPrice)+"","")+
WriteIf(Buy , "Total Profit/Loss for the Last trade Rs."+(SellPrice-C)+"","");

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
_SECTION_END();

/////////////////Optimize////////////////////
x = Optimize("B", 20, 1, 30, 1 );
y = Optimize("S", 20, 1, 30, 1 );
z = Optimize("L", 30, 2, 100, 2 );

/////////////////PercentR////////////////////
function PercentR( periods )
{
return -100 * ( HHV( H, periods ) - C )/( HHV( H, periods ) - LLV( L, periods
) );
}

/////////////////TrendScore ////////////////////
TrendScore =
IIf(C>=Ref(C,-11),1,-1)+
IIf(C>=Ref(C,-12),1,-1)+
IIf(C>=Ref(C,-13),1,-1)+
IIf(C>=Ref(C,-14),1,-1)+
IIf(C>=Ref(C,-15),1,-1)+
IIf(C>=Ref(C,-16),1,-1)+
IIf(C>=Ref(C,-17),1,-1)+
IIf(C>=Ref(C,-18),1,-1)+
IIf(C>=Ref(C,-19),1,-1)+
IIf(C>=Ref(C,-20),1,-1);

/////////////////AROON////////////////////

Period = 14;
LLVBarsSince = LLVBars(L, Period) + 1;
HHVBarsSince = HHVBars(H, Period) + 1;

AroonDn = 100 * (Period - LLVBarsSince) / (Period - 1);
AroonUp = 100 * (Period - HHVBarsSince) / (Period - 1);

/////////////////Basic////////////////////

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g,
Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleLine);
_SECTION_END();

_SECTION_BEGIN("EMA");
P = ParamField("Price field",-1);
Periods = Param("Periods", 7, 2, 200, 1, 10 );
Plot( EMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ),
ParamStyle("Style") );
_SECTION_END();

_SECTION_BEGIN("EMA2");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 200, 1, 10 );
Plot( EMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ),
ParamStyle("Style") );
_SECTION_END();

_SECTION_BEGIN("EMA1");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 200, 1, 10 );
Plot( EMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ),
ParamStyle("Style") );
_SECTION_END();

_SECTION_BEGIN("Percent Bands");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 100, 1 );
Width = Param("Width%", 2, 0, 10, 0.05 );
Color = ParamColor("Color", colorCycle );
Style = ParamStyle("Style");
CenterLine = EMA( P, Periods );
Plot( (1 + Width * 0.01) * CenterLine, "%EnvTop" + _PARAM_VALUES(), Color,
Style );
Plot( (1 - Width * 0.01) * CenterLine, "%EnvBot" + _PARAM_VALUES(), Color,
Style );
_SECTION_END();

_SECTION_BEGIN("Percent Bands1");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 100, 1 );
Width = Param("Width%", 2, 0, 10, 0.05 );
Color = ParamColor("Color", colorCycle );
Style = ParamStyle("Style");
CenterLine = EMA( P, Periods );
Plot( (1 + Width * 0.01) * CenterLine, "%EnvTop" + _PARAM_VALUES(), Color,
Style );
Plot( (1 - Width * 0.01) * CenterLine, "%EnvBot" + _PARAM_VALUES(), Color,
Style );
_SECTION_END();

_SECTION_BEGIN("EMA3");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 200, 1, 10 );
Plot( EMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ),
ParamStyle("Style") );
_SECTION_END();

_SECTION_BEGIN("EMA4");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 200, 1, 10 );
Plot( EMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ),
ParamStyle("Style") );
_SECTION_END();

/////////////////Twiggs Money Flow////////////////////

periods = Param( "Periods", 144, 5, 200, 1 );
TRH=Max(Ref(C,-1),H);
TRL=Min(Ref(C,-1),L);
TR=TRH-TRL;
WV=V+(Ref(V,-1)*0);
SmV= Wilders(WV,periods);

TMF= IIf(SmV==0,0,SmA/SmV);

//PositionSize=1500;

,EMA(C,5)>EMA(C,63) AND EMA(C,63)>Ref(EMA(C,63),-1)
AND Close>Open
//AND C>1 AND EMA(V,20)*EMA(C,20)>50000
AND AroonUp>70
//AND TMF>0
AND TSF(percentR(14),30)>-50
AND trendscore>5,0);

///////////////////////////Sell/////////////////////////////////

C<EMA(C,30)
//AND Close<Open
//AND EMA(percentR(14),30)<-50
//AND trendscore<-5
AND TMF<0 //OR (TMF>0 AND C<0.97*EMA(C,30))
AND AroonDn>70
,(EMA(C,5)<0.97*EMA(C,63))
//AND Close<Open
//AND EMA(percentR(14),30)<-50
//AND AroonDn>70
//AND TMF<0

//AND trendscore<-5
)
;

Sell = ExRem( Sell, Buy );

shape = Buy * shapeUpArrow + Sell * shapeDownArrow;

PlotShapes( shape, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, Low, High )
);
//GraphXSpace = 105;

///////////////////////////Filter/////////////////////////////////

Filter=C>5;
//EMA(V,20)*EMA(C,20)>50000;

AddColumn( C, "C", 1.2 );
AddColumn( V, "V", 1.2 );
AddColumn( EMA(V,20), "EMAV20", 1.2 );
AddColumn( EMA(V,20)*EMA(C,20), "V*C", 1.2 );

The combined code of the above two systems

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

n=Param("n",34,1,500,1);
A = Wilders( H,n );
B = Wilders( L,n );

SetPositionSize(1,spsShares);

x= Ref(A,-n);
y= Ref(B,-n);

p=Param("p",0.0009,0.0001,1,0.0001);
D=p;
xt=x+x*D;
xb=x-x*D;

yt=y+y*D;
yb=y-y*D;

SellCond1 = Cross(yt,Close);

_SECTION_END();

/////////////////Optimize////////////////////
x = Optimize("B", 20, 1, 30, 1 );
y = Optimize("S", 20, 1, 30, 1 );
z = Optimize("L", 30, 2, 100, 2 );

/////////////////PercentR////////////////////
function PercentR( periods )
{
return -100 * ( HHV( H, periods ) - C )/( HHV( H, periods ) - LLV( L, periods
) );
}

/////////////////TrendScore ////////////////////
TrendScore =
IIf(C>=Ref(C,-11),1,-1)+
IIf(C>=Ref(C,-12),1,-1)+
IIf(C>=Ref(C,-13),1,-1)+
IIf(C>=Ref(C,-14),1,-1)+
IIf(C>=Ref(C,-15),1,-1)+
IIf(C>=Ref(C,-16),1,-1)+
IIf(C>=Ref(C,-17),1,-1)+
IIf(C>=Ref(C,-18),1,-1)+
IIf(C>=Ref(C,-19),1,-1)+
IIf(C>=Ref(C,-20),1,-1);

/////////////////AROON////////////////////

Period = 14;
LLVBarsSince = LLVBars(L, Period) + 1;
HHVBarsSince = HHVBars(H, Period) + 1;

AroonDn = 100 * (Period - LLVBarsSince) / (Period - 1);
AroonUp = 100 * (Period - HHVBarsSince) / (Period - 1);

/////////////////Basic////////////////////

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g,
Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleLine);
_SECTION_END();

/////////////////Twiggs Money Flow////////////////////

periods = Param( "Periods", 144, 5, 200, 1 );
TRH=Max(Ref(C,-1),H);
TRL=Min(Ref(C,-1),L);
TR=TRH-TRL;
WV=V+(Ref(V,-1)*0);
SmV= Wilders(WV,periods);

TMF= IIf(SmV==0,0,SmA/SmV);

//PositionSize=1500;

,EMA(C,5)>EMA(C,63) AND EMA(C,63)>Ref(EMA(C,63),-1)
AND Close>Open
//AND C>1 AND EMA(V,20)*EMA(C,20)>50000
AND AroonUp>70
//AND TMF>0
AND TSF(percentR(14),30)>-50
AND trendscore>5,0);

///////////////////////////Sell/////////////////////////////////

C<EMA(C,30)
//AND Close<Open
//AND EMA(percentR(14),30)<-50
//AND trendscore<-5
AND TMF<0 //OR (TMF>0 AND C<0.97*EMA(C,30))
AND AroonDn>70
,(EMA(C,5)<0.97*EMA(C,63))
//AND Close<Open
//AND EMA(percentR(14),30)<-50
//AND AroonDn>70
//AND TMF<0

//AND trendscore<-5
)
;

///////////////////////////Filter/////////////////////////////////

Filter=C>5;
//EMA(V,20)*EMA(C,20)>50000;

AddColumn( C, "C", 1.2 );
AddColumn( V, "V", 1.2 );
AddColumn( EMA(V,20), "EMAV20", 1.2 );
AddColumn( EMA(V,20)*EMA(C,20), "V*C", 1.2 );
_SECTION_END();

Sell = ((SellCond1 AND SellCond2) OR (SellCond2 AND SellCond1));
Short=Sell;
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

#### boarders

##### Well-Known Member
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

n=Param("n",34,1,500,1);
A = Wilders( H,n );
B = Wilders( L,n );

SetPositionSize(1,spsShares);

x= Ref(A,-n);
y= Ref(B,-n);

p=Param("p",0.0009,0.0001,1,0.0001);
D=p;
xt=x+x*D;
xb=x-x*D;

yt=y+y*D;
yb=y-y*D;

SellCond1 = Cross(yt,Close);

_SECTION_END();

/////////////////Optimize////////////////////
x = Optimize("B", 20, 1, 30, 1 );
y = Optimize("S", 20, 1, 30, 1 );
z = Optimize("L", 30, 2, 100, 2 );

/////////////////PercentR////////////////////
function PercentR( periods )
{
return -100 * ( HHV( H, periods ) - C )/( HHV( H, periods ) - LLV( L, periods
) );
}

/////////////////TrendScore ////////////////////
TrendScore =
IIf(C>=Ref(C,-11),1,-1)+
IIf(C>=Ref(C,-12),1,-1)+
IIf(C>=Ref(C,-13),1,-1)+
IIf(C>=Ref(C,-14),1,-1)+
IIf(C>=Ref(C,-15),1,-1)+
IIf(C>=Ref(C,-16),1,-1)+
IIf(C>=Ref(C,-17),1,-1)+
IIf(C>=Ref(C,-18),1,-1)+
IIf(C>=Ref(C,-19),1,-1)+
IIf(C>=Ref(C,-20),1,-1);

/////////////////AROON////////////////////

Period = 14;
LLVBarsSince = LLVBars(L, Period) + 1;
HHVBarsSince = HHVBars(H, Period) + 1;

AroonDn = 100 * (Period - LLVBarsSince) / (Period - 1);
AroonUp = 100 * (Period - HHVBarsSince) / (Period - 1);

/////////////////Basic////////////////////

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g,
Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleLine);
_SECTION_END();

/////////////////Twiggs Money Flow////////////////////

periods = Param( "Periods", 144, 5, 200, 1 );
TRH=Max(Ref(C,-1),H);
TRL=Min(Ref(C,-1),L);
TR=TRH-TRL;
WV=V+(Ref(V,-1)*0);
SmV= Wilders(WV,periods);

TMF= IIf(SmV==0,0,SmA/SmV);

//PositionSize=1500;

,EMA(C,5)>EMA(C,63) AND EMA(C,63)>Ref(EMA(C,63),-1)
AND Close>Open
//AND C>1 AND EMA(V,20)*EMA(C,20)>50000
AND AroonUp>70
//AND TMF>0
AND TSF(percentR(14),30)>-50
AND trendscore>5,0);

///////////////////////////Sell/////////////////////////////////

C<EMA(C,30)
//AND Close<Open
//AND EMA(percentR(14),30)<-50
//AND trendscore<-5
AND TMF<0 //OR (TMF>0 AND C<0.97*EMA(C,30))
AND AroonDn>70
,(EMA(C,5)<0.97*EMA(C,63))
//AND Close<Open
//AND EMA(percentR(14),30)<-50
//AND AroonDn>70
//AND TMF<0

//AND trendscore<-5
)
;

///////////////////////////Filter/////////////////////////////////

Filter=C>5;
//EMA(V,20)*EMA(C,20)>50000;

AddColumn( C, "C", 1.2 );
AddColumn( V, "V", 1.2 );
AddColumn( EMA(V,20), "EMAV20", 1.2 );
AddColumn( EMA(V,20)*EMA(C,20), "V*C", 1.2 );
_SECTION_END();

Sell = ((SellCond1 AND SellCond2) OR (SellCond2 AND SellCond1) OR (shrt1 AND Sellcond2) OR (shrt2 AND Sellcond1) );
Short=Sell;
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

#### srichakra

##### Active Member
thanks a lot bro......
Does this work for Ft&O scrips too. Do you have any specific changes that needs to be done in the parameters for Futures?

#### JAYAKUMAR1975

##### Member
Any one add backtest above afl Advance thanks