I need some help with a switch device. Let me try to explain (sorry for my bad english):
The system I'm trying to write is not exactly this one, but it uses a similar mechanism.
I use 2 variables.
BuyModeOn and ShortModeOn. When BuyModeOn == 1 and theres a buy signal, I want the system to execute my buy. When BuyModeOn == 0, I want the system to ignore my buy signals. The same with ShortModeOn.
So, lets say BuyModeOn goes 1 when Price moves above an MA. The code I tried is like this:
Code
OnBarUpdate() int BuyModeOn = 0; int ShortModeOn = 0; EMA1 = EMA(10); EMA2 = EMA(100); if (Close[1] < EMA1[1] && Close[0] > EMA1[0]) //its a cross above { BuyModeOn = 1; //I want it to remain on even if it cross below EMA1 again! ShortModeOn = 0; if (//buycondition at the same bar...) { EnterLong() } } if (Close[1] > EMA2[1] && Close[0] < EMA2[0]) //its a cross below { BuyModeOn = 0; //It only becames ShortMode when cross below EMA2; ShortModeOn = 1; if (//shortcondition at the same bar...) { EnterShort() } } if (BuyModeOn == 1 && buycondition...) { EnterLong() } if (ShortModeOn == 1 && shortcondition...) { EnterShort() }
PS: While I was writing the thread, I just had the though that its because the variables are inside the OnBarUpdate(), initially setted to 0... So in every bar it becomes as 0... I'll try it soon, but I'll post it anyway... I may be wrong after all.
Thanks in advance!!
Comment