Ta-Lib ATR function always returns 0

时间:2016-02-12 20:31:46

标签: c# ta-lib

I am using the Ta-Lib in C# and for the most part it works great. I am using it to calculate a number of moving averages and they calculate perfectly.

When Calling the ATR function it returns 0, always. I use the same parameters (the ones that overlap) as I do for the SMA, but still 0. Here is the code in question. Member variable Period is an integer. I have more data in the queues than the period I am asking it to calculate, and my data in the queues is clean, I've triple checked that. The return code is Success, but 0 return value.

import requests
import csv
import os

temp_file_name = 'temp_csv.csv'
url = 'http://url.to/file.csv'
download = requests.get(url)

with open(temp_file_name, 'w') as temp_file:
    temp_file.writelines(download.content)

with open(temp_file_name, 'rU') as temp_file:
    csv_reader = csv.reader(temp_file, dialect=csv.excel_tab)
    for line in csv_reader:
        print line

# delete the temp file after process
os.remove(temp_file_name)

1 个答案:

答案 0 :(得分:1)

好的,我想了很多,终于经过了很多很多小时。在我的循环中,我检查以确保数组具有与Period相同数量的元素,如果是,我执行计算。实际上,数组需要Period + 1个元素来执行计算。这是我的整个功能。

    public override void Calculate()
    {
        int outbegldx = 0;
        int outnbelement = 0;
        int marketID = 0;
        TicTacTec.TA.Library.Core.RetCode retCode;
        //Queue<OHLC> input = new Queue<OHLC>();
        Queue<double> inputHigh = new Queue<double>();
        Queue<double> inputLow = new Queue<double>();
        Queue<double> inputClose = new Queue<double>();


        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.DB_CONNECTION_STRING))
        {
            using (SqlCommand cmd = new SqlCommand("[Data].[proc_GetHistoricalData]", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@StartIndex", SqlDbType.Int);
                cmd.Parameters.Add("@EndIndex", SqlDbType.Int);
                cmd.Parameters.Add("@Weekly", SqlDbType.Bit);
                cmd.Parameters["@StartIndex"].Value = this.startIndex;
                cmd.Parameters["@EndIndex"].Value = this.endIndex;
                cmd.Parameters["@Weekly"].Value = (int)this.TimeFrame;

                conn.Open();

                using (SqlDataReader rdr = cmd.ExecuteReader())
                {


                    while (rdr.Read())
                    {
                        Console.WriteLine(rdr.GetInt64(0));

                        if (marketID != rdr.GetInt32(2))
                        {
                            marketID = rdr.GetInt32(2);
                            inputHigh.Clear();
                            inputLow.Clear();
                            inputClose.Clear();
                        }

                        //input.Enqueue(new OHLC(rdr.GetDecimal(4), rdr.GetDecimal(5), rdr.GetDecimal(6), rdr.GetDecimal(7)));
                        inputHigh.Enqueue(Convert.ToDouble(rdr.GetDecimal(5)));
                        inputLow.Enqueue(Convert.ToDouble(rdr.GetDecimal(6)));
                        inputClose.Enqueue(Convert.ToDouble(rdr.GetDecimal(7)));

                        //We have enough data to calculate the ATR so do so
                        if (inputHigh.Count >= this.Period + 1)
                        {
                            double[] outreal = new double[inputHigh.Count - 1];
                            int lookBack = Core.AtrLookback(this.Period);

                            retCode = Core.Atr(0, inputHigh.Count - 1, inputHigh.ToArray(), inputLow.ToArray(), inputClose.ToArray(), lookBack, out outbegldx, out outnbelement, outreal);

                            //Calculated the ATR, now write it to the database
                            if (retCode == Core.RetCode.Success)
                            {
                                this.Output = new AverageTrueRangeOutput(this.Period, outreal[0], outbegldx, outnbelement);

                                //Now insert into db
                                using (SqlCommand cmdInsert = new SqlCommand("[Data].[proc_InsertHistoricalIndicator]", conn))
                                {
                                    cmdInsert.CommandType = CommandType.StoredProcedure;
                                    cmdInsert.Parameters.Add("@MarketID", SqlDbType.Int);
                                    cmdInsert.Parameters.Add("@IndicatorID", SqlDbType.Int);
                                    cmdInsert.Parameters.Add("@Date", SqlDbType.Date);
                                    cmdInsert.Parameters.Add("@Value", SqlDbType.Xml);
                                    cmdInsert.Parameters.Add("@Weekly", SqlDbType.Bit);
                                    cmdInsert.Parameters["@MarketID"].Value = rdr.GetInt32(2);
                                    cmdInsert.Parameters["@IndicatorID"].Value = this.Id;
                                    cmdInsert.Parameters["@Weekly"].Value = (int)this.TimeFrame;
                                    cmdInsert.Parameters["@Date"].Value = rdr.GetDateTime(3);  //IBLib.Util.GetDate(
                                    cmdInsert.Parameters["@Value"].Value = this.Output.toXML().OuterXml;

                                    cmdInsert.ExecuteNonQuery();
                                }
                            }
                            inputHigh.Dequeue();
                            inputLow.Dequeue();
                            inputClose.Dequeue();
                        }

                    }
                    rdr.Close();
                }
            }
        }
    }

有问题的一行是这个 - &#34; if(inputHigh.Count&gt; = this.Period + 1)&#34;而以前我只是&#34; if(inputHigh.Count&gt; = this.Period)&#34;