// Downloaded From https://www.WiseStockTrader.com
// ATR/Turtle Script by Andrew Senft
//
// Backtester Options
SetOption("AllowSameBarExit", False);
SetOption("MaxOpenPositions", 4);
PositionSize =  - 25;

// Optimization numbers
ATRPeriod = Optimize("ATRPeriod", 15, 10, 50, 5);
BuyThreshold = Optimize("BuyThreshold", .3, .1, .50, .05);
BuySlope = Optimize("BuySlope", 1.08, 1.05, 1.10, .01);
SellThreshold = Optimize("SellThreshold", .15, .1, .50, .05);
ProfitPercent = Optimize("ProfitPercent", 25, 25, 35, 5);

// Buy/Sell signals and prices
Buy = Ref(ATR(ATRPeriod),  - 1) < BuyThreshold AND 
	Ref(Close,  - 1) > Ref(Close,  - 1 * ATRPeriod / 2) AND 
	Ref(Close,  - 1 * ATRPeriod / 2) > Ref(Close,  - 1 * ATRPeriod) AND 
	Ref(Close,  - 1) / Ref(Close,  - 1 * ATRPeriod) > BuySlope AND 
	Ref(MA(Volume, 20),  - 1) > 3000 AND// 300,000 volume or more
  	Ref(Close,  - 1) >= 2; // stocks over $2BuyPrice = Open;
  
BeginATR = Ref(ValueWhen(Buy, ATR(ATRPeriod), 1),  - 1);
FilterBeginATR = ValueWhen(Buy, ATR(ATRPeriod), 1);
Sell = Ref(ATR(ATRPeriod),  - 1) > BeginATR + SellThreshold;
SellPrice = Open;
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

PositionScore = 100-Ref(ATR(ATRPeriod),  - 1);
ApplyStop(stopTypeProfit, stopModePercent, ProfitPercent, True, 0);