NexusFi: Find Your Edge


Home Menu

 





Conditions true a varying number of bars back


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one DavidBodhi with 5 posts (1 thanks)
    2. looks_two xplorer with 2 posts (0 thanks)
    3. looks_3 SpeculatorSeth with 2 posts (2 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
    1. trending_up 1,310 views
    2. thumb_up 3 thanks given
    3. group 2 followers
    1. forum 9 posts
    2. attach_file 1 attachments




 
Search this Thread

Conditions true a varying number of bars back

  #1 (permalink)
 
DavidBodhi's Avatar
 DavidBodhi 
Milwaukee, WI, USA
 
Experience: Intermediate
Platform: NinjaTrader
Trading: Equities
Posts: 209 since Oct 2014
Thanks Given: 23
Thanks Received: 207

Can anyone suggest code that would allow a strategy to check whether conditions were true for the previous X number of bars, where X is a user-input parameter?

I'm not even sure how to clearly ask a search engine, so I thought I'd post here.

Follow me on Twitter Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Exit Strategy
NinjaTrader
Increase in trading performance by 75%
The Elite Circle
What is Markets Chat (markets.chat) real-time trading ro …
Trading Reviews and Vendors
MC PL editor upgrade
MultiCharts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
30 thanks
Spoo-nalysis ES e-mini futures S&P 500
28 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
20 thanks
GFIs1 1 DAX trade per day journal
16 thanks
  #3 (permalink)
 
xplorer's Avatar
 xplorer 
London UK
Site Moderator
 
Experience: Beginner
Platform: CQG
Broker: S5
Trading: Futures
Posts: 5,972 since Sep 2015
Thanks Given: 15,487
Thanks Received: 15,382



DavidBodhi View Post
Can anyone suggest code that would allow a strategy to check whether conditions were true for the previous X number of bars, where X is a user-input parameter?

I'm not even sure how to clearly ask a search engine, so I thought I'd post here.

It's relatively simple - for example on NT7 it can be driven by the "new strategy" wizard.



The screenshot shows where your user-defined parameter is. You can set a default there, which you can then change when you activate the strategy.

This is how I would do it, although there's probably other ways to do it.

Reply With Quote
  #4 (permalink)
 
DavidBodhi's Avatar
 DavidBodhi 
Milwaukee, WI, USA
 
Experience: Intermediate
Platform: NinjaTrader
Trading: Equities
Posts: 209 since Oct 2014
Thanks Given: 23
Thanks Received: 207


xplorer View Post
It's relatively simple - for example on NT7 it can be driven by the "new strategy" wizard.



The screenshot shows where your user-defined parameter is. You can set a default there, which you can then change when you activate the strategy.

This is how I would do it, although there's probably other ways to do it.

Thanks for the reply, xplorer.

I'm aware of how to set a user-defined parameter. What I don't know how to do is use that parameter as a variable number of bar numbers.

For example, I can code:

if (Close[2] < KeltnerChannel(1.5, 10).Midline[2]
&& Close[1] < KeltnerChannel(1.5, 10).Midline[1]
&& Close[0] < KeltnerChannel(1.5, 10).Midline[0])
{ DO SOMETHING}

But I want one line that will do the same as the above when I input 3 as the user parameter, and do the equivalent of 4 or 5 or 25 lines like the above as I change that parameter.

(I *knew* I wasn't explaining clearly what I wanted...)

Follow me on Twitter Started this thread Reply With Quote
Thanked by:
  #5 (permalink)
 
xplorer's Avatar
 xplorer 
London UK
Site Moderator
 
Experience: Beginner
Platform: CQG
Broker: S5
Trading: Futures
Posts: 5,972 since Sep 2015
Thanks Given: 15,487
Thanks Received: 15,382


DavidBodhi View Post
Thanks for the reply, xplorer.

I'm aware of how to set a user-defined parameter. What I don't know how to do is use that parameter as a variable number of bar numbers.

For example, I can code:

if (Close[2] < KeltnerChannel(1.5, 10).Midline[2]
&& Close[1] < KeltnerChannel(1.5, 10).Midline[1]
&& Close[0] < KeltnerChannel(1.5, 10).Midline[0])
{ DO SOMETHING}

But I want one line that will do the same as the above when I input 3 as the user parameter, and do the equivalent of 4 or 5 or 25 lines like the above as I change that parameter.

(I *knew* I wasn't explaining clearly what I wanted...)

Assuming I understand correctly now, it would be a matter of replacing (just taking one line of code as example)
if (Close[2] < KeltnerChannel(1.5, 10).Midline[2]

with

if (Close[MyInput0] < KeltnerChannel(1.5, 10).Midline[2]

unless I misunderstood again?

Reply With Quote
  #6 (permalink)
 
DavidBodhi's Avatar
 DavidBodhi 
Milwaukee, WI, USA
 
Experience: Intermediate
Platform: NinjaTrader
Trading: Equities
Posts: 209 since Oct 2014
Thanks Given: 23
Thanks Received: 207


xplorer View Post
Assuming I understand correctly now, it would be a matter of replacing (just taking one line of code as example)
if (Close[2] < KeltnerChannel(1.5, 10).Midline[2]

with

if (Close[MyInput0] < KeltnerChannel(1.5, 10).Midline[2]

unless I misunderstood again?

I'm afraid this won't work, either. If the input number was 25, it'd check if the 25th bar back was lower than Keltner Channel's midline 2 bars ago. Though changing the midline's [2] to [MyInput0] would compare Close[25] to midline[25], which is incrementally closer to what I want.
I need it to check whether all the last 25 bars were lower than the Keltner midline's values for those same bars.

Follow me on Twitter Started this thread Reply With Quote
  #7 (permalink)
 SpeculatorSeth   is a Vendor
 
Posts: 780 since Apr 2016
Thanks Given: 22
Thanks Received: 1,018

You need some sort of loop. Here's a loop that I use to calculate how long we've been in a range for.

 
Code
int current = 0;
double high = High[current];
double low = Low[current];

do
{
  high = Math.Max(high, High[current]);
  low = Math.Min(low, Low[current]);
  current++;
}
while(high - low <= Width * TickSize && current < CurrentBar);

Reply With Quote
Thanked by:
  #8 (permalink)
 
DavidBodhi's Avatar
 DavidBodhi 
Milwaukee, WI, USA
 
Experience: Intermediate
Platform: NinjaTrader
Trading: Equities
Posts: 209 since Oct 2014
Thanks Given: 23
Thanks Received: 207


TWDsje View Post
You need some sort of loop. Here's a loop that I use to calculate how long we've been in a range for.

 
Code
int current = 0;
double high = High[current];
double low = Low[current];

do
{
  high = Math.Max(high, High[current]);
  low = Math.Min(low, Low[current]);
  current++;
}
while(high - low <= Width * TickSize && current < CurrentBar);

Ahhh... I get it. You use variables to track your conditions and then count how many times it's true.
Perfect. I can certainly use this concept for what I'm trying to do. Thank you!

But, I've never seen do/while before.
Does that mean keep doing the 'do' as long as the 'while' condition is true?

Follow me on Twitter Started this thread Reply With Quote
  #9 (permalink)
 SpeculatorSeth   is a Vendor
 
Posts: 780 since Apr 2016
Thanks Given: 22
Thanks Received: 1,018


DavidBodhi View Post
Ahhh... I get it. You use variables to track your conditions and then count how many times it's true.
Perfect. I can certainly use this concept for what I'm trying to do. Thank you!

But, I've never seen do/while before.
Does that mean keep doing the 'do' as long as the 'while' condition is true?

A do while loop always executes the code in the block first, and then checks the variable. So in this instance I'm using the do while loop to ensure that it always executes at least once for bar 0. So a do while might not always be the right solution, but the same idea would apply to a foreach or while loop. Keep iterating back in bars until the condition is no longer true.

Reply With Quote
Thanked by:
  #10 (permalink)
 
DavidBodhi's Avatar
 DavidBodhi 
Milwaukee, WI, USA
 
Experience: Intermediate
Platform: NinjaTrader
Trading: Equities
Posts: 209 since Oct 2014
Thanks Given: 23
Thanks Received: 207



TWDsje View Post
A do while loop always executes the code in the block first, and then checks the variable. So in this instance I'm using the do while loop to ensure that it always executes at least once for bar 0. So a do while might not always be the right solution, but the same idea would apply to a foreach or while loop. Keep iterating back in bars until the condition is no longer true.


Thank you. Much appreciated.

Follow me on Twitter Started this thread Reply With Quote




Last Updated on June 22, 2017


© 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