#__________INPUTS__________#
input tradeSize = 100;
##___BOLLINGER BANDS INPUTS___##
input price_boll = close;
input displace = 0;
input length_boll = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType_boll = AverageType.EXPONENTIAL;
#BOLLLINGER BANDS PLOT#
def sDev = StDev(data = price_boll[-displace], length = length_boll);
plot MidLine = MovingAverage(averageType_boll, data = price_boll[-displace], length = length_boll);
plot LowerBand = MidLine + Num_Dev_Dn * sDev;
plot UpperBand = MidLine + Num_Dev_up * sDev;
LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));
##___RSI___##
input length_RSI = 14;
input over_Bought = 70;
input over_Sold = 30;
input price_RSI = close;
input averageType_RSI = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;
#__________DEFINE__________#
##___BOLLINGER BANDS___##
def BollingerBands = BollingerBands(price_boll, displace, length_boll, Num_Dev_Dn, Num_Dev_up, averageType_boll);
def boll_upper_cross = close is greater than UpperBand;
def boll_lower_cross = close is less than LowerBand;
##___RSI LOWER & UPPER CROSS___##
def RSI = RSI(length_RSI, over_Bought, over_Sold, price_RSI, averageType_RSI, showBreakoutSignals);
def RSI_upper_cross = RSI is greater than over_Bought;
def RSI_lower_cross = RSI is less than over_Sold;
#__________CONDITIONS__________#
##___LONG CONDITION___##
def buySignalLong = boll_lower_cross from 1 bars ago and RSI_lower_cross from 1 bars ago and close is greater than LowerBand;
##___SHORT CONDITION___##
def sellSignalShort = boll_upper_cross from 1 bars ago and RSI_upper_cross from 1 bars ago and close is less than UpperBand;
#__________SALES__________#
##___LONG ENTRY___#
#LONG OPEN#
AddOrder(OrderType.BUY_TO_OPEN, buySignalLong, open[-1], tradeSize, Color.CYAN, Color.CYAN);
#LONG PROFIT#
##This order below is where I'm struggling. What I would like is to have my take profit be equal to the midline (moving average) of the bollinger band on the day that I entered and my stop loss be calculated as 50% of the distance between my entry price and the moving average. So for example if the day I entered my entry price was 40, and the midline was at 50, I'd like the take profit to be set at 50 and the stop loss to be set at 35.
##AddOrder(OrderType.SELL_TO_CLOSE, longProfit, longProfit, tradeSize, Color.GREEN, Color.GREEN);
#LONG LOSS#
##AddOrder(OrderType.SELL_TO_CLOSE, EntryPrice() - (MidLine - EntryPrice()), EntryPrice() - (MidLine - EntryPrice()), tradeSize, Color.RED, Color.RED);
##___SHORT ENTRY___##
##AddOrder(OrderType.SELL_TO_OPEN, sellSignalShort, open[-1], tradeSize, Color.BLUE, Color.BLUE);
#SHORT PROFIT#
##AddOrder(OrderType.BUY_TO_CLOSE, exitGoodShort, EntryPrice() * percMultShortProfit + EntryPrice(), tradeSize, Color.GREEN, Color.GREEN);
#SHORT LOSS#
##AddOrder(OrderType.BUY_TO_CLOSE, exitBadShort, EntryPrice() * percMultShortLoss + EntryPrice(), tradeSize, Color.RED, Color.RED);
#__________PLOTS__________#