Hello everybody,
The code example below, I'm only interested in the part "Market Criteria".
The following information beforehand: With such a system one would buy in the bull market and sell in the bear market. Do not be confused if this is the other way round, because I am only concerned with the correct detection of bull and bear markets. I use these in several strategies and have a general problem with them. Here I have just used a simple example.
The real problem:
A bull market is to be recognized when S & P500> MA50.
If a bear market is detected (S & P500 <MA50), this condition should be kept for at least 3 bars (for example, to prevent toggling).
The problem now is that with the "barsSince" function, the bull market / bear market detection will not work, for example, the strong bull market will trade in 2017, although according to code only the bear market should be traded.
If I look at it in Explore mode or as an indicator (also included in the code) then it's always right, both with and without "barsSince".
Again briefly described:
Why is there a difference in the backtest if the "barsSince" function is used but not in explore or indicator mode?
Thank you for any help
Frank
MaxHoldingPeriod = Param("Max Holding Period",25,1,999,1);
StoppLossATR21 = Param("x*ATR21 Stop",5,0,20,0.5);
CommissionPerShare = Param("Commission per share $",0.01,0.00,1,0.00);
StoppLossMax = Param("Stopp Loss max in %",30,0,100,1);
MaxEquity = 10000;
SetOption("AccountMargin",50);
SetTradeDelays( 1, 1, 1, 1 );
SetOption("InitialEquity",100000); // Kontogrösse am Anfang des Backtests
SetOption("AllowSameBarExit",True);
SetOption("ActivateStopsImmediately",True);
SetOption("AllowPositionShrinking",True);
SetOption("ReverseSignalForcesExit",True);
SetOption("MinShares",1);
SetOption("MinPosValue",0);
SetOption("UsePrevBarEquityForPosSizing",True);
//SetOption("PortfolioReportMode",0); //0: trade list, 1: detailed log, 2: summary
SetOption("HoldMinBars",0);
SetOption("MaxOpenLong",9); //zero: no limit
SetOption("MaxOpenShort",0); //zero: no limit
SetOption("MaxOpenPositions",999);
SetOption("UseCustomBacktestProc",False);
SetOption("CommissionMode",3); //per share
SetOption("CommissionAmount",CommissionPerShare);
//------------------------------------------------------------------------------------------------------------------
RoundLotSize = 1;
HoldingPeriod = MaxHoldingPeriod;
//---------------------------------------------------------------------------------------------------------------
//--- General
//---------------------------------------------------------------------------------------------------------------
SPY = "$SPX";
//SPY = "SPY";
SetForeign( SPY );
SPY_C = C;
RestorePriceArrays();
OnSecondLastBarOfDelistedSecurity = !IsNull(GetFnData("DelistingDate")) AND (BarIndex() == (LastValue(BarIndex()) -1) OR DateTime() >= GetFnData("DelistingDate") );
OnLastTwoBarsOfDelistedSecurity = !IsNull(GetFnData("DelistingDate")) AND (BarIndex() >= (LastValue(BarIndex()) -1) OR DateTime() >= GetFnData("DelistingDate") );
//---------------------------------------------------------------------------------------------------------------
//--- Market Criteria
//---------------------------------------------------------------------------------------------------------------
//bearmarket
lv_sell_market = (SPY_C < MA(SPY_C, 50));
lv_sell_signal = IIf(lv_sell_market == True AND Ref(lv_sell_market,-1) == False, True, False);
//bullmarket
lv_buy_market = (SPY_C > MA(SPY_C, 50));
lv_market = Flip(lv_buy_market, lv_sell_market);
//*****************************************************************************
//This does work, but without the required delay
//market_mod = IIf(lv_market == True,0,1);
//This doesn't work
market_mod = IIf(lv_market == True AND BarsSince(lv_sell_signal)>3,0,1);
//*****************************************************************************
//---------------------------------------------------------------------------------------------------------------
//--- Liquid Criteria
//---------------------------------------------------------------------------------------------------------------
Crit1 = C > 10;
Crit2 = MA(V,50) > 200000;
lv_buy_liquid = Crit1 AND Crit2;
//---------------------------------------------------------------------------------------------------------------
Buy = market_mod > 0 AND lv_buy_liquid AND NOT OnLastTwoBarsOfDelistedSecurity;
Sell = market_mod == 0 OR OnSecondLastBarOfDelistedSecurity;
MarketPhase = 15;
//---------------------------------------------------------------------------------------------------------------
BuyPrice = Open;
SellPrice = Open;
PositionScore = 100-RSI(14);
SetPositionSize( MarketPhase, spsPercentOfEquity );
ApplyStop( stopTypeLoss,stopModePercent,30 );
ApplyStop( stopTypeNBar, stopModeBars, HoldingPeriod-1 );
Filter=1;
AddColumn(market_mod,"market_mod");
Plot(market_mod,"market_mod",colorRed,styleLine, 0, 3);
//Plot(lv_buy_market,"buy",colorGreen,styleLine, 0, 3);