Hello everybody,

the following code should be a indicator which I would like to implement in a backtest too.

To the code I have the following questions:

- If I remove the second initialization (market_mod = 0), then the code does not work at all, which means that market_mod = empty is displayed. Why ?
- What do I have to do to calculate for each new bar (for example, every new week) the market_mod depending on the market_mod of the last bar, that is a kind of state machine function?

I tried this through the StaticVarSet function. This does not seem to work, because the results are independent of the last bar.

Is anyone able to help me ? I am happy about any help, because I can not get any further here.

```
// start of the formula:
market_mod =0;
market_mod = StaticVarGet("mystaticarray" );
market_mod =0;
GDdayMarketbuyStrong = Param("days of moving average market buysignal (strong)",50,1,999,1);
GDdayMarketsellStrong = Param("days of moving average market sellsignal (strong)",50,1,999,1);
GDdayMarketbuyLight = Param("days of moving average market buysignal (light)",25,1,999,1);
GDdayMarketsellLight = Param("days of moving average market sellsignal (light)",25,1,999,1);
SPY = "SPY";
SetForeign( SPY );
SPY_C = C;
RestorePriceArrays();
//strong bearmarket detection
//Verkaufssignal
lv_sell_market_strong = (SPY_C < MA(SPY_C, GDdayMarketsellStrong) AND Ref(MA(SPY_C,GDdayMarketsellStrong),-0) < Ref(MA(SPY_C,GDdayMarketsellStrong),-1));
//Kaufsignal
lv_buy_market_strong = (SPY_C > MA(SPY_C, GDdayMarketbuyStrong) AND MA(SPY_C,GDdayMarketbuyStrong) > Ref(MA(SPY_C,GDdayMarketbuyStrong),-1));
//light bearmarket detection
//Sellsignal
lv_sell_market_light = (SPY_C < MA(SPY_C, GDdayMarketsellLight) AND MA(SPY_C,GDdayMarketsellLight) < Ref(MA(SPY_C,GDdayMarketsellLight),-1));
//Buysignal
lv_buy_market_light = (SPY_C > MA(SPY_C, GDdayMarketbuyLight) AND MA(SPY_C,GDdayMarketbuyLight) > Ref(MA(SPY_C,GDdayMarketbuyLight),-1));
market_mod = IIf(market_mod==0,IIf(lv_sell_market_light,1,0),market_mod);
market_mod = IIf(market_mod==1,IIf(lv_sell_market_strong,2,1),market_mod);
market_mod = IIf(market_mod==1,IIf(lv_buy_market_light,0,1),market_mod);
market_mod = IIf(market_mod==2,IIf(lv_buy_market_strong,1,2),market_mod);
market_mod = IIf(market_mod==2,IIf(lv_buy_market_light,0,2),market_mod);
//---------------------------------------------------------------------------------------------------------------
//--- Plotten
//---------------------------------------------------------------------------------------------------------------
Plot(market_mod,"market_mod",colorRed,styleLine, 0, 3);
// at the end of the formula store to static
StaticVarSet("mystaticarray", market_mod );
```

Moderator comment:Added required code tags