Seeking coding solution – NinjaTrader - futures io
futures io futures trading



Seeking coding solution – NinjaTrader


Discussion in NinjaTrader

Updated
    1. trending_up 1,213 views
    2. thumb_up 4 thanks given
    3. group 2 followers
    1. forum 3 posts
    2. attach_file 1 attachments




Welcome to futures io: the largest futures trading community on the planet, with well over 125,000 members
  • 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 and simple.

-- Big Mike, Site Administrator

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

 
Search this Thread
 

Seeking coding solution – NinjaTrader

(login for full post details)
  #1 (permalink)
Germany
 
Experience: Intermediate
Platform: FlatTrader
Broker: GFT and Interactive Brokers
Trading: ES, DAX
 
Posts: 532 since Aug 2010
Thanks: 2,141 given, 726 received

Hello Coders,

I made an indicator that colors bars with a smaller open to close range than the previous 3 bars. I used this code:

 
Code
if( Close[0] > Open[0] && Math.Abs(Close[0] - Open[0]) < Math.Abs(Close[1] - Open[1]) && Math.Abs(Close[0] - Open[0]) < Math.Abs(Close[2] - Open[2]) && Math.Abs(Close[0] - Open[0]) < Math.Abs(Close[3] – Open[3]))

else if( Close[0] <= Open[0] && Math.Abs(Close[0] - Open[0]) <= Math.Abs(Close[1] - Open[1]) && Math.Abs(Close[0] - Open[0]) <= Math.Abs(Close[2] - Open[2]) && Math.Abs(Close[0] - Open[0]) <= Math.Abs(Close[3] – Open[3]))
I like now to improve that indicator by adding a selectable look back period and made these changes:

 
Code
#region Variables:
private int		barsback	=	3;

OnBarUpdate():
if (CurrentBar < barsback+1)	
{ return; }

for (int j=1; j<=barsback; j++)

if( Close[0] > Open[0] && Math.Abs(Close[0] - Open[0]) <= Math.Abs(Close[j] – Open[j]))

else if( Close[0] <= Open[0] && Math.Abs(Close[0] - Open[0]) <= Math.Abs(Close[j] – Open[j]))
Unfortunately that didn´t work and it colors every bar, with a smaller open to close range than the entire selected look back period
(see attached chart). What I´m doing wrong here?

Many thanks in advance for every tip.

Attached Thumbnails
Click image for larger version

Name:	IW StoppingCandlesPeriodX.jpg
Views:	63
Size:	63.7 KB
ID:	169815  
Started this thread Reply With Quote

Can you help answer these questions
from other members on futures io?
Automatic pattern research with NT
Elite Algorithmic NinjaTrader Trading
How to implement this?
MultiCharts
COTbase for quant trading?
Elite Algorithmic NinjaTrader Trading
Regarding Multicharts strategy trigger
MultiCharts
If ES tracks SPX, why do support/resistance, Level 2, vo …
Emini and Emicro Index
 
 
(login for full post details)
  #2 (permalink)
Market Wizard
Berlin, Europe
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,855 since Mar 2010
Thanks: 4,238 given, 26,731 received

It colors every bar because the code does not keep in mind the prior result. Basically your condition would be false, if there is a single bar within the lookback period "barsback" that has a smaller body then the current bar. A workaround would be to use a boolean variable "narrowBody", set that variable to true, and then loop through the prior bars. In case that you find a single bar that has a smaller body than the current bar you may set the boolean to false and exit the loop:

 
Code
bool narrowBodyBar = true;
double bodyRange = Math.Abs(Close[0] - Open[0]);
for int j = 1; j <= barsBack; j++)
   if(Math.Abs(Close[j]-Open[j]) < bodyRange)
   {
         narrowBodyBar=false;
         break;  //to exit loop, as the condition narrowBodyBar cannot come true anymore
   }


