Dear Statalisters,

I have a measure of capital account openness (ka), where 0 is open and 1 is closed. I'm trying to identify periods when countries opened up or closed. For this, I look at a standard deviation jump of ka (0.132) and check whether the three-year moving average of ka 2 years before the jump is more than 0.2 higher/lower than the-three year moving average starting from one year after the jump. When it is lower, it classifies as a period of liberalization, when it is higher it would be a restriction of the capital account.

I want to create two indicators, caplib and capclose, where the three years making up the three year moving average are coded 1 if it is below/above the 0.2 threshold and 0 otherwise. This process should stop when the moving average drops below the threshold again, after which can be checked for new jumps and new period of liberalization/restriction. Perhaps this is best explained with a data example:
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long country2 int year double ka float dka double ma_ka float jump
10 1995  .7222222222222222             .                  . 0
10 1996  .7222222222222222             0  .7231481481481481 0
10 1997  .7250000000000001    .002777778  .7490740740740742 0
10 1998                 .8          .075  .7833333333333333 0
10 1999               .825          .025  .8250000000000001 0
10 2000                .85          .025  .8388888888888889 0
10 2001  .8416666666666667   -.008333334  .7138888888888889 0
10 2002                .45     -.3916667  .5888888888888889 1
10 2003               .475          .025  .4666666666666666 0
10 2004               .475             0 .44722222222222224 0
10 2005  .3916666666666667    -.08333334 .38055555555555554 0
10 2006               .275    -.11666667 .33055555555555555 0
10 2007               .325           .05               .325 0
10 2008               .375           .05 .47500000000000003 0
10 2009  .7250000000000001           .35  .6083333333333334 1
10 2010               .725 -1.110223e-16  .7250000000000001 0
10 2011               .725             0  .6999999999999998 0
10 2012  .6499999999999999         -.075                .65 0
10 2013               .575         -.075                 .6 0
10 2014               .575             0                 .6 0
10 2015                .65          .075               .625 0
10 2016                .65             0                  . 0
54 1995             .84375             .                  . 0
54 1996  .7222222222222223    -.12152778  .7469907407407407 0
54 1997               .675    -.04722222  .7324074074074075 0
54 1998                 .8          .125  .7833333333333333 0
54 1999               .875          .075  .8166666666666668 0
54 2000               .775           -.1               .775 0
54 2001               .675           -.1  .6916666666666668 0
54 2002               .625          -.05  .6416666666666667 0
54 2003               .625             0 .48333333333333334 0
54 2004                 .2         -.425                 .3 1
54 2005 .07500000000000001         -.125 .11666666666666668 0
54 2006 .07500000000000001             0 .07500000000000001 0
54 2007 .07500000000000001             0 .07500000000000001 0
54 2008 .07500000000000001             0 .07500000000000001 0
54 2009 .07500000000000001             0 .07500000000000001 0
54 2010 .07500000000000001             0 .07500000000000001 0
54 2011 .07500000000000001             0 .07500000000000001 0
54 2012 .07500000000000001             0 .07500000000000001 0
54 2013 .07500000000000001             0 .07500000000000001 0
54 2014 .07500000000000001             0 .07500000000000001 0
54 2015 .07500000000000001             0 .07500000000000001 0
54 2016 .07500000000000001             0                  . 0
84 1995 .05555555555555555             .                  . 0
84 1996 .05555555555555555             0  .0537037037037037 0
84 1997                .05   -.005555556 .05185185185185185 0
84 1998                .05             0 .05000000000000001 0
84 1999                .05             0 .05000000000000001 0
84 2000                .05             0 .05000000000000001 0
84 2001                .05             0 .05000000000000001 0
84 2002                .05             0 .05000000000000001 0
84 2003                .05             0 .05000000000000001 0
84 2004                .05             0               .125 0
84 2005               .275          .225 .20000000000000004 1
84 2006               .275             0               .275 0
84 2007               .275             0               .275 0
84 2008               .275             0               .225 0
84 2009               .125          -.15 .17500000000000002 1
84 2010               .125             0               .125 0
84 2011               .125             0               .125 0
84 2012               .125             0 .19166666666666665 0
84 2013               .325            .2 .26666666666666666 1
84 2014 .35000000000000003          .025 .34166666666666673 0
84 2015 .35000000000000003             0 .35000000000000003 0
84 2016 .35000000000000003             0                  . 0
end
label values country2 country2
label def country2 10 "Brazil", modify
label def country2 54 "Malta", modify
label def country2 84 "Switzerland", modify
Wanted:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long country2 int year double ka float dka double ma_ka float(jump caplib capclose)
10 1995  .7222222222222222             .                  . 0 0 0
10 1996  .7222222222222222             0  .7231481481481481 0 0 0
10 1997  .7250000000000001    .002777778  .7490740740740742 0 0 0
10 1998                 .8          .075  .7833333333333333 0 0 0
10 1999               .825          .025  .8250000000000001 0 0 0
10 2000                .85          .025  .8388888888888889 0 0 0
10 2001  .8416666666666667   -.008333334  .7138888888888889 0 0 0
10 2002                .45     -.3916667  .5888888888888889 1 1 0
10 2003               .475          .025  .4666666666666666 0 1 0
10 2004               .475             0 .44722222222222224 0 1 0
10 2005  .3916666666666667    -.08333334 .38055555555555554 0 1 0
10 2006               .275    -.11666667 .33055555555555555 0 1 0
10 2007               .325           .05               .325 0 1 0
10 2008               .375           .05 .47500000000000003 0 1 0
10 2009  .7250000000000001           .35  .6083333333333334 1 1 0
10 2010               .725 -1.110223e-16  .7250000000000001 0 1 0
10 2011               .725             0  .6999999999999998 0 0 0
10 2012  .6499999999999999         -.075                .65 0 0 0
10 2013               .575         -.075                 .6 0 0 0
10 2014               .575             0                 .6 0 0 0
10 2015                .65          .075               .625 0 0 0
10 2016                .65             0                  . 0 0 0
54 1995             .84375             .                  . 0 0 0
54 1996  .7222222222222223    -.12152778  .7469907407407407 0 0 0
54 1997               .675    -.04722222  .7324074074074075 0 0 0
54 1998                 .8          .125  .7833333333333333 0 0 0
54 1999               .875          .075  .8166666666666668 0 0 0
54 2000               .775           -.1               .775 0 0 0
54 2001               .675           -.1  .6916666666666668 0 0 0
54 2002               .625          -.05  .6416666666666667 0 0 0
54 2003               .625             0 .48333333333333334 0 0 0
54 2004                 .2         -.425                 .3 1 1 0
54 2005 .07500000000000001         -.125 .11666666666666668 0 1 0
54 2006 .07500000000000001             0 .07500000000000001 0 1 0
54 2007 .07500000000000001             0 .07500000000000001 0 1 0
54 2008 .07500000000000001             0 .07500000000000001 0 1 0
54 2009 .07500000000000001             0 .07500000000000001 0 1 0
54 2010 .07500000000000001             0 .07500000000000001 0 1 0
54 2011 .07500000000000001             0 .07500000000000001 0 1 0
54 2012 .07500000000000001             0 .07500000000000001 0 1 0
54 2013 .07500000000000001             0 .07500000000000001 0 1 0
54 2014 .07500000000000001             0 .07500000000000001 0 1 0
54 2015 .07500000000000001             0 .07500000000000001 0 1 0
54 2016 .07500000000000001             0                  . 0 1 0
84 1995 .05555555555555555             .                  . 0 0 0
84 1996 .05555555555555555             0  .0537037037037037 0 0 0
84 1997                .05   -.005555556 .05185185185185185 0 0 0
84 1998                .05             0 .05000000000000001 0 0 0
84 1999                .05             0 .05000000000000001 0 0 0
84 2000                .05             0 .05000000000000001 0 0 0
84 2001                .05             0 .05000000000000001 0 0 0
84 2002                .05             0 .05000000000000001 0 0 0
84 2003                .05             0 .05000000000000001 0 0 0
84 2004                .05             0               .125 0 0 0
84 2005               .275          .225 .20000000000000004 1 0 1
84 2006               .275             0               .275 0 0 1
84 2007               .275             0               .275 0 0 1
84 2008               .275             0               .225 0 0 1
84 2009               .125          -.15 .17500000000000002 1 0 0
84 2010               .125             0               .125 0 0 0
84 2011               .125             0               .125 0 0 0
84 2012               .125             0 .19166666666666665 0 0 0
84 2013               .325            .2 .26666666666666666 1 0 1
84 2014 .35000000000000003          .025 .34166666666666673 0 0 1
84 2015 .35000000000000003             0 .35000000000000003 0 0 1
84 2016 .35000000000000003             0                  . 0 0 1
end
label values country2 country2
label def country2 10 "Brazil", modify
label def country2 54 "Malta", modify
label def country2 84 "Switzerland", modify
I imagine this would need a while loop starting from the year after the jump, comparing the moving average to the moving average two years before the jump, moving on one year if true and stopping otherwise, but I don't know how to do this.