NexusFi: Find Your Edge


Home Menu

 





Need some help for c sharp scripting


Discussion in NinjaTrader

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




 
Search this Thread
  #1 (permalink)
hugebaozi
Chicago
 
Posts: 1 since Jul 2014
Thanks Given: 0
Thanks Received: 0

Hi all:
I am new to c# and ninjatrader.. and i tried to make some simple strategy work. Now i just to long SPY when sma(10)>sma(20) and sell the other way.
1. About Add(some indicators), could i also specify the color?
2. In my code , I first want to Add(sma10), it looks like i can't do this. So how could i initialize a indicator object?
3. In my code, I tried to call BackColorAll = Color.Green; Yet the backtest result does not show any green color on the chart though the trading log did execute some selling orders.
4. In general , my logic is to first check if my position is empty, if so, check the long condition. if my position is not empty, check the short condition.

Could someone help check where i goes wrong?
 
Code
public class Crossover01 : Strategy
    {
        #region Variables
        // Wizard generated variables
        private int myInput0 = 1; // Default setting for MyInput0
        // 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>
		private IOrder entryOrder = null;
        protected override void Initialize()
        {
            CalculateOnBarClose = true;
			//global stop loss
			SetStopLoss(CalculationMode.Percent, 0.07);
			SetProfitTarget(CalculationMode.Percent,0.1);
			//Add(sma10);
			//Add(sma20);
			Add(SMA(10));
			Add(SMA(20));
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
			var sma10 = SMA(10);
			var sma20 = SMA(20);
			var sma50 = SMA(50);
			if(entryOrder == null)
			{
				if(CrossAbove(sma10,sma20,1)&& Rising(sma50) == true)
				{
					EnterLong();
					BackColorAll = Color.Red;
				}
			}
			else if (entryOrder != null)
			{
				if (CrossBelow(sma10,sma20,1)|| Falling(sma50) == true)
				{
					EnterShort();
					BackColorAll = Color.Green;
				}
				entryOrder = null;
			}
        }

        #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?
Penalties in Budapest, Peace Deadline in Tehran: Arsenal …
Prediction Markets & Event Contracts
CFTC Opens First COT Report Review in 20 Years -- Asks W …
Traders Hideout
CFTC Approves First US Bitcoin Perpetual Futures -- Kals …
Traders Hideout
UCL Final Kicks Off at Noon ET: PSG at 56.5% as Iran May …
Prediction Markets & Event Contracts
Trump Lands in Beijing on CPI Day: Iran Peace Expires To …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
NexusFi site changelog and issues/problem reporting
15 thanks
Darmok and Jalad at Tanagra
3 thanks
Big Mike in Ecuador
1 thanks
30 Sessions
1 thanks




Last Updated on July 9, 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