NexusFi: Find Your Edge


Home Menu

 





Boolean Flag help needed (strategy)


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one wallsteetking with 5 posts (0 thanks)
    2. looks_two shodson with 2 posts (4 thanks)
    3. looks_3 baruchs with 1 posts (0 thanks)
    4. looks_4 vegasfoster with 1 posts (0 thanks)
    1. trending_up 5,309 views
    2. thumb_up 4 thanks given
    3. group 5 followers
    1. forum 10 posts
    2. attach_file 1 attachments




 
Search this Thread

Boolean Flag help needed (strategy)

  #1 (permalink)
wallsteetking
MN
 
Posts: 25 since Feb 2011
Thanks Given: 4
Thanks Received: 0

hey guys I need some help with my code. What I want to do is create a long entry when two of my systems both fire a long on the same bar. what i did is i created a boolean and put both of my strategies into one strategy called Mashall. however when i write the code, and run it.. does not work for some reason. can someone please help me decipher why this does not work.

 
Code
/// <summary>
    /// Enter the description of your strategy here
    /// </summary>
    [Description("Enter the description of your strategy here")]
    public class MASHALL : Strategy
    {
        #region Variables
        // Wizard generated variables
        private int myInput0 = 1; // Default setting for MyInput0
        private bool enterLongStrat1 = false;
        private bool enterLongStrat2= false;
        // User defined variables (add any user defined variables below)
        #endregion

        /// <summary>
        /// This method is used to configure the strategy and is called once before any strategy method is called.
        /// </summary>
        protected override void Initialize()
        {
        CalculateOnBarClose = true;
    SetProfitTarget("", CalculationMode.Ticks, 4);
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            
            // Strategy 1
        if (Bars.BarsSinceSession >=7)
                
            if (Stochastics(7, 14, 3).D[0] >= 30)
            {
                enterLongStrat1 = true;
  //       enterLongStrat2 = false;
        DrawTriangleUp("My triangle up" + CurrentBar, true, 0, High[0] + 20 * TickSize, Color.Red);
                
            }
            
            //Strategy 2
            if (Bars.BarsSinceSession >=7)
                
            if ( Close[0] >= EMA(30)[0])
            {
                enterLongStrat2 = true;
 //         enterLongStrat1 = false; now, then both conditions can never be simultaneously true if you have this
            DrawTriangleUp("My triangle up" + CurrentBar, true, 0, High[0] + 30 * TickSize, Color.Blue);
                
            }
            
            if (Bars.BarsSinceSession >=7)
            if(enterLongStrat2 == true && enterLongStrat1 == true)
            {    
                  EnterLong(DefaultQuantity, "");

            }
        }

        #region Properties
        [Description("")]
        [GridCategory("Parameters")]
        public int MyInput0
        {
            get { return myInput0; }
            set { myInput0 = Math.Max(1, value); }
        }
    
        
        #endregion

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Exit Strategy
NinjaTrader
Futures True Range Report
The Elite Circle
ZombieSqueeze
Platforms and Indicators
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
39 thanks
NexusFi site changelog and issues/problem reporting
26 thanks
The Program
18 thanks
GFIs1 1 DAX trade per day journal
18 thanks
  #2 (permalink)
 traderwerks   is a Vendor
 
Posts: 692 since Jun 2009
Thanks Given: 436
Thanks Received: 465

At the top of the OnBarUpdate() loop reset the flags to false

 
Code
 
        protected override void OnBarUpdate()
        {
            enterLongStrat1 = false;
           enterLongStrat2= false;

Math. A gateway drug to reality.
Reply With Quote
  #3 (permalink)
 vegasfoster 
las vegas
 
Experience: Intermediate
Platform: Sierra Chart
Broker: Velocity/IB
Trading: 6E
Posts: 1,145 since Feb 2010
Thanks Given: 304
Thanks Received: 844


i think should be if(enterLongStrat2 == true || enterLongStrat1 == true)

Reply With Quote
  #4 (permalink)
wallsteetking
MN
 
Posts: 25 since Feb 2011
Thanks Given: 4
Thanks Received: 0

I tried both things and it still does not work. I have attached the file here can you guys check it .. this seems so simple to do but its not working.

Attached Files
Elite Membership required to download: Mashall.zip
Reply With Quote
  #5 (permalink)
wallsteetking
MN
 
Posts: 25 since Feb 2011
Thanks Given: 4
Thanks Received: 0

the reason you can tell that its not working is by looking at the triangles. when the strategy is activated you never see both red and blue triangles appear on the SAME BAR.

Reply With Quote
  #6 (permalink)
 baruchs 
Israel
 
Experience: Intermediate
Platform: NinjaTrader
Broker: pfg
Trading: eminis
Posts: 323 since Jun 2009

Why not just:
 
Code
if (Bars.BarsSinceSession >=7 && Stochastics(7, 14, 3).D[0] >= 30 && Close[0] >= EMA(30)[0])
{
DrawTriangleUp("My triangle up" + CurrentBar, true, 0, High[0] + 20 * TickSize, Color.Red);
EnterLong(DefaultQuantity, "");
}

Reply With Quote
  #7 (permalink)
wallsteetking
MN
 
Posts: 25 since Feb 2011
Thanks Given: 4
Thanks Received: 0

The reason that I am not doing it that way is because i am going to have more than 2 conditions (probably around 11)
And some of the other conditions will be inverse of other conditions. For example one would say go long only on
== 20K while another would say go long on >= 30K or greater. If i would combine this, it would not work that well. So I need to learn have to do boolean in ninjatrader. But i am having a very hard time making the code work.

Reply With Quote
  #8 (permalink)
wallsteetking
MN
 
Posts: 25 since Feb 2011
Thanks Given: 4
Thanks Received: 0

Hey guys just a quick question does anyone have a boolean trading system NOT INDICATOR for ninjatrader. that I can use and just put my conditions in instead of yours. your help would be appreciated

Reply With Quote
  #9 (permalink)
 
shodson's Avatar
 shodson 
OC, California, USA
Quantoholic
 
Experience: Advanced
Platform: IB/TWS, NinjaTrader, ToS
Broker: IB, ToS, Kinetick
Trading: stocks, options, futures, VIX
Posts: 1,976 since Jun 2009
Thanks Given: 533
Thanks Received: 3,709

I would approach it differently...

I would create boolean properties like

 
Code
bool PriceMovedUp { get { return Close[0] > Close[1]; } }
bool AboveMA { get { return Close[0] > SMA(200)[0]; } }
(calm down literal 'net, I know this it not optimized, just trying to show a point...)

then in your OnBarUpdate() you just stack together your predicates (true/false conditions) in easy-to-follow English

 
Code
if (PriceMovedUp && AboveMA)
    EnterLong();
Another approach would be to track a score of a setup. So for example if you have 8 possible conditions you like to look at, and if 6 pass then you take a trade. So when each condition is valid, add +1 to your score. This is one way you can layer on more and more conditions without adding a bunch of unmanageable if/then permutations.

 
Code
int score = 0;
if (PriceMovedUp) score++;
if (AboveMA) score++;

if (score >= 2)
    EnterLong();

To me, readability is very important, because code can grow and become very complex really quickly. In my opinion OnBarUpdate() needs to be the most English-reading code you write, because these are the core rules of your strategy. The gory/technical details behind each command and condition can be defined elsewhere, but OnBarUpdate() needs to be your most coarse-grained part of your strategy/indicator to keep your sanity when modifying and maintaining your strategy.

For example, here is a strategy I've been trading live for years: my gap fading strat. This is the OnBarUpdate() method. The whole strategy is almost 700 lines long, but I can open this open and quickly refresh my memory about how it works just by looking at OnBarUpdate() even though I haven't looked at this code in a long time.

 
Code
protected override void OnBarUpdate()
{
	if (TimeToDecide)
	{
		if (ShouldFadeGap)
		{
			if (GapIsBigEnough)
				FadeGap();
			else
				DontFadeGap();
		}
		else if (ShouldFadeExtendedTarget)
			FadeExtendedTarget();
		else
			DontFadeGap();
	}
	else if (_faded)
	{
		ManageTrade();
	}
}

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #10 (permalink)
 guw75 
Frankfurt+Germany
 
Experience: Intermediate
Platform: Ninja Trader, esignal
Broker: IB
Trading: futures, options, stocks
Posts: 16 since May 2013
Thanks Given: 2
Thanks Received: 1


@shodson
very interesting. I would like to code my strategy in the same structure. Could you please explain your syntax a bit more detailed.

Would be highly appreciate your help.

guw75



shodson View Post
I would approach it differently...

I would create boolean properties like

 
Code
bool PriceMovedUp { get { return Close[0] > Close[1]; } }
bool AboveMA { get { return Close[0] > SMA(200)[0]; } }
(calm down literal 'net, I know this it not optimized, just trying to show a point...)

then in your OnBarUpdate() you just stack together your predicates (true/false conditions) in easy-to-follow English

 
Code
if (PriceMovedUp && AboveMA)
    EnterLong();


Another approach would be to track a score of a setup. So for example if you have 8 possible conditions you like to look at, and if 6 pass then you take a trade. So when each condition is valid, add +1 to your score. This is one way you can layer on more and more conditions without adding a bunch of unmanageable if/then permutations.

 
Code
int score = 0;
if (PriceMovedUp) score++;
if (AboveMA) score++;

if (score >= 2)
    EnterLong();

To me, readability is very important, because code can grow and become very complex really quickly. In my opinion OnBarUpdate() needs to be the most English-reading code you write, because these are the core rules of your strategy. The gory/technical details behind each command and condition can be defined elsewhere, but OnBarUpdate() needs to be your most coarse-grained part of your strategy/indicator to keep your sanity when modifying and maintaining your strategy.

For example, here is a strategy I've been trading live for years: my gap fading strat. This is the OnBarUpdate() method. The whole strategy is almost 700 lines long, but I can open this open and quickly refresh my memory about how it works just by looking at OnBarUpdate() even though I haven't looked at this code in a long time.

 
Code
protected override void OnBarUpdate()
{
	if (TimeToDecide)
	{
		if (ShouldFadeGap)
		{
			if (GapIsBigEnough)
				FadeGap();
			else
				DontFadeGap();
		}
		else if (ShouldFadeExtendedTarget)
			FadeExtendedTarget();
		else
			DontFadeGap();
	}
	else if (_faded)
	{
		ManageTrade();
	}
}


Reply With Quote




Last Updated on January 28, 2016


© 2024 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 - Privacy Policy - Downloads - Top
no new posts