this is my pattern recognition program That i will release to the public
I need just a simple program in vb that if i enter today date and a future date i need to know how many trading days left must take into account hoilidays
this is my pattern recognition program That i will release to the public
I need just a simple program in vb that if i enter today date and a future date i need to know how many trading days left must take into account hoilidays
Nick are you talking about looking into the future and need to count trading days? That can all be done in AmiBroker but would be tedious as you need to find and eliminate all weekends and all future market holidays.
Can be done but not without effort. Here is a list of trading holidays in the US markets if it is of any help.
https://www.marketbeat.com/stock-market-holidays/
Looking into the past and counting trading days would be easy of course if that is what you need.
// Barcount via AA window
// Getting any barcount by selecting any range setting in AA window.
fbr = Status( "firstbarinrange" );
lbr = Status( "lastbarinrange" );
bi = BarIndex();
bars = ValueWhen( lbr, bi ) - ValueWhen( fbr, bi ) + 1;
Filter = lbr;
AddColumn( bars, "# of Bars", 1.0);
so I wrote this a few years ago, i’m absolutely sure there is a simpler way of doing it, and someone will probably show how to do it in three lines, but at the time I was learning amibroker.
The code uses a list of no-trading days (configured at asxNTDN, I just update the list every year with the next year’s NTDN when the exchange publish the list), and steps forward and backward to create setup signals 31 days before actual dividend dates, and 31 days before the next predicted dividend date (based on the last two dividend dates plus 12 months).
SetBarsRequired(sbrAll,0);
asxNTDN = "1100101,1100126,1100402,1100405,1100426,1100614,1101227,1101228," +
"1110103,1110126,1110422,1110425,1110426,1110613,1111226,1111227," +
"1120102,1120126,1120406,1120409,1120425,1120611,1121225,1121226," +
"1130101,1130128,1130329,1130401,1130425,1130610,1131225,1131226," +
"1140101,1140127,1140418,1140421,1140425,1140609,1141225,1141226," +
"1150101,1150126,1150403,1150406,1150525,1150608,1151225,1151228," +
"1160101,1160126,1160325,1160328,1160425,1160613,1161226,1161227," +
"1170102,1170126,1170414,1170417,1170425,1170612,1171225,1171226" ;
SetupLength = Param("Setup Length (calendar days)", 31, 5, 100, 1);
MinTradeDays = Param("Minimum Planned Market Days in Trade", 5, 2, 95, 1);
constituentIndex = ParamStr("Index Symbol","$XTL");
constituencyCheck = ParamToggle("Check for constituency", "No|Yes", True);
MinimumYield = Param("Yield Required to consider stock (%)", 4, 0, 10, 0.25);
SetTradeDelays(1,1,1,1);
SetPositionSize(10000, spsValue);
SetOption("InitialEquity", 100000);
SetOption("MaxOpenPositions", 8);
SetOption("AccountMargin", 15);
Buy = 0;
Sell = 0;
function DateNumberToRataDie( DateNumber )
{
num = DateNumber/10000;
yyyy = int(num) + 1900;
num = frac(num) * 100;
mm = int(num);
dd = frac(num)*100;
yyyy = yyyy + int((mm-14)/12);
mm = IIf(mm < 3, mm+12, mm);
RataDieNum = round(dd + int((153*mm-457)/5) + 365*yyyy + int(yyyy/4) - int(yyyy/100) + int(yyyy/400) - 306);
return (RataDieNum);
}
function RataDieToDateNumber(RataDieNum)
{
z = RataDieNum + 306;
g = z - 0.25;
a = int(G/36524.25);
b = a - int(A/4);
yr = int((b + g)/365.25);
Cc = b + z - int(365.25 * yr);
mm = int((5 * Cc + 456)/153);
dd = Cc - int((153*mm-457)/5);
yr = IIf(mm > 12, yr + 1, yr);
mm = IIf(mm > 12, mm - 12, mm);
dn = (yr-1900)*10000+mm*100+dd;
return dn;
}
function NoTradingDay( RataDieNum )
{
global DN, NasdaqNTDN;
DN = RataDieToDateNumber(RataDieNum );
DnStr = NumToStr(DN,1.0,False);
DW = RataDieNum%7;
return DW == 0 OR DW == 6 OR StrFind( asxNTDN, DNStr);
}
ExDivBar = 0;
PrevDBar = 0;
SetupBar = 0;
LastBBar = 0;
LastSBar = 0;
yield = Ref(Sum(Aux2, 260), -1) / OpenInt;
BarDT = DateTime();
BarDN = DateTimeConvert( 0, BarDT );
for (i = 1; i < BarCount ; i++)
{
if (Aux2[i] == 0) continue;
else
{
PrevDRD = DateNumberToRataDie( BarDN[i] );
ExDivRD = PrevDRD + 1;
while( NoTradingDay(ExDivRD) )
{
ExDivRD++;
}
LastSellSignalRD = PrevDRD - 1;
while( NoTradingDay(LastSellSignalRD) )
{
LastSellSignalRD--;
}
SetupRD = ExDivRD - SetupLength;
while( NoTradingDay(SetupRD) )
{
SetupRD--;
}
LastBuySignalRD = LastSellSignalRD;
Index = MinTradeDays;
while(Index > 0 AND LastBuySignalRD >= SetupRD)
{
LastBuySignalRD--;
while( NoTradingDay(LastBuySignalRD) )
{
LastBuySignalRD--;
}
Index--;
}
SetupDN = RataDieToDateNumber( SetupRD );
LastBuySignalDN = RataDieToDateNumber( LastBuySignalRD );
LastSellSignalDN = RataDieToDateNumber( LastSellSignalRD );
PrevDDN = BarDN[i];
ExDivDN = RataDieToDateNumber( ExDivRD );
SetupDT = DateTimeConvert( 2, SetupDN );
LastBuySignalDT = DateTimeConvert( 2, LastBuySignalDN );
LastSellSignalDT = DateTimeConvert( 2, LastSellSignalDN );
PrevDDT = BarDT[i];
ExDivDT = DateTimeConvert( 2, ExDivDN );
printf("\nSetup Date: " + DateTimeToStr(SetupDT) + "\nLastBuySignal: " + DateTimeToStr(LastBuySignalDT) +
"\nLastSellSignal: " + DateTimeToStr(LastSellSignalDT) + "\nPrev Date: " + DateTimeToStr(PrevDDT) +
"\nExDiv Date: " + DateTimeToStr(ExDivDT) + "\nCurrent Yield: " + yield[i] + "\n");
Index = BarCount - 1;
while(Index > 1) //inner loop to find the bars
{
if ( ( SetupDN <= BarDN[Index] ) AND ( SetupDN > BarDN[Index - 1] ) )
{
SetupBar[Index] = True; //SetupDT;
}
if ( ( LastBuySignalDN <= BarDN[Index] ) AND ( LastBuySignalDN > BarDN[Index - 1] ) )
{
LastBBar[Index] = True; //LastBuySignalDT;
}
if ( ( LastSellSignalDN <= BarDN[Index] ) AND ( LastSellSignalDN > BarDN[Index - 1] ) )
{
LastSBar[Index] = True; //LastSellSignalDT;
}
if ( ( PrevDDN <= BarDN[Index] ) AND ( PrevDDN > BarDN[Index - 1] ) )
{
PrevDBar[Index] = True; //PrevDDT;
}
if ( ( ExDivDN <= BarDN[Index] ) AND ( ExDivDN > BarDN[Index - 1] ) )
{
ExDivBar[Index] = True; //ExDivDT;
}
Index--;
}
}
}
//predict next two dividend dates (last two dates plus a year)
count = 0;
for (i = BarCount - 1; (count < 2) AND (i > 0); i--)
{
if (SetupBar[i] == True)
{
printf("\nfound Setup Bar at " + DateTimeToStr(barDT[i]));
ThisSetupDT = BarDT[i];
ThisSetupDN = DateTimeConvert( 0, ThisSetupDT );
ExDivRD = DateNumbertoRataDie( ThisSetupDN ) + SetupLength + 364;
ExDivDT = DateTimeConvert( 2, RataDieToDateNumber(ExDivRD) );
PrevDRD = ExDivRD - 1;
while( NoTradingDay(PrevDRD) )
{
PrevDRD--;
}
LastSellSignalRD = PrevDRD - 1;
while( NoTradingDay(LastSellSignalRD) )
{
LastSellSignalRD--;
}
SetupRD = ExDivRD - SetupLength;
while( NoTradingDay(SetupRD) )
{
SetupRD--;
}
LastBuySignalRD = LastSellSignalRD;
Index = MinTradeDays;
while(Index > 0 AND LastBuySignalRD >= SetupRD)
{
LastBuySignalRD--;
while( NoTradingDay(LastBuySignalRD) )
{
LastBuySignalRD--;
}
Index--;
}
SetupDN = RataDieToDateNumber( SetupRD );
LastBuySignalDN = RataDieToDateNumber( LastBuySignalRD );
LastSellSignalDN = RataDieToDateNumber( LastSellSignalRD );
PrevDDN = RataDieToDateNumber( PrevDRD );
SetupDT = DateTimeConvert( 2, SetupDN );
LastBuySignalDT = DateTimeConvert( 2, LastBuySignalDN );
LastSellSignalDT = DateTimeConvert( 2, LastSellSignalDN );
PrevDDT = DateTimeConvert( 2, PrevDDN );
printf("\nPredicted Setup Date: " + DateTimeToStr(SetupDT) + "\nLastBuySignal: " + DateTimeToStr(LastBuySignalDT) +
"\nLastSellSignal: " + DateTimeToStr(LastSellSignalDT) + "\nPrev Date: " + DateTimeToStr(PrevDDT) +
"\nPredicted ExDiv Date: " + DateTimeToStr(ExDivDT) + "\n" );
Index = BarCount - 1;
while(Index > 0) //inner loop to find the bars
{
if (SetupDN == BarDN[Index])
{
SetupBar[Index] = True; //SetupDT;
}
if (LastBuySignalDN == BarDN[Index])
{
LastBBar[Index] = True; //LastBuySignalDT;
}
if (LastSellSignalDN == BarDN[Index])
{
LastSBar[Index] = True; //LastSellSignalDT;
}
if (PrevDDN == BarDN[Index])
{
PrevDBar[Index] = True; //PrevDDT;
}
if (ExDivDN == BarDN[Index])
{
ExDivBar[Index] = True; //ExDivDT;
}
Index--;
}
count++;
}
}
OnSecondLastBarOfDelistedSecurity = BarIndex() == (LastValue(BarIndex()) -1) AND !IsNull(GetFnData("DelistingDate"));
rsilevel = RSI(10);
constituent = IIf(constituencyCheck, IsIndexConstituent(constituentIndex), 1);
for (i = 1; i < BarCount; i++)
{
if ( SetupBar[i] AND constituent[i] AND (yield[i] > MinimumYield/100) )
{
for (j = i; j < BarCount; j++)
{
if (LastBBar[j] ) break; //OR PrevDBar[j]
if (rsilevel[j] > 60 )
{
Buy[j] = 1;
printf("\n Buy Signal : " + DateTimeToStr(barDT[j]) );
for (k = j; (k < BarCount ) ; k++) //AND NOT(ExDivBar[k])
{
if (rsilevel[k] < 40 OR LastSBar[k] OR onSecondLastBarOfDelistedSecurity[k])
{
Sell[k] = 1;
printf("\n Sell Signal: " + DateTimeToStr(barDT[k]) );
break;
}
}
break;
}
}
}
}
for (i = 2; i < BarCount; i++)
{
if (Buy[i-1]) BuyPrice[i]=O[i];
if (Sell[i-1]) SellPrice[i] = IIf(PrevDBar[i],C[i],O[i]);
}