Storing Intraday Crossover Data From Previous Days in a Watchlist

craigjjmorrison

New member
VIP
I want to compare various potential exit points for positions from previous days. For instance, I would like to know the price at which a stock first crossed below a moving average, such as SMA 10, in each of the past 4 trading days. I would also want the same for an exponential moving average. Ideally, I could store the SMA in one 5-minute column and the EMA in another. To collect history, I would update and export the watchlist for each of the ten days.

I can collect this data for the current day, but I'm unable to retrieve data from previous days using:
Code:
Def OpeningBell = 930;
Def ClosingBell = 1600;
Def FloorIsOpen = SecondsFromTime(OpeningBell) >= 0 and SecondsTill(ClosingBell) >=0;
Def SMA10 = SimpleMovingAvg(Close,10);
Def XOver= if !FloorIsOpen then 0 else if XOver[1] == 0 and Crosses(Close,SMA10,CrossingDirection.below) then Close else XOver[1];
Plot X = XOver;

If today were Friday, I would like to use the watchlist to separately collect Mon, Tues, Wed, and Thurs.
 
Solution
Check it as a chart first, before moving on to a column or whatever else you're doing. We can work on that later once we're sure we've worked it out properly.

Code:
input DaysAgo = 1;

def  Clock =
     GetTime();
def  Date =
     GetYYYYMMDD();
def  Begin =
     RegularTradingStart(Date);
def  Finish =
     RegularTradingEnd(Date);
def  isOpen =
     Between(Clock, Begin, Finish);
def  DayBars =
     24 * 60 * 60 / GetAggregationPeriod() * 1000;
def  DayRoll =
     GetDay() <> GetDay()[1];
def  NewDay =
        (isOpen && !isOpen[1])
     or (isOpen &&  isOpen[1] && DayRoll);
     AddVerticalLine(NewDay,"",color.gray,curve.points);
def  Cond =
        open > simpleMovingAvg(close,10)
     && low  < simpleMovingAvg(close,10);
def...
I want to compare various potential exit points for positions from previous days. For instance, I would like to know the price at which a stock first crossed below a moving average, such as SMA 10, in each of the past 4 trading days. I would also want the same for an exponential moving average. Ideally, I could store the SMA in one 5-minute column and the EMA in another. To collect history, I would update and export the watchlist for each of the ten days.

I can collect this data for the current day, but I'm unable to retrieve data from previous days using:
Code:
Def OpeningBell = 930;
Def ClosingBell = 1600;
Def FloorIsOpen = SecondsFromTime(OpeningBell) >= 0 and SecondsTill(ClosingBell) >=0;
Def SMA10 = SimpleMovingAvg(Close,10);
Def XOver= if !FloorIsOpen then 0 else if XOver[1] == 0 and Crosses(Close,SMA10,CrossingDirection.below) then Close else XOver[1];
Plot X = XOver;

If today were Friday, I would like to use the watchlist to separately collect Mon, Tues, Wed, and Thurs.
Would you be interested in something like this>

Example:

Entry:
A+ CALL

Exit Method A:
5m SMA10

Exit Method B:
15m EMA20

Exit Method C:
VWAP

Exit Method D:
Structure Breakdown

Exit Method E:
V6 Trade Grade downgrade
 
Last edited by a moderator:

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

In the code below, I tried hard to isolate the close for the bar during which the price first crossed below the SMA. With every attempt to write this, the answer I get seems to be pulled from a hat. In some tests, I have to go back several days just to determine where the return falls within the stock's daily price range.

This code is in a watchlist column with a five-minute aggregation.
Code:
def FloorIsOpen = SecondsFromTime(930) >= 0 and SecondsTillTime(1600) >= 0;
def SMA10 = SimpleMovingAvg(Close,10);
def Top = Max(Open,Close);
def SMACross = if !FloorIsOpen[1] and FloorIsOpen then 0 else if FloorIsOpen and SMACross[1] == 0 and crosses(Top,SMA10,CrossingDirection.BELOW) then Close else SMACross[1];

Plot X = SMACross;
 
Last edited by a moderator:
In the code below, I tried hard to isolate the close for the bar during which the price first crossed below the SMA. With every attempt to write this, the answer I get seems to be pulled from a hat. In some tests, I have to go back several days just to determine where the return falls within the stock's daily price range.

This code is in a watchlist column with a five-minute aggregation.
Code:
def FloorIsOpen = SecondsFromTime(930) >= 0 and SecondsTillTime(1600) >= 0;
def SMA10 = SimpleMovingAvg(Close,10);
def Top = Max(Open,Close);
def SMACross = if !FloorIsOpen[1] and FloorIsOpen then 0 else if FloorIsOpen and SMACross[1] == 0 and crosses(Top,SMA10,CrossingDirection.BELOW) then Close else SMACross[1];

Plot X = SMACross;

You may be making it more difficult than needed... Either that or we need more details...

