TA_RetCode TA_TradeReportToFile( TA_TradeReport *tradeReport, FILE *out ) { TA_TradeReportPriv *tradeReportPriv; const TA_Timestamp *entryTimestamp; const TA_Timestamp *exitTimestamp; unsigned int i; double tmpReal; if( !tradeReport || !out ) return TA_BAD_PARAM; /* Make sure this TA_TradeReport is a valid object */ tradeReportPriv = (TA_TradeReportPriv *)tradeReport->hiddenData; if( !tradeReportPriv || (tradeReportPriv->magicNb != TA_TRADEREPORT_MAGIC_NB) ) return TA_BAD_OBJECT; fprintf( out, "\n" ); fprintf( out, "[MM/DD/YYYY HH:MM:SS .. MM/DD/YYYY HH:MM:SS] [ID]\n" ); fprintf( out, " Quantity Entry($) Profit(%%) MinFE(%%) MaxFE(%%) MAE(%%)\n" ); fprintf( out, "============================================================\n" ); for( i=0; i < tradeReport->nbTrades; i++ ) { entryTimestamp = &tradeReport->trades[i]->entryTimestamp; exitTimestamp = &tradeReport->trades[i]->exitTimestamp; fprintf( out, "[%02d/%02d/%04d %02d:%02d:%02d .. %02d/%02d/%04d %02d:%02d:%02d] [%s]\n", TA_GetMonth(entryTimestamp), TA_GetDay (entryTimestamp), TA_GetYear (entryTimestamp), TA_GetHour (entryTimestamp), TA_GetMin (entryTimestamp), TA_GetSec (entryTimestamp), TA_GetMonth(exitTimestamp), TA_GetDay (exitTimestamp), TA_GetYear (exitTimestamp), TA_GetHour (exitTimestamp), TA_GetMin (exitTimestamp), TA_GetSec (exitTimestamp), tradeReport->trades[i]->id->symString ); fprintf( out, "%10d", tradeReport->trades[i]->quantity ); fprintf( out, "%10.2f", tradeReport->trades[i]->entryPrice ); fprintf( out, "%10.2f", tradeReport->trades[i]->profit ); tmpReal = tradeReport->trades[i]->minfe; if( tmpReal != TA_REAL_DEFAULT ) fprintf( out, "%10.2f", tmpReal ); else fprintf( out, "%10s", "N/A" ); tmpReal = tradeReport->trades[i]->maxfe; if( tmpReal != TA_REAL_DEFAULT ) fprintf( out, "%10.2f", tmpReal ); else fprintf( out, "%10s", "N/A" ); tmpReal = tradeReport->trades[i]->mae; if( tmpReal != TA_REAL_DEFAULT ) fprintf( out, "%10.2f", tmpReal ); else fprintf( out, "%10s", "N/A" ); fprintf( out, "\n\n" ); } return TA_SUCCESS; }
/**** Local functions definitions. ****/ ErrorNumber test_period( TA_UDBase *unifiedDatabase ) { TA_RetCode retCode; TA_History *history; const TA_Timestamp *timestamp; unsigned int i; ErrorNumber retValue; TA_AddDataSourceParam addParam; TA_HistoryAllocParam histParam; printf( "Testing period/timeframe conversion\n" ); /* Add access to some intra-day data. */ memset( &addParam, 0, sizeof( TA_AddDataSourceParam) ); addParam.id = TA_ASCII_FILE; addParam.location = "..\\src\\tools\\ta_regtest\\sampling\\ES.csv"; addParam.info ="[YY][MM][DD][HH][MN=5][O][H][L][C][V]"; addParam.category = "TA_SIM_REF"; retCode = TA_AddDataSource( unifiedDatabase,&addParam ); if( retCode != TA_SUCCESS ) { printf( "Can't access [%s] (%d)\n", addParam.location, retCode ); return TA_PERIOD_HISTORYALLOC_FAILED; } /* Because period transformation are very * dependend on the "delta" timestamp functions, * let's indepedently verify some of these. */ retValue = testTimestampDelta(); if( retValue != TA_TEST_PASS ) return retValue; /* Verify everything from the checkTable. */ for( i=0; i < CHECK_TABLE_SIZE; i++ ) { /* Validate by requesting all the data in a * different timeframe. */ memset( &histParam, 0, sizeof( TA_HistoryAllocParam ) ); histParam.category = "TA_SIM_REF"; histParam.symbol = checkTable[i].symbol; histParam.field = TA_ALL; histParam.period = checkTable[i].period; histParam.flags = checkTable[i].flags; retCode = TA_HistoryAlloc( unifiedDatabase, &histParam, &history ); if( retCode != TA_SUCCESS ) { printf( "%s%d.1 [%d]\n", fail_header, i, retCode ); return TA_PERIOD_HISTORYALLOC_FAILED; } if( history->nbBars != checkTable[i].nbExpectedPriceBar ) { printf( "%s%d.2 [%d != %d]\n", fail_header, i, history->nbBars, checkTable[i].nbExpectedPriceBar ); TA_HistoryFree( history ); return TA_PERIOD_NBBAR_INCORRECT; } /* If the call is expected to return an empty history, no further check are done. */ if( checkTable[i].nbExpectedPriceBar == 0 ) { retCode = TA_HistoryFree( history ); if( retCode != TA_SUCCESS ) { printf( "%s%d.16 [%d]\n", fail_header, i, retCode ); return TA_PERIOD_HISTORYFREE_FAILED; } continue; } #define CHECK_VALUE_OK(varName, subtestNo ) \ { \ if( !TA_REAL_EQ( history->varName[checkTable[i].thePriceBarToCheck], checkTable[i].expected_##varName, 0.01 ) ) \ { \ printf( "%s%d.%d [%f != %f]\n", \ fail_header, i, subtestNo, \ (TA_Real) history->varName[checkTable[i].thePriceBarToCheck], \ (TA_Real) checkTable[i].expected_##varName ); \ TA_HistoryFree( history ); \ return TA_PERIOD_PRICE_INCORRECT; \ } \ } CHECK_VALUE_OK( open, 3 ); CHECK_VALUE_OK( high, 4 ); CHECK_VALUE_OK( low, 5 ); CHECK_VALUE_OK( close, 6 ); CHECK_VALUE_OK( volume, 7 ); if( history->openInterest != NULL ) { printf( "%s%d.8\n", fail_header, i ); TA_HistoryFree( history ); return TA_PERIOD_OPENINTEREST_INCORRECT; } timestamp = &history->timestamp[checkTable[i].thePriceBarToCheck]; if( TA_GetYear( timestamp ) != checkTable[i].expected_year ) { printf( "%s%d.9 %d\n", fail_header, i, TA_GetYear(timestamp) ); TA_HistoryFree( history ); return TA_PERIOD_TIMESTAMP_YEAR_INCORRECT; } if( TA_GetMonth( timestamp ) != checkTable[i].expected_month ) { printf( "%s%d.10 %d\n", fail_header, i, TA_GetMonth(timestamp) ); TA_HistoryFree( history ); return TA_PERIOD_TIMESTAMP_MONTH_INCORRECT; } if( TA_GetDay( timestamp ) != checkTable[i].expected_day ) { printf( "%s%d.11 %d\n", fail_header, i, TA_GetDay(timestamp) ); TA_HistoryFree( history ); return TA_PERIOD_TIMESTAMP_DAY_INCORRECT; } if( TA_GetHour( timestamp ) != checkTable[i].expected_hour ) { printf( "%s%d.12 %d\n", fail_header, i, TA_GetHour(timestamp) ); TA_HistoryFree( history ); return TA_PERIOD_TIMESTAMP_DAY_INCORRECT; } if( TA_GetMin( timestamp ) != checkTable[i].expected_min ) { printf( "%s%d.13 %d\n", fail_header, i, TA_GetMin(timestamp) ); TA_HistoryFree( history ); return TA_PERIOD_TIMESTAMP_DAY_INCORRECT; } if( TA_GetSec( timestamp ) != checkTable[i].expected_sec ) { printf( "%s%d.14 %d\n", fail_header, i, TA_GetSec(timestamp) ); TA_HistoryFree( history ); return TA_PERIOD_TIMESTAMP_DAY_INCORRECT; } retCode = TA_HistoryFree( history ); if( retCode != TA_SUCCESS ) { printf( "%s%d.15 [%d]\n", fail_header, i, retCode ); return TA_PERIOD_HISTORYFREE_FAILED; } } return 0; /* Succcess. */ }