BTW for the SPX strategy, I added the condition of the trigger with SMA 89... if above go long and if below go short... that would have avoided the trade that happened this morning...here is the updated code: it will reduce the number of trades though...
declare upper;
input price = close;
input SMAFastLength = 3;
input SMASlowLength = 9;
input SMA89 = 89;
input BBlength = 30;
input BBNum_Dev = 0.8;
input BBCrossInBars = 3;
input BBCrossDistance = 1;
input BBCrossDistance1 = -1;
input BBCrossDistance2 = 1;
input MACDfastLength = 8;
input MACDslowLength = 16;
input positionSize = 10000.0;
input entryPrice = close;
input entryPriceOffset = 0;
input MACDLength = 36;
input ERGODICLongLength = 2;
input ERGODICShortLength = 10;
input ERGODICSignalLength = 36;
input ERGODICAverageType = {"SIMPLE", default "EXPONENTIAL", "WEIGHTED", "WILDERS", "HULL"};
input ShowWarnArrows = yes;
input ShowStatistics = yes;
input ProfitDelta = 1.0;
# Check for 10min chart
Assert(GetAggregationPeriod() == AggregationPeriod.TEN_MIN, "Incorrect Chart Time, use 10m");
# MACD
def MACD_Data = MACD(fastLength = MACDfastLength, slowLength = MACDslowLength, MACDLength = MACDLength);
def MACD_Direction = if MACD_Data > MACD_Data[1] then 1 else -1;
def MACD_Down = if MACD_Data < MACD_Data[1] then 1 else -1;
def MACD_up = if MACD_Data > MACD_DATA[1] then 1 else -1;
# Ergodic
def Ergodic_Data = ErgodicOsc("long length" = ERGODICLongLength, "short length" = ERGODICShortLength, "signal length" = ERGODICSignalLength, "average type" = ERGODICAverageType).ErgodicOsc;
# SMAs
def SMA_Fast = SimpleMovingAvg(price, SMAFastLength);
def SMA_Slow = SimpleMovingAvg(price, SMASlowLength);
def SMA_89 = SimpleMovingAvg(price, SMA89);
# Signals
def buySignal = SMA_Fast > SMA_Slow and close > SMA_89 and Ergodic_Data > 0 and MACD_Direction == 1 and MACD_Data >= BBCrossDistance and MACD_Data crosses above 0 within BBCrossInBars bars;
def sellSignal = SMA_Fast < SMA_Slow and close < SMA_89 and Ergodic_Data < 0 and MACD_Direction == -1 and MACD_Data <= -BBCrossDistance and MACD_Data crosses below 0 within BBCrossInBars bars;
addOrder(OrderType.BUY_T
pen,buySignal and !buySignal[1], entryPrice[entryPriceOffset], round(positionSize / close, 0), color.gREEN, color.gREEN, name="LE");
addorder(orderType.SELL_to_close,MACD_Direction == -1 , entryPrice[entryPriceOffset], tickColor=color.reD, arrowColor=color.reD, name="LX");
addOrder(OrderType.SELL_T
pen, sellSignal and !sellSignal[1], entryPrice[entryPriceOffset], round(positionSize / close, 0), color.dark_gREEN, color.dark_gREEN, name="SE");
addorder(orderType.BUY_to_close,MACD_Direction == 1, entryPrice[entryPriceOffset], tickColor=color.dark_reD,arrowColor=color.dark_reD, name="SX");
# Plots
plot buy = buySignal and !buySignal[1];
buy.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buy.setDefaultColor(Color.GREEN);
buy.setLineWeight(3);
plot sell = sellSignal and !sellSignal[1];
sell.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sell.setDefaultColor(Color.RED);
sell.setLineWeight(3);
# Alerts
Alert(buy, "Buy", Alert.BAR, Sound.Ring);
Alert(sell, "Sell", Alert.BAR, Sound.Ring);
# PnL
def entryPrice1 = if (buySignal and !buySignal[1]) or (sellSignal and !sellSignal[1]) then close else entryPrice1[1];
def profitTarget = if buySignal and !buySignal[1] then entryPrice + ProfitDelta else if sellSignal and !sellSignal[1] then entryPrice - ProfitDelta else profitTarget[1];
def orderDir = if BarNumber() == 1 then 0
else if orderDir[1] == 0 and buySignal and !buySignal[1] then 1
else if orderDir[1] == 1 and (MACD_Direction != 1 or close crosses above profitTarget) then 0
else if orderDir[1] == 0 and sellSignal and !sellSignal[1] then -1
else if orderDir[1] == -1 and (MACD_Direction != -1 or close crosses below profitTarget) then 0
else orderDir[1];
def isOrder = if IsNaN(orderDir) then no else orderDir crosses 0;
def orderCount = CompoundValue(1, if IsNaN(isOrder) then 0 else if isOrder then orderCount[1] + 1 else orderCount[1], 0);
def orderWinners = if BarNumber() == 1 then 0
else if orderDir[1] == 1 and orderDir == 0 then
if close >= profitTarget[1] then orderWinners[1] + 1 else orderWinners[1]
else if orderDir[1] == -1 and orderDir == 0 then
if close <= profitTarget[1] then orderWinners[1] + 1 else orderWinners[1]
else orderWinners[1];
def winRate = orderWinners / orderCount;
AddLabel(ShowStatistics, "Ergo Strategy", Color.Cyan);
AddLabel(ShowStatistics, "" + orderCount + " Trades | " + AsPercent(winRate), if winRate > 0.5 then Color.GREEN else Color.RED);