Ruby:
close crosses below Average(close, 10);
close[1] crosses below Average(close, 10)[1];

This code will trigger each time close crosses below the SMA10... You can also use an offset to test if the cross was in the previous candle by using the second example... I would recommend the first example... That's all the code needed for the Scan to return those results... For only between 9:30 and 16:00 just leave Extended Hours unchecked...
 
Check it as a chart first, before moving on to a column or whatever else you're doing. We can work on that later once we're sure we've worked it out properly.

Code:
input DaysAgo = 1;

def  Clock =
     GetTime();
def  Date =
     GetYYYYMMDD();
def  Begin =
     RegularTradingStart(Date);
def  Finish =
     RegularTradingEnd(Date);
def  isOpen =
     Between(Clock, Begin, Finish);
def  DayBars =
     24 * 60 * 60 / GetAggregationPeriod() * 1000;
def  DayRoll =
     GetDay() <> GetDay()[1];
def  NewDay =
        (isOpen && !isOpen[1])
     or (isOpen &&  isOpen[1] && DayRoll);
     AddVerticalLine(NewDay,"",color.gray,curve.points);
def  Cond =
        open > simpleMovingAvg(close,10)
     && low  < simpleMovingAvg(close,10);
def  FirstFlip =
     if NewDay and Cond
      then simpleMovingAvg(close,10)
     else if NewDay and !Cond
      then 0
     else if !FirstFlip[1] and Cond
      then simpleMovingAvg(close,10)
     else FirstFlip[1];
plot Marker =
     FirstFlip && FirstFlip != FirstFlip[1];
     Marker.setpaintingStrategy(
         paintingStrategy.BOOLEAN_ARROW_DOWN);
     Marker.setdefaultColor(color.magenta);
plot Line = firstflip;
     Line.setpaintingStrategy(12);
     Line.setdefaultColor(color.white);
def  Search =
     fold Index = 0 to Max(0,DaysAgo - 1)
     with Data = GetmaxValueOffset(NewDay,DayBars) + 1
     do   Data + GetValue(GetmaxValueOffset(NewDay,DayBars) + 1,Data);
plot PrevCross = getvalue(firstFlip,search);
     PrevCross.setpaintingStrategy(paintingStrategy.POINTS);
plot SMA = simpleMovingAvg(close,10);
 
Solution
You may be making it more difficult than needed... Either that or we need more details...

Ruby:
close crosses below Average(close, 10);
close[1] crosses below Average(close, 10)[1];

This code will trigger each time close crosses below the SMA10... You can also use an offset to test if the cross was in the previous candle by using the second example... I would recommend the first example... That's all the code needed for the Scan to return those results... For only between 9:30 and 16:00 just leave Extended Hours unchecked...
Thank you. I did neglect to note that I was running the code in a watchlist column. I use the code you offered in scan and charts.
 
Check it as a chart first, before moving on to a column or whatever else you're doing. We can work on that later once we're sure we've worked it out properly.

Code:
input DaysAgo = 1;

def  Clock =
     GetTime();
def  Date =
     GetYYYYMMDD();
def  Begin =
     RegularTradingStart(Date);
def  Finish =
     RegularTradingEnd(Date);
def  isOpen =
     Between(Clock, Begin, Finish);
def  DayBars =
     24 * 60 * 60 / GetAggregationPeriod() * 1000;
def  DayRoll =
     GetDay() <> GetDay()[1];
def  NewDay =
        (isOpen && !isOpen[1])
     or (isOpen &&  isOpen[1] && DayRoll);
     AddVerticalLine(NewDay,"",color.gray,curve.points);
def  Cond =
        open > simpleMovingAvg(close,10)
     && low  < simpleMovingAvg(close,10);
def  FirstFlip =
     if NewDay and Cond
      then simpleMovingAvg(close,10)
     else if NewDay and !Cond
      then 0
     else if !FirstFlip[1] and Cond
      then simpleMovingAvg(close,10)
     else FirstFlip[1];
plot Marker =
     FirstFlip && FirstFlip != FirstFlip[1];
     Marker.setpaintingStrategy(
         paintingStrategy.BOOLEAN_ARROW_DOWN);
     Marker.setdefaultColor(color.magenta);
plot Line = firstflip;
     Line.setpaintingStrategy(12);
     Line.setdefaultColor(color.white);
def  Search =
     fold Index = 0 to Max(0,DaysAgo - 1)
     with Data = GetmaxValueOffset(NewDay,DayBars) + 1
     do   Data + GetValue(GetmaxValueOffset(NewDay,DayBars) + 1,Data);
plot PrevCross = getvalue(firstFlip,search);
     PrevCross.setpaintingStrategy(paintingStrategy.POINTS);
plot SMA = simpleMovingAvg(close,10);
Thank you for the swift response. I am unexpectedly out of pocket and look forward to trying this.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
904 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