EMA 4 Scale Out

{credit Preston Girard for much of this work.
This is not a recommendation. For example and education only.}

inputs:
EMA_zip(4),
EMA_fast(20),
EMA_medium(80),
EMA_slow(100),
Tradesize(1000),
profit1(.15),
profit2(.30);

vars:
EMAz(0),
EMAf(0),
EMAm(0),
EMAs(0),
originalSize(0);

EMAz = xaverage(c,EMA_zip);
EMAf = xaverage(c,EMA_fast);
EMAm = xaverage(c,EMA_medium);
EMAs = xaverage(c,EMA_slow);

if entryprice > 0 then originalsize = tradesize/Entryprice else originalsize = 1; {not sure if this is needed now?}
If C > EMAz and C > EMAs and C > EMAm and C > EMAf then begin
Buy Tradesize/C shares next bar h stop;
end;
If marketposition = 1 and C < EMAz then begin
Sell (originalsize * profit1) shares next bar l stop;
end;
If marketposition = 1 and C < EMAf then begin
Sell (originalsize * profit2) shares next bar l stop;
end;

If marketposition = 1 and C < EMAm then begin
Sell next bar l stop;
end;

{if C < emam then sellshort (tradesize/standarddev(c,200,0)) shares this bar close;
If marketposition = -1 and C > EMAz then begin
Buytocover (originalsize * profit1) shares next bar h stop;
end;
If marketposition = -1 and C > EMAf then begin
Buytocover (originalsize * profit2) shares next bar h stop;
end;
If marketposition = -1 and C > EMAm then begin
Buytocover next bar h stop;
end;}