// Downloaded From https://www.WiseStockTrader.com
_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 ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

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



SetTradeDelays(0, 0, 0, 0); 
BuyPrice = Close; 
SellPrice = Close; 

previousClose = Ref(Close, -1); 
rsi2 = RSI(2); 

Sell = Cross(rsi2, 70); 

bull100 = Close > EMA(Close, 200) AND Sum(rsi2 < 25, 2) == 2; 
inFirstPos = Flip(bull100, Sell); 

bull200 = Close < previousClose AND inFirstPos AND Ref(inFirstPos, -1); 
inSecondPos = Flip(bull200, Sell); 

bull300 = Close < previousClose AND inSecondPos AND Ref(inSecondPos, -1); 
inThirdPos = Flip(bull300, Sell); 

bull400 = Close < previousClose AND inThirdPos AND Ref(inThirdPos, -1); 
inFourthPos = Flip(bull400, Sell); 

firstTrigger = ExRem(inFirstPos, Sell); 
secondTrigger = ExRem(inSecondPos, Sell); 
thirdTrigger = ExRem(inThirdPos, Sell); 
fourthTrigger = ExRem(inFourthPos, Sell); 

Buy = firstTrigger + (secondTrigger * sigScaleIn) + (thirdTrigger * sigScaleIn) + (fourthTrigger * sigScaleIn); 
SetPositionSize(IIf(firstTrigger, 100, IIf(secondTrigger, 200, IIf(thirdTrigger, 300, 400))), spsShares); 


Buy= ExRem(Buy,Sell); Sell=ExRem(Sell,Buy); 
//Short=ExRem(Short,Cover); Cover=ExRem(Cover,Short);

priceColors = IIf(Close > previousClose, colorDarkGreen, IIf(Close < previousClose, colorDarkRed, colorDarkGrey)); 
buyColors = IIf(firstTrigger, colorGreen, IIf(secondTrigger, colorLime, IIf(thirdTrigger, colorYellow, colorOrange))); 

Plot(Close, "Price", priceColors, styleBar); 
PlotShapes((Buy > 0) * shapeUpArrow, buyColors, 0, L, -10);