// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("10 DMA system");
// Long Set up:  Close above 10 Day (default) Simple Moving average  (default)  
// Long Entry:  Close above the previous candle (which is above 10 Day SMA) 
// Long Exit: Short Entry     
// Short Set up:  Close below 10 Day Simple Moving average  
// Short Entry:  Close below the previous candle (which is above 10 Day SMA) 
// Short Exit:  Long Entry     

//SetTradeDelays(0,0,0,0);
//SetOption("CommissionMode",3);
//SetOption("CommissionAmount",2.01);
//SetOption("FuturesMode",True);
//NumContracts=1;
//PositionSize=NumContracts*MarginDeposit;
//SetOption("MaxOpenPositions",4);

procedure exit_proc(Buy,BuyPrice,Short,ShortPrice)
{
global BuyAdjusted;
global BuyPriceAdjusted;
global ShortAdjusted;
global ShortPriceAdjusted;
global SellAdjusted;
global SellPriceAdjusted;
global CoverAdjusted;
global CoverPriceAdjusted;
global LongStopTrail;
global ShortStopTrail;

BuyAdjusted=0;
BuyPriceAdjusted=0;
ShortAdjusted=0;
ShortPriceAdjusted=0;
SellAdjusted=0;
SellPriceAdjusted=0;
CoverAdjusted=0;
CoverPriceAdjusted=0;
LongStopTrail=Null;
ShortStopTrail=Null;
delay=1;

for(i=1;i<BarCount;i++) 
{
	if(Buy[i]) 
	{
		BuyAdjusted[i]=1;
		BuyPriceAdjusted[i]=BuyPrice[i];	
		for(j=i+delay;j<BarCount;j++) 
		{
			if(Short[j])
			{
				SellAdjusted[j]=1;
				SellPriceAdjusted[j]=C[j];
				i=j-1;
				break;
			}
			else if(j==BarCount-1) 
			{
				i=BarCount;
				break;				
			}			
		}
	}
	else if(Short[i]) 
	{
		ShortAdjusted[i]=1;
		ShortPriceAdjusted[i]=ShortPrice[i];	
		for(j=i+delay;j<BarCount;j++) 
		{
			if(Buy[j])
			{
				CoverAdjusted[j]=1;
				CoverPriceAdjusted[j]=C[j];
				i=j-1;
				break;
			}
			else if(j==BarCount-1) 
			{
				i=BarCount;
				break;				
			}				
		}
	}

}

}
per=Optimize("per",10,2,100,1);
sma=MA(C,per);

triggerLong=ValueWhen(Cross(C,sma),H);
Buy=Cross(C,triggerLong);BuyPrice=Close;
triggerShort=ValueWhen(Cross(sma,C),L);
Short=Cross(triggerShort,C);ShortPrice=C;

exit_proc(Buy,BuyPrice,Short,ShortPrice);
Buy=BuyAdjusted;
BuyPrice=BuyPriceAdjusted;
Short=ShortAdjusted;
ShortPrice=ShortPriceAdjusted;
Sell=SellAdjusted;
SellPrice=SellPriceAdjusted;
Cover=CoverAdjusted;
CoverPrice=CoverPriceAdjusted;

SetChartOptions(0,chartShowDates);
SetBarFillColor(IIf(C>O,ColorRGB(0,75,0),IIf(C<=O,ColorRGB(75,0,0),colorLightGrey)));
Plot(C,"\nPrice",IIf(C>O,ColorRGB(0,255,0),IIf(C<=O,ColorRGB(255,0,0),colorLightGrey)),64,0,0,0,0);
Plot(sma,"\nSMA",colorBlue,1);
Plot(triggerLong,"",colorGreen,styleDashed);
Plot(triggerShort,"",colorRed,styleDashed);

PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorDarkGreen,0,L,-15);
PlotShapes(IIf(Buy,shapeSmallCircle,shapeNone),colorWhite,0,BuyPrice,0);
PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,0,H,-15);
PlotShapes(IIf(Sell,shapeSmallCircle,shapeNone),colorWhite,0,SellPrice,0);
PlotShapes(IIf(Short,shapeSmallDownTriangle,shapeNone),colorRed,0,H,IIf(Short AND Sell,-30,-15));
PlotShapes(IIf(Short,shapeSmallCircle,shapeNone),colorWhite,0,ShortPrice,0);
PlotShapes(IIf(Cover,shapeSmallUpTriangle,shapeNone),colorDarkGreen,0,L,IIf(Cover AND Buy,-30,-15));
PlotShapes(IIf(Cover,shapeSmallCircle,shapeNone),colorWhite,0,CoverPrice,0);
_SECTION_END();