NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 12-20-2007, 07:03 PM   #1
Burga1
Senior Member
 
Join Date: Nov 2007
Posts: 388
Thanks: 0
Thanked 0 times in 0 posts
Default Setting Swing to a variable?

Hi,

How is it possible to set a variable equal to the last swing high (price and # of bars back)?

I've tried:

Opening = Bars.BarsSinceSession;
HI_PERIOD = Swing(4).SwingHighBar(0, 1, Opening);

I've tried using a dataseries:

Opening = Bars.BarsSinceSession;
HI_PERIOD.Set(Swing(4).SwingHighBar(0, 1, Opening));

When I have variables then I can use them to reference other commands, such as Stochastics and Momentum...

double StochHI_K = Stochastics(14, 3, 3).K[HI_PERIOD];
double HI_MOM = Momentum(14)[HI_PERIOD];
Burga1 is offline  
Reply With Quote
Old 12-20-2007, 10:41 PM   #2
Burga1
Senior Member
 
Join Date: Nov 2007
Posts: 388
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi,

I've made some progress, however it seems (according to Print statements) these variables seem to be returning exactly the same value...can somebody point out the error here?

HI_PERIOD = Math.Abs(Math.Max(0, Swing(7).SwingHighBar(0, 1, Opening))); //most recent high bar, back from CurrentBar
HI_PERIOD2 = Math.Abs(Math.Max(0, Swing(7).SwingHighBar(0, 2, Opening))); //2nd most recent high bar, back from CurrentBar
Burga1 is offline  
Reply With Quote
Old 12-20-2007, 11:33 PM   #3
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

To debug you want to remove as much excess variables as possible. Just run print statements on Swing(7).SwingHighBar(0, 1, CurrentBar) and Swing(7).SwingHighBar(0, 2, CurrentBar). Manually look at your chart to try and match up things. I usually print Time[0] to help mediate the pairing of prints with the chart too.

Code:
Print(Time[0] + " " + Swing(7).SwingHighBar(0, 1, CurrentBar) + " " + Swing(7).SwingHighBar(0, 2, CurrentBar));
NinjaTrader_Josh is offline  
Reply With Quote
Old 12-21-2007, 08:49 AM   #4
Burga1
Senior Member
 
Join Date: Nov 2007
Posts: 388
Thanks: 0
Thanked 0 times in 0 posts
Default

So is the syntax for what I'm trying to do correct? In other words this syntax is correct to determine the last 2 "highbars" (most recent and second most recent)...I'll do the print statements I just wanted to be sure I had the code correct.
Burga1 is offline  
Reply With Quote
Old 12-21-2007, 01:00 PM   #5
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

Yes it should work, but you really don't need the Math.Abs after the Math.Max. If you have to be at least 0 already you are already positive and Math.Abs is redundant.
NinjaTrader_Josh is offline  
Reply With Quote
Old 12-21-2007, 01:33 PM   #6
Burga1
Senior Member
 
Join Date: Nov 2007
Posts: 388
Thanks: 0
Thanked 0 times in 0 posts
Default

Thank you.
Burga1 is offline  
Reply With Quote
Old 12-21-2007, 09:58 PM   #7
Burga1
Senior Member
 
Join Date: Nov 2007
Posts: 388
Thanks: 0
Thanked 0 times in 0 posts
Default

Hmm, well this is odd--this seems this works fine:

HI_PERIOD = Math.Abs(Math.Max(0, Swing(7).SwingHighBar(0, 1, Opening))); //most recent high bar, back from CurrentBar
LAST_HI = High[HI_PERIOD];

but this is returning an incorrect value (from Print statements), I'm at a loss as to why:

LO_PERIOD = Math.Abs(Math.Min(0, Swing(7).SwingLowBar(0, 1, Opening))); //most recent low bar, back from CurrentBar
LAST_LO = Low[LO_PERIOD];


The "LO_PERIOD" DOES calculate back to the correct bar #, however the value for "LAST_LO" is returning something strange...is there an obvious code error here??
Burga1 is offline  
Reply With Quote
Old 12-21-2007, 11:25 PM   #8
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

Are you sure you are just not viewing the wrong bar in your manual comparison? If you tell it to print the value of Low[100] it will, without fail, return you the low of 100 bars ago.
NinjaTrader_Josh is offline  
Reply With Quote
Old 12-22-2007, 11:28 AM   #9
KBJ
Senior Member
 
Join Date: Mar 2007
Location: , Florida, USA
Posts: 663
Thanks: 36
Thanked 7 times in 6 posts
Default

I don't see that the Math.Abs, Math.Min and Math.Max functions are buying you anything but added complexity and trouble.

The value of Math.Min(0, ...) will never exceed zero. All valid bar numbers are zero or greater.

Thus you will only get a single valid bar number as LO_PERIOD and that will be zero (the most recent bar.)

You may want a check the return value from "Swing(x).Swing...Bar(...)" to make sure it isn't "-1" which indicates "not found", but I don't think Math.Min/Math.Max is going to give you the desired results in any/all cases.

And, as Josh pointed out, the Math.Abs is redundant. I believe it's always best to remove redundant or unneeded code, so one doesn't inadvertantly confuse oneself with it later.

Good luck.
KBJ is offline  
Reply With Quote
Old 12-22-2007, 01:40 PM   #10
Burga1
Senior Member
 
Join Date: Nov 2007
Posts: 388
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks for your responses. I have a related but different question. Is it possible to use the same (or similar) logic (or perhaps completely different logic) to determine the last time the Stochastics value was above/below a certain level (80/20 for example)??
Burga1 is offline  
Reply With Quote
Old 12-22-2007, 02:29 PM   #11
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

You sure can. Check out MRO. http://www.ninjatrader-support.com/H...urenceMRO.html
NinjaTrader_Josh is offline  
Reply With Quote
Old 12-23-2007, 11:13 PM   #12
Burga1
Senior Member
 
Join Date: Nov 2007
Posts: 388
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks, that command seems to work nicely. I'm using the following:

Opening = Bars.BarsSinceSession;
int HI_CONSTRAINT = MRO(delegate {return Stochastics(3, 14, 7).K[0] > 80;}, 1, Opening);
int LO_CONSTRAINT = MRO(delegate {return Stochastics(3, 14, 7).K[0] < 20;}, 1, Opening);
if((HI_CONSTRAINT < 0) || (LO_CONSTRAINT < 0)){return;} //Constrain to Stochastic above AND below 80/20

Is there a smooth way to compare and/or test for a "swing" at the bars that the MRO command returns? For example if (for the "HI_CONSTRAINT") a value is returned of "16" bars ago what might be a command to use that could be used to check if there is a "swing" at that period 16 bars ago?

Burga1 is offline  
Reply With Quote
Old 12-23-2007, 11:18 PM   #13
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

I would say just take the MRO's return and pop it into the lookback period for finding the swing. If the swing returns the same number then you have a winner on that bar. You may or may not need to also play with the barsAgo variable. Maybe have that set to one bar before what the MRO returns and then have lookback be one bar.
NinjaTrader_Josh is offline  
Reply With Quote
Old 12-24-2007, 12:03 AM   #14
Burga1
Senior Member
 
Join Date: Nov 2007
Posts: 388
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks, I've tried your suggestion both ways but am getting some odd results, I've tried:

HI_PERIOD = Math.Max(0, Swing(7).SwingHighBar(HI_CONSTRAINT - 1, 1, 1));
LO_PERIOD = Math.Max(0, Swing(7).SwingLowBar(LO_CONSTRAINT - 1, 1, 1));

And:

HI_PERIOD = Math.Max(0, Swing(7).SwingHighBar(0, 1, HI_CONSTRAINT));
LO_PERIOD = Math.Max(0, Swing(7).SwingLowBar(0, 1, LO_CONSTRAINT));



It is possible I mis-understood your last posting...
Burga1 is offline  
Reply With Quote
Old 12-24-2007, 02:36 AM   #15
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

The problem is that HI_CONSTRAINT can return as 0 or even -1. This becomes a problem when you simply do HI_CONSTRAINT - 1 to it because the barsAgo property needs to be at least a 0. You will need a check to prevent this from being below zero.
NinjaTrader_Josh is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cracking the Swing Code Richard Von Indicator Development 3 07-30-2011 11:36 PM
Swing High/Low and Zig Zag Indicators whitmark Suggestions And Feedback 39 01-19-2011 10:47 AM
Swing High/Low and Zig Zag Indicators Alfred Charting 9 09-03-2010 05:55 AM
If and when Gann swing charts come to NT.... Antraman Suggestions And Feedback 2 04-23-2009 06:10 AM
Swing Low/High indicator ts888 Indicator Development 10 07-12-2007 07:56 AM


All times are GMT -6. The time now is 11:08 PM.