New at Programming, stuck with a crossover strat - NinjaTrader Programming | futures io social day trading
futures io futures trading


New at Programming, stuck with a crossover strat
Updated: Views / Replies:1,516 / 13
Created: by DeadCatBounced Attachments:0

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors – all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you don’t need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 
Thread Tools Search this Thread
 

New at Programming, stuck with a crossover strat

  #1 (permalink)
Elite Member
Baltimore MD US
 
Futures Experience: Intermediate
Platform: NinjaTrader
Favorite Futures: ES, NQ
 
DeadCatBounced's Avatar
 
Posts: 283 since Apr 2013
Thanks: 1,507 given, 620 received

New at Programming, stuck with a crossover strat

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?

Reply With Quote
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received


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
The following 2 users say Thank You to gregid for this post:
 
  #4 (permalink)
Elite Member
Baltimore MD US
 
Futures Experience: Intermediate
Platform: NinjaTrader
Favorite Futures: ES, NQ
 
DeadCatBounced's Avatar
 
Posts: 283 since Apr 2013
Thanks: 1,507 given, 620 received

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?

Reply With Quote
 
  #5 (permalink)
Elite Member
Portland Oregon, United States
 
Futures Experience: Beginner
Platform: Ninjatrader®
Broker/Data: CQG, Kinetick
Favorite Futures: Gameplay Klownbine® Trading of Globex
 
Zondor's Avatar
 
Posts: 1,327 since Jul 2009
Thanks: 1,246 given, 2,635 received

Don't get into the habit of using this horrible programming technique

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
Reply With Quote
The following user says Thank You to Zondor for this post:
 
  #6 (permalink)
Elite Member
Baltimore MD US
 
Futures Experience: Intermediate
Platform: NinjaTrader
Favorite Futures: ES, NQ
 
DeadCatBounced's Avatar
 
Posts: 283 since Apr 2013
Thanks: 1,507 given, 620 received

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?

Reply With Quote
 
  #7 (permalink)
Elite Member
Baltimore MD US
 
Futures Experience: Intermediate
Platform: NinjaTrader
Favorite Futures: ES, NQ
 
DeadCatBounced's Avatar
 
Posts: 283 since Apr 2013
Thanks: 1,507 given, 620 received

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?

Reply With Quote
 
  #8 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received

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
The following user says Thank You to gregid for this post:
 
  #9 (permalink)
Elite Member
Baltimore MD US
 
Futures Experience: Intermediate
Platform: NinjaTrader
Favorite Futures: ES, NQ
 
DeadCatBounced's Avatar
 
Posts: 283 since Apr 2013
Thanks: 1,507 given, 620 received

Thanks for the help gregid, no luck tho :/

Reply With Quote
 
  #10 (permalink)
Elite Member
Cedar Rapids, iowa
 
Futures Experience: Intermediate
Platform: Ninjatrader
Broker/Data: Ninjatrader - Continuum
Favorite Futures: 6E, TF, 6J
 
Tasker_182's Avatar
 
Posts: 626 since Aug 2009
Thanks: 426 given, 1,126 received



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
The following user says Thank You to Tasker_182 for this post:

Reply



futures io > > > > > New at Programming, stuck with a crossover strat

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Linda Bradford Raschke: Reading The Tape

Elite only

Adam Grimes: TBA

Elite only

NinjaTrader: TBA

January

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual Programming (programming without coding) LukeGeniol Platforms and Indicators 11 September 16th, 2016 01:49 PM
MTF Strat question.... Tanju23 Elite Automated NinjaTrader Trading 2 April 25th, 2012 03:42 AM
Pimp My Strat! MXASJ The Elite Circle 53 August 19th, 2011 02:44 PM
Stuck on FN key timmyb Tech Support 1 January 24th, 2011 02:52 PM
Anyone have a strat they would like ported to 7? ZTR NinjaTrader 1 February 22nd, 2010 11:33 AM


All times are GMT -4. The time now is 02:48 AM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts
Page generated 2017-12-12 in 0.12 seconds with 19 queries on phoenix via your IP 54.90.207.75