Ejemplo n.º 1
0
// free function
static void ta_func_free(void *phv)
{
  int i = 0; // this now properly cleans up memory from very last call
  ParamHolder* ph = (ParamHolder*)phv;
  for (i = 0; i < IN_CNT; i++) if (ph->in[i]) free(ph->in[i]);
  for (i = 0; i < OUT_CNT; i++) if (ph->out[i]) free(ph->out[i]);
  TA_ParamHolderFree( ph->p );
  free(ph);
}
Ejemplo n.º 2
0
TA_RetCode TA_ParamHolderAlloc( const TA_FuncHandle *handle,
                                TA_ParamHolder **allocatedParams )
{
   
   TA_FuncDef *funcDef;
   unsigned int allocSize, i;
   TA_ParamHolderInput    *input;
   TA_ParamHolderOptInput *optInput;
   TA_ParamHolderOutput   *output;

   const TA_FuncInfo *funcInfo;
   TA_ParamHolder *newParams;
   TA_ParamHolderPriv *newParamsPriv;

   const TA_InputParameterInfo    **inputInfo;
   const TA_OptInputParameterInfo **optInputInfo;
   const TA_OutputParameterInfo   **outputInfo;

   /* Validate the parameters. */
   if( !handle || !allocatedParams)
   {
      return TA_BAD_PARAM;
   }

   /* Validate that this is a valid funcHandle. */
   funcDef = (TA_FuncDef *)handle;
   if( funcDef->magicNumber != TA_FUNC_DEF_MAGIC_NB )
   {
      *allocatedParams = NULL;
      return TA_INVALID_HANDLE;
   }

   /* Get the TA_FuncInfo. */
   funcInfo = funcDef->funcInfo;
   if( !funcInfo ) return TA_INVALID_HANDLE;

   /* Allocate the TA_ParamHolder. */
   newParams = (TA_ParamHolder *)TA_Malloc( sizeof(TA_ParamHolder) + sizeof(TA_ParamHolderPriv));
   if( !newParams )
   {
      *allocatedParams = NULL;
      return TA_ALLOC_ERR;
   }

   memset( newParams, 0, sizeof(TA_ParamHolder) + sizeof(TA_ParamHolderPriv) );
   newParamsPriv = (TA_ParamHolderPriv *)(((char *)newParams)+sizeof(TA_ParamHolder));
   newParamsPriv->magicNumber = TA_PARAM_HOLDER_PRIV_MAGIC_NB;
   newParams->hiddenData = newParamsPriv;

   /* From this point, TA_ParamHolderFree can be safely called. */

   /* Allocate the array of structure holding the info
    * for each parameter.
    */
   if( funcInfo->nbInput == 0 ) return TA_INTERNAL_ERROR(2);

   allocSize = (funcInfo->nbInput) * sizeof(TA_ParamHolderInput);
   input = (TA_ParamHolderInput *)TA_Malloc( allocSize );

   if( !input )
   {
      TA_ParamHolderFree( newParams );
      *allocatedParams = NULL;
      return TA_ALLOC_ERR;
   }
   memset( input, 0, allocSize );
   newParamsPriv->in = input;

   if( funcInfo->nbOptInput == 0 )
      optInput = NULL;
   else
   {
      allocSize = (funcInfo->nbOptInput) * sizeof(TA_ParamHolderOptInput);
      optInput = (TA_ParamHolderOptInput *)TA_Malloc( allocSize );

      if( !optInput )
      {
         TA_ParamHolderFree( newParams );
         *allocatedParams = NULL;
         return TA_ALLOC_ERR;
      }
      memset( optInput, 0, allocSize );
   }
   newParamsPriv->optIn = optInput;

   allocSize = (funcInfo->nbOutput) * sizeof(TA_ParamHolderOutput);
   output = (TA_ParamHolderOutput *)TA_Malloc( allocSize );
   if( !output )
   {
      TA_ParamHolderFree( newParams );
      *allocatedParams = NULL;
      return TA_ALLOC_ERR;
   }
   memset( output, 0, allocSize );
   newParamsPriv->out = output;

   newParamsPriv->funcInfo = funcInfo;

   inputInfo    = (const TA_InputParameterInfo **)funcDef->input;
   optInputInfo = (const TA_OptInputParameterInfo **)funcDef->optInput;
   outputInfo   = (const TA_OutputParameterInfo   **)funcDef->output;

   for( i=0; i < funcInfo->nbInput; i++ )
   {
      input[i].inputInfo = inputInfo[i];
      newParamsPriv->inBitmap <<= 1;
      newParamsPriv->inBitmap |= 1;
   }

   for( i=0; i < funcInfo->nbOptInput; i++ )
   {
      optInput[i].optInputInfo = optInputInfo[i];
      if( optInput[i].optInputInfo->type == TA_OptInput_RealRange )
         optInput[i].data.optInReal = optInputInfo[i]->defaultValue;
      else
         optInput[i].data.optInInteger = (TA_Integer)optInputInfo[i]->defaultValue;
   }

   for( i=0; i < funcInfo->nbOutput; i++ )
   {
      output[i].outputInfo = outputInfo[i];
      newParamsPriv->outBitmap <<= 1;
      newParamsPriv->outBitmap |= 1;
   }

   /* Succcess, return the result to the caller. */
   *allocatedParams = newParams;

   return TA_SUCCESS;
}
Ejemplo n.º 3
0
static ErrorNumber do_test( const TA_History *history,
                            const TA_Test *test )
{
   TA_RangeTestParam testParam;
   ErrorNumber errNb;
   TA_RetCode retCode;

   (void)test;

   /* Set to NAN all the elements of the gBuffers.  */
   clearAllBuffers();

   /* Build the input. */
   setInputBuffer( 0, history->open,  history->nbBars );
   setInputBuffer( 1, history->high,  history->nbBars );
   setInputBuffer( 2, history->low,   history->nbBars );
   setInputBuffer( 3, history->close, history->nbBars );
      
   
#if 0
   /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
   /* Test for specific value not yet implemented */

   /* Make a simple first call. */
   switch( test->theFunction )
   {
   case TA_CCI_TEST:
      retCode = TA_CCI( test->startIdx,
                        test->endIdx,
                        gBuffer[0].in,
                        gBuffer[1].in,
                        gBuffer[2].in,
                        test->optInTimePeriod,
                        &outBegIdx,
                        &outNbElement,
                        gBuffer[0].out0 );
      break;

   case TA_WILLR_TEST:
      retCode = TA_WILLR( test->startIdx,
                          test->endIdx,
                          gBuffer[0].in,
                          gBuffer[1].in,
                          gBuffer[2].in,
                          test->optInTimePeriod,
                          &outBegIdx,
                          &outNbElement,
                          gBuffer[0].out0 );
      break;

   default:
      retCode = TA_INTERNAL_ERROR(133);
   }

   /* Check that the input were preserved. */
   errNb = checkDataSame( gBuffer[0].in, history->high,history->nbBars );
   if( errNb != TA_TEST_PASS )
      return errNb;
   errNb = checkDataSame( gBuffer[1].in, history->low, history->nbBars );
   if( errNb != TA_TEST_PASS )
      return errNb;
   errNb = checkDataSame( gBuffer[2].in, history->close,history->nbBars );
   if( errNb != TA_TEST_PASS )
      return errNb;

   CHECK_EXPECTED_VALUE( gBuffer[0].out0, 0 );

   outBegIdx = outNbElement = 0;

   /* Make another call where the input and the output are the
    * same buffer.
    */
   switch( test->theFunction )
   {
   case TA_CCI_TEST:
      retCode = TA_CCI( test->startIdx,
                        test->endIdx,
                        gBuffer[0].in,
                        gBuffer[1].in,
                        gBuffer[2].in,
                        test->optInTimePeriod,
                        &outBegIdx,
                        &outNbElement,
                        gBuffer[0].in );
      break;
   case TA_WILLR_TEST:
      retCode = TA_WILLR( test->startIdx,
                          test->endIdx,
                          gBuffer[0].in,
                          gBuffer[1].in,
                          gBuffer[2].in,
                          test->optInTimePeriod,
                          &outBegIdx,
                          &outNbElement,
                          gBuffer[0].in );
      break;
   default:
      retCode = TA_INTERNAL_ERROR(134);
   }

   /* Check that the input were preserved. */
   errNb = checkDataSame( gBuffer[1].in, history->low, history->nbBars );
   if( errNb != TA_TEST_PASS )
      return errNb;
   errNb = checkDataSame( gBuffer[2].in, history->close,history->nbBars );
   if( errNb != TA_TEST_PASS )
      return errNb;

   /* The previous call to TA_MA should have the same output
    * as this call.
    *
    * checkSameContent verify that all value different than NAN in
    * the first parameter is identical in the second parameter.
    */
   errNb = checkSameContent( gBuffer[0].out0, gBuffer[0].in );
   if( errNb != TA_TEST_PASS )
      return errNb;

   CHECK_EXPECTED_VALUE( gBuffer[0].in, 0 );
   setInputBuffer( 0, history->high,  history->nbBars );

   /* Make another call where the input and the output are the
    * same buffer.
    */
   switch( test->theFunction )
   {
   case TA_CCI_TEST:
      retCode = TA_CCI( test->startIdx,
                        test->endIdx,
                        gBuffer[0].in,
                        gBuffer[1].in,
                        gBuffer[2].in,
                        test->optInTimePeriod,
                        &outBegIdx,
                        &outNbElement,
                        gBuffer[1].in );
      break;
   case TA_WILLR_TEST:
      retCode = TA_WILLR( test->startIdx,
                          test->endIdx,
                          gBuffer[0].in,
                          gBuffer[1].in,
                          gBuffer[2].in,
                          test->optInTimePeriod,
                          &outBegIdx,
                          &outNbElement,
                          gBuffer[1].in );
      break;
   default:
      retCode = TA_INTERNAL_ERROR(135);
   }

   /* Check that the input were preserved. */
   errNb = checkDataSame( gBuffer[0].in, history->high,history->nbBars );
   if( errNb != TA_TEST_PASS )
      return errNb;
   errNb = checkDataSame( gBuffer[2].in, history->close,history->nbBars );
   if( errNb != TA_TEST_PASS )
      return errNb;

   /* The previous call should have the same output as this call.
    *
    * checkSameContent verify that all value different than NAN in
    * the first parameter is identical in the second parameter.
    */
   errNb = checkSameContent( gBuffer[0].out0, gBuffer[1].in );
   if( errNb != TA_TEST_PASS )
      return errNb;

   CHECK_EXPECTED_VALUE( gBuffer[1].in, 0 );
   setInputBuffer( 1, history->low,   history->nbBars );

   /* Make another call where the input and the output are the
    * same buffer.
    */
   switch( test->theFunction )
   {
   case TA_CCI_TEST:
      retCode = TA_CCI( test->startIdx,
                        test->endIdx,
                        gBuffer[0].in,
                        gBuffer[1].in,
                        gBuffer[2].in,
                        test->optInTimePeriod,
                        &outBegIdx,
                        &outNbElement,
                        gBuffer[2].in );
      break;
   case TA_WILLR_TEST:
      retCode = TA_WILLR( test->startIdx,
                          test->endIdx,
                          gBuffer[0].in,
                          gBuffer[1].in,
                          gBuffer[2].in,
                          test->optInTimePeriod,
                          &outBegIdx,
                          &outNbElement,
                          gBuffer[2].in );
      break;
   default:
      retCode = TA_INTERNAL_ERROR(136);
   }

   /* Check that the input were preserved. */
   errNb = checkDataSame( gBuffer[0].in, history->high,history->nbBars );
   if( errNb != TA_TEST_PASS )
      return errNb;
   errNb = checkDataSame( gBuffer[1].in, history->low, history->nbBars );
   if( errNb != TA_TEST_PASS )
      return errNb;

   /* The previous call to TA_MA should have the same output
    * as this call.
    *
    * checkSameContent verify that all value different than NAN in
    * the first parameter is identical in the second parameter.
    */
   errNb = checkSameContent( gBuffer[0].out0, gBuffer[2].in );
   if( errNb != TA_TEST_PASS )
      return errNb;

   CHECK_EXPECTED_VALUE( gBuffer[2].in, 0 );
   setInputBuffer( 2, history->close, history->nbBars );
#endif

   /* Do a systematic test of most of the
    * possible startIdx/endIdx range.
    */
   testParam.test  = test;
   testParam.open  = history->open;
   testParam.high  = history->high;
   testParam.low   = history->low;
   testParam.close  = history->close;
   testParam.paramHolder = NULL;

   if( test->doRangeTestFlag )
   {
      
      errNb = doRangeTest( rangeTestFunction, 
                           TA_FUNC_UNST_NONE,
                           (void *)&testParam, 1, 0 );

      if( testParam.paramHolder )
      {
         retCode = TA_ParamHolderFree( testParam.paramHolder );
         if( retCode != TA_SUCCESS )
         {
            printf( "TA_ParamHolderFree failed [%d]\n", retCode );
            return TA_TSTCDL_PARAMHOLDERFREE_FAIL;
         }
      }
       
      if( errNb != TA_TEST_PASS )
         return errNb;
   }

   return TA_TEST_PASS;
}
Ejemplo n.º 4
0
/* Abstract call for all candlestick functions.
 *
 * Call the function by 'name'.
 * 
 * Optional inputs are pass as an array of double.
 * Elements will be converted to integer as needed.
 *
 * All outputs are returned in the remaining parameters.
 *
 * 'lookback' is the return value of the corresponding Lookback function.
 * taFuncRetCode is the return code from the call of the TA function.
 *
 */
