I would like to do some portfolio backtesting of many long and short "strategies" on different symbols/tickers.
I had a naive idea that I would just simply have each (for "strategy" and symbol) formula in separate file and then bring them all together with #include "path_to_file"; in one afl file where I would backtest them all...
Of course I do not know how to properly do this or perhaps is not even possible...
Details...
Let's have 4 "sub strategies" one short and one long for each "regime" (above/below long term moving average).
"Long - Uptrend.afl":
if (Name() == "SPY") {
isAboveLongTermMovingAverage = C > MA(C, 200);
rsiLongUptrend = RSIa(C, periods=4);
Buy = isAboveLongTermMovingAverage AND (rsiLongUptrend < 5);
BuyPrice = Close;
Sell = (C > MA(C, 10)) OR (rsiLongUptrend > 50);
SellPrice = Close;
}
"Long - Dwontrend.afl":
if (Name() == "SPY") {
isBelowLongTermMovingAverage = C < MA(C, 200);
rsiLongDowntrend = RSIa(C, periods=10);
Buy = isBelowLongTermMovingAverage AND (rsiLongDowntrend < 5);
BuyPrice = Close;
Sell = (C > MA(C, 5)) OR (rsiLongUptrend > 50);
SellPrice = Close;
}
"Short - Uptrend.afl":
if (Name() == "SPY") {
isAboveLongTermMovingAverage = C > MA(C, 200);
rsiShortUptrend = RSIa(C, periods=20);
Short = isAboveLongTermMovingAverage AND (rsiShortUptrend > 95);
ShortPrice = Close;
Cover = (C < MA(C, 5)) OR (rsiShortUptrend < 50);
CoverPrice = Close;
}
"Short - Downtrend.afl":
if (Name() == "SPY") {
isBelowLongTermMovingAverage = C < MA(C, 200);
rsiShortDowntrend = RSIa(C, periods=5);
Short = isBelowLongTermMovingAverage AND (rsiShortUptrend > 80);
ShortPrice = Close;
Cover = (C < MA(C, 5)) OR (rsiShortUptrend < 50);
CoverPrice = Close;
}
"Long - Short - Uptrend - Downtrend - Backtest.afl"
initialEquity = 100000;
maxOpenPositions = 1;
PositionSizePctOfEquity = 100/maxOpenPositions;
PositionSizeDollars = initialEquity/maxOpenPositions;
SetOption("AllowSameBarExit", False);
SetOption("MaxOpenPositions", maxOpenPositions);
setOption("holdminbars", 1);
SetOption("initialEquity", initialEquity);
SetPositionSize(100, spsPercentOfEquity);
SetTradeDelays( 0, 0, 0, 0 );
RoundLotSize = 1;
Buy = Sell = Short = Cover = 0;
BuyPrice = SellPrice = ShortPrice = CoverPrice = 0;
if (Name() == "SPY") {
isAboveLongTermMovingAverage = C > MA(C, 200);
isBelowLongTermMovingAverage = C < MA(C, 200);
rsiLongUptrend = RSIa(C, periods=4);
rsiLongDowntrend = RSIa(C, periods=10);
rsiShortUptrend = RSIa(C, periods=20);
rsiShortDowntrend = RSIa(C, periods=5);
Buy = (isAboveLongTermMovingAverage AND (rsiLongUptrend < 5)) OR
(isBelowLongTermMovingAverage AND (rsiLongDowntrend < 5));
BuyPrice = Close;
Sell = (C > MA(C, 10)) OR (C > MA(C, 5)) OR (rsiLongUptrend > 50);
SellPrice = Close;
Short = (isAboveLongTermMovingAverage AND (rsiShortUptrend > 95)) OR
(isBelowLongTermMovingAverage AND (rsiShortUptrend > 80));
ShortPrice = Close;
Cover = (C < MA(C, 5)) OR (rsiShortUptrend < 50);
CoverPrice = Close;
}
"Long - Short - Uptrend - Downtrend - Backtest - naive.afl" (not working, of course):
initialEquity = 100000;
maxOpenPositions = 1;
PositionSizePctOfEquity = 100/maxOpenPositions;
PositionSizeDollars = initialEquity/maxOpenPositions;
SetOption("AllowSameBarExit", False);
SetOption("MaxOpenPositions", maxOpenPositions);
setOption("holdminbars", 1);
SetOption("initialEquity", initialEquity);
SetPositionSize(100, spsPercentOfEquity);
SetTradeDelays( 0, 0, 0, 0 );
RoundLotSize = 1;
Buy = Sell = Short = Cover = 0;
BuyPrice = SellPrice = ShortPrice = CoverPrice = 0;
#include "C:\\tmp\\Trading\\Amibroker\\Questions\\Long - Uptrend.afl";
#include "C:\\tmp\\Trading\\Amibroker\\Questions\\Long - Downtrend.afl";
#include "C:\\tmp\\Trading\\Amibroker\\Questions\\Short - Uptrend.afl";
#include "C:\\tmp\\Trading\\Amibroker\\Questions\\Short - Downtrend.afl";
I would like to have something as close as the last code sample. Because otherwise I need to make very diligent edit/additions/deletions to Buy, Sell, Short, Cover. How to do this in some more elegant, user friendly way?