Category Archives: Stocks & Commodities traders tips

Backtesting A Mean-Reversion Strategy In Python

The importable AIQ EDS file based on Anthony Garner’s article in May 2019 Stocks & Commodities “Backtesting A Mean-Reversion Strategy In Python,” can be obtained on request via email to info@TradersEdgeSystems.com. The code is also shown below.

I backtested the author’s mean-reversion system (MeanRev.eds) using both the EDS module, which tests every trade on a one-share basis, and also via the Portfolio Manager, which performs a trading simulation.

The short side strategy showed a loss overall in the EDS test so I tested only the long side in the Portfolio Manager. I selected trades using the z-score, taking the lowest values.

For capitalization, I used max of three trades per day with a max total of 10 open trades at one time, 10% allocated to each position. I did not deduct slippage but did deduct commissions. I used a recent list of the NASDAQ 100 stocks to run the test. The equity curve and account statistics report are shown in Figure 7.

Sample Chart

FIGURE 7: AIQ. This shows the equity curve (blue line) from long-only trading the NASDAQ 100 list of stocks from 1999 to March 15, 2019. The red line is the NDX index.

!Backtesting a Mean-Reversion Strategy In Python !Author: Anthony Garner, TASC May 2019 !Coded by: Richard Denning 3/14/19 !www.TradersEdgeSystems.com 

!ABBREVIATIONS:
C is [close].

!INPUTS:
meanLen is 10.
longZmult is -1.
shortZmult is 1.
meanMult is 10.

!FORMULAS:

SMA is simpleavg(C,meanLen).
LMA is simpleavg(C,meanLen*meanMult).
STD is sqrt(variance(C,meanLen)).
zScore is (C - SMA) / STD.

!TRADING SIGNALS & EXITS:

buyLong if zScore < longZmult and SMA > LMA.
sellShort if zScore > shortZmult and SMA < LMA.
exitLong if valresult(zScore,1) < -0.5 and zScore > 0.5.
exitShort if valresult(zScore,1) > 0.5 and zScore < -0.5.

showValues if 1.

—Richard Denning
info@TradersEdgeSystems.com
for AIQ Systems

Weekly & Daily Stochastics

The AIQ code based on Vitali Apirine’s article in the September issue of Stocks and Commodities, “Weekly and Daily Stochastics, is provided below

Using Apirine’s weekly and daily stochastic indicators and a moving average to determine trend direction, I created an example system (long only) with the following rules:

Enter long next bar at open when all of the following are true:

  1. The 200-day simple average of the NDX is greater than the day before
  2. The 200-day simple average of the stock is greater than the day before
  3. Both the weekly and daily stochastic indicators have been below 20 in the last five days
  4. Both the weekly and daily stochastic indicators are greater than the day before.

I tested three exits. Figure 8 shows a 21-day hold then exit. Figure 9 shows a three-moving-average trend-following exit. Figure 10 shows an exit using only the weekly &amp; daily stochastic, once both are lower than the day before.

Sample Chart

FIGURE 8: AIQ, BUY and HOLD. Here is the sample equity curve (blue) compared to the NDX (red) for the test using a 21-day hold exit.

Sample Chart

FIGURE 9: AIQ, TREND-FOLLOWING EXIT. Here is the sample equity curve (blue) compared to the NDX index (red) for the test using a trend-following exit.

Sample Chart

FIGURE 10: AIQ, W and D STOCHASTIC EXIT. Here is the sample equity curve (blue) compared to the NDX index (red) for the test using the weekly and daily stochastic indicators.

The 21-day hold test showed a 11.2% return with a maximum drawdown of 29.3%. The trend-following exit test showed a 17.6% return with a maximum drawdown of 28.8%. The test using an exit based on only the weekly and daily stochastic indicators showed a return of 2.9% with a maximum drawdown of 32.5%. All the tests used the same entry rule and were run on an old 2016 list of the NASDAQ 100 stocks with the stocks that are no longer trading deleted.

