#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_DAYS 1000
// Fungsi untuk menghitung Moving Average secara iteratif
void moving_average_iterative( double prices[ ] , double ma[ ] , int days, int period) {
for ( int i = 0 ; i <= days - period; i++ ) {
double sum = 0.0 ;
for ( int j = 0 ; j < period; j++ ) {
sum += prices[ i + j] ;
}
ma[ i] = sum / period;
}
}
// Fungsi untuk menghitung Moving Average secara rekursif
void moving_average_recursive( double prices[ ] , double ma[ ] , int days, int period, int index) {
if ( index > days - period) return ; // Basis kasus
double sum = 0.0 ;
for ( int j = 0 ; j < period; j++ ) {
sum += prices[ index + j] ;
}
ma[ index] = sum / period;
moving_average_recursive( prices, ma, days, period, index + 1 ) ; // Rekursi
}
// Fungsi untuk menghitung profit dari strategi trading
double calculate_profit( double prices[ ] , double ma[ ] , int days) {
double profit = 0.0 ;
for ( int i = 1 ; i < days; i++ ) {
if ( prices[ i] > ma[ i - 1 ] ) {
profit += prices[ i] - prices[ i - 1 ] ; // Buy signal
} else if ( prices[ i] < ma[ i - 1 ] ) {
profit -= prices[ i - 1 ] - prices[ i] ; // Sell signal
}
}
return profit;
}
int main( ) {
double prices[ MAX_DAYS] ;
int days = MAX_DAYS;
int period = 5 ; // Periode MA
// Menghasilkan data harga acak
for ( int i = 0 ; i < days; i++ ) {
prices
[ i
] = 100 + ( rand ( ) % 100 ) ; // Harga antara 100 dan 199 }
double ma_iterative[ MAX_DAYS] = { 0 } ;
double ma_recursive[ MAX_DAYS] = { 0 } ;
// Mengukur waktu untuk fungsi iteratif
clock_t start_iterative
= clock ( ) ; moving_average_iterative( prices, ma_iterative, days, period) ;
clock_t end_iterative
= clock ( ) ; double time_iterative = ( double ) ( end_iterative - start_iterative) / CLOCKS_PER_SEC;
// Mengukur waktu untuk fungsi rekursif
clock_t start_recursive
= clock ( ) ; moving_average_recursive( prices, ma_recursive, days, period, 0 ) ;
clock_t end_recursive
= clock ( ) ; double time_recursive = ( double ) ( end_recursive - start_recursive) / CLOCKS_PER_SEC;
// Menghitung profit
double profit_iterative = calculate_profit( prices, ma_iterative, days) ;
double profit_recursive = calculate_profit( prices, ma_recursive, days) ;
// Menampilkan beberapa nilai harga
printf ( "Prices (first 100 days):\n " ) ; for ( int i = 0 ; i < 100 ; i++ ) {
printf ( "Day %d: %.2f\n " , i
+ 1 , prices
[ i
] ) ; }
// Menampilkan hasil
printf ( "\n Profit (Iterative): %.2f\n " , profit_iterative
) ; printf ( "Profit (Recursive): %.2f\n " , profit_recursive
) ;
// Menampilkan waktu eksekusi
printf ( "\n Execution Time (Iterative): %.6f seconds\n " , time_iterative
) ; printf ( "Execution Time (Recursive): %.6f seconds\n " , time_recursive
) ;
FILE * gnuplotPipe = popen( "gnuplot -persistent" , "w" ) ;
if ( gnuplotPipe) {
fprintf ( gnuplotPipe
, "set title 'Prices and Moving Average'\n " ) ;
fprintf ( gnuplotPipe
, "set xlabel 'Days'\n " ) ;
fprintf ( gnuplotPipe
, "set ylabel 'Price'\n " ) ;
fprintf ( gnuplotPipe
, "plot 'data.txt' using 1:2 with lines title 'Prices', '' using 1:3 with lines title 'Moving Average'\n " ) ;
pclose( gnuplotPipe) ;
} else {
fprintf ( stderr
, "Could not open gnuplot.\n " ) ;
}
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHRpbWUuaD4KCiNkZWZpbmUgTUFYX0RBWVMgMTAwMAoKLy8gRnVuZ3NpIHVudHVrIG1lbmdoaXR1bmcgTW92aW5nIEF2ZXJhZ2Ugc2VjYXJhIGl0ZXJhdGlmCnZvaWQgbW92aW5nX2F2ZXJhZ2VfaXRlcmF0aXZlKGRvdWJsZSBwcmljZXNbXSwgZG91YmxlIG1hW10sIGludCBkYXlzLCBpbnQgcGVyaW9kKSB7CiAgICBmb3IgKGludCBpID0gMDsgaSA8PSBkYXlzIC0gcGVyaW9kOyBpKyspIHsKICAgICAgICBkb3VibGUgc3VtID0gMC4wOw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