How to program a kind of state machine in afl?

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

@FrankR,

I can only guess/assume that if you remove the second zero assignment, then your StaticVarGet must be failing.

Now in my quick glance at your code, I see you using REF with a -0 offset. Not sure why...

I would suggest that you use brackets around each piece of the Multiple comparisons in your assignments. I don't know if it will make a difference, but that way you are sure.

You are re-assigning the market_mod 5 times. I am not sure, but I suspect you are trying to determine the previous value of the market_mod, and would be better served using REF to get that value and use it in your calculation. Don't have time to think through it all now.

Here are a couple of threads that you should take a look at..


There is one thing in your formula that makes it broken:

market_mod = StaticVarGet("mystaticarray" ); // reading static variable (OK)
market_mod =0; // but then.... reseting it back to zero ! (WRONG)

You read the static variable that holds the state but you later RESET it back to zero.
You should REMOVE this line

market_mod =0; // REMOVE THAT 

If you want to have default value of 0 instead of Null (when there is no static var at all), you should just write this single line instead of 3 first lines that you have on top.

// reading static variable (OK) 
// and default (when static var does not yet exist) will be zero thanks to Nz function
market_mod = Nz( StaticVarGet("mystaticarray" ) ); 
2 Likes

I do not know exactly why it works, but it works !
Many Thanks.