NexusFi: Find Your Edge


Home Menu

 





Adding BarsSinceEntry causes a problem on backtest


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one PhillyD with 3 posts (0 thanks)
    2. looks_two NinjaTrader with 2 posts (1 thanks)
    3. looks_3 Quick Summary with 1 posts (0 thanks)
    4. looks_4 zr6bcm with 1 posts (0 thanks)
    1. trending_up 2,080 views
    2. thumb_up 1 thanks given
    3. group 2 followers
    1. forum 6 posts
    2. attach_file 0 attachments




 
Search this Thread

Adding BarsSinceEntry causes a problem on backtest

  #1 (permalink)
PhillyD
Houston, TX
 
Posts: 4 since Sep 2014
Thanks Given: 1
Thanks Received: 0

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

Can you help answer these questions
from other members on NexusFi?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Exit Strategy
NinjaTrader
Better Renko Gaps
The Elite Circle
Trade idea based off three indicators.
Traders Hideout
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Diary of a simple price action trader
26 thanks
Just another trading journal: PA, Wyckoff & Trends
25 thanks
Tao te Trade: way of the WLD
22 thanks
My NQ Trading Journal
16 thanks
HumbleTraders next chapter
9 thanks
  #3 (permalink)
 
NinjaTrader's Avatar
 NinjaTrader  NinjaTrader is an official Site Sponsor
Site Sponsor

Web: NinjaTrader
AMA: Ask Me Anything
Webinars: NinjaTrader Webinars
Elite offer: Click here
 
Posts: 1,713 since May 2010
Thanks Given: 203
Thanks Received: 2,686


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
    }


Follow me on Twitter Reply With Quote
Thanked by:
  #4 (permalink)
PhillyD
Houston, TX
 
Posts: 4 since Sep 2014
Thanks Given: 1
Thanks Received: 0

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)
 
NinjaTrader's Avatar
 NinjaTrader  NinjaTrader is an official Site Sponsor
Site Sponsor

Web: NinjaTrader
AMA: Ask Me Anything
Webinars: NinjaTrader Webinars
Elite offer: Click here
 
Posts: 1,713 since May 2010
Thanks Given: 203
Thanks Received: 2,686


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.

Follow me on Twitter Reply With Quote
  #6 (permalink)
 zr6bcm 
Heidelberg, Germany
 
Experience: Intermediate
Platform: NinjaTrader
Trading: Stocks
Posts: 12 since Jun 2014
Thanks Given: 9
Thanks Received: 12


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)
PhillyD
Houston, TX
 
Posts: 4 since Sep 2014
Thanks Given: 1
Thanks Received: 0


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




Last Updated on October 27, 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