static ErrorNumber callCandlestick( TA_ParamHolder **paramHolderPtr,
                                    const char   *name,
                                    int           startIdx,
                                    int           endIdx,
                                    const double *inOpen,
                                    const double *inHigh,
                                    const double *inLow,
                                    const double *inClose,
                                    const double  optInArray[],
                                    int          *outBegIdx,
                                    int          *outNbElement,                                    
                                    int           outInteger[],
                                    int          *lookback,
                                    TA_RetCode   *taFuncRetCode )
{

   /* Use the abstract interface to call the function by name. */
   TA_ParamHolder *paramHolder;
   const TA_FuncHandle *handle;
   const TA_FuncInfo *funcInfo;
   const TA_InputParameterInfo *inputInfo;
   const TA_OutputParameterInfo *outputInfo;

   TA_RetCode retCode;

   (void)optInArray;
   
   /* Speed optimization if paramHolder is already initialized. */   
   paramHolder = *paramHolderPtr;
   if( !paramHolder )
   {
      retCode = TA_GetFuncHandle( name, &handle );
      if( retCode != TA_SUCCESS )
      {
         printf( "Can't get the function handle [%d]\n", retCode );
         return TA_TSTCDL_GETFUNCHANDLE_FAIL;   
      }
                             
      retCode = TA_ParamHolderAlloc( handle, &paramHolder );
      if( retCode != TA_SUCCESS )
      {
         printf( "Can't allocate the param holder [%d]\n", retCode );
         return TA_TSTCDL_PARAMHOLDERALLOC_FAIL;
      }

      *paramHolderPtr = paramHolder;
      TA_GetFuncInfo( handle, &funcInfo );

      /* Verify that the input are only OHLC. */
      if( funcInfo->nbInput != 1 )
      {
         printf( "Candlestick are expected to use only OHLC as input.\n" );
         return TA_TSTCDL_NBINPUT_WRONG;
      }

      TA_GetInputParameterInfo( handle, 0, &inputInfo );

      if( inputInfo->type != TA_Input_Price )
      {
         printf( "Candlestick are expected to use only OHLC as input.\n" );
         return TA_TSTCDL_INPUT_TYPE_WRONG;
      }
   
      if( inputInfo->flags != (TA_IN_PRICE_OPEN |
                               TA_IN_PRICE_HIGH |
                               TA_IN_PRICE_LOW  |
                               TA_IN_PRICE_CLOSE) )
      {
         printf( "Candlestick are expected to use only OHLC as input.\n" );
         return TA_TSTCDL_INPUT_FLAG_WRONG;
      }
    
      /* Set the optional inputs. */
   
      /* Verify that there is only one output. */
      if( funcInfo->nbOutput != 1 )
      {
         printf( "Candlestick are expected to have only one output array.\n" );
         return TA_TSTCDL_NBOUTPUT_WRONG;
      }

      TA_GetOutputParameterInfo( handle, 0, &outputInfo );
      if( outputInfo->type != TA_Output_Integer )
      {
         printf( "Candlestick are expected to have only one output array of type integer.\n" );
         return TA_TSTCDL_OUTPUT_TYPE_WRONG;
      }

      /* !!!!!!!!!!!!! TO BE DONE !!!!!!!!!!!!!!!!!! 
       * For now all candlestick functions will be called with default optional parameter.
       */
   }

   /* Set the input buffers. */
   TA_SetInputParamPricePtr( paramHolder, 0,
                             inOpen, inHigh, inLow, inClose, NULL, NULL );

   TA_SetOutputParamIntegerPtr(paramHolder,0,outInteger);


   /* Do the function call. */
   *taFuncRetCode = TA_CallFunc(paramHolder,startIdx,endIdx,outBegIdx,outNbElement);

   if( *taFuncRetCode != TA_SUCCESS )
   {
      printf( "TA_CallFunc() failed [%d]\n", *taFuncRetCode );
      TA_ParamHolderFree( paramHolder );
      return TA_TSTCDL_CALLFUNC_FAIL;
   }      

   /* Do the lookback function call. */
   retCode = TA_GetLookback( paramHolder, lookback );
   if( retCode != TA_SUCCESS )
   {
      printf( "TA_GetLookback() failed [%d]\n", retCode );
      TA_ParamHolderFree( paramHolder );
      return TA_TSTCDL_GETLOOKBACK_FAIL;
   }

   return TA_TEST_PASS;   
}
Ejemplo n.º 5
0
PlotLine * TALIB::calculateCustom (QString &p, QPtrList<PlotLine> &d)
{
  // format: METHOD, ..., ...., ..... etc (first parm must be the method)

  QStringList l = QStringList::split(",", p, FALSE);

  if (! l.count())
  {
    qDebug("TALIB::calculateCustom: no method parm");
    return 0;
  }
  
  TA_Integer start = 0;
  TA_Integer end = data->count() - 1;
  if (d.count())
    end = d.at(0)->getSize() - 1;
  TA_Integer outstart;
  TA_Integer count;
  PlotLine *line = new PlotLine;
  // sometimes are not enough data available
  // to calc anything
  if(end < 0) 
    return line;
  
  // open a TALIB handle
  const TA_FuncHandle *handle;
  TA_RetCode retCode = TA_GetFuncHandle(l[0], &handle);
  if (retCode != TA_SUCCESS)
  {
    qDebug("TALIB::calculateCustom:can't open handle");
    return 0;
  }

  // get info on the function
  const TA_FuncInfo *theInfo;
  retCode = TA_GetFuncInfo(handle, &theInfo);
  if (retCode != TA_SUCCESS)
  {
    qDebug("TALIB::calculateCustom:can't get function info");
    return 0;
  }

  // create parm holder
  TA_ParamHolder *parmHolder;
  retCode = TA_ParamHolderAlloc(handle, &parmHolder);
  if (retCode != TA_SUCCESS)
  {
    qDebug("TALIB::calculateCustom:can't create parm holder");
    return 0;
  }

  // create and input arrays
  int loop = data->count();
  TA_Real open[loop];
  TA_Real high[loop];
  TA_Real low[loop];
  TA_Real close[loop];
  TA_Real volume[loop];
  TA_Real oi[loop];
  TA_Real treal[loop];

  int sparmIndex = 1;

  // setup the input arrays
  const TA_InputParameterInfo *inputParms;
  for (loop = 0; loop < (int) theInfo->nbInput; loop++ )
  {
    TA_GetInputParameterInfo(theInfo->handle, loop, &inputParms);

    if (inputParms->type == TA_Input_Price)
    {
      // populate the input arrays
      int loop2;
      for (loop2 = 0; loop2 < data->count(); loop2++)
      {
        open[loop2] = (TA_Real) data->getOpen(loop2);
        high[loop2] = (TA_Real) data->getHigh(loop2);
        low[loop2] = (TA_Real) data->getLow(loop2);
        close[loop2] = (TA_Real) data->getClose(loop2);
        volume[loop2] = (TA_Real) data->getVolume(loop2);
        oi[loop2] = (TA_Real) data->getOI(loop2);
      }

      retCode = TA_SetInputParamPricePtr(parmHolder, loop, &open[0], &high[0], &low[0], &close[0],
                                         &volume[0], &oi[0]);
      if (retCode != TA_SUCCESS)
      {
        qDebug("TALIB::calculateCustom:cannot set input prices");
        return 0;
      }
    }

    if (inputParms->type == TA_Input_Real)
    {
      if (! d.count())
      {
        qDebug("TALIB::calculateCustom: no input");
        return 0;
      }

      if (sparmIndex >= (int) l.count())
      {
        qDebug("TALIB::calculateCustom: input invalid number of parms");
        return 0;
      }

      PlotLine *line =  d.at(0);
      int loop2;
      for (loop2 = 0; loop2 < line->getSize(); loop2++)
        treal[loop2] = (TA_Real) line->getData(loop2);

      retCode = TA_SetInputParamRealPtr(parmHolder, loop, &treal[0]);
      if (retCode != TA_SUCCESS)
      {
        qDebug("TALIB::calculateCustom:cannot set real price");
        return 0;
      }

      sparmIndex++;
    }
  }

  if (sparmIndex < (int) l.count())
  {
    QStringList mal;
    getMATypes(mal);
    mal.remove("Wilder");
    int t = 0;

    // setup the optinput parms
    const TA_OptInputParameterInfo *optInfo;
    for (loop = 0; loop < (int) theInfo->nbOptInput; loop++ )
    {
      TA_GetOptInputParameterInfo(theInfo->handle, loop, &optInfo);
      switch (optInfo->type)
      {
        case TA_OptInput_RealRange:
          if (sparmIndex >= (int) l.count())
          {
            qDebug("TALIB::calculateCustom: optinput real invalid number of parms");
            return 0;
          }

          retCode = TA_SetOptInputParamReal(parmHolder, loop, (TA_Real) l[sparmIndex].toDouble());
          if (retCode != TA_SUCCESS)
          {
            qDebug("TALIB::calculateCustom:cannot set inputopt real");
            return 0;
          }
          sparmIndex++;
          break;
        case TA_OptInput_IntegerRange:
          if (sparmIndex >= (int) l.count())
          {
            qDebug("TALIB::calculateCustom: optinput integer invalid number of parms");
            return 0;
          }

          retCode = TA_SetOptInputParamInteger(parmHolder, loop, (TA_Integer) l[sparmIndex].toInt());
          if (retCode != TA_SUCCESS)
          {
            qDebug("TALIB::calculateCustom:cannot set inputopt integer");
            return 0;
          }
          sparmIndex++;
          break;

        case TA_OptInput_IntegerList:
          if (sparmIndex >= (int) l.count())
          {
            qDebug("TALIB::calculateCustom: optinput integerList invalid number of parms");
            return 0;
          }
    
          t = mal.findIndex(l[sparmIndex]);
          if (t == -1)
            t = 0;
          retCode = TA_SetOptInputParamInteger(parmHolder, loop, (TA_Integer) t);
          if (retCode != TA_SUCCESS)
          {
            qDebug("TALIB::calculateCustom:cannot set inputopt integer");
            return 0;
          }
          sparmIndex++;
          break;
        default:
          break;
      }
    }
  }

  // setup the output arrays
  TA_Real out1[data->count()];
  TA_Real out2[data->count()];
  TA_Real out3[data->count()];
  TA_Integer out4[data->count()];
  const TA_OutputParameterInfo *outInfo;
  for (loop = 0; loop < (int) theInfo->nbOutput; loop++)
  {
    retCode = TA_GetOutputParameterInfo(handle, loop, &outInfo);
    if (retCode != TA_SUCCESS)
    {
      qDebug("TALIB::calculateCustom:cannot get output info");
      return 0;
    }

    switch (loop)
    {
      case 0:
        if (outInfo->type == TA_Output_Integer)
        {
          retCode = TA_SetOutputParamIntegerPtr(parmHolder, loop, &out4[0]);
          if (retCode != TA_SUCCESS)
          {
            qDebug("TALIB::calculateCustom:cannot set output4");
            return 0;
          }
        }
        else
        {
          retCode = TA_SetOutputParamRealPtr(parmHolder, loop, &out1[0]);
          if (retCode != TA_SUCCESS)
          {
            qDebug("TALIB::calculateCustom:cannot set output1");
            return 0;
          }
        }
        break;      
      case 1:
        retCode = TA_SetOutputParamRealPtr(parmHolder, loop, &out2[0]);
        if (retCode != TA_SUCCESS)
        {
          qDebug("TALIB::calculateCustom:cannot set output2");
          return 0;
        }
        break;      
      case 2:
        retCode = TA_SetOutputParamRealPtr(parmHolder, loop, &out3[0]);
        if (retCode != TA_SUCCESS)
        {
          qDebug("TALIB::calculateCustom:cannot set output3");
          return 0;
        }
        break;
      default:
        break;
    }
  }

  // call the function
  /*
  TA_Integer start = 0;
  TA_Integer end = data->count() - 1;
  if (d.count())
    end = d.at(0)->getSize() - 1;
  TA_Integer outstart;
  TA_Integer count;
  PlotLine *line = new PlotLine;
  */
  retCode = TA_CallFunc(parmHolder, start, end, &outstart, &count);
  if (retCode != TA_SUCCESS)
  {
    printError(QString("TALIB::calculateCustom:TA_CallFunc"), retCode);
    qDebug("p=%s  start=%d  end=%d",p.ascii(), start, end);
  }
  else
  {
    // create the plotlines
    int loop2;
    retCode = TA_GetOutputParameterInfo(handle, 0, &outInfo);
    if (retCode != TA_SUCCESS)
    {
      qDebug("TALIB::calculateCustom:cannot get output info");
      return 0;
    }
    
    if (outInfo->type == TA_Output_Integer)
    {
      for (loop2 = 0; loop2 < count; loop2++)
        line->append((double) out4[loop2]);
    }
    else
    {
      if (theInfo->nbOutput > 1)
      {
        bool ok;
        l[l.count() - 1].toInt(&ok);
        if (! ok)
        {
          qDebug("TALIB::calculateCustom: parm #%i invalid, not an INTEGER", loop + 1);
          return 0;
        }

        switch (l[l.count() - 1].toInt(&ok))
        {
          case 2:
            for (loop2 = 0; loop2 < count; loop2++)
              line->append((double) out2[loop2]);
            break;
          case 3:
            for (loop2 = 0; loop2 < count; loop2++)
              line->append((double) out3[loop2]);
            break;
          default:
            for (loop2 = 0; loop2 < count; loop2++)
              line->append((double) out1[loop2]);
            break;
        }
      }
      else
      {
        for (loop2 = 0; loop2 < count; loop2++)
          line->append((double) out1[loop2]);
      }
    }
  }

  retCode = TA_ParamHolderFree(parmHolder);
  if (retCode != TA_SUCCESS)
    qDebug("TALIB::calculateCustom:can't delete parm holder");

  return line;
}
Ejemplo n.º 6
0
Indicator * TALIB::calculate ()
{
  Indicator *output = new Indicator;
  output->setDateFlag(dateFlag);
  output->setLogScale(logScale);

  // open a TALIB handle
  const TA_FuncHandle *handle;
  QString ts = "method";
  QString ts2;
  parms.getData(ts, ts2);
  TA_RetCode retCode = TA_GetFuncHandle(ts2, &handle);
  if (retCode != TA_SUCCESS)
  {
    qDebug("TALIB::calculate:can't open handle");
    return output;
  }

  // get info on the function
  const TA_FuncInfo *theInfo;
  retCode = TA_GetFuncInfo(handle, &theInfo);
  if (retCode != TA_SUCCESS)
  {
    qDebug("TALIB::calculate:can't get function info");
    return output;
  }

  // create parm holder
  TA_ParamHolder *parmHolder;
  retCode = TA_ParamHolderAlloc(handle, &parmHolder);
  if (retCode != TA_SUCCESS)
  {
    qDebug("TALIB::calculate:can't create parm holder");
    return output;
  }

  // create and populate the input arrays
  int loop = data->count();
  TA_Real open[loop];
  TA_Real high[loop];
  TA_Real low[loop];
  TA_Real close[loop];
  TA_Real volume[loop];
  TA_Real oi[loop];
  for (loop = 0; loop < data->count(); loop++)
  {
    open[loop] = (TA_Real) data->getOpen(loop);
    high[loop] = (TA_Real) data->getHigh(loop);
    low[loop] = (TA_Real) data->getLow(loop);
    close[loop] = (TA_Real) data->getClose(loop);
    volume[loop] = (TA_Real) data->getVolume(loop);
    oi[loop] = (TA_Real) data->getOI(loop);
  }

  // setup the input arrays
  const TA_InputParameterInfo *inputParms;
  for (loop = 0; loop < (int) theInfo->nbInput; loop++ )
  {
    TA_GetInputParameterInfo(theInfo->handle, loop, &inputParms);
    QString s;
    int tint = 3;
    switch (inputParms->type)
    {
      case TA_Input_Price:
        retCode = TA_SetInputParamPricePtr(parmHolder, loop, &open[0], &high[0], &low[0],
                                           &close[0], &volume[0], &oi[0]);
        if (retCode != TA_SUCCESS)
          qDebug("TALIB::calculate:cannot set input prices");
        break;
      case TA_Input_Real:
        s = QObject::tr("input") + QString::number(loop + 1);
        tint = parms.getInt(s);
        switch (tint)
        {
          case 0:
            retCode = TA_SetInputParamRealPtr(parmHolder, loop, &open[0]);
            if (retCode != TA_SUCCESS)
              qDebug("TALIB::calculate:cannot set real price");
            break;
          case 1:
            retCode = TA_SetInputParamRealPtr(parmHolder, loop, &high[0]);
            if (retCode != TA_SUCCESS)
              qDebug("TALIB::calculate:cannot set real price");
            break;
          case 2:
            retCode = TA_SetInputParamRealPtr(parmHolder, loop, &low[0]);
            if (retCode != TA_SUCCESS)
              qDebug("TALIB::calculate:cannot set real price");
            break;
          case 3:
            retCode = TA_SetInputParamRealPtr(parmHolder, loop, &close[0]);
            if (retCode != TA_SUCCESS)
              qDebug("TALIB::calculate:cannot set real price");
            break;
          case 4:
            retCode = TA_SetInputParamRealPtr(parmHolder, loop, &volume[0]);
            if (retCode != TA_SUCCESS)
              qDebug("TALIB::calculate:cannot set integer price");
            break;
          case 5:
            retCode = TA_SetInputParamRealPtr(parmHolder, loop, &oi[0]);
            if (retCode != TA_SUCCESS)
              qDebug("TALIB::calculate:cannot set integer price");
            break;
          default:
            break;
        }
        break;
      case TA_Input_Integer:
        break;
      default:
        break;
    }
  }

  // setup the optinput parms
  const TA_OptInputParameterInfo *optInfo;
  for (loop = 0; loop < (int) theInfo->nbOptInput; loop++ )
  {
    TA_GetOptInputParameterInfo(theInfo->handle, loop, &optInfo);
    QString s = optInfo->displayName;
    switch (optInfo->type)
    {
      case TA_OptInput_RealRange:
        retCode = TA_SetOptInputParamReal(parmHolder, loop, (TA_Real) parms.getDouble(s));
        if (retCode != TA_SUCCESS)
          qDebug("TALIB::calculate:cannot set inputopt real");
        break;
      case TA_OptInput_IntegerRange:
        retCode = TA_SetOptInputParamInteger(parmHolder, loop, (TA_Integer) parms.getInt(s));
        if (retCode != TA_SUCCESS)
          qDebug("TALIB::calculate:cannot set inputopt integer");
        break;
      case TA_OptInput_IntegerList:
        retCode = TA_SetOptInputParamInteger(parmHolder, loop, (TA_Integer) parms.getInt(s));
        if (retCode != TA_SUCCESS)
          qDebug("TALIB::calculate:cannot set inputopt integerlist");
        break;
      default:
        break;
    }
  }

  // setup the output arrays
  TA_Real out1[data->count()];
  TA_Real out2[data->count()];
  TA_Real out3[data->count()];
  TA_Integer out4[data->count()];
  const TA_OutputParameterInfo *outInfo;
  for (loop = 0; loop < (int) theInfo->nbOutput; loop++)
  {
    retCode = TA_GetOutputParameterInfo(handle, loop, &outInfo);
    if (retCode != TA_SUCCESS)
    {
      qDebug("TALIB::calculate:cannot get output info");
      continue;
    }

    switch (loop)
    {
      case 0:
        if (outInfo->type == TA_Output_Integer)
        {
          retCode = TA_SetOutputParamIntegerPtr(parmHolder, loop, &out4[0]);
          if (retCode != TA_SUCCESS)
            qDebug("TALIB::calculate:cannot set output4");
        }
        else
        {
          retCode = TA_SetOutputParamRealPtr(parmHolder, loop, &out1[0]);
          if (retCode != TA_SUCCESS)
            qDebug("TALIB::calculate:cannot set output1");
        }
        break;      
      case 1:
        retCode = TA_SetOutputParamRealPtr(parmHolder, loop, &out2[0]);
        if (retCode != TA_SUCCESS)
          qDebug("TALIB::calculate:cannot set output2");
        break;      
      case 2:
        retCode = TA_SetOutputParamRealPtr(parmHolder, loop, &out3[0]);
        if (retCode != TA_SUCCESS)
          qDebug("TALIB::calculate:cannot set output3");
        break;
      default:
        break;
    }
  }

  // call the function
  TA_Integer start = 0;
  TA_Integer end = data->count() - 1;
  TA_Integer outstart;
  TA_Integer count;
  retCode = TA_CallFunc(parmHolder, start, end, &outstart, &count);
  if (retCode != TA_SUCCESS)
    qDebug("TALIB::calculate:call function failed");
  else
  {
    // create the plotlines
    const TA_OutputParameterInfo *outInfo;
    for (loop = 0; loop < (int) theInfo->nbOutput; loop++ )
    {
      TA_GetOutputParameterInfo(theInfo->handle, loop, &outInfo);
      QString base = outInfo->paramName;
      base = base.right(base.length() - 3);
      if (! base.left(4).compare("Real"))
        base = base.right(base.length() - 4);
      if (! base.left(7).compare("Integer"))
        base = base.right(base.length() - 7);
      if (! base.length())
        base = QObject::tr("Plot");

      PlotLine *line = new PlotLine;

      QString s = base + " " + QObject::tr("Color");
      parms.getData(s, ts);
      QColor color(ts);
      line->setColor(color);

      s = base + " " + QObject::tr("Label");
      parms.getData(s, ts);
      line->setLabel(ts);

      s = base + " " + QObject::tr("Line Type");
      line->setType((PlotLine::LineType)parms.getInt(s));

      retCode = TA_GetOutputParameterInfo(handle, loop, &outInfo);
      if (retCode != TA_SUCCESS)
      {
        qDebug("TALIB::calculate:cannot get output info");
        delete line;
        continue;
      }

      int loop2;
      switch (loop)
      {
        case 0:
          if (outInfo->type == TA_Output_Integer)
          {
            for (loop2 = 0; loop2 < count; loop2++)
              line->append((double) out4[loop2]);
          }
          else
          {
            for (loop2 = 0; loop2 < count; loop2++)
              line->append((double) out1[loop2]);
          }
          break;      
        case 1:
          for (loop2 = 0; loop2 < count; loop2++)
            line->append((double) out2[loop2]);
          break;      
        case 2:
          for (loop2 = 0; loop2 < count; loop2++)
            line->append((double) out3[loop2]);
          break;
        default:
          break;
      }

      if (line->getType() == PlotLine::Histogram || line->getType() == PlotLine::HistogramBar)
        output->prependLine(line);
      else
        output->addLine(line);
    }
  }

  retCode = TA_ParamHolderFree(parmHolder);
  if (retCode != TA_SUCCESS)
    qDebug("TALIB::calculate:can't delete parm holder");

  return output;
}
Ejemplo n.º 7
0
/**** Global functions definitions.   ****/
ErrorNumber test_abstract( void )
{
    ErrorNumber retValue;
    TA_RetCode retCode;
    TA_ParamHolder *paramHolder;
    const TA_FuncHandle *handle;

    printf( "Testing Abstract interface\n" );

    retValue = allocLib();
    if( retValue != TA_TEST_PASS )
        return retValue;

    /* Verify TA_GetLookback. */
    retCode = TA_GetFuncHandle( "STOCH", &handle );
    if( retCode != TA_SUCCESS )
    {
        printf( "Can't get the function handle [%d]\n", retCode );
        return TA_ABS_TST_FAIL_GETFUNCHANDLE;
    }

    retCode = TA_ParamHolderAlloc( handle, &paramHolder );
    if( retCode != TA_SUCCESS )
    {
        printf( "Can't allocate the param holder [%d]\n", retCode );
        return TA_ABS_TST_FAIL_PARAMHOLDERALLOC;
    }

    retValue = testLookback(paramHolder);
    if( retValue != TA_SUCCESS )
    {
        printf( "testLookback() failed [%d]\n", retValue );
        TA_ParamHolderFree( paramHolder );
        return retValue;
    }

    retCode = TA_ParamHolderFree( paramHolder );
    if( retCode != TA_SUCCESS )
    {
        printf( "TA_ParamHolderFree failed [%d]\n", retCode );
        return TA_ABS_TST_FAIL_PARAMHOLDERFREE;
    }

    retValue = freeLib();
    if( retValue != TA_TEST_PASS )
        return retValue;

    /* Call all the TA functions through the abstract interface. */
    retValue = allocLib();
    if( retValue != TA_TEST_PASS )
        return retValue;

    retValue = test_default_calls();
    if( retValue != TA_TEST_PASS )
    {
        printf( "TA-Abstract default call failed\n" );
        return retValue;
    }

    retValue = freeLib();
    if( retValue != TA_TEST_PASS )
        return retValue;

    return TA_TEST_PASS; /* Succcess. */
}
Ejemplo n.º 8
0
static ErrorNumber callWithDefaults( const char *funcName, const double *input, const int *input_int, int size )
{
    TA_ParamHolder *paramHolder;
    const TA_FuncHandle *handle;
    const TA_FuncInfo *funcInfo;
    const TA_InputParameterInfo *inputInfo;
    const TA_OutputParameterInfo *outputInfo;

    TA_RetCode retCode;
    unsigned int i;
    int j;
    int outBegIdx, outNbElement, lookback;

    retCode = TA_GetFuncHandle( funcName, &handle );
    if( retCode != TA_SUCCESS )
    {
        printf( "Can't get the function handle [%d]\n", retCode );
        return TA_ABS_TST_FAIL_GETFUNCHANDLE;
    }

    retCode = TA_ParamHolderAlloc( handle, &paramHolder );
    if( retCode != TA_SUCCESS )
    {
        printf( "Can't allocate the param holder [%d]\n", retCode );
        return TA_ABS_TST_FAIL_PARAMHOLDERALLOC;
    }

    TA_GetFuncInfo( handle, &funcInfo );

    for( i=0; i < funcInfo->nbInput; i++ )
    {
        TA_GetInputParameterInfo( handle, i, &inputInfo );
        switch(inputInfo->type)
        {
        case TA_Input_Price:
            TA_SetInputParamPricePtr( paramHolder, i,
                                      inputInfo->flags&TA_IN_PRICE_OPEN?input:NULL,
                                      inputInfo->flags&TA_IN_PRICE_HIGH?input:NULL,
                                      inputInfo->flags&TA_IN_PRICE_LOW?input:NULL,
                                      inputInfo->flags&TA_IN_PRICE_CLOSE?input:NULL,
                                      inputInfo->flags&TA_IN_PRICE_VOLUME?input_int:NULL, NULL );
            break;
        case TA_Input_Real:
            TA_SetInputParamRealPtr( paramHolder, i, input );
            break;
        case TA_Input_Integer:
            TA_SetInputParamIntegerPtr( paramHolder, i, input_int );
            break;
        }
    }

    for( i=0; i < funcInfo->nbOutput; i++ )
    {
        TA_GetOutputParameterInfo( handle, i, &outputInfo );
        switch(outputInfo->type)
        {
        case TA_Output_Real:
            TA_SetOutputParamRealPtr(paramHolder,i,&output[i][0]);
            for( j=0; j < 2000; j++ )
                output[i][j] = TA_REAL_MIN;
            break;
        case TA_Output_Integer:
            TA_SetOutputParamIntegerPtr(paramHolder,i,&output_int[i][0]);
            for( j=0; j < 2000; j++ )
                output_int[i][j] = TA_INTEGER_MIN;
            break;
        }
    }

    /* Do the function call. */
    retCode = TA_CallFunc(paramHolder,0,size-1,&outBegIdx,&outNbElement);
    if( retCode != TA_SUCCESS )
    {
        printf( "TA_CallFunc() failed zero data test [%d]\n", retCode );
        TA_ParamHolderFree( paramHolder );
        return TA_ABS_TST_FAIL_CALLFUNC_1;
    }

    /* Verify consistency with Lookback */
    retCode = TA_GetLookback( paramHolder, &lookback );
    if( retCode != TA_SUCCESS )
    {
        printf( "TA_GetLookback() failed zero data test [%d]\n", retCode );
        TA_ParamHolderFree( paramHolder );
        return TA_ABS_TST_FAIL_CALLFUNC_2;
    }

    if( outBegIdx != lookback )
    {
        printf( "TA_GetLookback() != outBegIdx [%d != %d]\n", lookback, outBegIdx );
        TA_ParamHolderFree( paramHolder );
        return TA_ABS_TST_FAIL_CALLFUNC_3;
    }

    for( i=0; i < funcInfo->nbOutput; i++ )
    {
        switch(outputInfo->type)
        {
        case TA_Output_Real:
            for( j=0; j < outNbElement; j++ )
            {
                if( trio_isnan(output[i][j]) ||
                        trio_isinf(output[i][j]))
                {
                    printf( "Failed for output[%d][%d] = %e\n", i, j, output[i][j] );
                    return TA_ABS_TST_FAIL_INVALID_OUTPUT;
                }
            }
            break;
        case TA_Output_Integer:
            break;
        }
    }

    /* Do another function call where startIdx == endIdx == 0.
     * In that case, outBegIdx should ALWAYS be zero.
     */
    retCode = TA_CallFunc(paramHolder,0,0,&outBegIdx,&outNbElement);
    if( retCode != TA_SUCCESS )
    {
        printf( "TA_CallFunc() failed data test 4 [%d]\n", retCode );
        TA_ParamHolderFree( paramHolder );
        return TA_ABS_TST_FAIL_CALLFUNC_4;
    }

    if( outBegIdx != 0 )
    {
        printf( "failed outBegIdx=%d when startIdx==endIdx==0\n", outBegIdx );
        TA_ParamHolderFree( paramHolder );
        return TA_ABS_TST_FAIL_STARTEND_ZERO;
    }

    retCode = TA_ParamHolderFree( paramHolder );
    if( retCode != TA_SUCCESS )
    {
        printf( "TA_ParamHolderFree failed [%d]\n", retCode );
        return TA_ABS_TST_FAIL_PARAMHOLDERFREE;
    }

    return TA_TEST_PASS;
}