Beispiel #1
0
int TA_MA_Lookback( TA_Integer    optInTimePeriod_0, /* From 2 to TA_INTEGER_MAX */
                    TA_MAType     optInMAType_1 ) 
/**** END GENCODE SECTION 1 - DO NOT DELETE THIS LINE ****/
{
   /* insert lookback code here. */

   switch( optInMAType_1 )
   {
   case TA_MAType_SMA:
      return TA_SMA_Lookback( optInTimePeriod_0 );
      break;

   case TA_MAType_EMA:
      return TA_EMA_Lookback( optInTimePeriod_0 );
      break;

   case TA_MAType_WMA:
      return TA_WMA_Lookback( optInTimePeriod_0 );
      break;

   case TA_MAType_DEMA:
      return TA_DEMA_Lookback( optInTimePeriod_0 );
      break;

   case TA_MAType_TEMA:
      return TA_TEMA_Lookback( optInTimePeriod_0 );
      break;

   case TA_MAType_TRIMA:
      return TA_TRIMA_Lookback( optInTimePeriod_0 );
      break;

   case TA_MAType_KAMA:
      return TA_KAMA_Lookback( optInTimePeriod_0 );
      break;

   case TA_MAType_MAMA:
      return TA_MAMA_Lookback( 0.5, 0.05 );
      break;

   case TA_MAType_T3:
      return TA_T3_Lookback( optInTimePeriod_0, 0.7 );
      break;
   }

   return 0;
}
Beispiel #2
0
static ErrorNumber do_test_ma( const TA_History *history,
                               const TA_Test *test )
{
   TA_RetCode retCode;
   ErrorNumber errNb;
   TA_Integer outBegIdx;
   TA_Integer outNbElement;
   TA_RangeTestParam testParam;
   TA_Integer temp, temp2;
   const TA_Real *referenceInput;

   TA_SetCompatibility( test->compatibility );

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

   /* Build the input. */
   setInputBuffer( 0, history->close, history->nbBars );
   setInputBuffer( 1, history->close, history->nbBars );

   /* Re-initialize all the unstable period to zero. */
   TA_SetUnstablePeriod( TA_FUNC_UNST_ALL, 0 );
   
   /* Set the unstable period requested for that test. */
   switch( test->optInMAType_1 )
   {
   case TA_MAType_TEMA:
   case TA_MAType_DEMA:
   case TA_MAType_EMA:
      retCode = TA_SetUnstablePeriod( TA_FUNC_UNST_EMA, test->unstablePeriod );
      break;
   case TA_MAType_KAMA:
      retCode = TA_SetUnstablePeriod( TA_FUNC_UNST_KAMA, test->unstablePeriod );
      break;
   case TA_MAType_MAMA:
      retCode = TA_SetUnstablePeriod( TA_FUNC_UNST_MAMA, test->unstablePeriod );
      break;
   case TA_MAType_T3:
      retCode = TA_SetUnstablePeriod( TA_FUNC_UNST_T3, test->unstablePeriod );
      break;
   default:
      retCode = TA_SUCCESS;
      break;
   }

   if( retCode != TA_SUCCESS )
      return TA_TEST_TFRR_SETUNSTABLE_PERIOD_FAIL;

   /* Transform the inputs for MAMA (it is an AVGPRICE in John Ehlers book). */
   if( test->optInMAType_1 == TA_MAType_MAMA )
   {
      TA_MEDPRICE( 0, history->nbBars-1, history->high, history->low,
                   &outBegIdx, &outNbElement, gBuffer[0].in );

      TA_MEDPRICE( 0, history->nbBars-1, history->high, history->low,
                   &outBegIdx, &outNbElement, gBuffer[1].in );

      /* Will be use as reference */
      TA_MEDPRICE( 0, history->nbBars-1, history->high, history->low,
                   &outBegIdx, &outNbElement, gBuffer[2].in );
      referenceInput = gBuffer[2].in;
   }
   else
      referenceInput = history->close;

   /* Make a simple first call. */
   switch( test->id )
   {
   case TA_ANY_MA_TEST:
      retCode = TA_MA( test->startIdx,
                       test->endIdx,
                       gBuffer[0].in,
                       test->optInTimePeriod_0,
                       test->optInMAType_1,
                       &outBegIdx,
                       &outNbElement,
                       gBuffer[0].out0 );
      break;
   case TA_MAMA_TEST:
      retCode = TA_MAMA( test->startIdx,
                         test->endIdx,
                         gBuffer[0].in,
                         0.5, 0.05,                       
                         &outBegIdx,
                         &outNbElement,
                         gBuffer[0].out0,
                         gBuffer[0].out2 );

     break;
   case TA_FAMA_TEST:
      retCode = TA_MAMA( test->startIdx,
                         test->endIdx,
                         gBuffer[0].in,
                         0.5, 0.05,                       
                         &outBegIdx,
                         &outNbElement,
                         gBuffer[0].out2,
                         gBuffer[0].out0 );

     break;
   }

   errNb = checkDataSame( gBuffer[0].in, referenceInput, history->nbBars );
   if( errNb != TA_TEST_PASS )
      return errNb;

   errNb = checkExpectedValue( gBuffer[0].out0, 
                               retCode, test->expectedRetCode,
                               outBegIdx, test->expectedBegIdx,
                               outNbElement, test->expectedNbElement,
                               test->oneOfTheExpectedOutReal,
                               test->oneOfTheExpectedOutRealIndex );   
   if( errNb != TA_TEST_PASS )
      return errNb;

   outBegIdx = outNbElement = 0;

   /* Make another call where the input and the output are the
    * same buffer.
    */
   switch( test->id )
   {
   case TA_ANY_MA_TEST:
      retCode = TA_MA( test->startIdx,
                       test->endIdx,
                       gBuffer[1].in,
                       test->optInTimePeriod_0,
                       test->optInMAType_1,
                       &outBegIdx,
                       &outNbElement,
                       gBuffer[1].in );
      break;
   case TA_MAMA_TEST:
      retCode = TA_MAMA( test->startIdx,
                         test->endIdx,
                         gBuffer[1].in,
                         0.5, 0.05,                       
                         &outBegIdx,
                         &outNbElement,
                         gBuffer[1].in,
                         gBuffer[0].out2 );
     break;
   case TA_FAMA_TEST:
      retCode = TA_MAMA( test->startIdx,
                         test->endIdx,
                         gBuffer[1].in,
                         0.5, 0.05,                       
                         &outBegIdx,
                         &outNbElement,
                         gBuffer[0].out2,
                         gBuffer[1].in );
     break;
   }

   /* 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[1].in );
   if( errNb != TA_TEST_PASS )
      return errNb;

   errNb = checkExpectedValue( gBuffer[1].in, 
                               retCode, test->expectedRetCode,
                               outBegIdx, test->expectedBegIdx,
                               outNbElement, test->expectedNbElement,
                               test->oneOfTheExpectedOutReal,
                               test->oneOfTheExpectedOutRealIndex );   
   if( errNb != TA_TEST_PASS )
      return errNb;

  /* Verify that the "all-purpose" TA_MA_Lookback is consistent
   * with the corresponding moving average lookback function.
   */
   switch( test->optInMAType_1 )
   {
   case TA_MAType_WMA:
      temp = TA_WMA_Lookback( test->optInTimePeriod_0 );
      break;

   case TA_MAType_SMA:
      temp = TA_SMA_Lookback( test->optInTimePeriod_0 );
      break;

   case TA_MAType_EMA:
      temp = TA_EMA_Lookback( test->optInTimePeriod_0 );
      break;

   case TA_MAType_DEMA:
      temp = TA_DEMA_Lookback( test->optInTimePeriod_0 );
      break;

   case TA_MAType_TEMA:
      temp = TA_TEMA_Lookback( test->optInTimePeriod_0 );
      break;

   case TA_MAType_KAMA:
      temp = TA_KAMA_Lookback( test->optInTimePeriod_0 );
      break;

   case TA_MAType_MAMA:
      temp = TA_MAMA_Lookback( 0.5, 0.05 );
      break;

   case TA_MAType_TRIMA:
      temp = TA_TRIMA_Lookback( test->optInTimePeriod_0 );
      break;

   case TA_MAType_T3:
      temp = TA_T3_Lookback( test->optInTimePeriod_0, 0.7 );
      break;

   default:
      return TA_TEST_TFRR_BAD_MA_TYPE;
   }

   temp2 = TA_MA_Lookback( test->optInTimePeriod_0, test->optInMAType_1 );

   if( temp != temp2 )
      return TA_TEST_TFFR_BAD_MA_LOOKBACK;

   /* Do a systematic test of most of the
    * possible startIdx/endIdx range.
    */
   testParam.test  = test;
   testParam.close = referenceInput;

   if( test->doRangeTestFlag )
   {
      switch( test->optInMAType_1 )
      {
      case TA_MAType_TEMA:
      case TA_MAType_DEMA:
      case TA_MAType_EMA:
         errNb = doRangeTest( rangeTestFunction, 
                              TA_FUNC_UNST_EMA,
                              (void *)&testParam, 1, 0 );
         break;
      case TA_MAType_T3:
         errNb = doRangeTest( rangeTestFunction, 
                              TA_FUNC_UNST_T3,
                              (void *)&testParam, 1, 0 );
         break;
      case TA_MAType_KAMA:
         errNb = doRangeTest( rangeTestFunction, 
                              TA_FUNC_UNST_KAMA,
                              (void *)&testParam, 1, 0 );
         break;
      case TA_MAType_MAMA:
         errNb = doRangeTest( rangeTestFunction, 
                              TA_FUNC_UNST_MAMA,
                              (void *)&testParam, 2, 0 );
         break;
      default:
         errNb = doRangeTest( rangeTestFunction, 
                              TA_FUNC_UNST_NONE,
                              (void *)&testParam, 1, 0 );
      }

      if( errNb != TA_TEST_PASS )
         return errNb;
   }

   return TA_TEST_PASS;
}