gpt4 book ai didi

c - 用 C 语言编写的回测器

转载 作者:行者123 更新时间:2023-11-30 19:28:29 25 4
gpt4 key购买 nike

我正在编写一个用于回测股价的代码,最初的编码有一些错误,我在 SO 成员的帮助下发现了这些错误。现在代码已进入最后阶段。我在回测中添加了各种功能。该程序执行以下步骤:

  1. 从txt文件中读取值
  2. 将数据存储到数组
  3. 计算二维数组 mag[][] 中的移动平均值(针对一系列值)
  4. 在每笔交易中指出 MA 与价格的交叉(股票市场的基本 MA 交叉规则)。
  5. 列出交易 list 。
  6. 提供所有值(范围)测试的摘要。

一切似乎都运行良好。所有值在主文件中时都会被打印...但是在完成代码后,为了交叉检查,我尝试再次读取程序末尾的数组值..这次它显示了一些垃圾而不是真实值。

下面是代码

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_FILE_NAME 100
#define MAXCHAR 1000
int main()
{
FILE *fp;
int count=0,k1=0,k2=0,k=0;
char filename[MAX_FILE_NAME];
char c; // To store a character read from file

// Get file name from user. The file should be
// either in current folder or complete path should be provided
printf("Enter file name or full path: ");
scanf("%s", filename);
printf("Enter the minimum rolling period for calculation : \n");
scanf("%d", &k1);
printf("Enter the maximum rolling period for calculation : \n");
scanf("%d", &k2);
// Open the file

fp = fopen(filename, "r");

// Check if file exists
if (fp == NULL)
{
printf("Could not open file %s", filename);
return 0;
}

// Extract characters from file and store in character c
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n') // Increment count if this character is newline
count = count + 1;
// Close the file
fclose(fp);
//printf("The file %s has %d lines\n", filename, count);
FILE *myFile;
myFile = fopen(filename, "r");
//read file into array
float numberArray[count];
int i;
if (myFile == NULL)
{
printf("Error Reading File\n");
exit (0);
}
for (i = 0; i < count; i++)
{
fscanf(myFile, "%f,", &numberArray[i]);
}

fclose(myFile);
float data[count],mag[k2+1][count];
int buy[k2+1][count],sell[k2+1][count];
int ftrade[k2+1],fbtrade[k2+1],fstrade[k2+1],ltrade[k2+1];
for (k=k1; k<=k2; k++)
{
double avg=0,sum=0;
for (i=0; i<k-1; i++)
{
mag[k][i-1]=0;
sum=sum+numberArray[i];
}
for(i=k-1; i<=count; i++)
{
mag[k][i-1]=avg;
sum=sum+numberArray[i]-numberArray[i-k];
avg = sum/k;
}
}

for(k=k1; k<=k2; k++)
{

for(i=0; i<count; i++)
{
if((numberArray[i]<mag[k][i]) && (numberArray[i+1]>mag[k][i+1]))
buy[k][i+1]=1;
else
buy[k][i+1]=0;
}
for(i=0; i<count; i++)
{
if((numberArray[i]>mag[k][i]) && (numberArray[i+1]<mag[k][i+1]))
sell[k][i+1]=-1;
else
sell[k][i+1]=0;
}
}

for(k=k1; k<=k2; k++)
{
for(i=0; i<count; i++)
{
if(buy[k][i]==1 || sell[k][i]==-1)
{
ftrade[k]=i;
break;
}
}
}

for(k=k1; k<=k2; k++)
{
for(i=0; i<count; i++)
{
if(buy[k][i]==1)
{
fbtrade[k]=i;
break;
}
}
}

for(k=k1; k<=k2; k++)
{

for(i=0; i<count; i++)
{
if(sell[k][i]==-1)
{
fstrade[k]=i;
break;
}
}
}

for(k=k1; k<=k2; k++)
{
for(i=0; i<count; i++)
{
if(buy[k][i]==1 || sell[k][i]==-1)
{
ltrade[k]=i;
}
}
}


for(k=k1; k<=k2; k++)
{
printf("\nFirst Trade for %d = %d",k,ftrade[k]);
printf("\nFirst Buy Trade for %d = %d",k,fbtrade[k]);
printf("\nFirst Sell Trade for %d = %d",k,fstrade[k]);
printf("\nLast Trade for %d = %d",k,ltrade[k]);
}


/**************************************
STEP - 8 : Count the number of trades (Buy & SELL)
************************************************/
int btradesarray[k2],stradesarray[k2];
for(k=k1; k<=k2; k++)
{
btradesarray[k]=0,stradesarray[k]=0;
int btrades=0,strades=0;
for(i=0; i<=count; i++)
{
if(buy[k][i]==1)
{
btrades=btrades+1;
btradesarray[k]=btrades;
}
else if(sell[k][i]==-1)
{
strades=strades+1;
stradesarray[k]=strades;
}
}
}

for(k=k1; k<=k2; k++)
{
printf("\nTotal Buy Trades for %d is = %d",k,btradesarray[k]);
}

/**************************************
STEP - 9 : TOTAL TRADES
************************************************/

int ttrades[k2];
for(k=k1; k<=k2; k++)
{
ttrades[k]=btradesarray[k]+stradesarray[k]-1;
}

for(k=k1; k<=k2; k++)
{
printf("\nTotal Trades for %d is = %d",k,ttrades[k]);
}

/**************************************
STEP - 9 : Making Trade List and profit calculation
************************************************/
float tradelist[k2][count];
float profit[k2][count];
int j=0;
for(k=k1; k<=k2; k++)
{
for(i=ftrade[k]; i<count; i++)
{

if(buy[i]!=0)
{
tradelist[k][j]= numberArray[i];
j++;
}
else if (sell[i]!=0)
{
tradelist[k][j]= -1*numberArray[i];
j++;
}
}
}

printf("\n+++++++++++++ CHECKING ++++++++++++++++");
for(k=k1; k<=k2; k++)
{
printf("\nTotal Trades for %d is = %d",k,ttrades[k]);
printf("\nTotal Buy Trades for %d is = %d",k,btradesarray[k]);
}
}

