a1cturner
Well-known member
Need a little help with this. Searched and couldn't figure it out.
I created a Histogram based on TSI and the MACD Avg shown below:
Using this Histogram I created a strategy shown below:
I will enter on any bright green or bright red bar as long as there is an obvious trend change from red to green or green to red (subject to change)
The problem I am having is with my exits. I have tried multiple but wanted to add in a HighestAll(Diff) or LowestAll(Diff) and use that to test out exiting at possibly 50% of the highest Diff since entry or 50% of the lowest Diff since entry.
This is definitely the part of the code that is the problem.
Basically this is what I want in sentence form.... If not already in a trade and EnterLong then get HighestAll(Diff) value from that point forward. If at any point during that period if the Diff is < DiffHigh * 50% than sell.
Below is a visual representation of what I want. It is drawn on the lower study but will be used to create an order and/or buy/sell bubbles on the upper strategy.
Entry does need a little work I know. That is something I can continue to work on myself.
I created a Histogram based on TSI and the MACD Avg shown below:
Code:
declare lower;
#TSI
input TSILongLength = 25;
input TSIShortLength = 13;
#TRUE STENGTH INDEX(TSI)
def DiffShort = close - close[1];
def DoubleSmoothedAbsDiffShort = ExpAverage(ExpAverage(AbsValue(DiffShort), TSILongLength), TSIShortLength);
#TSI
def TSI = if DoubleSmoothedAbsDiffShort == 0 then 0 else Round((100 * (ExpAverage(ExpAverage(DiffShort, TSILongLength), TSIShortLength)) / DoubleSmoothedAbsDiffShort), 2);
def TSIBull = TSI > 10;
def TSIBear = TSI < -10;
input fastLength = 10;
input slowLength = 22;
input MACDLength = 8;
input averageType = AverageType.EXPONENTIAL;
def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);
plot Diff = if
((TSI > Avg) and (TSI > 0) and (Avg > 0)) then TSI - Avg
else if
((TSI > Avg) and (TSI > 0) and (Avg < 0)) then TSI + Avg
else if
((TSI > Avg) and (TSI < 0) and (Avg < 0)) then -TSI + Avg
else if
((TSI < Avg) and (TSI < 0) and (Avg < 0)) then TSI + -Avg
else if
((TSI < Avg) and (TSI < 0) and (Avg > 0)) then TSI + Avg
else if
((TSI < Avg) and (TSI > 0) and (Avg > 0)) then -TSI + Avg
else 0;
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.DefineColor("Neutral", Color.GRAY);
Diff.AssignValueColor(if Diff > 3 and Diff > Diff[1] then Diff.color("Positive and Up") else if Diff > 3 and Diff < Diff[1] then Diff.color("Positive and Down") else if Diff < -3 and Diff < Diff[1] then Diff.color("Negative and Down") else if Diff < -3 and Diff > Diff[1] then Diff.color("Negative and Up") else Diff.color("Neutral"));
Using this Histogram I created a strategy shown below:
Code:
declare upper;
input ShowOrders = no;
#Times
def TradingDay = SecondsFromTime(0930) > 0 and (SecondsTillTime(1530) > 0);
def EndOfDay = SecondsTillTime(1450) == 0;
#TRUE STENGTH INDEX(TSI)
input TSILongLength = 25;
input TSIShortLength = 13;
def DiffShort = close - close[1];
def DoubleSmoothedAbsDiffShort = ExpAverage(ExpAverage(AbsValue(DiffShort), TSILongLength), TSIShortLength);
def TSI = if DoubleSmoothedAbsDiffShort == 0 then 0 else Round((100 * (ExpAverage(ExpAverage(DiffShort, TSILongLength), TSIShortLength)) / DoubleSmoothedAbsDiffShort), 2);
def TSIBull = TSI > 10;
def TSIBear = TSI < -10;
input fastLength = 10;
input slowLength = 22;
input MACDLength = 8;
input averageType = AverageType.EXPONENTIAL;
#MOVING AVERAGE CONVERGENCE/DIVERGENCE (MACD)
def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);
#TSI AND MACD DIFF
def Diff = if
((TSI > Avg) and (TSI > 0) and (Avg > 0)) then TSI - Avg
else if
((TSI > Avg) and (TSI > 0) and (Avg < 0)) then TSI + Avg
else if
((TSI > Avg) and (TSI < 0) and (Avg < 0)) then -TSI + Avg
else if
((TSI < Avg) and (TSI < 0) and (Avg < 0)) then TSI + -Avg
else if
((TSI < Avg) and (TSI < 0) and (Avg > 0)) then TSI + Avg
else if
((TSI < Avg) and (TSI > 0) and (Avg > 0)) then -TSI + Avg
else 0;
def PosUp = (Diff > 3) and (Diff > Diff[1]);
def PosDn = (Diff > 3) and (Diff < Diff[1]);
def NegDn = (Diff < -3) and (Diff < Diff[1]);
def NegUp = (Diff < -3) and (Diff > Diff[1]);
def EnterLong = TradingDay and PosUp and close > open;
def EnterShort = TradingDay and NegDn and close < open;
def DiffHigh = if TradingDay then HighestAll(Diff) else double.nan;
def DiffLow = if TradingDay then LowestAll(Diff) else double.nan;
def SellLong = Diff < (DiffHigh * 0.5);
def SellShort = Diff > (DiffLow * 0.5);
#def SellLong = ((Diff > 3) and (Diff < Diff[3])) or Diff < 0;
#def SellShort = ((Diff < -3) and (Diff > Diff[3])) or Diff > 0;
#COLOR BARS
AssignPriceColor(if PosUp then color.green else if PosDn then color.dark_green else if NegDn then color.red else if NegUp then color.dark_red else color.gray);
#BUY ORDERS
AddOrder(OrderType.BUY_TO_OPEN, ShowOrders and EnterLong, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN);
AddOrder(OrderType.SELL_TO_OPEN, ShowOrders and EnterShort, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN);
#SELL ORDERS
AddOrder(OrderType.SELL_TO_CLOSE, close < open and (EndOfDay or SellLong), open[-1], 100, Color.RED, Color.LIGHT_RED);
AddOrder(OrderType.BUY_TO_CLOSE, close > open and (EndOfDay or SellShort), open[-1], 100, Color.RED, Color.LIGHT_RED);
I will enter on any bright green or bright red bar as long as there is an obvious trend change from red to green or green to red (subject to change)
The problem I am having is with my exits. I have tried multiple but wanted to add in a HighestAll(Diff) or LowestAll(Diff) and use that to test out exiting at possibly 50% of the highest Diff since entry or 50% of the lowest Diff since entry.
This is definitely the part of the code that is the problem.
Code:
def EnterLong = TradingDay and PosUp and close > open;
def EnterShort = TradingDay and NegDn and close < open;
def DiffHigh = if TradingDay then HighestAll(Diff) else double.nan;
def DiffLow = if TradingDay then LowestAll(Diff) else double.nan;
def SellLong = Diff < (DiffHigh * 0.5);
def SellShort = Diff > (DiffLow * 0.5);
Basically this is what I want in sentence form.... If not already in a trade and EnterLong then get HighestAll(Diff) value from that point forward. If at any point during that period if the Diff is < DiffHigh * 50% than sell.
Below is a visual representation of what I want. It is drawn on the lower study but will be used to create an order and/or buy/sell bubbles on the upper strategy.
Entry does need a little work I know. That is something I can continue to work on myself.
Last edited: