NexusFi: Find Your Edge


Home Menu

 





Programming my first strategy - need a little review


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one roreilly with 2 posts (2 thanks)
    2. looks_two shodson with 1 posts (2 thanks)
    3. looks_3 Quick Summary with 1 posts (0 thanks)
    4. looks_4 ratfink with 1 posts (2 thanks)
    1. trending_up 1,445 views
    2. thumb_up 6 thanks given
    3. group 2 followers
    1. forum 4 posts
    2. attach_file 0 attachments




 
Search this Thread

Programming my first strategy - need a little review

  #1 (permalink)
 roreilly 
Milwaukee WI
 
Experience: Intermediate
Platform: Firetip
Trading: oil, spy, qqq,
Posts: 17 since Sep 2012
Thanks Given: 60
Thanks Received: 22

I am coding my first strategy from scratch, using a boolseries.

Entry is very simple: on the change in trend from the HeikinAshiPaintBars indicator.

Exit is 5 ticks beyond the extreme of the bar in which the indicator reverses itself again.

I still need to add a stoploss when a new position is opened; I will use onorderupdate() for that.

But I need help with the #region Properties

I have getting the error namespace member declaration expected on the last line, and so it won't compile. I can't figure out what I am supposed to add here. Any help would be appreciated.

 
Code
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
    /// <summary>
    /// This is the starter for my trading system.  Ides is to try to stay in trends longer.
    /// </summary>
    [Description("This is the starter for my trading system.  Ides is to try to stay in trends longer.")]
    public class HeikinAshiTFsystem : Strategy
    {
        #region Variables
        // Wizard generated variables
        private int initialStopLoss = 30; // Default setting for InitialStopLoss
        private int cushionOnStop = 6; // Default setting for CushionOnStop
        private int targetGain = 100; // Default setting for TargetGain
        // 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()
        {
            Add(anaHeikinAshiPaintBars());

            CalculateOnBarClose = true;
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            // Condition set 1
            if (ToTime(Time[0]) >= ToTime(8, 59, 0)
                && ToTime(Time[0]) <= ToTime(10, 0, 0)
				&& (Position.MarketPosition == MarketPosition.Flat)
				&& (anaHeikinAshiPaintBars.UpTrend(false)[1])
				&& (anaHeikinAshiPaintBars.UpTrend(true)[0]))
            {
                EnterLong(DefaultQuantity, "");
            }

            // Condition set 2
            if (ToTime(Time[0]) >= ToTime(8, 59, 0)
                && ToTime(Time[0]) <= ToTime(10, 0, 0)
				&& (Position.MarketPosition == MarketPosition.Flat)
				&& (anaHeikinAshiPaintBars.UpTrend(true)[1])
				&& (anaHeikinAshiPaintBars.UpTrend(false)[0]))
            {
                EnterShort(DefaultQuantity, "");
            }
        
		// Condition set 3
			if ((Position.MarketPosition == MarketPosition.Long)
				&& (anaHeikinAshiPaintBars.UpTrend(true)[1])
				&& (anaHeikinAshiPaintBars.UpTrend(false)[0]))
			(
				ExitLongStop(Low[1]-5*TickSize)
			);
				
		// Condition set 4
			if ((Position.MarketPosition == MarketPosition.Short)
				&& (anaHeikinAshiPaintBars.UpTrend(false)[1])
				&& (anaHeikinAshiPaintBars.UpTrend(true)[0]))
			(
				ExitShortStop(High[1]-5*TickSize)

			);
			else 
			
        #region Properties

				
        #endregion Properties
    )}}}}

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
What broker to use for trading palladium futures
Commodities
About a successful futures trader who didnt know anythin …
Psychology and Money Management
Better Renko Gaps
The Elite Circle
Trade idea based off three indicators.
Traders Hideout
Quantum physics & Trading dynamics
The Elite Circle
 
  #3 (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


You are mixing parentheses ( ) with curly brackets { } so the compiler is confused when certain code blocks begin and end. Code blocks begin and end with brackets, not parentheses. The Condition Set 3 and Condition Set 4 blocks are using parentheses when they should use brackets. Also I noticed there is a closing parentheses at the bottom where you have all of those brackets stacked up.

You should vertically align your begin and ending brackets to make sure they are matching up properly, not pile them all together like you did at the end of the file.

Programming requires exact attention to details like these.

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #4 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,426

@roreilly

