NexusFi: Find Your Edge


Home Menu

 





New at Programming, stuck with a crossover strat


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one DeadCatBounced with 7 posts (0 thanks)
    2. looks_two gregid with 3 posts (4 thanks)
    3. looks_3 Zondor with 1 posts (1 thanks)
    4. looks_4 Tasker_182 with 1 posts (1 thanks)
      Best Posters
    1. looks_one gregid with 1.3 thanks per post
    2. looks_two Zondor with 1 thanks per post
    3. looks_3 Tasker_182 with 1 thanks per post
    4. looks_4 shodson with 1 thanks per post
    1. trending_up 4,253 views
    2. thumb_up 7 thanks given
    3. group 5 followers
    1. forum 13 posts
    2. attach_file 0 attachments




 
Search this Thread

New at Programming, stuck with a crossover strat

  #1 (permalink)
 
DeadCatBounced's Avatar
 DeadCatBounced 
Baltimore MD US
 
Experience: Intermediate
Platform: NinjaTrader
Trading: ES, NQ
Posts: 293 since Apr 2013
Thanks Given: 1,514
Thanks Received: 736

Hello

Just getting into programming so I thought I would set myself a challenge of building what I thought would be a fairly simple strategy. I am attempting to create a strategy that does the following: When the price reaches a point that is + or - 5 ticks of it either goes long or short.

Example: my indicator (lets use a SMA just for example) is at 95.05 the price is currently at 94.8. What I would like to have happen is that as the price moves up, at 95.00 it will enter a short position. If the price continues to go up at 95.10 it would reverse and enter a long position. (or vice versa ofc if the price was coming down to my SMA.

I started out using the wizard, and then looking at the view code button of the different sample strategies figured I would need to use an if, then else if statement to effect the reverse.

Here is my code currently.

 
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>
    /// bam
    /// </summary>
    [Description("bam")]
    public class MA2 : Strategy
    {
        #region Variables
        // Wizard generated variables
        private int sMAlookback = 50; // Default setting for SMAlookback
        private int offset = 5; // Default setting for Offset
        private int offsetneg = -5; // Default setting for Offsetneg
        // 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(SMA(SMAlookback));
            Add(SMA(SMAlookback));

            CalculateOnBarClose = false;
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
			Print("LongCurrentAsk: "+GetCurrentAsk()+" Offset+Tick: "+(Offset * TickSize)+" SMA: "+SMA(SMAlookback)[0]);
			Print("Calculated value: "+(GetCurrentAsk() + Offset * TickSize));
			
			Print("ShortCurrentBid: "+GetCurrentBid()+" Offsetneg+Tick: "+(Offsetneg * TickSize)+" SMA: "+SMA(SMAlookback)[0]);
			Print("Calculated value: "+(GetCurrentBid() + Offsetneg * TickSize));
			
            if (CrossAbove(GetCurrentAsk() + Offset * TickSize, SMA(SMAlookback), 1))
                EnterLong(DefaultQuantity, "");
            else if (CrossBelow(GetCurrentBid() + Offsetneg * TickSize, SMA(SMAlookback), 1))
                EnterShort(DefaultQuantity, "");
        }
		
		

        #region Properties
        [Description("")]
        [GridCategory("Parameters")]
        public int SMAlookback
        {
            get { return sMAlookback; }
            set { sMAlookback = Math.Max(50, value); }
        }

        [Description("")]
        [GridCategory("Parameters")]
        public int Offset
        {
            get { return offset; }
            set { offset = Math.Max(5, value); }
        }

        [Description("")]
        [GridCategory("Parameters")]
        public int Offsetneg
        {
            get { return offsetneg; }
            set { offsetneg = Math.Max(-5, value); }
        }
        #endregion
    }
}
Anyways, currently it does not seem anything is working. Not sure what I am doing wrong - when I hit the compile button it doesn't come up with any logic errors. If someone could help me with this that would be great!

Also, I was looking at purchasing this ebook: Smashwords ? NinjaScript Programmer's Launch Pad ? A book by Scott Daggett to assist me in learning the basics of ninjascript programming - does anyone have experience with this and if it is worth the time?

Visit my NexusFi Trade Journal Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
How to apply profiles
Traders Hideout
Cheap historycal L1 data for stocks
Stocks and ETFs
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
What broker to use for trading palladium futures
Commodities
Trade idea based off three indicators.
Traders Hideout
 
  #3 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 650 since Aug 2009
Thanks Given: 320
Thanks Received: 623


add brackets in correct places:
change:
 
Code
GetCurrentAsk() + Offset * TickSize
to:
 
Code
GetCurrentAsk() + (Offset * TickSize)

Also if you are new to programming always use curly brackets with conditions:
 
Code
if (condition1)
{
    something;
}
else
{
   somethingelse;
}
this will save you a lot of problems in the near future

Reply With Quote
Thanked by:
  #4 (permalink)
 
DeadCatBounced's Avatar
 DeadCatBounced 
Baltimore MD US
 
Experience: Intermediate
Platform: NinjaTrader
Trading: ES, NQ
Posts: 293 since Apr 2013
Thanks Given: 1,514
Thanks Received: 736

Thanks for the help.

I added in the brackets so the code now looks like this:

 
Code
            if (CrossAbove(GetCurrentAsk() + (Offset * TickSize), SMA(SMAlookback), 1))
			{
                EnterShort(DefaultQuantity, "");
			}
            else if (CrossBelow(GetCurrentBid() + (Offsetneg * TickSize), SMA(SMAlookback), 1))
			{
                EnterLong(DefaultQuantity, "");
			}
