#rsi
input longEntries = yes;
input ShortEntries = yes;
input price = close;
input tradesize = 1;
input StartTrading = 930;
input StopTrading = 1600;
input ShowTodayOnly = {"No", default "Yes"};
def s=ShowTodayOnly;
def today=if s==0 OR getday()==getlastday() AND secondsfromtime(StartTrading)>=0 then 1 else 0;
def t2trade = today and secondsFromTime(StartTrading) > 0 and secondsFromTime(StopTrading)<0 ;
## ema stoploss ##############
input emaStopLoss = yes;
input emaStopLenght = 9;
input emaLongDisplace = 0;
input emaShortDisplace = 0;
def emaX = MovingAverage(AverageType.EXPONENTIAL, close, emaStopLenght);
input rsiStopLoss = yes;
input rsiEntryOffset = 0;
input rsiStopOffset = 0 ;
input length = 14;
input OB = 70;
input OS = 30;
input neutral= 50;
input rsiAverageType = AverageType.WILDERS;
def over_bought = OB;
def over_sold = OS ;
def rsi = reference RSI(price = price, length = length, averageType = rsiAverageType);
##### Stop Loss / Profit Target ####
input offsetType = {default VALUE,percent, tick};
input longTarget = 50;
input shortTarget = 50;
input longStop = 50;
input shortStop = 50;
def entryPrice = open;#EntryPrice();
def mult;
switch (offsetType) {
case percent:
mult =entryPrice / 100;
case value:
mult = 1;
case tick:
mult = TickSize();
}
def longstopPrice;
def shortstopPrice;
def longTargetPrice;
def shortTargetPrice;
if high[1] >= shortStopPrice[1]{
longstopPrice = Double.NaN;
shortstopPrice = Double.NaN;
longTargetPrice = Double.NaN;
shortTargetPrice = Double.NaN;
}else if low[1] <= shortTargetPrice[1]{
longstopPrice = Double.NaN;
shortstopPrice = Double.NaN;
longTargetPrice = Double.NaN;
shortTargetPrice = Double.NaN;
}else if high[1] >= longTargetPrice[1]{
longstopPrice = Double.NaN;
shortstopPrice = Double.NaN;
longTargetPrice = Double.NaN;
shortTargetPrice = Double.NaN;
}else if low[1] <= longstopPrice[1]{
longstopPrice = Double.NaN;
shortstopPrice = Double.NaN;
longTargetPrice = Double.NaN;
shortTargetPrice = Double.NaN;
}else if rsi[1] crosses above over_bought[1] and IsNaN(longstopPrice[1]) {
longstopPrice = entryPrice - longstop * mult;
shortstopPrice = Double.NaN;
longTargetPrice = entryPrice + longtarget * mult;
shortTargetPrice = Double.NaN;
}else if rsi[1] crosses below over_sold[1]and IsNaN(shortstopPrice[1]) {
longstopPrice = Double.NaN;
shortstopPrice = entryPrice + shortstop * mult;
longTargetPrice = Double.NaN;
shortTargetPrice = entryPrice - shorttarget * mult;
}else{
longstopPrice = longstopPrice[1];
shortstopPrice = shortstopPrice[1];
longTargetPrice = longTargetPrice[1];
shortTargetPrice = shortTargetPrice[1];}
def long = rsi[0] crosses above over_bought[0];
def long2 = rsi[0] crosses above neutral ;
def longStopT = !isnan(longstopprice) and low <= longstopprice;
def longProfit = !isnan(longTargetPrice) and high >= longTargetPrice;
def short = rsi[0] crosses below over_sold[0] ;
def short2 = rsi[0] crosses below neutral ;
def shortStopT = !isnan(shortstopprice) and high >= shortstopprice;
def shortProfit = !isnan(shortTargetprice) and low <= shortTargetprice;
def OpenOrders = GetQuantity();
def ordfill = OpenOrders > 0 ;
def ordempty = OpenOrders < 1 ;
def orderx; if ordfill {orderx = 1;} else if ordempty {orderx = 0;} else {orderx = orderx[1];}
## Ema Stoploss ######
def emaLongStopT = close crosses below emaX[emaLongDisplace] ;
def emaShortStopT = close crosses above emaX[emaShortDisplace] ;
def emaBelow = emaLongStopT;
def emaAbove = emaShortStopT;
def emaLongStopTrigger = if emaBelow[1] then 1 else 0;
def emaShortStopTrigger = if emaAbove[1] then 1 else 0;
## Rsi Stoploss #######
def rsiLongStopT = rsi crosses below over_bought[rsiStopOffset] ;
def rsiShortStopT = rsi crosses above over_sold[rsiStopOffset];
def rsiBelow = rsiLongStopT;
def rsiAbove = rsiShortStopT;
def rsiLongStopTrigger = if rsiBelow[1] then 1 else 0;
def rsiShortStopTrigger = if rsiAbove[1] then 1 else 0;
#### Orders ####
AddOrder(OrderType.BUY_AUTO, (longEntries) and t2trade and long[-1], price = open[-2], tickcolor = GetColor(6), arrowcolor = GetColor(6), name = "Long Entry", tradeSize= tradesize);
AddOrder(OrderType.BUY_AUTO, (longEntries) and t2trade and long2[-1] , price = open[-2], tickcolor = Color.CYAN, arrowcolor = Color.CYAN, name = "nLong Entry", tradeSize= tradesize);
AddOrder(OrderType.SELL_TO_CLOSE, t2trade and longStopT[-1], price = longstopPrice[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "Long Stop", tradeSize= tradesize);
AddOrder(OrderType.SELL_TO_CLOSE, t2trade and longProfit[-1], price = longTargetPrice[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "Long Profit", tradeSize= tradesize);
AddOrder(OrderType.SELL_TO_CLOSE, (longEntries) and t2trade and rsiLongStopT[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "rLong Stop", tradeSize= tradesize);
AddOrder(OrderType.SELL_TO_CLOSE, (longEntries) and (emaStopLoss) and t2trade and emaLongStopT[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "eLong Stop", tradeSize= tradesize);
AddOrder(OrderType.SELL_AUTO,(ShortEntries) and t2trade and short[-1], price = open[-2], tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "Short Entry", tradeSize= tradesize);
AddOrder(OrderType.SELL_AUTO, (ShortEntries) and t2trade and short2[-1] , price = open[-2], tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "nShort Entry", tradeSize= tradesize);
AddOrder(OrderType.BUY_TO_CLOSE, t2trade and shortStopT[-1], price = shortstopPrice[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "Short Stop", tradeSize= tradesize);
AddOrder(OrderType.BUY_TO_CLOSE, t2trade and shortProfit[-1], price = shortTargetPrice[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "Short Profit", tradeSize= tradesize);
AddOrder(OrderType.BUY_TO_CLOSE, (ShortEntries) and t2trade and rsiShortStopT[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "rShort Stop", tradeSize= tradesize);
AddOrder(OrderType.BUY_TO_CLOSE, (ShortEntries) and (emaStopLoss) and t2trade and emaShortStopT[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "eShort Stop", tradeSize= tradesize);
def LongStopTrigger = t2trade and (!IsNaN(longStopPrice) and IsNaN(longStopPrice[-1]) and IsNaN(shortStopPrice[-1]) and low <= longStopPrice);
def LongTargetTrigger = t2trade and (!IsNaN(longStopPrice) and IsNaN(longStopPrice[-1]) and IsNaN(shortStopPrice[-1]) and high >= longTargetPrice);
def ShortStopTrigger = t2trade and (!IsNaN(shortStopPrice) and IsNaN(shortStopPrice[-1]) and IsNaN(longStopPrice[-1]) and high >= shortStopPrice);
def ShortTargetTrigger = t2trade and (!IsNaN(shortStopPrice) and IsNaN(shortStopPrice[-1]) and IsNaN(longStopPrice[-1]) and low <= shortTargetPrice);
#### Labels ####
AddLabel(yes, " ", if (longEntries) and t2trade and long[1] or (longEntries) and t2trade and long2[1] then CreateColor(153, 255, 153) else Color.GRAY);
AddLabel(yes, " ", if (ShortEntries) and t2trade and short[1] or (ShortEntries) and t2trade and short2[1] then CreateColor(255, 102, 102) else Color.DARK_GRAY);
AddLabel(IsNaN(LongStopTrigger) and !ShortStopTrigger and !ShortTargetTrigger," ",Color.WHITE);
AddLabel(!LongStopTrigger and IsNaN(ShortStopTrigger) and !LongTargetTrigger, " ",Color.WHITE);
AddLabel(IsNaN(LongStopTrigger) and IsNaN(ShortStopTrigger), " ",Color.WHITE);
AddLabel(LongStopTrigger, " ", Color.YELLOW);
AddLabel(LongTargetTrigger, " ", Color.YELLOW);
AddLabel(ShortStopTrigger, " ",Color.YELLOW);
AddLabel(ShortTargetTrigger, " ",Color.YELLOW);
AddLabel(yes, " ", if emaLongStopTrigger ==1 or rsiLongStopTrigger == 1 then Color.PLUM else if emaShortStopTrigger ==1 or rsiShortStopTrigger == 1 then Color.DARK_ORANGE else Color.PINK);
AddLabel(yes, if orderx == 1 then " " else " ", if orderx == 1 then Color.Blue else Color.Magenta);