NinjaTrader Support Forum Bars since a condition

 Indicator Development Support for the development of custom indicators using NinjaScript.

 02-07-2010, 02:11 PM #16 Paul79 Junior Member   Join Date: Feb 2010 Posts: 20 Thanks: 0 Thanked 0 times in 0 posts Thanks Ralph but what i mean by highestsince is different (this is from amibroker AFL and this is what i want): AFL Function Reference - HIGHESTSINCE HIGHESTSINCE - highest value since condition met SYNTAX highestsince( EXPRESSION, ARRAY, Nth = 1 ) RETURNS ARRAY FUNCTION Returns the highest ARRAY value since EXPRESSION was true on theNth most recent occurrence. EXAMPLE highestsince( Cross( macd(), 0 ), Close, 1 ) returns the highest close price since macd() has crossed above zero. Also there is valuewhen AFL Function Reference - VALUEWHEN VALUEWHEN - get value of the array when condition met SYNTAX valuewhen(EXPRESSION, ARRAY, n = 1) RETURNS ARRAY FUNCTION Returns the value of the ARRAY when the EXPRESSION was true on the n -th most recent occurrence. Note: this function allows also 0 and negative values for n - this enables referencing future EXAMPLE valuewhen( cross( close, ma(close,5) ) ,macd(), 1) so basically the above ready made function I search. In the end i will probably manage to solve them and if i define them within "UserdefinedMethods" I should be able to use them in any indicator. what I do not understand is why they are not already defined, should be extremely easy for a programmer but for me its pretty difficult to learn c++ and I dont need C++ to trade. That is why I said that Ninja programmers should make this easier for traders by defining such functions because I personally am really not interested in learning computer languages. PS: Ralph if you can solve them fast and put them here I wont mind
 02-07-2010, 02:55 PM #17 Ralph Senior Member   Join Date: Jul 2008 Posts: 531 Thanks: 0 Thanked 14 times in 11 posts Something like this? Note that BarsSince starts with 1 for this application. int BarsSince; int hBar; protected override void OnBarUpdate() { BarsSince = CrossAbove(CCI(14), 100, 1) ? 1 : BarsSince++; hBar = HigestBar(, BarsSince); } Regards Ralph
 02-07-2010, 03:11 PM #18 Paul79 Junior Member   Join Date: Feb 2010 Posts: 20 Thanks: 0 Thanked 0 times in 0 posts Not quite, Highestbar method (function returns number of bars ago highest price value occured for lookback period and I need the highest high for the data series since the event took place. Besides that highestsince should be able to return more than last highest high. Its hard to explain but this says all: highestsince( EXPRESSION, ARRAY, Nth = 1 ) Returns the highest ARRAY value since EXPRESSION was true on theNth most recent occurrence. Like for example finding the highest high of the close price since second last time when CCI(14) crosses 100 would be highestsince(CrossAbove(CCI(14)[0],100,1),Close,2) ...just as a example if it would be defined, so basically thats what I want to create and then define with "UserDefinedMethods" so I can replace CCI and Close with any other indicators or prices. Thanks anyway Ralph.
 02-07-2010, 05:35 PM #19 Ralph Senior Member   Join Date: Jul 2008 Posts: 531 Thanks: 0 Thanked 14 times in 11 posts The List hBars should contain the bar numbers of the highest bar after each crossover event (not tested). Regards Ralph List hBars = new List(); int lastIndex = -1; double lastHigh; protected override void OnBarUpdate() { if (CurrentBar < 2) return; if (FirstTickOfBar) { if (CCI(14)[1] > 100 && CCI(14)[2] < 100) { hBars.Add(CurrentBar - 1); lastHigh = High[1]; lastIndex++; } else if (lastIndex >= 0) { if (High[1] > lastHigh) { lastHigh = High[1]; hBars[lastIndex] = CurrentBar - 1; } } }
 02-08-2010, 05:45 AM #20 Paul79 Junior Member   Join Date: Feb 2010 Posts: 20 Thanks: 0 Thanked 0 times in 0 posts Thanks Ralph but its not what I want. Thanks anyway. However here`s a new problem: i have defined barssince function (method) within "UserDefinedMethods" so I can use it with any custom indicator but the problem is that it only works once. I mean if i try to use it 2 times in the same custom indicator then it messes up everything and does not give the right result. here is the code used in "UserDefinedMethods" partial class Indicator { int trigger; public int barssince(bool array) { if (array==true) { trigger=CurrentBar; } if(CurrentBar>trigger) { return(CurrentBar-trigger); } else { return(0); } } } the above code defines a function barssince(event) that will count the number of bars since event was true. It works like this barssince(CrossAbove(CCI(14),100,1)) this will count the number of bars after each cross of CCI14 above 100 then will reset to zero and count again at the next cross. It works fine alone but if I try to add another barssince like this barssince(CrossBelow(CCI(14),-100,1)) in the same custom indicators then it does not give the right result. see the code bellow; double a; double b; protected override void OnBarUpdate() { if(CurrentBar>0) { a=barssince(CrossAbove(CCI(14),100,1)); } if(CurrentBar>0) { b=barssince(CrossBelow(CCI(14),-100,1)); } Plot0.Set(a); Plot1.Set(b); } Maybe someone knows why and how to solve it? Maybe there is something I did not do right in "UserDefinedMethods" and because of that it messes up everything when used twice in the same custom indicator? ...help???
 02-08-2010, 05:56 AM #21 Paul79 Junior Member   Join Date: Feb 2010 Posts: 20 Thanks: 0 Thanked 0 times in 0 posts It seems, I post i answer, however sometimes the simpliest solutions are the hardest to see. I just added another barssince named barssince1 in to the userdefined methods like this partial class Indicator { int trigger; public int barssince(bool array) { if (array==true) { trigger=CurrentBar; } if(CurrentBar>trigger) { return(CurrentBar-trigger); } else { return(0); } } int trigger1; public int barssince1(bool array1) { if (array1==true) { trigger1=CurrentBar; } if(CurrentBar>trigger1) { return(CurrentBar-trigger1); } else { return(0); } } }
 02-08-2010, 06:01 AM #22 Ralph Senior Member   Join Date: Jul 2008 Posts: 531 Thanks: 0 Thanked 14 times in 11 posts Exactly Paul, you need one for CrossAbove and one for CrossBelow. A better approach would be to implement barssince() as a class. Then you could instantiate class instances as often as you desire and use them independently without the need to duplicate code. Regards Ralph
 02-08-2010, 06:09 AM #23 Paul79 Junior Member   Join Date: Feb 2010 Posts: 20 Thanks: 0 Thanked 0 times in 0 posts what is that Ralph? I mean..what is a class? and how to implement it?. If you mean to have a single barssince(event) method that works anywhere like a new function then that will be great. If you know how to do it then maybe we could add a few new functions to Ninja to make it a little easier. i have no ideea what a class means but I supose that is what you meant.
 02-08-2010, 07:28 AM #24 Ralph Senior Member   Join Date: Jul 2008 Posts: 531 Thanks: 0 Thanked 14 times in 11 posts An indicator is implemented as a class for instance. First you design a class. Second you instantiate an instance of your class in your application and then you can access the class' public methods and properties. However, since this concept is unknown to you lets consider this task from another side: Why even implement it as a function? I mean the functionality required is as simple as this: int BarsSinceCrossAbove; protected override void OnBarUpdate() { BarsSinceCrossAbove= CrossAbove(CCI(14), 100, 1) ? 1 : BarsSinceCrossAbove++; Plot0.Set(BarsSinceCrossAbove); } Regards Ralph
 02-08-2010, 07:42 AM #25 Paul79 Junior Member   Join Date: Feb 2010 Posts: 20 Thanks: 0 Thanked 0 times in 0 posts Ralph, the below code does not work, however I solved barssince and it would be truly nice to implement it as a class from what I understand. I am not used with all the "nuts and bolts" C++ requires and honestly dont want to know them but I know very well to code in languages like Metstock`s or easylanguage or amibroker`s. Implementing functions like barssince(event) as a class so they could be used as easy as a SMA for example would be great and I think would not be such a big deal for someone that is a programmer. I dont understand why ninja people want to make programmers out of traders. Now I have to solve that highestsince function (method) and again I am stuck...by the time I will finish I could start to write a new software myself and I trade not develop software...LOL. Thanks for your help.
 02-08-2010, 07:56 AM #26 NinjaTrader_Bertrand NinjaTrader Customer Service     Join Date: Sep 2008 Location: Germany Posts: 26,232 Thanks: 492 Thanked 1,856 times in 1,790 posts Paul79, jumping in here...did you check into the MRO / LRO methods supplied per default for those tasks? BertrandNinjaTrader Customer ServiceUse Kinetick, NinjaTrader’s preferred market data service - Learn MoreFree online training events - View Schedule
 02-08-2010, 08:09 AM #27 Paul79 Junior Member   Join Date: Feb 2010 Posts: 20 Thanks: 0 Thanked 0 times in 0 posts Bertrand I have no clue what MRO / LRO or whatever means, and about jumping in here, I expected to solve some simple problems in less than 1 hour as i did with whatever other software for traders BUT Ninja that took me 2 days to solve barssince(event)... "nuts and bolts" as I was saying. Now if you are kind enough to explain MRO /LRO and enlighten me that would be great else thanks for mention I will search them myself.
 02-08-2010, 08:11 AM #28 Paul79 Junior Member   Join Date: Feb 2010 Posts: 20 Thanks: 0 Thanked 0 times in 0 posts Oh..Bertrand you sure you know how to talk with customers? 995\$ paid for the software its not a small amount and I hate when people are rude after I paid money.
 02-08-2010, 08:19 AM #29 Paul79 Junior Member   Join Date: Feb 2010 Posts: 20 Thanks: 0 Thanked 0 times in 0 posts For that matter your MRO /LRO Bertrand has nothing to do with solving methods like valuewhen, highestsince or others and pretty much makes no difference, barssince I solve it already...after 2 days.
02-08-2010, 11:18 AM   #30
Ralph
Senior Member

Join Date: Jul 2008
Posts: 531
Thanks: 0
Thanked 14 times in 11 posts

Hi Paul, here is an implementation of the trigger bar counter as a separate class (HelperClass). In HelperClassTest you can see how this class is applied 2 times (up- and down-counter). Just use File->Utilities->NinjaScriptImport to install.

The little picture depicts the HelperClassTest in action. The green line counts the bars after the down trigger events, the orange line accordingly vice versa.

However, with this example you can see which way to go if using this approach. You should try to understand this concept before starting own developments, otherwise you won't get happy with that.

Regards
Ralph
Attached Images
 HelperClassTest.jpg (42.5 KB, 63 views)
Attached Files
 HelperClassTest.zip (5.8 KB, 33 views)

 Thread Tools Display Modes Linear Mode

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home News and Announcements     News and Announcements NinjaTrader 8     Platform Technical Support     Add-On Development     Indicator Development     Strategy Development NinjaTrader 7     Platform Technical Support     General Development     Indicator Development     Strategy Development NinjaScript File Sharing     NinjaScript File Sharing Discussion     Version 8 Add-Ons     Version 8 Indicators     Version 8 Strategies     Version 7 Indicators     Version 7 Strategies     Version 7 Miscellaneous NinjaScript Educational Resources     Reference Samples     Tips Suggestions and Feedback     Suggestions And Feedback Historical Beta Archive     Version 8 Beta

 Similar Threads Thread Thread Starter Forum Replies Last Post ju1234 Strategy Development 11 06-01-2010 01:07 PM coolraz Strategy Development 3 12-15-2008 10:59 AM ericadam General Development 7 09-05-2008 09:07 AM Akros Indicator Development 7 06-10-2007 04:55 AM

All times are GMT -6. The time now is 12:22 AM.

Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.

CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content.

 -- Fixed Width ---- Fluid Width NinjaTrader Support Forum - Archive - Top