input fixedSharesRange = 1000;
input timeFilterHour = 645;
input minPctDifferenceChoice = {default "0.0005", "0.005", "0.05"};
input currentRangeChoice = {default "3", "6", "8"};
input lookbackPeriodChoice = {default "3", "6", "8"};
input lowChoice = {default "Low", "Body Low"};
input maxSpreadChoice = {default "0.03", "0.05", "0.07", "0.10", "0.20", "0.50"};
def stockPrice = close;
def averageType = AverageType.WILDERS;
def allowMultiplePositions = no;
def openQuantity = GetQuantity();
def hasOpenPosition = openQuantity > 0;
def minavgvolume = 5000;
def minPctDifference;
def currentRange;
def maxSpread;
def lookbackPeriod;
def Active = SecondsFromTime(0645) >=0 and SecondsTillTime(1600) > 0;
switch (minPctDifferenceChoice) {
case "0.0005":
minPctDifference = 0.0005;
case "0.005":
minPctDifference = 0.005;
case "0.05":
minPctDifference = 0.05;
}
switch (currentRangeChoice) {
case "3":
currentRange = Average(high - low, 3);
case "6":
currentRange = Average(high - low, 6);
case "8":
currentRange = Average(high - low, 8);
}
switch (maxSpreadChoice) {
case "0.03":
maxSpread = 0.03;
case "0.05":
maxSpread = 0.05;
case "0.07":
maxSpread = 0.07;
case "0.10":
maxSpread = 0.10;
case "0.20":
maxSpread = 0.20;
case "0.50":
maxSpread = 0.50;
}
switch (lookbackPeriodChoice) {
case "3":
lookbackPeriod = 3;
case "6":
lookbackPeriod = 6;
case "8":
lookbackPeriod = 8;
}
# Parameters changed less frequently
def atrPeriod = 3; # Not used
def volumeAvgPeriod = 3;
# Bid, Ask, and Spread
def bid = close(priceType = PriceType.BID);
def ask = close(priceType = PriceType.ASK);
def spread = ask - bid;
def spreadCondition = spread <= maxSpread;
# Double Bottom and Volume Check
def doubleBottom = low >= Lowest(low, lookbackPeriod) and (low - Lowest(low, lookbackPeriod)) / Lowest(low, lookbackPeriod) <= minPctDifference;
def volumeCheck = Average(volume, volumeAvgPeriod) >= minavgvolume;
# Time Filter
def timeFilter = SecondsFromTime(timeFilterHour) >= 0;
def atr = MovingAverage(AverageType.WILDERS, TrueRange(high, close, low), atrPeriod);
# Hard Stop Loss Price
def selectedLow;
switch (lowChoice) {
case "Low":
selectedLow = low;
case "Body Low":
selectedLow = Min(open, close);
}
def doubleBottomLow = Lowest(selectedLow, lookbackPeriod);
# Buy Signal
# Sell Signal
# Entry Price, Target Price, and Bars Since Entry
def buySignal;
def sellSignal;
def entryPrice;
def hardStopLossPrice;
def targetPrice;
if !buySignal[1] and doubleBottom and volumeCheck and timeFilter and spreadCondition and Active{
buySignal = 1;
sellSignal = 0;
entryPrice = close;
hardStopLossPrice = doubleBottomLow;
targetPrice = entryPrice + currentRange;
}
else if buySignal[1] and (low <= hardStopLossPrice[1] or high >= targetPrice[1] or !Active) {
buySignal = 0;
sellSignal = 1;
entryPrice = double.nan;
hardStopLossPrice = double.nan;
targetPrice = double.nan;
}
else {
buySignal = buySignal[1];
sellSignal = sellSignal[1];
entryPrice = entryPrice[1];
hardStopLossPrice = hardStopLossPrice[1];
targetPrice = targetPrice[1];
}
AddOrder(OrderType.BUY_TO_OPEN, buySignal, open[-1], fixedSharesRange, Color.GREEN, Color.GREEN);
AddOrder(OrderType.SELL_TO_CLOSE, sellSignal, open[-1], fixedSharesRange, Color.RED, Color.RED);
plot target = if targetPrice then targetPrice else Double.NaN;
target.SetDefaultColor(Color.CYAN);
target.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
# Plot Hard Stop Loss Price and Trailing Stop
def entryBarNumber = if (buySignal) then BarNumber() else entryBarNumber[1];
plot hardStop = if hardStopLossPrice then hardStopLossPrice else Double.NaN;
hardStop.SetDefaultColor(Color.YELLOW);
hardStop.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
# Buy Signal Arrow Plot
plot currentBarBuySignal = if !IsNaN(close) and buySignal and !buySignal[1] then low - 0.2 * atr else Double.NaN;
currentBarBuySignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
currentBarBuySignal.AssignValueColor(Color.GREEN);
currentBarBuySignal.SetLineWeight(3);
# Buy and sell execution prices
def buyExecutionPrice = if buySignal then close else buyExecutionPrice[1];
def sellExecutionPrice = if sellSignal then close else sellExecutionPrice[1];
# Buy and sell execution prices
def buyExecPrice;
def sellExecPrice;
if (buySignal) {
buyExecPrice = close;
} else {
buyExecPrice = buyExecPrice[1];
}
if (sellSignal) {
sellExecPrice = close;
} else {
sellExecPrice = sellExecPrice[1];
}