在检查部分,它正在打印在程序运行期间正确打印的同一变量的垃圾。

以下是输出:

enter image description here

最佳答案

我终于做到了。

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_FILE_NAME 100
#define MAXCHAR 1000
int main()
{
FILE *fp;
int count=0,k1=0,k2=0,k=0;
char filename[MAX_FILE_NAME];
char c; // To store a character read from file

// Get file name from user. The file should be
// either in current folder or complete path should be provided
printf("Enter file name or full path: ");
scanf("%s", filename);
printf("Enter the minimum rolling period for calculation : \n");
scanf("%d", &k1);
printf("Enter the maximum rolling period for calculation : \n");
scanf("%d", &k2);
// Open the file

fp = fopen(filename, "r");

// Check if file exists
if (fp == NULL)
{
printf("Could not open file %s", filename);
return 0;
}

// Extract characters from file and store in character c
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n') // Increment count if this character is newline
count = count + 1;
// Close the file
fclose(fp);
//printf("The file %s has %d lines\n", filename, count);
FILE *myFile;
myFile = fopen(filename, "r");
//read file into array
float numberArray[count];
int i;
if (myFile == NULL)
{
printf("Error Reading File\n");
exit (0);
}
for (i = 0; i < count; i++)
{
fscanf(myFile, "%f,", &numberArray[i]);
}

fclose(myFile);
float data[count],mag[k2+1][count];
int buy[k2+1][count],sell[k2+1][count];
int ftrade[k2+1],fbtrade[k2+1],fstrade[k2+1],ltrade[k2+1];
for (k=k1; k<=k2; k++)
{
double avg=0,sum=0;
for (i=0; i<k-1; i++)
{
mag[k][i-1]=0;
sum=sum+numberArray[i];
}
for(i=k-1; i<=count; i++)
{
mag[k][i-1]=avg;
sum=sum+numberArray[i]-numberArray[i-k];
avg = sum/k;
}
}

for(k=k1; k<=k2; k++)
{

for(i=0; i<count; i++)
{
if((numberArray[i]<mag[k][i]) && (numberArray[i+1]>mag[k][i+1]))
buy[k][i+1]=1;
else
buy[k][i+1]=0;
}
for(i=0; i<count; i++)
{
if((numberArray[i]>mag[k][i]) && (numberArray[i+1]<mag[k][i+1]))
sell[k][i+1]=-1;
else
sell[k][i+1]=0;
}
}

for(k=k1; k<=k2; k++)
{
for(i=0; i<count; i++)
{
if(buy[k][i]==1 || sell[k][i]==-1)
{
ftrade[k]=i;
break;
}
}
}

for(k=k1; k<=k2; k++)
{
for(i=0; i<count; i++)
{
if(buy[k][i]==1)
{
fbtrade[k]=i;
break;
}
}
}

for(k=k1; k<=k2; k++)
{

for(i=0; i<count; i++)
{
if(sell[k][i]==-1)
{
fstrade[k]=i;
break;
}
}
}

for(k=k1; k<=k2; k++)
{
for(i=0; i<count; i++)
{
if(buy[k][i]==1 || sell[k][i]==-1)
{
ltrade[k]=i;
}
}
}


for(k=k1; k<=k2; k++)
{
printf("\nFirst Trade for %d = %d",k,ftrade[k]);
printf("\nFirst Buy Trade for %d = %d",k,fbtrade[k]);
printf("\nFirst Sell Trade for %d = %d",k,fstrade[k]);
printf("\nLast Trade for %d = %d",k,ltrade[k]);
}


/**************************************
STEP - 8 : Count the number of trades (Buy & SELL)
************************************************/
int btradesarray[k2],stradesarray[k2];
for(k=k1; k<=k2; k++)
{
btradesarray[k]=0,stradesarray[k]=0;
int btrades=0,strades=0;
for(i=0; i<=count; i++)
{
if(buy[k][i]==1)
{
btrades=btrades+1;
btradesarray[k]=btrades;
}
else if(sell[k][i]==-1)
{
strades=strades+1;
stradesarray[k]=strades;
}
}
}

for(k=k1; k<=k2; k++)
{
printf("\nTotal Buy Trades for %d is = %d",k,btradesarray[k]);
}

/**************************************
STEP - 9 : TOTAL TRADES
************************************************/

int ttrades[k2];
for(k=k1; k<=k2; k++)
{
ttrades[k]=btradesarray[k]+stradesarray[k]-1;
}

for(k=k1; k<=k2; k++)
{
printf("\nTotal Trades for %d is = %d",k,ttrades[k]);
}

/**************************************
STEP - 9 : Making Trade List and profit calculation
************************************************/
float tradelist[k2+1][count];
float profit[k2+1][count];
int j;
for(k=k1; k<=k2; k++)
{
for(i=ftrade[k]; i<count; i++)
{
if(buy[k][i]==1)
{
tradelist[k][j]= numberArray[i];
}
else if (sell[k][i]==-1)
{
tradelist[k][j]= -1*numberArray[i];
}
j++;
}
if(j>=count)
{
j=0;
}

printf("\nThe value of J = %d",j);
}



printf("\n+++++++++++++ CHECKING ++++++++++++++++");
for(k=k1; k<=k2; k++)
{
printf("\nTotal Trades for %d is = %d",k,ttrades[k]);
printf("\nTotal Buy Trades for %d is = %d",k,btradesarray[k]);
}
}

这是结果输出:

enter image description here

关于c - 用 C 语言编写的回测器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53894109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com