Hi All,
I am trying to go through given watchlist, calculate 50 days SMA for today and have a count if todays price is greater the sma calculated and print it.
Code which I tried:
_SECTION_BEGIN("Count Stocks Above 50 SMA");
watchlist="MasterList";
// Define the period for the SMA
smaPeriod = 50;
// Calculate the 50-period SMA for each stock
sma = MA(C, smaPeriod);
// Initialize a counter
aboveSmaCount = 0;
belowSmaCount = 0;
if(InWatchListName(watchlist)){
// Loop through all the symbols
for (i = 0; i < BarCount; i++) {
// Check if the closing price is above the 50-period SMA for the current bar
if (C[i] >= sma[i] ){
aboveSmaCount = aboveSmaCount + 1;
}else{
belowSmaCount=belowSmaCount+1;
}
}
}
// Display the count of stocks above the 50-period SMA
noOfStocks = "Stocks Above 50 SMA: " + aboveSmaCount+" Below "+belowSmaCount;
printf(noOfStocks);
_SECTION_END();
And is it possible to print only count in exploration.
I am able to print all stocks name but not only count.
_SECTION_BEGIN("Print SMA in Analysis");
// Define the period for the SMA
smaPeriod = 50;
// Calculate the 50-period SMA
sma = MA(C, smaPeriod);
Filter= C>=sma;
AddRankColumn();
// Plot the SMA on the chart
AddColumn(sma,"SMA 50");
AddColumn(C,"Price");
// Use printf to display the SMA values in the Analysis commentary
printf("SMA Values: %g", sma);
_SECTION_END();