Is There a way to create a flip flop switch with NT?
Hello! Here I am again!
Well, this time 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:
So, the code works... but only at the EXACTLY time of the cross above or below. It seems like the ModeOn values dont hold for the rest of the bars. Why is that? Do I need to make it inside a loop?
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.
Hi @Momozo, it might help you to see things more clearly if you investigate using the 'bool' type for flags and also the built-in CrossAbove and CrossBelow functions. Always keep clear meanings for flags, usually it's a sign of bugs to follow if their meanings and uses starts to merge.
The following user says Thank You to ratfink for this post:
Thanks, Ratfink. I already tried using bool type, using true and false instead of 1 and 0. But it gave me errors. I've spent a lot of time searching for a solution, but with no success.
In Amibroker, there was a function called "flip(condition1, condition2)", that worked for this purpose. When condition 1 gave a true signal, it would remain true until condition 2 became true. Then both conditions became false. Quite handy!
I saw a sample at ninjatrader forum that was exactly like the way i wrote the code, but there was one difference: they used variable0 instead of BuyModeOn (dont know if it influences...).
Well, I'll keep looking. Oh, and I use MRO() for one of the conditions. Do you think it can cause problems?
The following user says Thank You to Momozo for this post: