Click here to Login





                                                   Indexing: First !NaN value w/o looping?

  0

0
Alexander Horn
2014-01-25 22:18:03


Suppose I have c# code like below to set index base == 1 to a vector.

Is there a smarter way to find first non NaN value "firstbar" in vector to avoid looping?

string asset = "SPY"; // assets[j];
VectorD closes = cFunctions.GetTimeframeData(asset, (int)cFunctions.Timeframe, "close").ToVectorD();

//determine first !NaN value as divisor
int firstbar = 0;
for(int bars=1;bars<closes.Length;bars++){
if(!double.IsNaN(closes[bars])){
firstbar = Math.Max(bars,curbar); // curbar is first visible bar defined separaetly
bars = closes.Length;}}
// determine indexed vectors
VectorD vect1 = (closes / TA.Value(closes,firstbar));



Zoidberg
2014-01-26 03:25:26

  0

Hi Alexander,
I am not sure how VectorD is defined, but if it is enumerable maybe you could use something like in this page: http://msdn.microsoft.com/en-us/library/bb549039(v=vs.110).aspx

Btw Do you intend to start at bars == 1, not 0 (maybe yes, that may be what you say in the first line with "base == 1").



Alexander Horn
2014-01-26 09:48:25

  0

Thanks Zoid, the bars ==1 is just nostalgia from my VB times with arrays base 1. This little inaccuracy will not kill my system, ahhm hopefully. Thanks for the hint! ;-)

Nop, Enumerable.FirstOrDefault does not work, only supported in: .NET Framework4.5, 4, 3. I googled a bit, but gurus also propose to loop.. Maybe QS has a way with a fancy TA.function, but in other indicators he also looped..

If not I close this question, neither does a bit of more looping kill my system..



Zoidberg
2014-01-26 19:44:08

  0

Probably OK to loop a bit, after all the Code is presumably compiled to MSIL.
After all, as Knuth/Hoare said: "premature optimization is the root of all evil" (not that this would necessarily be premature) :-)
Ref [http://en.wikipedia.org/wiki/Program_optimization]



QuantShare
2014-01-27 13:08:07

  0

Best Answer
Looping is the only way to find the first NaN value



No more messages
0




Reply:

No html code. URLs turn into links automatically.

Type in the trading objects you want to include: - Add Objects
To add a trading object in your message, type in the object name, select it and then click on "Add Objects"










QuantShare

Trading Items
Measure the value of the stock market using the Q-Ratio valuation...
PREV - Previous value of the same formula
Set the First Trading Bar Values to Not a Number
Horizontal Line - Changes by Value and Percentage
Indicator/Price Value on a Specific Trading Date

How-to Lessons
How to quickly select stocks based on the last value of a databas...
How to display the number of stocks per day for different RSI gro...
How to change volume bar colors when the price closes down
How to create a custom trendline
How to optimize the stop limit of a trading system

Related Forum Threads
Examining value used
Default Value in Optimization
Compare an indicators current value to past values
Function/property to catch the first visible bar/date within a ch...
Watchlist and adding a Value

Blog Posts
How to Create Your First Meta-Strategy
Let me Show You How to Create Hundreds of Profitable Trading Syst...
Troubleshooting a Trading System
Learn How to Create Your Own Technical Analysis Indicators
Fundamental analysis: How to track economic indicators in the For...









QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright © 2024 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
Follow us on Google+
RSS Trading Items



Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.