I'm trying to calculate the +DI and -DI values (period 28) using the TA-lib library. The values I am getting are not similar to the values on my charting platform. Please find my code below and let me know if I'm missing anything. Also, the ADX value returned from the TA-lib is also wrong. Thanks.
Below, you'll find the code to calculate the +DI using the TA-lib library. The value returned by the library is far from the value I see on my charting platform. The only inputs are highPrices, lowPrices and closePrices. These are arrays contains the high, low and closing prices. The values in these arrays match the values on my charting platform and yet the value returned is wrong.
Thanks for your time.
double[] output = new double[closePrices.Length];
int begin;
int length;
Core.RetCode retCode = Core.PlusDI(closePrices.Length - 1, closePrices.Length - 1, highPrices, lowPrices, closePrices, period, out begin, out length, output);
if (retCode == Core.RetCode.Success)
{
for (int i = 0; i < length; i++)
{
result = Math.Round(output[i], 5);
}
}
return result;
Related
So I've been trying to write a program in C# that returns all factors for a given number (I will implement user input later). The program looks as follows:
//Number to divide
long num = 600851475143;
//initializes list
List<long> list = new List<long>();
//Defines combined variable for later output
var combined = string.Join(", ", list);
for (int i = 1; i < num; i++)
{
if (num % i == 0)
{
list.Add(i);
Console.WriteLine(i);
}
}
However, after some time the program starts to also try to divide negative numbers, which after some time ends in a System.DivideByZeroException. It's not clear to me why it does this. It only starts to do this after the "num" variable contains a number with 11 digits or more. But since I need such a high number, a fix or similiar would be highly appreciated. I am still a beginner.
Thank you!
I strongly suspect the problem is integer overflow. num is a 64-bit integer, whereas i is a 32-bit integer. If num is more than int.MaxValue, then as you increment i it will end up overflowing back to negative values and then eventually 0... at which point num % i will throw.
The simplest option is just to change i to be a long instead:
for (long i = 1; i < num; i++)
It's unfortunate that there'd be no warning in your original code - i is promoted to long where it needs to be, because there's an implicit conversion from int to long. It's not obvious to me what would need to change for this to be spotted in the language itself. It would be simpler for a Roslyn analyzer to notice this sort of problem.
I'm trying to use the k-nearest-neighbor that Accord library implements.
First of all , I used
double[][] inputs = new double[15000][];
int[] outputs = new int[15000];
for (int list_counter= 0; list_counter < training_set.Count; list_counter ++ ) {
outputs[list_counter] = (char.Parse(training_set[list_counter].letter));
double[] input = new double[16];
for(int i =0; i< 16; i++) {
input[i] = (double)training_set[list_counter].integers[i];
}
inputs[list_counter] = input;
}
var knn = new KNearestNeighbors(k: 4);
knn.NumberOfInputs = 16;
Console.WriteLine("Learning the algorithm");
knn.Learn(inputs, outputs);
this piece of code to teach knn the algorithm , I have a 15000 set of integers,which I first convert to double and use as input. Then I have a 15000 set of 1 character strings which I first convert to char to get the integer value , and then classify them as outputs.Some screenshots of the inputs and the outputs.
I also set the number of inputs to 16 to avoid this kind of problem.
But on this piece of code
for (int list_counter = 0; list_counter < validation_set.Count; list_counter++) {
double[] input = new double[16];
for (int i = 0; i < 16; i++) {
input[i] = (double)validation_set[list_counter].integers[i];
}
int answer = knn.Decide(input);
Whenever I try to knn.decide , I get an IndexOutOfRangeException. Which seems strange because I used the exact same logic to insert inputs (an array[15000] of a double[16] array.
Here is a screenshot of the input[] before the program crashes
The decide method documentation didn't help me,but I'll leave the links :
knn decide documentation
knn documentation
EDIT :
So ,the answer to this particular problem is weird , and I couldn't find it in the documentation of the knn algorithm.
The problem was that outputs on knn.Learn part must start with 0 and be counting upwards . Converting capital character to int gave me a minimum of 65 ('A') , I changed the first of the code
outputs[list_counter] = (char.Parse(training_set[list_counter].letter)) -65 ;
and now everything runs like clockwork!
This is my idea to program a simple math module (function) that can be called from another main program. It calculates the FWHM(full width at half the max) of a curve. Since this is my first try at Visual Studio and C#. I would like to know few basic programming structures I should learn in C# coming from a Mathematica background.
Is double fwhm(double[] data, int c) indicate the input arguments
to this function fwhm should be a double data array and an Integer
value? Did I get this right?
I find it difficult to express complex mathematical equations (line 32/33) to express them in parenthesis and divide one by another, whats the right method to do that?
How can I perform Mathematical functions on elements of an Array like division and store the results in the same Array?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DEV_2
{
class fwhm
{
static double fwhm(double[] data, int c) // data as 2d data and c is integer
{
double[] datax;
double[] datay;
int L;
int Mag = 4;
double PP = 2.2;
int CI;
int k;
double Interp;
double Tlead;
double Ttrail;
double fwhm;
L = datay.Length;
// Create datax as index for the number of elemts in data from 1-Length(data).
for (int i = 1; i <= data.Length; i++)
{
datax[i] = (i + 1);
}
//Find max in datay and divide all elements by maxValue.
var m = datay.Length; // Find length of datay
Array.ForEach(datay, (x) => {datay[m++] = x / datay.Max();}); // Divide all elements of datay by max(datay)
double maxValue = datay.Max();
CI = datay.ToList().IndexOf(maxValue); // Push that index to CI
// Start to search lead
int k = 2;
while (Math.Sign(datay[k]) == Math.Sign(datay[k-1]-0.5))
{
k=k+1;
}
Interp = (0.5-datay[k-1])/(datay[k]-datay[k-1]);
Tlead = datax[k-1]+Interp*(datax[k]-datax[k-1]);
CI = CI+1;
// Start search for the trail
while (Math.Sign(datay[k]-0.5) == Math.Sign(datay[k-1]-0.5) && (k<=L-1))
{
k=k+1;
}
if (k != L)
{
Interp = (0.5-datay[k-1])/(datay[k]-datay[k-1]);
Ttrail = datax[k-1] + Interp*(datax[k]-datax[k-1]);
fwhm =((Ttrail-Tlead)*PP)/Mag;
}
}//end main
}//end class
}//end namespace
There are plenty of pitfalls in C#, but working through problems is a great way to find and learn them!
Yes, when passing parameters to a method the correct syntax is MethodName(varType varName) seperated by a comma for multiple parameters. Some pitfalls arise here with differences in passing Value types and Reference types. If you're interested here is some reading on the subject.
Edit: As pointed out in the comments you should write code as best as possible to require as few comments as possible (thus paragraph between #3 and #4), however if you need to do very specific and slightly complex math then you should comment to clarify what is occuring.
If you mean difficulties understanding, make sure you comment your code properly. If you mean difficulties writing it, you can create variables to simplify reading your code (but generally unnecessary) or look up functions or libraries to help you, this is a bit open ended question if you have a particular functionality you are looking for perhaps we could be of more help.
You can access your array via indexes such as array[i] will get the ith index. Following this you can manipulate the data that said index is pointing to in any way you wish, array[i] = (array[i]/24)^3 or array[i] = doMath(array[i])
A couple things you can do if you like to clean a little, but they are preference based, is not declare int CI; int k; in your code before you initialize them with int k = 2;, there is no need (although you can if it helps you). The other thing is to correctly name your variables, common practice is a more descriptive camelCase naming, so perhaps instead of int CI = datay.ToList().IndexOf(maxValue); you coud use int indexMaxValueYData = datay.ToList().IndexOf(maxValue);
As per your comment question "What would this method return?" The method will return a double, as declared above. returnType methodName(parameters) However you need to add that in your code, as of now I see no return line. Such as return doubleVar; where doubleVar is a variable of type double.
I'm trying to calculate
If we calculated every possible combination of numbers from 0 to (c-1)
with a length of x
what set would occur at point i
For example:
c = 4
x = 4
i = 3
Would yield:
[0000]
[0001]
[0002]
[0003] <- i
[0010]
....
[3333]
This is very nearly the same problem as in the related question Logic to select a specific set from Cartesian set. However, because x and i are large enough to require the use of BigInteger objects, the code has to be changed to return a List, and take an int, instead of a string array:
int PossibleNumbers;
public List<int> Get(BigInteger Address)
{
List<int> values = new List<int>();
BigInteger sizes = new BigInteger(1);
for (int j = 0; j < PixelArrayLength; j++)
{
BigInteger index = BigInteger.Divide(Address, sizes);
index = (index % PossibleNumbers);
values.Add((int)index);
sizes *= PossibleNumbers;
}
return values;
}
This seems to behave as I'd expect, however, when I start using values like this:
c = 66000
x = 950000
i = (66000^950000)/2
So here, I'm looking for the ith value in the cartesian set of 0 to (c-1) of length 950000, or put another way, the halfway point.
At this point, I just get a list of zeroes returned. How can I solve this problem?
Notes: It's quite a specific problem, and I apologise for the wall-of-text, I do hope it's not too much, I was just hoping to properly explain what I meant. Thanks to you all!
Edit: Here are some more examples: http://pastebin.com/zmSDQEGC
Here is a generic base converter... it takes a decimal for the base10 value to convert into your newBase and returns an array of int's. If you need a BigInteger this method works perfectly well with just changing the base10Value to BigInteger.
EDIT: Converted method to BigInteger since that's what you need.
EDIT 2: Thanks phoog for pointing out BigInteger is base2 so changing the method signature.
public static int[] ConvertToBase(BigInteger value, int newBase, int length)
{
var result = new Stack<int>();
while (value > 0)
{
result.Push((int)(value % newBase));
if (value < newBase)
value = 0;
else
value = value / newBase;
}
for (var i = result.Count; i < length; i++)
result.Push(0);
return result.ToArray();
}
usage...
int[] a = ConvertToBase(13, 4, 4) = [0,0,3,1]
int[] b = ConvertToBase(0, 4, 4) = [0,0,3,1]
int[] c = ConvertToBase(1234, 12, 4) = [0,8,6,10]
However the probelm you specifically state is a bit large to test it on. :)
Just calculating 66000 ^ 950000 / 2 is a good bit of work as Phoog mentioned. Unless of course you meant ^ to be the XOR operator. In which case it's quite fast.
EDIT: From the comments... The largest base10 number that can be represented given a particular newBase and length is...
var largestBase10 = BigInteger.Pow(newBase, length)-1;
The first expression of the problem boils down to "write 3 as a 4-digit base-4 number". So, if the problem is "write i as an x-digit base-c number", or, in this case, "write (66000^950000)/2 as a 950000-digit base 66000 number", then does that make it easier?
If you're specifically looking for the halfway point of the cartesian product, it's not so hard. If you assume that c is even, then the most significant digit is c / 2, and the rest of the digits are zero. If your return value is all zeros, then you may have an off-by-one error, or the like, since actually only one digit is incorrect.
I am unable to assign dynamic value to an array which is double. If i keep it static, I am able to do it. Help me out.
I have defined a array as double whose size could be dynamic. Then I calculated some value and want to assign that value to this double array.
I did..
double[] entropy_db_col;
But I am getting an error. array out of index.
If i calculate, (double)entropy_db - (double)a, I am getting a static value.
Even if I do entropy_db_col[0] = (double)entropy_db - (double)a;, then also I am getting the error? What should I do ?
Then I want to find the largest value from this array? Is this code right ?
largest = Convert.ToInt32(entropy_db_col[0].ToString());
Please help me out.
It would be great.
I solved this, but now the problem I get is :
I get entropy_db_col[0] value, then..when I iterate and get entropy_db_col[1] value, the value of entropy_db_col[0] becomes 0. Similarly when I get entropy_db_col[2], the value of entropy_db_col[1] and entropy_db_col[0] becomes 0..what should i do ?
create memory with data.count
before it used
entropy_db_col=new double[data.count]
Dynamic array
A double[] is not a dynamic array so much as an array of undefined size. It must first be defined with a concrete size, such as new double[data.Count] before it can be used, but this causes its size to be fixed.
If you want an array that can change size at any time, use a List<double> instead from the System.Collections.Generic namespace.
using System.Collections.Generic;
...
List<double> entropy_db_col = new List<double>();
for (int i = 0; i < data.Count; i++)
{
\\some calculation
entropy_db_col.Add((double)entropy_db - (double)a);
}
Largest value
To find the largest value in .Net 3.5 or later, you can use:
using System.Linq;
...
double largest = entropy_db_col.Max();
Or if you're using an version of .Net older than 3.5, you can use:
double largest = entropy_db_col[0];
for (int j = 1; j < entropy_db_col.Length; j++)
{
if (largest < entropy_db_col[j])
{
largest = entropy_db_col[j];
}
}