!WEEKLY AND DAILY STOCHASTIC
!Author: Vitali Apirine, TASC Sept 2018
!Coded by: Richard Denning 7/7/2018
!www.TradersEdgeSystems.com

!INPUTS:
Periods is 14.
Periods1 is 3.
Pds is 70. 
Pds1 is 3.
smaLen1 is 70.
exitType is 1.

!ABBREVIATIONS:
C is [close].
H is [high].
L is [low].

!INDICATOR CODE:
STOCD is (C-LOWRESULT(L,Periods))/(HIGHRESULT(H,Periods)-LOWRESULT(L,Periods))*100. 
SD is Simpleavg(Stocd,Periods1).
StocW is (C-LOWRESULT(L,Pds))/(HIGHRESULT(H,Pds)-LOWRESULT(L,Pds))*100.
SW is Simpleavg(Stocw,Pds1).
HD if hasdatafor(1000) &gt;= 500.
SMA200 is simpleavg(C,200).
SMA200ndx is tickerUDF("NDX",SMA200).

!SYSTEM CODE:
Buy if SMA200ndx &gt; valresult(SMA200ndx,1)
          and SMA200 &gt; valresult(SMA200,1)
          and SW &gt; valresult(SW,1) 
          and SD &gt; valresult(SD,1) 
          and countof(SW &lt; 20,5)&gt;=1 
          and countof(SD &lt; 20,5)&gt;=1 
          and HD.
smaLen2 is smaLen1*2.
smaLen3 is smaLen1*4.
SMA1 is simpleavg(C,smaLen1).
SMA2 is simpleavg(C,smaLen2).
SMA3 is simpleavg(C,smaLen3).
PD is {position days}.

