NexusFi: Find Your Edge


Home Menu

 





Seeking coding solution – NinjaTrader


Discussion in NinjaTrader

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




 
Search this Thread

Seeking coding solution – NinjaTrader

  #1 (permalink)
 Abde 
Stuttgart / Germany
 
Experience: Intermediate
Platform: FlatTrader
Broker: GFT and Interactive Brokers
Trading: ES, DAX
Frequency: Every few days
Duration: Days
Posts: 533 since Aug 2010
Thanks Given: 2,141
Thanks Received: 729

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:	177
Size:	63.7 KB
ID:	169815  
Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
MC PL editor upgrade
MultiCharts
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
Exit Strategy
NinjaTrader
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
ZombieSqueeze
Platforms and Indicators
 
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
23 thanks
My NQ Trading Journal
16 thanks
HumbleTraders next chapter
9 thanks
  #2 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102

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
Thanked by:
  #3 (permalink)
 Abde 
Stuttgart / Germany
 
Experience: Intermediate
Platform: FlatTrader
Broker: GFT and Interactive Brokers
Trading: ES, DAX
Frequency: Every few days
Duration: Days
Posts: 533 since Aug 2010
Thanks Given: 2,141
Thanks Received: 729



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
  #4 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102


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
Thanked by:




Last Updated on December 22, 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