Join useThinkScript to post your question to a community of 21,000+ developers and traders.
input STime = 0930 ; #hint STime: Start of normal trading hours
input ETime = 1600 ; #hint ETime: End of normal trading hours
def ema = ExpAverage(close, 5);
def ema2 = ExpAverage(close, 10);
def above_ema = low[1] > ema[1] and ema[1] > ema2[1] ;
def below_ema = high[1] < ema[1] and ema[1] < ema2[1] ;
def IsActive = if secondsTillTime(ETime) > 0 and
secondsFromTime(STime) >= 0
then 1
else 0;
def above_ema_count= CompoundValue(1, if IsActive and above_ema then above_ema_count[1] + 1 else 0, 0);
def above_ema_count= CompoundValue(1, if IsActive and below_ema then below_ema_count[1] + 1 else 0, 0);
addlabel(yes, concat("above_ema_count: " + above_ema_count , "."));
addlabel(yes, concat("below_ema_count: " + below_ema_count , "."));
crosses up and down from what? in order to cross anything you have to specify what it crosses.. define "crosses up"Hello everyone,
I was wondering if anyone could help me write a ThinkScript code that displays the number of up/down days when the 20 day SMA crosses up or down. For example, when the 20 day SMA cross up, I want the script to count how many down days there were before it. When the 20 day SMA crosses down, I want the script to count how many up days there were before it.
I hope my description wasn't too hard to get. I am a little new at this so any help is appreciated!
Thank you!
Thanks for getting back to me! When I say cross up or down, I mean when the 20days SMA changes direction. So say the SMA is positive for 10 days, then it switches to a down trend. When that switch happens, I want my code to count how many days passed before the SMA switched. So in this example, it will say 10 days. I hope this is clearer. If not maybe I can try to provide a picture?crosses up and down from what? in order to cross anything you have to specify what it crosses.. define "crosses up"
input EMA = 20;
input EMA2 = 50;
input price = close;
input displace = 0;
Plot AvgExp = ExpAverage(price[-displace], EMA);
Plot AvgExp2 = ExpAverage(price[-displace], EMA2);
def above = (avgexp > avgexp2);
def below = (avgexp < avgexp2);
def aboveCount = if above then aboveCount[1] + 1 else 0;
def belowcount = if below then belowcount[1] + 1 else 0;
def FinalCount = if above==1 then abovecount else if below==1 then belowcount else 0;
def Both = avgexp > avgexp2 or avgexp < avgexp2;
def Total = CompoundValue(1, if both then total[1] + 1 else 0, 0);
def prior = total-finalcount;
AddLabel (yes, "Bars Since " + ema + "/" + ema2 + " Cross: " + FinalCount );
AddLabel (yes, "Prior Bars before " + ema + "/" + ema2 + " Cross: " + prior );
It's what I want, Thank you very much!if you mean the amount of bars that the crossing is bullish or bearish here is the code for the count
Counts the number of bars since the specified moving averages last crossed
Code:input EMA = 20; input EMA2 = 50; input price = close; input displace = 0; Plot AvgExp = ExpAverage(price[-displace], EMA); Plot AvgExp2 = ExpAverage(price[-displace], EMA2); def above = (avgexp > avgexp2); def below = (avgexp < avgexp2); def aboveCount = if above then aboveCount[1] + 1 else 0; def belowcount = if below then belowcount[1] + 1 else 0; def FinalCount = if above==1 then abovecount else if below==1 then belowcount else 0; def Both = avgexp > avgexp2 or avgexp < avgexp2; def Total = CompoundValue(1, if both then total[1] + 1 else 0, 0); def prior = total-finalcount; AddLabel (yes, "Bars Since " + ema + "/" + ema2 + " Cross: " + FinalCount ); AddLabel (yes, "Prior Bars before " + ema + "/" + ema2 + " Cross: " + prior );
Say i do a MovingAvg5 and MovingAvg10 on a 1min chart.
How do i calculate how many MA cross-up and MA cross-down in the whole trading session (0930 - 1600hr) ?
Addlabel on the chart - Num of Cross-up and Num of Cross-down
#( CUMULATIVE ) COUNT OF THE TOTAL NUMBER OF Moving Average Cross above the Other Specified Moving Average
# ON THE Same Day
# By XeoNoX via Usethinkscript.com
input EMA = 20;
input EMA2 = 50;
input price = close;
input displace = 0;
def AvgExp = ExpAverage(price[-displace], EMA);
def AvgExp2 = ExpAverage(price[-displace], EMA2);
def Active = GetDay() == GetLastDay(); #Today
def var =avgexp crosses above avgexp2;
def cumulative = if Active and !Active[1] then var else if Active then cumulative[1] + var else cumulative[1];
def scan = cumulative;
AddLabel (yes, EMA + "MA Crossed Above " + ema2 + "MA " + (scan) + " Times Today" );
How would I get it so instead of the day it just goes to the first day on the chart? I want to use something like this to be apart of a bigger script, but I would want it to apply to any timeframe I look at, and just start the count at the beginning of it. I have a gap stats script that counts whether you do 1 year or 5. If you could just tell me what needs to be done to this script I would greatly appreciate it.heres what you requested.
if you want to to cound the down then just change
def var =avgexp crosses above avgexp2;
to
def var =avgexp crosses below avgexp2;
COUNT OF THE TOTAL NUMBER OF TIMES THE Moving Average Crosses above the Other Specified Moving Average on the SAME DAY
Code:#( CUMULATIVE ) COUNT OF THE TOTAL NUMBER OF Moving Average Cross above the Other Specified Moving Average # ON THE Same Day # By XeoNoX via Usethinkscript.com input EMA = 20; input EMA2 = 50; input price = close; input displace = 0; def AvgExp = ExpAverage(price[-displace], EMA); def AvgExp2 = ExpAverage(price[-displace], EMA2); def Active = GetDay() == GetLastDay(); #Today def var =avgexp crosses above avgexp2; def cumulative = if Active and !Active[1] then var else if Active then cumulative[1] + var else cumulative[1]; def scan = cumulative; AddLabel (yes, EMA + "MA Crossed Above " + ema2 + "MA " + (scan) + " Times Today" );
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
M | How to count how many times "AddOrder" is executed? | Questions | 12 | |
S | Count how many times stock is above 200 SMA in thinkscript | Questions | 10 | |
New Highs count Watchlist Column | Questions | 0 | ||
M | Count number of active RSI chart bubbles and create alert | Questions | 6 | |
D | Bar Count.. every THREE bars | Questions | 2 |
Start a new thread and receive assistance from our community.
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.
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.