!EXIT TYPE 1 USES THE INDICATOR ONLY
!EXIT TYPE 2 IS TREND FOLLOWING
Sell if (SD &lt; valresult(SD,1) and SW &lt; valresult(SW,1) and exitType=1)
       or (exitType = 2 
           and ((Valresult(C,PD)valresult(SMA1,PD) And Cvalresult(SMA2,PD) And Cvalresult(SMA3,PD) And C 250)).

RSS is C/valresult(C,120).
RSL is C/valresult(C,240).

—Richard Denning

info@TradersEdgeSystems.com

for AIQ Systems

Portfolio Strategy Based On Accumulation/Distribution

The AIQ code based on Domenico D’Errico’s article in the August issue of Stocks &amp; Commodities magazine, “Portfolio Strategy Based On Accumulation/Distribution,” is shown below.

“Whether you are an individual trader or an asset manager, your main goal in reading a chart is to detect the intentions of major institutions, large operators, well-informed insiders, bankers and so on, so you can follow them. Here, we’ll build an automated stock portfolio strategy based on a cornerstone price analysis theory.”

!Portfolio Strategy Based on Accumulation/Distribution
!Author: Domenic D'Errico, TASC Aug 2018
!Coded by: Richard Denning 6/10/18
!www.TradersEdgeSystem.com
!Portfolio Strategy Based on Accumulation/Distribution
!Author: Domenic D'Errico, TASC Aug 2018
!Coded by: Richard Denning 6/10/18
!www.TradersEdgeSystem.com

!SET TO WEEKLY MODE IN PROPERTIES
!ALSO VIEW CHARTS IN WEEKLY MODE

!INPUTS:
rLen is 4.
consolFac is 75. ! in percent
adxTrigger is 30.
volRatio is 1.
volAvgLen is 4.
volDelay is 4.

!CODING ABREVIATIONS:
H is [high].
L is [low].
C is [close].
C1 is valresult(C,1).
H1 is valresult(H,1).
L1 is valresult(L,1).

!RANGE ACCUMULATION/DISTRIBUTION:
theRange is hival([high],rLen) - loval([low],rLen).
Consol if theRange < consolFac/100 * valresult(theRange,rLen).
rRatio is theRange/valresult(theRange,4)*100.

!AVERAGE TRUE RANGE ACCUMULATION/DISTRIBUTION:
avgLen is rLen * 2 - 1.	
TR  is Max(H-L,max(abs(C1-L),abs(C1-H))).
ATR  is expAvg(TR,avgLen).

ConsolATR if ATR < consolFac/100 * valresult(ATR,rLen). atrRatio is ATR / valresult(ATR,4)*100. !ADX ACCUMULATION/DISTRIBUTION: !ADX INDICATOR as defined by Wells Wilder rhigh is (H-H1). rlow is (L1-L). DMplus is iff(rhigh > 0 and rhigh > rlow, rhigh, 0).
DMminus is iff(rlow > 0 and rlow >= rhigh, rlow, 0).
AvgPlusDM is expAvg(DMplus,avgLen).
AvgMinusDM is expavg(DMminus,avgLen).           	
PlusDMI is (AvgPlusDM/ATR)*100.	
MinusDMI is AvgMinusDM/ATR*100.	
DIdiff is PlusDMI-MinusDMI. 		
Zero if PlusDMI = 0 and MinusDMI =0.
DIsum is PlusDMI+MinusDMI.
DX is iff(ZERO,100,abs(DIdiff)/DIsum*100).
ADX is ExpAvg(DX,avgLen).

ConsolADX if ADX < adxTrigger. !CODE FOR ACCUMULATIOIN/DISTRIBUTION RANGE BREAKOUT: consolOS is scanany(Consol,250) then offsettodate(month(),day(),year()). Top is highresult([high],rLen,^consolOS). Top0 is valresult(Top,^consolOS) then resetdate(). Bot is loval([low],rLen,^consolOS). AvgVol is simpleavg([volume],volAvgLen). Bot12 is valresult(Bot,12). BuyRngBO if [close] > Top
and ^consolOS <= 5 and ^consolOS >= 1
and Bot > Bot12
and valresult(AvgVol,volDelay)>volRatio*valresult(AvgVol,volAvgLen+volDelay).
EntryPrice is [close].

Sell if [close] < loval([low],rLen,1).
ExitPrice is [close].

Figure 9 shows the summary backtest results of the range accumulation breakout system using NASDAQ 100 stocks from December 2006 to June 2018. The exits differ from the author’s as follows: I used two of the built-in exits — a 20% stop-loss and a profit-protect of 40% of profits once profit reaches 10%.

Sample Chart

FIGURE 9: AIQ. Here are the summary results of a backtest using NASDAQ 100 stocks.

Figure 10 shows a color study on REGN. The yellow bars show where the range accumulation/distribution shows a consolidation.

Sample Chart

FIGURE 10: AIQ. This color study shows range consolidation (yellow bars).

—Richard Denning

info@TradersEdgeSystems.com

for AIQ Systems

A Technical Method For Rating Stocks

The AIQ code based on Markos Katsanos’ article in this issue, “A Technical Method For Rating Stocks,” is shown below.
Synopsis from Stocks & Commodities June 2018
I’s it possible to create a stock rating system using multiple indicators or other technical criteria? If so, how does it compare with analyst ratings? Investors around the world move billions of dollars every day on advice from Wall Street research analysts. Most retail investors do not have the time or can’t be bothered to read the full stock report and rely solely on the bottom line: the stock rating. They believe these ratings are reliable and base their investment decisions at least partly on the analyst buy/sell rating. But how reliable are those buy/sell ratings? In this article I will present a technical stock rating system based on five technical criteria and indicators, backtest it, and compare its performance to analyst ratings.
!A TECHNICAL METHOD FOR RATING STOCKS
!Author: Markos Katsanos, TASC June 2018
!Coded by: Richard Denning, 4/18/18
!www.TradersEdgeSystems.com

!INPUTS:
  MAP is 63. 
  STIFFMAX is 7. 
  VFIPeriod is 130. 
  MASPY is 100. 
  MADL is 100.
  SCORECRIT is 5.
  W1 is 1.
  W2 is 1.
  W3 is 1.
  W4 is 1.
  W5 is 2.
 
!VFI FORMULA: 
  COEF is 0.2.
  VCOEF is 2.5.
  Avg is ([high]+[low]+[close])/3.
  inter is ln( Avg ) - ln( Valresult( Avg, 1 ) ). 
  vinter is sqrt(variance(inter, 30 )).
  cutoff is Coef * Vinter * [Close].
  vave is Valresult(simpleavg([volume], VFIPeriod ), 1 ).
  vmax is Vave * Vcoef.
  vc is Min( [volume], VMax ).
  mf is Avg - Valresult( Avg, 1 ).
  vcp is iff(MF > Cutoff,VC,iff(MF < -Cutoff,-VC,0)).
  vfitemp is Sum(VCP , VFIPeriod ) / Vave.
  vfi is expavg(VFItemp, 3 ).

!STIFFNESS 
  ma100 is Avg. 
  CLMA if [close] < MA100.
  STIFFNESS is countof(CLMA,MAP).

!CONDITIONS:
 ! MONEY FLOW:
   COND1 is iff(VFI>0,1,0). 
 !SIMPLEAVG:
    SMA is simpleavg([close],MADL).                              
    COND2 is iff([close]>SMA,1,0).  
 !SIMPLEAVG DIRECTION:                       
    COND3 is iff(SMA>valresult(SMA,4),1,0).  
!STIFFNESS:                          
    COND4 is iff(STIFFNESS<= STIFFMAX,1,0).  
!MARKET DIRECTION:
    SPY is TickerUDF("SPY",[close]).
    COND5 is iff(EXPAVG(SPY,MASPY)>= 
 valresult(EXPAVG(SPY,MASPY),2),1,0).            

SCORE is  W1*COND1+W2*COND2+W3*COND3+
   W4*COND4+W5*COND5.

 buy if Score>=SCORECRIT and hasdatafor(300)>=268. 
Figure 11 shows the summary results of a backtest using NASDAQ 100 stocks during a generally bullish period from April 2009 to April 2018. Figure 12 shows the backtest using the same list of NASDAQ 100 stocks during a period that had two bear markets (April 1999 to April 2009). The average results are similar except that there are fewer trades during the period that contained the two bear markets. Both backtests use a fixed 21-bar exit.
Sample Chart

FIGURE 11: AIQ, BULL MARKET. Here are the summary results of a backtest using NASDAQ 100 stocks during a generally bullish period from April 2009 to April 2018.
Sample Chart

FIGURE 12: AIQ, BEAR MARKET. Here are the summary results of a backtest using NASDAQ 100 stocks during a period from April 1999 to April 2009 that contained two bear markets.
—Richard Denning info@TradersEdgeSystems.com for AIQ Systems

Weekly & Daily Percentage Price Oscillator

The AIQ code based on Vitali Apirine’s article in Stocks & Commodities issue, “Weekly & Daily Percentage Price Oscillator,”  Modifying a traditional indicator can make you look at a chart differently. You can compare indexes, look at price movements during extended periods of time, and make trading decisions based on your observations is provided here:

!WEEKLY & DAILY PPO
!Author: Vitali Apirine, TASC Feb 2018
!Coded by: Richard Denning 12/17/17
!www.TradersEdgeSystems.com

!INPUTS:
S is 12.
L is 26.

EMA1 is expavg([Close],S).
EMA2 is expavg([Close],L).
EMA3 is expavg([Close],S*5).
EMA4 is expavg([Close],L*5).
DM is (EMA1 - EMA2)/EMA4*100.
WM is (EMA3 - EMA4)/EMA4*100.
WD_PPO is WM + DM.
Figure below shows the daily and weekly PPO indicator on a chart of the Nasdaq 100 index (NDX) from 2015 to 2017.

 

Sample Chart

 Here, the daily & weekly PPO is displayed on a chart of the NDX.

 

 

—Richard Denning info@TradersEdgeSystems.com for AIQ Systems