HighestAll or LowestAll Since Trigger

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:

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.

iwOFfGH.png


Entry does need a little work I know. That is something I can continue to work on myself.
 
Last edited:
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:

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.

iwOFfGH.png


Entry does need a little work I know. That is something I can continue to work on myself.

short answer for now,
you can put a formula inside of highestall()

if you want the highest value, after some bar, then add a compare of the barnumber

Code:
def bn = barnumber();

#save barnumber on a buy
def longbn = if enterlong then bn else longbn[1];

def DiffHigh = if TradingDay then HighestAll( if bn >= longbn then Diff else 0 ) else double.nan;
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
502 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top