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" );