![]() |
|
|||||||
| Automated Trading Support for automated trading systems using NinjaScript. Support for our ATI (Automated Trading Interface) used to link an external application such as TradeStation and eSignal to NinjaTrader. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Member
Join Date: Mar 2008
Posts: 89
Thanks: 0
Thanked 0 times in 0 posts
|
I am using this code below and have avariable set called newTp and another called newBt which I am setting manully when I open the strategy to be the high and low of the last 5 minute bar, and boxsize is set at 1 pip, However when I print out the results if the High is greater than newTp it should add 1 pip to the value of newTp but in fact it is adding 1 pip to the highest high of the whole chart. This is actually what I was looking for earlier but I cannot see why it is doing it? Nor can I get it to do what I actually want in this instance and just add 1 pip to the new high.
protectedoverridevoid OnBarUpdate() { if (High[0]>=(newTp+boxSize)) { newTp = High[0]; } if (Low[0]<=(newBt-boxSize)) { newBt = Low[0]; } These are my print statements which shows the results, and NewTop and NewBottom do calculate the correct figures but newTp and newBt do not, I am very puzzeled ?? I have also attached the actual startegy if you want to run it to see what I mean. Help!! Print((newTp+boxSize)); Print((newBt-boxSize)); Print("High" + High[0]); Print("Low" + Low[0]); Print("Boxsize" + boxSize); Print("Box3" + (boxSize*3)); Print("NewTop" + (High[0] + 0.0001)); Print("NewBottom" + (Low[0] -0.0001)); |
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Aug 2007
Location: Breda, Netherlands
Posts: 11,215
Thanks: 82
Thanked 332 times in 324 posts
|
Hello,
I believe the logic in your if statement is not correct. I suggest to use the following: if (High[0] >= newTp) { newTp = newTp + boxsize } Only when the High is higher than newTp, the boxsize is added to the newTp.
Jason
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Member
Join Date: Mar 2008
Posts: 89
Thanks: 0
Thanked 0 times in 0 posts
|
Thanks Jason, but it's not the logic itself but the figures I'm getting, I've used your code in case but still get the same from my print staements viz:-
1.9971 being the value of newTp 1.8966 being the value of lowBt these are the alltime high showing on the chart and not just the current bar high with 1 boxsize added to it. That should be 1.8985 and 1.8962 respectively which it can calculate correctly from this code:- Print("NewTop" + (High[0] + 0.0001)); Print("NewBottom" + (Low[0] -0.0001)); What I cannot understand is where it is getting the alltime high from and why the logic will not work as planned ?? Any thoughts |
|
|
|
|
|
#4 |
|
Member
Join Date: Mar 2008
Posts: 89
Thanks: 0
Thanked 0 times in 0 posts
|
One of us is missing the point, I can't see why it is finding the alltime high and the alltime low, I'm not asking it to as far as I can see, it is correctly adding 1 pip to that level OK.
|
|
|
|
|
|
#5 |
|
NinjaTrader Customer Service
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
|
Hello,
You will want to reset the newTp and the newBt variables once you have reached the point at which you want to start over calculating the values. Right now newTp and newBt continue to carry the highest value that has been stored thus far. At some point you have to reset these values. Do you want to store the high + boxsize for each bar on the chart? If so then I suggest the following approach: using a custom DataSeries to carry your values and associate them with each bar and reference back in the DataSeries to do what you want. This link will help you with building a custom DataSeries: http://www.ninjatrader-support.com/H...verview24.html
Ben
NinjaTrader Customer Service |
|
|
|
|
|
#6 |
|
Member
Join Date: Mar 2008
Posts: 89
Thanks: 0
Thanked 0 times in 0 posts
|
Thanks Ben,
I was not thinking that it would calculate the newBt value right through the chart, I'll have a look at your example. |
|
|
|
|
|
#7 |
|
Member
Join Date: Mar 2008
Posts: 89
Thanks: 0
Thanked 0 times in 0 posts
|
I am using this code to find the highest bar and the lowest bar in the last 20 bars which works fine and gives the correct result:-
highBar = (HighestBar(Close, 20)); lowBar = (LowestBar(Close, 20)); lastTop = MAX(High, highBar)[0]; lastBottom = MIN(Low, lowBar)[0]; However I only want this to run when I first start the program so I have a variable called first_run set by default to true. WHen I run this code:- if (first_run) { highBar = (HighestBar(Close, 20)); lowBar = (LowestBar(Close, 20)); lastTop = MAX(High, highBar)[0]; lastBottom = MIN(Low, lowBar)[0]; first_run = false; } I get a totally different answer and the high and low are somewhere way back in history and not within the last 20 bars. can you tell me where I am going wrong please? Many thanks. |
|
|
|
|
|
#8 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
What do you mean run on the 1st start of the program?
For example, if you have a chart with 100 bars on it, OnBarUpdate() will called 100 times when "Calculate on bar close" is set to true. On what bar would you like to run that code?
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#9 |
|
Member
Join Date: Mar 2008
Posts: 89
Thanks: 0
Thanked 0 times in 0 posts
|
When I first start the strategy I want to find the Highest high in the last 20 bars only, that's why I thought it would do so if using a variable so it only called that routime once?
|
|
|
|
|
|
#10 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
You can only determine that if you are on the close of the 20th bar on the chart...
So what you could do is: if (CurrentBar == 20) { highBar = (HighestBar(Close, 20)); lowBar = (LowestBar(Close, 20)); lastTop = MAX(High, highBar)[0]; lastBottom = MIN(Low, lowBar)[0]; }
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#11 |
|
Member
Join Date: Mar 2008
Posts: 89
Thanks: 0
Thanked 0 times in 0 posts
|
Sorry Ray I'm not explaining myself well. I have about 5000 bars on the chart it's not the highest high within the first 20 bars which I wnat but the highest high within the the most current 20 bars, ignoring the previous 4880 or so bars?
|
|
|
|
|
|
#12 |
|
NinjaTrader Customer Service
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
|
Hello,
Try using: In your variables make sure you are starting with zero: int highBar = 0; etc... In OnBarUpdate(): if(!Historical && highBar == 0) { //returns the index of the bar with the highest close over the //last 21 bars, note current bar does not have a close so use 21 as //the lookback to get 20 bars highBar = (HighestBar(Close, 21)); lowBar = (LowestBar(Close, 21)); //returns the max high price over the last twenty bars //excluding the currently forming bar lastTop = MAX(High, 20)[1]; lastBottom = MIN(Low, 20)[1]; } The above assumes you want to exclude using the current bar. Here is a link on Historical: http://www.ninjatrader-support.com/H...istorical.html
Ben
NinjaTrader Customer Service
Last edited by NinjaTrader_Ben; 08-14-2008 at 05:46 PM.
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| High - Low between 0AM - 8AM | stefy | Strategy Development | 11 | 07-02-2008 10:00 AM |
| EMA of High - Low | glenng | Indicator Development | 2 | 02-02-2008 07:45 PM |
| 52 week high/low... | funk101 | Indicator Development | 1 | 01-16-2008 11:02 PM |
| CrossAbove(High, High, 1) | Lost Trader | Indicator Development | 4 | 01-11-2008 09:27 AM |
| High greater than Highest High of last 20 Bars | SamIam | Strategy Development | 1 | 08-19-2007 04:58 PM |