The baisc logic of the indicator - detecting a narrow range - requires that it runs in mode "CalculateOnBarClose = true". If you wish to run it in "CalculateOnBarClose = false" to get a preliminary result, then the code above would be highly inefficient, as you would need to run through the entire loop with every incoming tick. Therefore to make the indicator efficient for use with "CalculateOnBarClose = false" you would need to modify the code, for example:

 
Code
#region  Variables
double minBody = 0.0;
bool narrowBodyBar = false;
#endregion

// within OnBarUpdate
if(FirstTickOfBar) // executed once per bar
{
    minBody = Math.Abs(Close[1] - Open[1])
    for (int j = 2; j <= barsBack; j++)
        if(Math.Abs(Close[j] - Open[j]) < minBody)
             minBody = Math.Abs(Close[j] - Open[j]);
}
if (Math.Abs(Close[0] - Open[0]) <= minBody) // executed with every incoming tick
   narrowBodyBar = true;

Not tested.

Reply With Quote
The following 3 users say Thank You to Fat Tails for this post:
 
(login for full post details)
  #3 (permalink)
Germany
 
Experience: Intermediate
Platform: FlatTrader
Broker: GFT and Interactive Brokers
Trading: ES, DAX
 
Posts: 532 since Aug 2010
Thanks: 2,141 given, 726 received



Fat Tails View Post
It colors every bar because the code does not keep in mind the prior result. Basically your condition would be false, if there is a single bar within the lookback period "barsback" that has a smaller body then the current bar. A workaround would be to use a boolean variable "narrowBody", set that variable to true, and then loop through the prior bars. In case that you find a single bar that has a smaller body than the current bar you may set the boolean to false and exit the loop:

 
Code
bool narrowBodyBar = true;
double bodyRange = Math.Abs(Close[0] - Open[0]);
for int j = 1; j <= barsBack; j++)
   if(Math.Abs(Close[j]-Open[j]) < bodyRange)
   {
         narrowBodyBar=false;
         break;  //to exit loop, as the condition narrowBodyBar cannot come true anymore
   }


The baisc logic of the indicator - detecting a narrow range - requires that it runs in mode "CalculateOnBarClose = true". If you wish to run it in "CalculateOnBarClose = false" to get a preliminary result, then the code above would be highly inefficient, as you would need to run through the entire loop with every incoming tick. Therefore to make the indicator efficient for use with "CalculateOnBarClose = false" you would need to modify the code, for example:

 
Code
#region  Variables
double minBody = 0.0;
bool narrowBodyBar = false;
#endregion

// within OnBarUpdate
if(FirstTickOfBar) // executed once per bar
{
    minBody = Math.Abs(Close[1] - Open[1])
    for (int j = 2; j <= barsBack; j++)
        if(Math.Abs(Close[j] - Open[j]) < minBody)
             minBody = Math.Abs(Close[j] - Open[j]);
}
if (Math.Abs(Close[0] - Open[0]) <= minBody) // executed with every incoming tick
   narrowBodyBar = true;

Not tested.

@Fat Tails

Many Thanks for your help Harry!

It makes no sense to run this indicator in mode "CalculateOnBarClose = false. I will try therefore your first code.
Does the complete code goes to the "OnBarUpdate()" section?

Started this thread Reply With Quote
 
(login for full post details)
  #4 (permalink)
Market Wizard
Berlin, Europe
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,855 since Mar 2010
Thanks: 4,238 given, 26,731 received


Abde View Post
@Fat Tails

Many Thanks for your help Harry!

It makes no sense to run this indicator in mode "CalculateOnBarClose = false. I will try therefore your first code.
Does the complete code goes to the "OnBarUpdate()" section?

The first example should be included with OnBarUpdate().

Reply With Quote
The following user says Thank You to Fat Tails for this post:


futures io Trading Community Platforms and Indicators NinjaTrader > Seeking coding solution – NinjaTrader


Last Updated on December 22, 2014


Upcoming Webinars and Events
 

NinjaTrader Indicator Challenge!

Ongoing
 

Battlestations! Show us your trading desk - $1,500 in prizes!

March
 

Importance of Finding Your Own Way w/Adam Grimes

Elite only
 

Journal Challenge w/Jigsaw

April
     



Copyright © 2021 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