It is sort of working. For whatever reason it was going long when I meant it to go short and short when I meant it to go long so I switched the short/ long orders and the directions are atleast good.

However it doesnt seem to reverse, it only does the one trade?

Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #5 (permalink)
 
Zondor's Avatar
 Zondor 
Portland Oregon, United States
 
Experience: Beginner
Platform: Ninjatrader®
Broker: CQG, Kinetick
Trading: Gameplay Klownbine® Trading of Globex
Posts: 1,333 since Jul 2009
Thanks Given: 1,246
Thanks Received: 2,731

You are not using predefined reusable instances of the external class, which in this case is the SMA class..

Not doing so exacts a huge performance penalty.

Refer to the "Anyone have any hints for Optimizing C# code" thread for a further explanation.

The strategy wizard produces code in a simplistic manner without regard for its operational efficiency.

The examples in the Ninjatrader Help coding tutorials likewise ignore such considerations.

"If we don't loosen up some money, this sucker is going down." -GW Bush, 2008
“Lack of proof that something is true does not prove that it is not true - when you want to believe.” -Humpty Dumpty, 2014
“The greatest shortcoming of the human race is our inability to understand the exponential function.”
Prof. Albert Bartlett
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #6 (permalink)
 
DeadCatBounced's Avatar
 DeadCatBounced 
Baltimore MD US
 
Experience: Intermediate
Platform: NinjaTrader
Trading: ES, NQ
Posts: 293 since Apr 2013
Thanks Given: 1,514
Thanks Received: 736

Hey Zondor thanks for the reply.

So what I am reading in that other thread is basically the way the SMA is calculated is every tick throughout the bar it is recalculated which obviously takes up computing power.

Looking at the code for the stochastic that you optimized you put in a If statement

if(FirstTickOfBar)

Whereas in Richard Todd's article that I found on the ninjatrader link he uses

a void OnBarUpdate.

im thinking that because I do want every tick to check if it has gotten close to the SMA for my entry conditions I will use the void OnBarUpdate?

Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #7 (permalink)
 
DeadCatBounced's Avatar
 DeadCatBounced 
Baltimore MD US
 
Experience: Intermediate
Platform: NinjaTrader
Trading: ES, NQ
Posts: 293 since Apr 2013
Thanks Given: 1,514
Thanks Received: 736

I still cannot get it to reverse positions for me.

When I looked at the code for the SampleMACrossOver strat that ninja supplies (here is their code)

 
Code
			if (CrossAbove(SMA(Fast), SMA(Slow), 1))
			    EnterLong();
			else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
			    EnterShort();
It reverses fine.

Yet my code, which I think has the same basic logic:

 
Code
            if (CrossAbove(GetCurrentAsk() + (Offset * TickSize), SMA(SMAlookback), 1))
			{
                EnterShort(DefaultQuantity, "");
			}
            else if (CrossBelow(GetCurrentBid() + (Offsetneg * TickSize), SMA(SMAlookback), 1))
			{
                EnterLong(DefaultQuantity, "");
			}

Only goes has the one entry and will not reverse on me :/

Any ideas on what I am doing wrong?

Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #8 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 650 since Aug 2009
Thanks Given: 320
Thanks Received: 623

Try removing the empty string in your EnterLong and EnterShort:

 
Code
EnterLong(DefaultQuantity);
Give it a try as (possibly!) the string identifies the order therefore NT waits for you exiting the named order first.

Reply With Quote
Thanked by:
  #9 (permalink)
 
DeadCatBounced's Avatar
 DeadCatBounced 
Baltimore MD US
 
Experience: Intermediate
Platform: NinjaTrader
Trading: ES, NQ
Posts: 293 since Apr 2013
Thanks Given: 1,514
Thanks Received: 736

Thanks for the help gregid, no luck tho :/

Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #10 (permalink)
 
Tasker_182's Avatar
 Tasker_182 
Cedar Rapids, iowa
Legendary Market Wizard
 
Experience: Intermediate
Platform: Ninjatrader
Broker: Ninjatrader - Continuum
Posts: 716 since Aug 2009
Thanks Given: 476
Thanks Received: 1,401



Trader Chris View Post
I still cannot get it to reverse positions for me.

When I looked at the code for the SampleMACrossOver strat that ninja supplies (here is their code)

 
Code
			if (CrossAbove(SMA(Fast), SMA(Slow), 1))
			    EnterLong();
			else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
			    EnterShort();
It reverses fine.

Yet my code, which I think has the same basic logic:

 
Code
            if (CrossAbove(GetCurrentAsk() + (Offset * TickSize), SMA(SMAlookback), 1))
			{
                EnterShort(DefaultQuantity, "");
			}
            else if (CrossBelow(GetCurrentBid() + (Offsetneg * TickSize), SMA(SMAlookback), 1))
			{
                EnterLong(DefaultQuantity, "");
			}

Only goes has the one entry and will not reverse on me :/

Any ideas on what I am doing wrong?


If you haven't already done so, read the NinjaTrader help file and in particular look for the statements you are using and what their requirements are. It takes time to go through the help file but there really is quite a bit of relevant info and examples in there.

In general:
1) When something does not seem to execute check the Ninja Log for any error statements. Also bring up the output window to see if anything is printed there.
2)use Print statements (that output to the Output window) to identify that your code is executing through the logical area in question. For example use a Print statement in your "else if" section to show that the program is going through the "else if" section. If it does then use further print statements perhaps to advise the status of the variables being used to help understand the logic decisions.

It can be frustrating yet rewarding when you make breakthroughs in coding, just keep plugging away.

Reply With Quote
Thanked by:




Last Updated on June 12, 2013


© 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