SECTION_BEGIN("Mean Reversion Trading System");
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 ) ) ));
SetTradeDelays( 1, 1, 1, 1 );
SetOption( "InitialEquity", 200000);
SetOption("FuturesMode" ,True);
SetOption("MinShares",1);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",100);
SetOption("AccountMargin",10);
SetOption("RefreshWhenCompleted",True);
SetPositionSize(150,spsShares);
SetOption( "AllowPositionShrinking", True );
BuyPrice=Open;
SellPrice=Open;
ShortPrice=Open;
CoverPrice=Open;
Periods =optimize("Periods",110,10,200,20);
Width =optimize("width",2,1,10,1);
MAPeriods = 200;
BBColor = colorOrange;
Style = 0;
bTop=BBandTop( Close, Periods, Width );
bBottom=BBandBot( Close, Periods, Width );
ADXPeriods=optimize("ADXPeriods",10,2,20,1);
BuyCondition=IIf(Ref(C,-1)<bBottom AND close>bBottom AND ADX(ADXPeriods)<35,1,0);
ShortCondition=IIf(Ref(C,-1)>bTop AND close<bTop AND ADX(ADXPeriods)<35,1,0);
Buy = IIf(Ref(C,-1)<bBottom AND close>bBottom AND ADX(ADXPeriods)<35,1,0);
Short = IIf(Ref(C,-1)>bTop AND close<bTop AND ADX(ADXPeriods)<35,1,0);
BarsSinceBuy=BarsSince(BuyCondition);
BarsSinceShort=BarsSince(ShortCondition);
LastBuyPrice=ValueWhen(Buy AND BarsSinceBuy<BarsSinceShort,BuyPrice);
LastShortPrice=ValueWhen(Short AND BarsSinceShort<BarsSinceBuy,ShortPrice);
StopLoss=optimize("SL",0.5,0.2,1,0.2);
Target=optimize("Target",1,0.5,5,0.5);
BuySL=BarsSinceBuy<BarsSinceShort AND Close<=LastBuyPrice-LastBuyPrice*(Stoploss/100);
ShortSL=BarsSinceShort<BarsSinceBuy AND Close>=LastShortPrice+LastBuyPrice*(Stoploss/100);
BuyTarget=BarsSinceBuy<BarsSinceShort AND Close>=LastBuyPrice+LastBuyPrice*(Target/100);
ShortSL=BarsSinceShort<BarsSinceBuy AND Close<=LastShortPrice-LastBuyPrice*(Target/100);
Sell=Short OR Close<bBottom OR BuySL OR BuyTarget;
Cover=Buy OR Close>bTop OR ShortSL OR BuyTarget;
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);
ApplyStop(Type=0,Mode=1,Amount=StopLoss);
ApplyStop(Type=1,Mode=1,Amount=Target);
printf("\nBuy : " + Buy );
printf("\nShort : " + Short );
printf("\nCover : " + Cover );
printf("\nSell : " + Sell );
printf("\nBuyPrice : " + BuyPrice );
printf("\nShortPrice : " + ShortPrice );
printf("\nLastBuyPrice : "+ LastBuyPrice);
printf("\nLastShortPrice : "+ LastShortPrice);
printf("\nBuyCondition : "+ BuyCondition);
printf("\nShortCondition : "+ ShortCondition);
printf("\nBuySL : "+BuySL);
printf("\nShortSL : "+ShortSL);
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
Plot( bTop, "BBTop" + _PARAM_VALUES(), BBColor , Style );
Plot( bBottom, "BBBot" + _PARAM_VALUES(), BBColor , Style );
/* Plot Buy and Sell Signal Arrows */
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(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Cover, 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);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
_SECTION_END();
_SECTION_BEGIN("Yasu_S&R");
SetChartOptions(0,chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g,Hi %g,Lo %g, Close %g (%.1f%%)
Vol " +WriteVal( V*100, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
m = Param("Periods", 20, 2,200, 1 );
n = Param("No.", 3, 2,20, 1 );
A1=ExRemSpan(Ref(H,-1)==HHV(H,m),n);
A2=ExRemSpan(Ref(L,-1)==LLV(L,m),n);
A3=Cross(A1,0.9);
A4=Cross(A2,0.9);
TOP=Ref(H,-BarsSince(A3));
bot=Ref(L,-BarsSince(A4));
Plot(top,"Top",colorRed,styleDots+styleNoLine);
Plot(bot,"Bottom",colorBrightGreen,styleDots+styleNoLine);
_SECTION_END();
_SECTION_BEGIN("Lintra Indicator");
SetBarsRequired( sbrAll );
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));
//Initial Parameters
SetTradeDelays( 1,1,1, 1 );
SetOption( "InitialEquity", 200000);
SetOption("FuturesMode" ,True);
SetOption("MinShares",1);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",100);
SetOption("AccountMargin",10);
SetOption("RefreshWhenCompleted",True);
SetPositionSize(150,spsShares);
SetOption( "AllowPositionShrinking", True );
NH = Param("LINTRA Gann Hi-Lo ",25,5,400,5);
NL = NH;//Param("McGinley Dynamic NL ",25,5,400,5);
MDH[0] = H[0];
MDL[0] = L[0];
for( i = 1; i < BarCount; i++ )
{
MDH[ i ] = MDH[ i - 1 ] + (H[i]-MDH[i-1])/( NH*(H[i] / MDH[i-1])^4) ;
MDL[ i ] = MDL[ i - 1 ] + (L[i]-MDL[i-1])/( NL*(L[i] / MDL[i-1])^4) ;
}
Plot( Close, "Price",colorWhite, styleCandle );
Plot(MDH,"McGinley Dynamic",colorYellow,1|styleDashed);
Plot(MDL,"McGinley Dynamic",colorYellow,1|styleDashed);
Buy=C>MDH AND C>Ref(C,-1) AND Ref(C,-1)>Ref(C,-2);
Short=C<MDL AND C<Ref(C,-1) AND Ref(C,-1)<Ref(C,-2);;
Sell=Short;
Cover=Buy;
BuyPrice=Open;
SellPrice=Open;
ShortPrice=Open;
CoverPrice=Open;
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);
printf("\nBuy : " + Buy );
printf("\nSell : " + Sell );
printf("\nShort : " + Short );
printf("\nCover : " + Cover );
StopLoss=Param("SL",10,1,10,1);
Target=Param("Target",40,5,40,5);
ApplyStop(Type=0,Mode=1,Amount=StopLoss);
ApplyStop(Type=1,Mode=1,Amount=Target);
/* Plot Buy and Sell Signal Arrows */
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(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Cover, 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);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
_SECTION_END();
_SECTION_BEGIN("NR7 Trading");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));
//Initial Parameters
SetTradeDelays( 0,0,0, 0 );
SetOption( "InitialEquity", 200000);
SetOption("FuturesMode" ,True);
SetOption("MinShares",1);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",100);
SetOption("AccountMargin",10);
SetOption("RefreshWhenCompleted",True);
SetPositionSize(50,spsPercentOfEquity);
//SetPositionSize(150,spsShares);
SetOption( "AllowPositionShrinking", True );
Plot( Close, "Price", colorWhite, styleCandle );
//==================================================================================
//NR7 RANGE IDENTIFICATION
range = H-L;
Condition0 = range<Ref(range,-1) AND range<Ref(range,-2) AND range<Ref(range,-3)AND range<Ref(range,-4)AND range<Ref(range,-5)AND range<Ref(range,-6);
NR7 = IIf(Condition0,True, False);
printf("\nNR7 : " + NR7 );
BuyPrice=Open;
SellPrice=Open;
ShortPrice=Open;
CoverPrice=Open;
TGT= Param("Target Percent",4,1,20,1);
SL= Param("SL Percent",1,1,5,1);
Buy=Ref(NR7,-1) AND GapUp();
Short=Ref(NR7,-1) AND GapDown();
BuyTgt=ValueWhen(Buy,BuyPrice,1)*(1+TGT/100);
BuySL=ValueWhen(Buy,BuyPrice,1)*(1-SL/100);
Sell= H>=BuyTgt OR L<=BuySL;
ShortTgt=ValueWhen(Short,ShortPrice,1)*(1-TGT/100);
ShortSL=ValueWhen(Short,ShortPrice,1)*(1+SL/100);
Cover= L<=ShortTgt OR H>=ShortSL;
SellPrice = IIf(Sell, IIf(H>=BuyTgt, BuyTgt, BuySL), Null);
CoverPrice = IIf(Cover, IIf(L<=ShortTgt, ShortTgt, ShortSL), Null);
Buy = ExRem(Buy,Sell);
Sell =ExRem(Sell,Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);
printf("\nBuy : " + Buy );
printf("\nSell : " + Sell );
printf("\nShort : " + Short );
printf("\nCover : " + Cover );
printf("\nBuyPrice : " + BuyPrice );
printf("\nShortPrice : " + ShortPrice );
printf("\nSellPrice : " + SellPrice );
printf("\nCoverPrice : " + CoverPrice );
printf("\nBuyTgt : " + BuyTgt );
printf("\nBuySL : " + BuySL );
printf("\nShortTgt : " + ShortTgt );
printf("\nShortSL : " + ShortSL );
/* Plot Buy and Sell Signal Arrows */
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(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(IIf(Sell, shapeStar, shapeNone),colorWhite, 0, H, Offset=25);
PlotShapes(IIf(Cover, shapeStar, shapeNone),colorWhite, 0,L, Offset=-25);
_SECTION_END();
_SECTION_BEGIN("back up of ami file");
Filter=1;
AddColumn(O,"Open");
AddColumn(H,"High");
AddColumn(L,"LOW");
AddColumn(C,"Close");
AddColumn(V,"Volume");
_SECTION_END();
// Set the Starting and End Dates
// Make sure these dates are not holidays.
StartDate = ParamDate("Starting Date", "31-12-2014");
EndDate = ParamDate("Start Date", "31-12-2015");
//Find the corresponding Bar Numbers
StartBar = Max(0, LastValue(ValueWhen(DateNum() == StartDate, BarIndex(),1)));
EndBar = Min(BarCount - 1, LastValue(ValueWhen(DateNum() == EndDate, BarIndex(),1)));
Filter = 1; // This allows all required data to be included
// Before running the AFL Make sure that C:\SaveData directory exists
fh = fopen( "c:\\SaveData\\"+Name()+".csv", "w");
if( fh )
{
fputs( "Ticker,Date,Open,High,Low,Close,Volume \n", fh );
y = Year();
m = Month();
d = Day();
//r = Hour();
//e = Minute();
//n = Second();
for( i = StartBar; i <= EndBar; i++ )
{
fputs( Name() + "," , fh );
ds = StrFormat("%02.0f-%02.0f-%02.0f,",
y[ i ], m[ i ], d[ i ] );
fputs( ds, fh );
//ts = StrFormat("%02.0f:%02.0f:%02.0f,",
//r[ i ],e[ i ],n[ i ] );
//fputs( ts, fh );
qs = StrFormat("%.4f,%.4f,%.4f,%.4f,%.0f\n",
O[ i ],H[ i ],L[ i ],C[ i ],V[ i ] );
fputs( qs, fh );
}
fclose( fh );
}
// The following lines are redundant but are required for the Exploration to work.
// These lines will just output the data being written to the file.
SetOption("NoDefaultColumns", True);
AddTextColumn(Name(), "Symbol", 77);
AddColumn(DateTime(), "Date", formatDateTime);
AddColumn(O, "Open", 6.2);
AddColumn(H, "High", 6.2);
AddColumn(L, "Low", 6.2);
AddColumn(C, "Colse", 6.2);
AddColumn(V, "Volume", 10.0);
_SECTION_BEGIN("back up of ami file");
Filter=1;
AddColumn(O,"Open");
AddColumn(H,"High");
AddColumn(L,"LOW");
AddColumn(C,"Close");
AddColumn(V,"Volume");
_SECTION_END();
// Set the Starting and End Dates
// Make sure these dates are not holidays.
StartDate = ParamDate("Starting Date", "31-12-2014");
EndDate = ParamDate("Start Date", "31-12-2015");
//Find the corresponding Bar Numbers
StartBar = Max(0, LastValue(ValueWhen(DateNum() == StartDate, BarIndex(),1)));
EndBar = Min(BarCount - 1, LastValue(ValueWhen(DateNum() == EndDate, BarIndex(),1)));
Filter = 1; // This allows all required data to be included
// Before running the AFL Make sure that C:\SaveData directory exists
fh = fopen( "c:\\SaveData\\"+Name()+".csv", "w");
if( fh )
{
fputs( "Ticker,Date,Open,High,Low,Close,Volume \n", fh );
y = Year();
m = Month();
d = Day();
//r = Hour();
//e = Minute();
//n = Second();
for( i = StartBar; i <= EndBar; i++ )
{
fputs( Name() + "," , fh );
ds = StrFormat("%02.0f-%02.0f-%02.0f,",
y[ i ], m[ i ], d[ i ] );
fputs( ds, fh );
//ts = StrFormat("%02.0f:%02.0f:%02.0f,",
//r[ i ],e[ i ],n[ i ] );
//fputs( ts, fh );
qs = StrFormat("%.4f,%.4f,%.4f,%.4f,%.0f\n",
O[ i ],H[ i ],L[ i ],C[ i ],V[ i ] );
fputs( qs, fh );
}
fclose( fh );
}
// The following lines are redundant but are required for the Exploration to work.
// These lines will just output the data being written to the file.
SetOption("NoDefaultColumns", True);
AddTextColumn(Name(), "Symbol", 77);
AddColumn(DateTime(), "Date", formatDateTime);
AddColumn(O, "Open", 6.2);
AddColumn(H, "High", 6.2);
AddColumn(L, "Low", 6.2);
AddColumn(C, "Colse", 6.2);
AddColumn(V, "Volume", 10.0);
fh = fopen( "c:\\AmiBackup\\"+Name()+".csv", "w");
if( fh )
{
fputs( "Ticker,Date,Open,High,Low,Close,Volume \n", fh );
y = Year();
m = Month();
d = Day();
//r = Hour();
//e = Minute();
//n = Second();
for( i = 0; i < BarCount; i++ )
{
fputs( Name() + "," , fh );
ds = StrFormat("%02.0f-%02.0f-%02.0f,",
y[ i ], m[ i ], d[ i ] );
fputs( ds, fh );
//ts = StrFormat("%02.0f:%02.0f:%02.0f,",
//r[ i ],e[ i ],n[ i ] );
//fputs( ts, fh );
qs = StrFormat("%.4f,%.4f,%.4f,%.4f,%.0f\n",
O[ i ],H[ i ],L[ i ],C[ i ],V[ i ] );
fputs( qs, fh );
}
fclose( fh );
}
Buy = 0;
_SECTION_BEGIN("ECG+");
Z=Param("zig",0.1,0,10,0.01);
Plot( z1 = Zig(C,z), "Zig-zag Indicator" + _PARAM_VALUES(), ParamColor("Zig-zag color", colorOrange ), ParamStyle("Zig-zag style") );
Buy = Cover = Zig(C,z)>Ref(Zig(C,z),-1) AND Ref(Zig(C,z),-2)>Ref(Zig(C,z),-1);
Sell = Short = Zig(C,z)<Ref(Zig(C,z),-1) AND Ref(Zig(C,z),-2)<Ref(Zig(C,z),-1);
Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);
Filter = Buy OR Sell;
PlotShapes(IIf(Buy, shapeSmallCircle, shapeNone),colorBrightGreen, 0,BuyPrice, 0);
PlotShapes(IIf(Sell, shapeSmallCircle, shapeNone),colorRed, 0,SellPrice, 0);
dist = 1.5*ATR(10);
for( i = 0; i < BarCount; i++ )
{
if( Buy[i] ) PlotText( "BUY\n"+C[ i ], i, BuyPrice[i]-dist[i],colorWhite, colorGreen );
if( Sell[i] ) PlotText( "SELL\n"+C[ i ], i, SellPrice[i]+dist[i],colorWhite, colorRed );
}