Programming An End Of Month Stock Index System in TS-18

This video shows you how to program a simple system that buys the stock market at closing price of any month whenever that closing price is greater than the 50-day moving average.  Long positions are exited on the fourth day, following entry, at the close. During those four days, the system will exit on a close that is below the 50-day moving average.  This system is a short term hold, but uses a longer term moving average as trend indicator.  In the video I thought I was testing through Feb 2020 but in fact it was 2019.  At the end of this post I will publish the performance metrics through May 2020.

The key to this strategy is to be able to sneak a peek at the date of curBar + 1.  In doing so, you can determine if myDate[curBar] is the end of the current month.  TradeStation’s EasyLanguage allows you to look at tomorrow’s date but will not allow you to execute on this bars close.  You could execute on the next bar’s open but what if the month ends on a weekend.  Then you might get in at a worse price.  Anyways this was a good example where you can cheat by sneaking a peak, but not taking advantage of this information.  TradingSimula-18 will let you do almost anything, but with power comes responsibility.

You can re-use this code for determining the month end as long as you make program any Good Friday’s that is the last day of the month.  In a 15 year period this only happened in 2013 and 2018.

Here is the EasyLanguage code:

Inputs: movAvgPeriods(50);
vars: endOfMonth(false),theDayOfWeek(0),theMonth(0),theDayOfMonth(0),isLeapYear(False);

endOfMonth = false;
theDayOfWeek = dayOfWeek(date);
theMonth = month(date);
theDayOfMonth = dayOfMonth(date);
isLeapYear = mod(year(d),4) = 0;

// 29th of the month and a Friday
if theDayOfMonth = 29 and theDayOfWeek = 5 then
endOfMonth = True;
// 30th of the month and a Friday
if theDayOfMonth = 30 and theDayOfWeek = 5 then
endOfMonth = True;
// 31st of the month
if theDayOfMonth = 31 then
endOfMonth = True;
// 30th of the month and April, June, Sept, or Nov
if theDayOfMonth = 30 and (theMonth=4 or theMonth=6 or theMonth=9 or theMonth=11) then
endOfMonth = True;
// 28th of the month and February and not leap year
if theDayOfMonth = 28 and theMonth = 2 and not(isLeapYear) then
endOfMonth = True;
// 29th of the month and February and a leap year or 28th, 27th and a Friday
if theMonth = 2 and isLeapYear then
Begin
If theDayOfMonth = 29 or ((theDayOfMonth = 28 or theDayOfMonth = 27) and theDayOfWeek = 5) then
endOfMonth = True;
end;
// 28th of the month and Friday and April, June, Sept, or Nov
if theDayOfMonth = 28 and (theMonth = 4 or theMonth = 6 or
theMonth = 9 or theMonth =11) and theDayOfWeek = 5 then
endOfMonth = True;
// 27th, 28th of Feb and Friday
if theMonth = 2 and theDayOfWeek = 5 and theDayOfMonth = 27 then
endOfMonth = True;
// 26th of Feb and Friday and not LeapYear
if theMonth = 2 and theDayOfWeek = 5 and theDayOfMonth = 26 and not(isLeapYear) then
endOfMonth = True;
// Memorial day adjustment
If theMonth = 5 and theDayOfWeek = 5 and theDayOfMonth = 28 then
endOfMonth = True;
//Easter 2013 adjustment
If theMonth = 3 and year(d) = 113 and theDayOfMonth = 28 then
endOfMonth = True;
//Easter 2018 adjustment
If theMonth = 3 and year(d) = 118 and theDayOfMonth = 29 then
endOfMonth = True;

if endOfMonth and c > average(c,movAvgPeriods) then
Buy("BuyDay") this bar on close;

If C <average(c,movAvgPeriods) then
Sell("MovAvgExit") this bar on close;
If BarsSinceEntry=4 then
Sell("4days") this bar on close;
EasyLanguage Code To Determine End Of Month

Here is the heart of the TradingSimula-18 code that executes the trades.

#  Long Entry
# Okay Let's put in some logic to create a long position
if isNewMonth(myDate,curBar+1) and myClose[curBar] > myAverage and mp !=1:
price = myClose[curBar]
tradeName = "LDOM-BUY"
numShares = posSize
if mp <= -1:
profit,curShares,trades = bookTrade(-1,0,price,myDate[curBar],"RevshrtLiq",curShares)
marketMonitorList[curMarket].tradesList.append(trades);todaysCTE = profit
profit,curShares,trades = bookTrade(entry,buy,price,myDate[curBar],tradeName,numShares)
barsSinceEntry = 1
marketMonitorList[curMarket].setSysMarkTrackingInfo(tradeName,cumuProfit,mp,barsSinceEntry,curShares,trades)
# Long Exit
if mp == 1 and myClose[curBar] < myAverage and barsSinceEntry > 1:
price = myClose[curBar]
tradeName = "Lxit-MAVG"
numShares = curShares
profit,curShares,trades = bookTrade(exit,ignore,price,myDate[curBar],tradeName,numShares)
todaysCTE = profit;barsSinceEntry = 0
marketMonitorList[curMarket].setSysMarkTrackingInfo(tradeName,cumuProfit,mp,barsSinceEntry,curShares,trades)

# Long Exit
if mp == 1 and barsSinceEntry > 4:
price = myClose[curBar]
tradeName = "4BarXIT"
numShares = curShares
profit,curShares,trades = bookTrade(exit,ignore,price,myDate[curBar],tradeName,numShares)
todaysCTE = profit;barsSinceEntry = 0
marketMonitorList[curMarket].setSysMarkTrackingInfo(tradeName,cumuProfit,mp,barsSinceEntry,curShares,trades)
TradingSimula-18 Snippet

TradingSimula-18 Performance Metrics:

 

-----------------------------------------
Sys Name : LastDOMSys
Mkt Symb : ES
Profit/Loss : 28037.5
Num Trades : 124
Percent Wins : 0.613
Avg Win : 1103.62
Avg Loss : -1163.28
Avg Trade: 226.11
Max DrawDown: 14200.0
Avg DrawDown: 3372.5
AVG Monthly Return: 150.73925
Monthly StdDev: 1443.12687
-----------------------------------------

Start Trade Drawdown Analysis Of : LastDOMSys : Max. ClsTrd Draw Down 13550
-----------------------------------------
Probability of DD < 677 is 0.008
Probability of DD < 1355 is 0.008
Probability of DD < 2033 is 0.008
Probability of DD < 2713 is 0.008
Probability of DD < 3393 is 0.008
Probability of DD < 4075 is 0.056
Probability of DD < 4757 is 0.056
Probability of DD < 5441 is 0.056
Probability of DD < 6125 is 0.056
Probability of DD < 6811 is 0.056
Probability of DD < 7497 is 0.056
Probability of DD < 8185 is 0.056
Probability of DD < 8873 is 0.056
Probability of DD < 9563 is 0.056
Probability of DD < 10253 is 0.145
Probability of DD < 10945 is 0.145
Probability of DD < 11637 is 0.145
Probability of DD < 12331 is 0.145
Probability of DD < 13025 is 0.145
Probability of DD < 13721 is 1.000
LDOM Equity Curve from TS-18

classic

yes

About This Site

This site is home to George’s Excellent Adventure into TradingSimula_18 and Python.  George grew tired of the old and expensive back testing software so he created his own and now is able to test and develop  Trend Following Systems utilizing EOD data and EOD intra-testing portfolio management.  This software, TradingSimula_18 can be found in his Trend Following Systems: A DIY Project – Batteries Included book – now in its 2nd edition.

December 2024
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031