Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Adding BarsSinceEntry causes a problem on backtest
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.
Can you help answer these questions from other members on NexusFi?
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.
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.
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:
// 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();
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:
// 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:
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:
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.