As well as the parentheses mix ups mentioned by @shodson you are missing the definitions for the anaHeikenAshipaintBarSeries (ref'd in )


You also appear to have deleted the NinjaScript generated code for handling the input parameters, I have tidied the code and commented out the missing requirements until you download and install them in your system. I don't have them in my system so check with @Fat Tails if you don't already have them. (I tried a couple of forum searches but nothing showed up, but then it often does that to me.)


Clicking on brackets or parentheses will highlight the matching one in the editor, and clicking on the left hand side [+]/[-] boxes to see sections in/out also helps. When it doubt, comment it out and add bits back in slowly, and sometimes do it into a complete newly generated file in case of really hard to spot junk characters or oddball stuff that's crept in.


Formatting better will help a lot, but cut/paste from Ninja into these CODE blocks in posts can give messy results due to lack of TAB handling on editing and different mixes of tabs/spaces, etc so just edit as per your prefs, where possible use tabs consistently before adding spaces to align, i.e. whatyouseemightnotbewhatyouget (as you can see here.) Using a ZIP file is preferable for large projects, but this way can be ok for fast glances.

It compiles fine now but I doubt that it is either correct or will make you a fortune if it is, have fun.

 
Code
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
    /// <summary>
    /// Enter the description of your strategy here
    /// </summary>
    [Description("Enter the description of your strategy here")]
    public class Tstrat : Strategy
    {
        #region Variables
        // Wizard generated variables
        private int initialStopLoss = 30; // Default setting for InitialStopLoss
        private int cushionOnStop = 6; // Default setting for CushionOnStop
        private int targetGain = 100; // Default setting for TargetGain
        // User defined variables (add any user defined variables below)
		
		//private anaHeikinAshiType  candleType  =  anaHeikinAshiType.Dan_Valcu_Smoothed;   
		//private int period  =   10;
		//private anaHAMAType  smoothingType  =  anaHAMAType.Ehlers;
		//private Indicator.anaHeikinAshi  AHA  =   null;        
		#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()
        {
			//AHA = anaHeikinAshi(candleType, period, smoothingType);
			//Add(AHA);
			
           CalculateOnBarClose = true;
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            // Condition set 1
			
            if (ToTime(Time[0]) >= ToTime(8, 59, 0)
                && ToTime(Time[0]) <= ToTime(10, 0, 0)
				&& (Position.MarketPosition == MarketPosition.Flat)
				/*&& (anaHeikinAshiPaintBars.UpTrend(false)[1])
				&& (anaHeikinAshiPaintBars.UpTrend(true)[0])*/ )
            {
                EnterLong(DefaultQuantity, "");
            }


            // Condition set 2
			
            if (ToTime(Time[0]) >= ToTime(8, 59, 0)
                && ToTime(Time[0]) <= ToTime(10, 0, 0)
				&& (Position.MarketPosition == MarketPosition.Flat)
				/*&& (anaHeikinAshiPaintBars.UpTrend(true)[1])
				&& (anaHeikinAshiPaintBars.UpTrend(false)[0])*/ )
            {
                EnterShort(DefaultQuantity, "");
            }
        
			// Condition set 3
			
			if ((Position.MarketPosition == MarketPosition.Long)
				/*&& (anaHeikinAshiPaintBars.UpTrend(true)[1])
				&& (anaHeikinAshiPaintBars.UpTrend(false)[0]) */)
			{
				ExitLongStop(Low[1]-5*TickSize);
			}
			else
			{
				// if you want different code
			}
				
			// Condition set 4
			if ((Position.MarketPosition == MarketPosition.Short)
				/* && (anaHeikinAshiPaintBars.UpTrend(false)[1])
				&& (anaHeikinAshiPaintBars.UpTrend(true)[0]) */)
			{
				ExitShortStop(High[1]-5*TickSize);

			}
			else
			{
				// if you want different code
			}
		}
  

        #region Properties
        [Description("")]
        [GridCategory("Parameters")]
        public int InitialStopLoss
        {
            get { return initialStopLoss; }
            set { initialStopLoss = Math.Max(1, value); }
        }
		
        [Description("")]
        [GridCategory("Parameters")]
        public int CushionOnStop
        {
            get { return cushionOnStop; }
            set { cushionOnStop = Math.Max(1, value); }
        }
		
        [Description("")]
        [GridCategory("Parameters")]
        public int TargetGain
        {
            get { return targetGain; }
            set { targetGain = Math.Max(1, value); }
        }
        #endregion
    }
}

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #5 (permalink)
 roreilly 
Milwaukee WI
 
Experience: Intermediate
Platform: Firetip
Trading: oil, spy, qqq,
Posts: 17 since Sep 2012
Thanks Given: 60
Thanks Received: 22


shodson View Post
You are mixing parentheses ( ) with curly brackets { } so the compiler is confused when certain code blocks begin and end. Code blocks begin and end with brackets, not parentheses. The Condition Set 3 and Condition Set 4 blocks are using parentheses when they should use brackets. Also I noticed there is a closing parentheses at the bottom where you have all of those brackets stacked up.

You should vertically align your begin and ending brackets to make sure they are matching up properly, not pile them all together like you did at the end of the file.

Programming requires exact attention to details like these.

Your comment was spot on. The text was so small on my screen, the parentheses and curly brackets looked the same. I bumped up the font size, and problem solved! Thank you so much, I had been spinning my wheels for hours.

Started this thread Reply With Quote
Thanked by:




Last Updated on August 5, 2014


© 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