NexusFi: Find Your Edge


Home Menu

 





Is There a way to create a flip flop switch with NT?


Discussion in NinjaTrader

Updated
    1. trending_up 1,685 views
    2. thumb_up 2 thanks given
    3. group 1 followers
    1. forum 3 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
Momozo
fortaleza + ceara/brazil
 
Posts: 23 since Jun 2014
Thanks Given: 26
Thanks Received: 4

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:


 
Code
OnBarUpdate()

BuyModeOn = 0;
ShortModeOn = 0;

if (Close[1] < EMA(10)[1]  && Close[0] > EMA(10)[0]) //its a cross above
{
   BuyModeOn = 1;
   ShortModeOn = 0;
   if (//buycondition at the same bar...)
   {
      EnterLong()
   }
}

if (Close[1] > EMA(10)[1]  && Close[0] < EMA(10)[0]) //its a cross below
{
   BuyModeOn = 0;
   ShortModeOn = 1;
   if (//shortcondition at the same bar...)
   {
      EnterShort()
   }
}

if (BuyModeOn == 1 && buycondition...)
{
   EnterLong()
}

if (ShortModeOn == 1 && shortcondition...)
{
   EnterShort()
}
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.

Thanks in advance!!


Reply With Quote

Can you help answer these questions
from other members on NexusFi?
UMA Votes Tonight: Polymarkets $80M Strategy Bitcoin Bat …
Prediction Markets & Event Contracts
Cboe Files for Near 24x5 Equities Trading -- December 20 …
Traders Hideout
The Week Ahead -- CPI Wednesday With Oil Past $90, PPI L …
Traders Hideout
MegaETH Proves the Crowd Right: Prediction Markets Calle …
Prediction Markets & Event Contracts
Topstep Acquires The Futures Desk -- Prop Firm Consolida …
Funded Trading Evaluation Firms
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
21 thanks
2026 Jlab journal
10 thanks
Trying to learn Volume and price action correlation
8 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Lady Vols Primer: Trading Volatility Journal
5 thanks
  #3 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,550 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,430


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.


Travel Well
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #4 (permalink)
Momozo
fortaleza + ceara/brazil
 
Posts: 23 since Jun 2014
Thanks Given: 26
Thanks Received: 4


ratfink View Post
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.


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?


Reply With Quote
Thanked by:




Last Updated on July 28, 2014


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts