Adding BarsSinceEntry causes a problem on backtest - NinjaTrader Programming | futures io social day trading
futures io futures trading


Adding BarsSinceEntry causes a problem on backtest
Updated: Views / Replies:843 / 6
Created: by PhillyD 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
 

Adding BarsSinceEntry causes a problem on backtest

  #1 (permalink)
Trading Apprentice
Houston, TX
 
Futures Experience: Intermediate
Platform: CQG, Interactive Brokers
Favorite Futures: ES, CL
 
Posts: 4 since Sep 2014
Thanks: 1 given, 0 received

Adding BarsSinceEntry causes a problem on backtest

Hi all,

I've just started working with NinjaScript to build an automated trading bot for a strategy I've been trading manually. I have a little bit of programming experience, years ago, and I figured the easiest way to jump start this project was to construct as much as I could of the strategy in the Strategy Wizard, then when I hit the limits of what I can in the Wizard, unlock the code and program it manually from there.

I made a bit of progress today but I've hit a roadblock and I'm not sure what's causing it.

The problem is as follows: my strategy does what it's supposed to in the historical backtest until I add the following:

BarsSinceEntry() >= EntryDistance

whereupon when I backtest the strategy all the trade entries and exits simply disappear. No trades are placed any more. I'm not sure why adding BarsSinceEntry() causes this. EntryDistance is a user defined input integer with a default value of 6. What I'm trying to accomplish is to have at least 6 bars between consecutive long entries.

Any help would be appreciated. I've copied my code below.

 
Code
        protected override void Initialize()
        {
            SetStopLoss("LongEntry", CalculationMode.Ticks, 15, false);

            CalculateOnBarClose = true;
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            // Condition set 1
            if (EMA(MA1Shortperiod)[0] > EMA(MA1Longperiod)[0]
                && RSI(RSIperiod, 3).Avg[0] > RSIbuylevel
                && ToTime(Time[0]) > ToTime(9, 0, 0)
                && ToTime(Time[0]) < ToTime(13, 59, 0)
                && Position.MarketPosition != MarketPosition.Short
                && BarsSinceEntry() >= EntryDistance)
            {
                EnterLongLimit(DefaultQuantity, Close[0], "LongEntry");
            }

        }

        #region Properties
        [Description("Short MA on smallest time frame")]
        [GridCategory("Parameters")]
        public int MA1Shortperiod
        {
            get { return mA1Shortperiod; }
            set { mA1Shortperiod = Math.Max(1, value); }
        }

        [Description("Long MA on smallest time frame")]
        [GridCategory("Parameters")]
        public int MA1Longperiod
        {
            get { return mA1Longperiod; }
            set { mA1Longperiod = Math.Max(1, value); }
        }

        [Description("RSI on smallest time frame")]
        [GridCategory("Parameters")]
        public int RSIperiod
        {
            get { return rSIperiod; }
            set { rSIperiod = Math.Max(1, value); }
        }

        [Description("ATR on largest time frame")]
        [GridCategory("Parameters")]
        public int ATRperiod
        {
            get { return aTRperiod; }
            set { aTRperiod = Math.Max(1, value); }
        }

        [Description("RSI buy level")]
        [GridCategory("Parameters")]
        public int RSIbuylevel
        {
            get { return rSIbuylevel; }
            set { rSIbuylevel = Math.Max(1, value); }
        }

        [Description("RSI sell level")]
        [GridCategory("Parameters")]
        public int RSIselllevel
        {
            get { return rSIselllevel; }
            set { rSIselllevel = Math.Max(1, value); }
        }

        [Description("Distance between entries, in bars")]
        [GridCategory("Parameters")]
        public int EntryDistance
        {
            get { return entryDistance; }
            set { entryDistance = Math.Max(1, value); }
        }
        #endregion
    }

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
Denver, CO
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: NinjaTrader Brokerage
Favorite Futures: ES
 
NinjaTrader's Avatar
 
Posts: 1,260 since May 2010
Thanks: 153 given, 1,890 received


Quick guess that BarSinceEntry() is zero on start thus your logic will never call EnterLongLimit()...


PhillyD View Post
Hi all,

I've just started working with NinjaScript to build an automated trading bot for a strategy I've been trading manually. I have a little bit of programming experience, years ago, and I figured the easiest way to jump start this project was to construct as much as I could of the strategy in the Strategy Wizard, then when I hit the limits of what I can in the Wizard, unlock the code and program it manually from there.

I made a bit of progress today but I've hit a roadblock and I'm not sure what's causing it.

The problem is as follows: my strategy does what it's supposed to in the historical backtest until I add the following:

BarsSinceEntry() >= EntryDistance

whereupon when I backtest the strategy all the trade entries and exits simply disappear. No trades are placed any more. I'm not sure why adding BarsSinceEntry() causes this. EntryDistance is a user defined input integer with a default value of 6. What I'm trying to accomplish is to have at least 6 bars between consecutive long entries.

Any help would be appreciated. I've copied my code below.

 
Code
        protected override void Initialize()
        {
            SetStopLoss("LongEntry", CalculationMode.Ticks, 15, false);

            CalculateOnBarClose = true;
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            // Condition set 1
            if (EMA(MA1Shortperiod)[0] > EMA(MA1Longperiod)[0]
                && RSI(RSIperiod, 3).Avg[0] > RSIbuylevel
                && ToTime(Time[0]) > ToTime(9, 0, 0)
                && ToTime(Time[0]) < ToTime(13, 59, 0)
                && Position.MarketPosition != MarketPosition.Short
                && BarsSinceEntry() >= EntryDistance)
            {
                EnterLongLimit(DefaultQuantity, Close[0], "LongEntry");
            }

        }

        #region Properties
        [Description("Short MA on smallest time frame")]
        [GridCategory("Parameters")]
        public int MA1Shortperiod
        {
            get { return mA1Shortperiod; }
            set { mA1Shortperiod = Math.Max(1, value); }
        }

        [Description("Long MA on smallest time frame")]
        [GridCategory("Parameters")]
        public int MA1Longperiod
        {
            get { return mA1Longperiod; }
            set { mA1Longperiod = Math.Max(1, value); }
        }

        [Description("RSI on smallest time frame")]
        [GridCategory("Parameters")]
        public int RSIperiod
        {
            get { return rSIperiod; }
            set { rSIperiod = Math.Max(1, value); }
        }

        [Description("ATR on largest time frame")]
        [GridCategory("Parameters")]
        public int ATRperiod
        {
            get { return aTRperiod; }
            set { aTRperiod = Math.Max(1, value); }
        }

        [Description("RSI buy level")]
        [GridCategory("Parameters")]
        public int RSIbuylevel
        {
            get { return rSIbuylevel; }
            set { rSIbuylevel = Math.Max(1, value); }
        }

        [Description("RSI sell level")]
        [GridCategory("Parameters")]
        public int RSIselllevel
        {
            get { return rSIselllevel; }
            set { rSIselllevel = Math.Max(1, value); }
        }

        [Description("Distance between entries, in bars")]
        [GridCategory("Parameters")]
        public int EntryDistance
        {
            get { return entryDistance; }
            set { entryDistance = Math.Max(1, value); }
        }
        #endregion
    }


Reply With Quote
The following user says Thank You to NinjaTrader for this post:
 
  #4 (permalink)
Trading Apprentice
Houston, TX
 
Futures Experience: Intermediate
Platform: CQG, Interactive Brokers
Favorite Futures: ES, CL
 
Posts: 4 since Sep 2014
Thanks: 1 given, 0 received

Hi NT,

Thanks for pointing that out. I missed that.

How would I work around that? Is it possible to set BarsSinceEntry() with an initial starting value? My idea is to set it equal to EntryDistance, right before Condition Set 1.

Reply With Quote
 
  #5 (permalink)
Elite Member
Denver, CO
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: NinjaTrader Brokerage
Favorite Futures: ES
 
NinjaTrader's Avatar
 
Posts: 1,260 since May 2010
Thanks: 153 given, 1,890 received


PhillyD View Post
Hi NT,

Thanks for pointing that out. I missed that.

How would I work around that? Is it possible to set BarsSinceEntry() with an initial starting value? My idea is to set it equal to EntryDistance, right before Condition Set 1.

You cannot set that. I have a team of NinjaScript experts who can help point you in the right direction but they don't respond here. Please post in our support forum to enlist their help.

Reply With Quote
 
  #6 (permalink)
Elite Member
Heidelberg, Germany
 
Futures Experience: Intermediate
Platform: NinjaTrader
Favorite Futures: Stocks
 
Posts: 11 since Jun 2014
Thanks: 6 given, 12 received


PhillyD View Post
What I'm trying to accomplish is to have at least 6 bars between consecutive long entries.

I suspect the correct function you are looking for is BarsSinceExit() and NOT BarsSinceEntry().

From the documentation: "Returns the number of bars that have elapsed since the last specified exit." With this small code example:

 
Code
// Only enter if at least 10 bars has passed since our last exit or if we have never traded yet
if ((BarsSinceExit() > 10 || BarsSinceExit() == -1) && CrossAbove(SMA(10), SMA(20), 1))
     EnterLong();
Brian

Reply With Quote
 
  #7 (permalink)
Trading Apprentice
Houston, TX
 
Futures Experience: Intermediate
Platform: CQG, Interactive Brokers
Favorite Futures: ES, CL
 
Posts: 4 since Sep 2014
Thanks: 1 given, 0 received


zr6bcm View Post
I suspect the correct function you are looking for is BarsSinceExit() and NOT BarsSinceEntry().

From the documentation: "Returns the number of bars that have elapsed since the last specified exit." With this small code example:

 
Code
// Only enter if at least 10 bars has passed since our last exit or if we have never traded yet
if ((BarsSinceExit() > 10 || BarsSinceExit() == -1) && CrossAbove(SMA(10), SMA(20), 1))
     EnterLong();
Brian

Hi Brian,

The guys on the NT support forum helped me with this one. The correct function is indeed BarsSinceEntry, and not BarsSinceExit.

The method we used was to use an "if" statement containing the BarsSinceEntry condition and combining it with a boolean variable which acts like a toggle switch, allowing the first trade of the day to be taken, and then subsequent trades are distanced correctly from the most recent trade.

Simple example in pseudocode for the first trade of the session:

 
Code
bool InitialTradeEntry = false;

if (InitialTradeEntry = false && [insert any other conditions])
       EnterLong; //first trade
       InitialTradeEntry == true; //the boolean is now flipped to true
And for subsequent trades:

 
Code
if (BarsSinceEntry >= 6 && InitialTradeEntry = true && [insert any other conditions])
       EnterLong2; //next trade
Hope this helps anyone looking for the answer to a similar issue in the future.

Reply With Quote

Reply



futures io > > > > > Adding BarsSinceEntry causes a problem on backtest

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
Renko/manual backtest/hybrid backtest BrianH NinjaTrader 0 November 4th, 2013 02:07 PM
Backtest parameters rows - adding Astrogirl NinjaTrader Programming 2 October 20th, 2013 04:21 AM
How I Backtest GaryD Traders Hideout 3 December 17th, 2011 01:01 PM
Anyone want to backtest!!!!!!! incometrade Traders Hideout 7 September 22nd, 2010 10:42 PM
Backtest Big Mike Terms (Glossary) 0 June 27th, 2010 02:28 PM


All times are GMT -4. The time now is 04:40 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.10 seconds with 19 queries on phoenix via your IP 54.82.112.193