Hello community, I'm having some troubles testing the following idea:
The system calculate IBS indicator (internal bar strength) and if its value is inside a particular range then it buys a position and hold for a certain amount of bars. If while the position is still open it occurs that another value of IBS is into the range, then it buys another position and hold it for the same amount of bars (so this part of the trade will be closed after the first part). Buy the next Monday open and sell at the nth week close. This have to be allowed for a certain amount of times.
Furthermore this code have to work for a portfolio of stocks each one with it's IBS and holding period parameters.
I have studied the commands and backtest documentation and read several forum posts like
and few others
but there is something that I can't grasp.
I'm happy to share with you my attempts and I apologize in advance for the long message.
To fix the ideas please consider to trade on the ticker A2A.MI (data from yahoo! finance) and WEEKLY time frame. Backtest period: the first quarter of the 2010. IBS range is [0.4 : 0.55]. Holding period 3 weeks.
FIRST ATTEMPT
initEqty = 3000;
maxPositions = 3;
investedSize = 1000;
holdingPeriod = 3;
SetBacktestMode(backtestRegularRawMulti);
SetOption("InitialEquity", initEqty);
SetOption("MaxOpenPositions", maxPositions);
SetPositionSize(investedSize, spsValue);
SetOption("CommissionMode", 1);
SetOption("CommissionAmount", 0.0);
SetTradeDelays(1,0,0,0);
IBS = SafeDivide((C-L),(H-L), Null);
Buy = IBS >= 0.40 AND IBS <= 0.55;
Sell = Ref(Buy, -holdingPeriod);
This doesn't work for me due the fact the sell command closes all the position at the same time, but I want a scale out closing instead.
SECOND ATTEMPT
Seeing that the duration of the trade is fixed, I've tried to use the applyStop function:
initEqty = 3000;
maxPositions = 3;
investedSize = 1000;
holdingPeriod = 3;
SetBacktestMode(backtestRegularRawMulti);
SetOption("InitialEquity", initEqty);
SetOption("MaxOpenPositions", maxPositions);
SetPositionSize(investedSize, spsValue);
SetOption("CommissionMode", 1);
SetOption("CommissionAmount", 0.0);
SetTradeDelays(1,0,0,0);
BuyPrice = Open;
SellPrice = Close;
IBS = (C-L)/(H-L);
Buy = IBS >= 0.40 AND IBS <= 0.55;
Sell = 0;
ApplyStop(stopTypeNBar, stopModeBars, holdingPeriod-1);
The resulting detailed report is the following:
which is CORRECT:
there are no entry signal until the 19th of February, so the first buy is the next week Monday open and the close at the 12/03/2010 (three week later), the week after the first signal another IBS value is inside the range, so the trading system opens another position that is closed at the 19/03/2010.
THIRD ATTEMPT
Now that I have a working single stock trading system, I've slightly modified it to handle different time series for the portfolio backtesting:
initEqty = 3000;
SetBacktestMode(backtestRegularRawMulti);
SetOption("InitialEquity", initEqty);
SetOption("CommissionMode", 1);
SetOption("CommissionAmount", 0.0);
SetTradeDelays(1,0,0,0);
BuyPrice = Open;
SellPrice = Close;
if (Name() == "A2A.MI")
{
IBS = SafeDivide((C-L),(H-L),0);
holdingPeriod = 3;
maxPositions = holdingPeriod;
SetOption("MaxOpenPositions", maxPositions);
SetPositionSize(initEqty / holdingPeriod, spsValue);
Buy = IBS >= 0.40 AND IBS <= 0.55;
Sell = 0;
ApplyStop(stopTypeNBar, stopModeBars, holdingPeriod-1);
}
Unfortunately this doesn't work in the portfolio backtesting because the closes are all at the same time at the end of the backtesting time, as shown below.
on a single "current" time series it works:
the same on multiple time series doesn't work anymore:
Why this behavior?
Please note that there are also some days different than the ones of the previous backtest (for example the 14/02/10). I've verified that this occurs due the fact I did't padded and aligned the time series to a common one.
FOURTH ATTEMPT
My last attempt is using the sigScaleIn and sigScaleOut parameter for the buy command:
SetBacktestMode(backtestRegularRawMulti);
SetOption("AllowSameBarExit", True );
SetOption("InitialEquity", initEqty);
SetOption("MaxOpenPositions", maxPositions);
SetPositionSize(investedSize, spsValue);
SetOption("CommissionMode", 1);
SetOption("CommissionAmount", 0.0);
SetTradeDelays(1,0,0,0);
BuyPrice = O;
SellPrice = C;
IBS = SafeDivide((C-L), (H-L), Null);
Buy = False;
Sell = False;
IBScond = False;
scaleOutCond = False;
duration = 0;
// Trading System
if (Name() == "A2A.MI")
{
duration = 3;
IBScond = IBS >= 0.40 AND IBS <= 0.55;
SetOption("HoldMinBars", duration);
scaleOutCond = Ref(IBScond, -duration);
Buy = IIf(IBScond AND scaleOutCond == False, sigScaleIn, IIf(scaleOutCond AND IBScond == False, sigScaleOut,0));
Sell = 0;
}
// plottings
PlotOHLC(O,H,L,C, "", colorBlack, styleBar);
SetChartOptions(0, chartShowDates);
PlotShapes(IIf(IBScond,shapeUpArrow,shapeNone), colorBlack, 0, Ref(O, 1), 0, 1);
PlotShapes(IIf(scaleOutcond,shapeDownArrow,shapeNone), colorRed, 0, C, 0, 1);
I've set the option "AllowSameBarExit" to True, because I intended to hard control the signals in the Buy statement:
IBScond AND scaleOutCond == False
scaleOutCond AND IBScond == False
this is the result after padding and aligning to A2A.MI
only for current timeserie
portfolio backtest:
As you can see there are 2 kind of errors:
- a wrong entry signal at the 08/01/2010 which shouldn't be
- the system doesn't close the trades after 3 weeks.
I can't figure out how to fix this, what I'm doing wrong?
Many thanks for any insight
Marco