Hi, I am trying to insert a Setforeign Filter to this code. With the current "GFC" {Global Fear of CoronasVirus} it is apparent that an emergency parachute is required. I have found some code that I had for a Gann trend system which works as a ribbon, what I have been unsuccessful in achieving is for this to set an immediate sell when the Foreign code, in this case XAO goes negative.
The code that I have inserted being:
//TRYING TO ESTABLISH A EMERGENCY FILTER
tmfrm = inWeekly;
_SECTION_BEGIN("SetForeign");
TimeFrameSet( tmfrm );
SetForeign( "XAO" );
GraphXSpace = 5;
_SECTION_BEGIN( "Gann Trend" );
Us = BarsSince(Sum(H>Ref(H,-1),2)==2);
Ds = BarsSince(Sum(L<Ref(L,-1),2)==2);
Hc = HighestSince(Us==0,H,1);
Lc = LowestSince(Ds==0,L,1);
Sd1 = IIf(Us==0,IIf((L!=Lc) AND (Ref(L,-1)!=Lc),1,0),IIf(Ds==0,IIf((H!=HC) AND (Ref(H,-1)!=Hc),-1,0),0));
Sd2 = IIf(Sd1==1, IIf(Ref(BarsSince(Sd1==1),-1) >Ref(BarsSince(Sd1==-1),-1),1,0), IIf(Sd1==-1,IIf(Ref(BarsSince(Sd1==1),-1) < Ref(BarsSince(Sd1==-1),-1),-1,0),0));
TD1 = ValueWhen(Sd2!=0,Sd2,1);
//{Gann-Trend 4/27/99}
Sd = TD1 ; //
//{Swing Change High}
Sch = IIf(Sd==1 AND Ref(sd,-1)==-1,1,0);
//{Swing Change Low}
Scl = IIf(Sd==-1 AND Ref(Sd,-1)==1,1,0);
//{Peak Value}
Pv = IIf(Scl==1,HighestSince(Sch==1,H,1),0);
//{Trough Value}
Tv = IIf(Sch==1,LowestSince(Scl==1,L,1),0);
//{Trend Direction}
Td = IIf(H>ValueWhen(Pv>0,Pv,1),1,IIf(L<ValueWhen(Tv>0,Tv,1),-1,0));
//{UpTrend=1 DownTrend =-1}
Tdv=ValueWhen(Td!=0,Td,1); //value when TD is not equal to zero
Plot( 1, "", IIf(tdv == 1,colorDarkGreen, IIf(tdv < 1, colorDarkRed, 0)), styleOwnScale|styleArea|styleNoLabel, -0.1, 50 );
// This is the plot bar on bottom of screen, "GREEN" is BUY, "RED" is SELL
//== Equal to
//!= Not equal to
RestorePriceArrays();
TimeFrameRestore();
_SECTION_END();
For clarity the full AFL is
//TRYING TO ESTABLISH A EMERGENCY FILTER
tmfrm = inWeekly;
_SECTION_BEGIN("SetForeign");
TimeFrameSet( tmfrm );
SetForeign( "XAO" );
GraphXSpace = 5;
_SECTION_BEGIN( "Gann Trend" );
Us = BarsSince(Sum(H>Ref(H,-1),2)==2);
Ds = BarsSince(Sum(L<Ref(L,-1),2)==2);
Hc = HighestSince(Us==0,H,1);
Lc = LowestSince(Ds==0,L,1);
Sd1 = IIf(Us==0,IIf((L!=Lc) AND (Ref(L,-1)!=Lc),1,0),IIf(Ds==0,IIf((H!=HC) AND (Ref(H,-1)!=Hc),-1,0),0));
Sd2 = IIf(Sd1==1, IIf(Ref(BarsSince(Sd1==1),-1) >Ref(BarsSince(Sd1==-1),-1),1,0), IIf(Sd1==-1,IIf(Ref(BarsSince(Sd1==1),-1) < Ref(BarsSince(Sd1==-1),-1),-1,0),0));
TD1 = ValueWhen(Sd2!=0,Sd2,1);
//{Gann-Trend 4/27/99}
Sd = TD1 ; //
//{Swing Change High}
Sch = IIf(Sd==1 AND Ref(sd,-1)==-1,1,0);
//{Swing Change Low}
Scl = IIf(Sd==-1 AND Ref(Sd,-1)==1,1,0);
//{Peak Value}
Pv = IIf(Scl==1,HighestSince(Sch==1,H,1),0);
//{Trough Value}
Tv = IIf(Sch==1,LowestSince(Scl==1,L,1),0);
//{Trend Direction}
Td = IIf(H>ValueWhen(Pv>0,Pv,1),1,IIf(L<ValueWhen(Tv>0,Tv,1),-1,0));
//{UpTrend=1 DownTrend =-1}
Tdv=ValueWhen(Td!=0,Td,1); //value when TD is not equal to zero
Plot( 1, "", IIf(tdv == 1,colorDarkGreen, IIf(tdv < 1, colorDarkRed, 0)), styleOwnScale|styleArea|styleNoLabel, -0.1, 50 );
// This is the plot bar on bottom of screen, "GREEN" is BUY, "RED" is SELL
//== Equal to
//!= Not equal to
RestorePriceArrays();
TimeFrameRestore();
_SECTION_END();
// possible fix version 1.0
// https://forum.amibroker.com/t/debug-code-unexpected-result/15628/7
//Settings
SetOption("InitialEquity",300000);
SetOption("CommissionMode",2);/* set commissions AND costs as $ per trade*/
SetOption("CommissionAmount",9.50); /* commissions AND cost */
SetTradeDelays( 0, 0, 0, 0); // we use Ref() instead
SetBacktestMode( backtestRegular );
Totalpositions = 20;
SetOption("MaxOpenPositions", Totalpositions );
PositionSize = -100 / Totalpositions;
new_month = Month() != Ref(Month(), -1);
FallingN = Optimize("Falling",3,2,5,1);
n = -FallingN;
EMA10opt = Optimize("OM_10",11,2,11,1);
EMA50OPT = Optimize("OM-50",18,12,23,1);
EMA150OPT = Optimize("OM_150",24,24,34,1);
tmfrm = inMonthly;
TimeFrameSet( tmfrm );
EMA10 = EMA(C,EMA10opt);
EMA50 = EMA(C,EMA50opt);
EMA150 = EMA(C,EMA150opt);
EMACondition = Round( EMA10 * 100 ) > Round( Ref( EMA10, n ) * 100 ) AND
Round( EMA50 * 100 ) > Round( Ref( EMA50, n ) * 100 ) AND
Round( EMA150 * 100 ) > Round( Ref( EMA150, n ) * 100 );
//This is filter to try and filter out low volume stocks
VolA = Volume * C;
MAVol = MA( VolA, 90 );
TimeFrameRestore();
expandmode = expandLast;
EMA10 = TimeFrameExpand( EMA10, tmfrm, expandmode );
EMA50 = TimeFrameExpand( EMA50, tmfrm, expandmode );
EMA150 = TimeFrameExpand( EMA150, tmfrm, expandmode );
EMACondition = TimeFrameExpand( EMACondition, tmfrm, expandmode );
MAVol = TimeFrameExpand( MAVol, tmfrm, expandmode );
MAVol = TimeFrameExpand( VolA, tmfrm, expandmode );
price = TimeFrameGetPrice("C", tmfrm, 0, expandmode);
ED2 = 1251231; // Long end date for symbols
dn = DateNum();
switch( Name() ) {
case "CSL":
enddate1 = 1110217;
Reentry = 1110614;
enddate2 = 1160818;
enddate3 = ED2;
Reentry2 = 1170413;
datecond = ( dn >= 1060824 AND dn < enddate1 ) OR ( dn >= reentry AND dn < enddate2 ) OR ( dn >= reentry2 AND dn < enddate3 );
exitdatecond1 = dn >= enddate1 AND dn < reentry;
exitdatecond2 = dn >= enddate2 AND dn < reentry2 ;
exitdatecond3 = dn >= enddate3; exitdatecond = exitdatecond1 OR exitdatecond2 OR exitdatecond3 ;
break;
default:
datecond = true;
exitdatecond = false;
break;
}
BuyPrice = Open;
SellPrice = Open;
Buy = (price>EMA10) AND (price>EMA50) AND (price>EMA150) AND datecond AND MAVol >500000 AND EMACondition;
Buy = Ref(Buy, -1) AND new_month;
Sell = EMA50> price;
Sell = (new_month AND Ref(Sell,-1)) OR Ref(exitdatecond,-1);
Sell = tdv<1;
Short = Cover = 0;
Filter = new_month OR exitdatecond; // show start of month only
AddColumn(price,"Close", 1.2);
AddColumn(EMA10,"EMA10",1.4);
AddColumn(EMA50,"EMA50",1.4);
AddColumn(EMA150,"EMA150",1.4);
AddColumn(MAVol, "90day$MAVOL",1.0);
AddColumn(VolA, "$VOL",1.0);
AddColumn( IIf( Buy, 66, 83 ), "Signal", formatChar );
AddColumn( Buy, "Buy", 1 );
AddColumn( Sell, "Sell", 1 );
//buy = ExRem( buy, sell );
//sell = ExRem( sell, buy );
PlotShapes( Buy*shapeUpArrow, colorGreen, 0, Low );
PlotShapes( Sell*shapeDownArrow, colorRed, 0, High );
SetCustomBacktestProc("");
if( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject();
bo.Backtest();
AddToComposite( bo.EquityArray,
"~~~MY_EQUITY_SDT", "X",
atcFlagDeleteValues | atcFlagEnableInPortfolio );
}
_SECTION_BEGIN("Price1");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - " + FullName() + " - {{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", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
Plot(EMA10, "EMA_SHORT", ColorRed, styleNoTitle);
Plot(EMA50, "EMA_MEDIUM", ColorGreen, styleNoTitle);
Plot(EMA150, "EMA_LONG", ColorYellow, styleNoTitle);
PlotShapes( Buy*shapeUpArrow, colorGreen, 0, Low );
PlotShapes( Sell*shapeDownArrow, colorRed, 0, High );
_SECTION_END();
In the above code I have entered at row 139 the following:
Sell = tdv<1;
Can someone identify where I am going wrong. Thanks