View Full Version : Setting Swing to a variable?
Burga1
12-20-2007, 07:03 PM
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
12-20-2007, 10:41 PM
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
NinjaTrader_Josh
12-20-2007, 11:33 PM
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.
Print(Time[0] + " " + Swing(7).SwingHighBar(0, 1, CurrentBar) + " " + Swing(7).SwingHighBar(0, 2, CurrentBar));
Burga1
12-21-2007, 08:49 AM
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.
NinjaTrader_Josh
12-21-2007, 01:00 PM
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.
Burga1
12-21-2007, 01:33 PM
Thank you.
Burga1
12-21-2007, 09:58 PM
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??
NinjaTrader_Josh
12-21-2007, 11:25 PM
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.
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.
Burga1
12-22-2007, 01:40 PM
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)??
NinjaTrader_Josh
12-22-2007, 02:29 PM
You sure can. Check out MRO. http://www.ninjatrader-support.com/HelpGuideV6/MostRecentOccurenceMRO.html
Burga1
12-23-2007, 11:13 PM
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?
NinjaTrader_Josh
12-23-2007, 11:18 PM
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.
Burga1
12-24-2007, 12:03 AM
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...
NinjaTrader_Josh
12-24-2007, 02:36 AM
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_Dierk
12-24-2007, 03:25 AM
Burga1, you always should check the logs first in case your NinjaScript does not work as expected. They usually hold a pretty good indication on what's going wrong. As next steps you always should try debugging by the links we provided to you.
Due to bandwidth reasons we are unable to provide support down to the very last bits and bytes and each and every statement.
As last resort the certified NinjaScript consultants are ready to help you out: http://www.ninjatrader.com/webnew/partners_onlinetrading_NinjaScript.htm
Thanks for your understanding.