#define NUM64 64
#define NUM99 99
#define NUM128 128
struct tAcctInfo
{
double last_year_asset;
double cash;
double asset;
double hold;
double y_get;
double d_get;
};
struct tItemInfo
{
int No;
char code[NUM64];
char name[NUM64];
double price;
double rise;
int numb;
double hold;
int stop;
};
tAcctInfo g_acctInfo;
tItemInfo g_itemInfo[NUM99];
int g_itemnumb; // 总数量
int g_freshtime;
int g_freshflag;
void doCmd(char* pszCmd);
void Trim(string &str)
{
int s=str.find_first_not_of(" \t");
int e=str.find_last_not_of(" \t");
str=str.substr(s,e-s+1);
}
void init()
{
system("color 70");
g_itemnumb = 0;
g_freshflag = 1;
g_freshtime = 1;
g_acctInfo.last_year_asset = 0.0;
g_acctInfo.asset = 0.0;
g_acctInfo.cash = 0.0;
g_acctInfo.hold = 0.0;
g_acctInfo.y_get = 0.0;
g_acctInfo.d_get = 0.0;
}
void getValue(string& str, int n, string& strRet)
{
string::size_type n1 = str.find(",");
string::size_type n2 = str.rfind(",");
if (n == 1)
{
strRet = str.substr(0, n1);
return;
}
if (n == 2)
{
strRet = str.substr(n1+1, n2 - n1-1);
return;
}
if (n == 3)
{
strRet = str.substr(n2+1, str.length() - n2);
return;
}
}
void initItemInfo(tItemInfo& tIemInfo)
{
memset(g_itemInfo[g_itemnumb].code, 0, NUM64);
memset(g_itemInfo[g_itemnumb].name, 0, NUM64);
g_itemInfo[g_itemnumb].rise = 0;
g_itemInfo[g_itemnumb].hold = 0;
g_itemInfo[g_itemnumb].price = 0;
g_itemInfo[g_itemnumb].stop = 1;
g_itemInfo[g_itemnumb].No = 0;
g_itemInfo[g_itemnumb].numb = 0;
}
void loadBaseData(char* pCfgFile)
{
string strKey;
string strValue;
string strLine;
ifstream inFile(pCfgFile);
int nRecord = 0;
while (getline(inFile, strLine))
{
string::size_type n4 = strLine.find("=");
if (n4 != string::npos)
{
strKey = strLine.substr(0, n4);
strValue = strLine.substr(n4+1, strLine.length()-n4-1);
Trim(strKey);
Trim(strValue);
nRecord++;
if ("" == strKey)
{
continue;
}
if (1 == nRecord)
{
if (0 == strcmp(strKey.c_str(), "last_year_asset"))
{
g_acctInfo.last_year_asset = atof(strValue.c_str());
}
}
else if (2 == nRecord)
{
if (0 == strcmp(strKey.c_str(), "cash"))
{
g_acctInfo.cash = atof(strValue.c_str());
}
}
else if (3 == nRecord)
{
if (0 == strcmp(strKey.c_str(), "freshtime"))
{
g_freshtime = atoi(strValue.c_str());
}
}
else
{
if (g_itemnumb >= NUM99)
{
return ;
}
initItemInfo(g_itemInfo[g_itemnumb]);
strncpy(g_itemInfo[g_itemnumb].code, strKey.c_str(), NUM64 - 1);
string strRet;
getValue(strValue, 1, strRet);
strncpy(g_itemInfo[g_itemnumb].name, strRet.c_str(), NUM64 - 1);
getValue(strValue, 2, strRet);
g_itemInfo[g_itemnumb].numb = atoi(strRet.c_str());
getValue(strValue, 3, strRet);
g_itemInfo[g_itemnumb].price = atof(strRet.c_str());
g_itemInfo[g_itemnumb].No = g_itemnumb + 1;
g_itemnumb++;
}
}
}
inFile.close();
}
void showInfo()
{
system("cls");
int i = 0;
printf("--------------------------------------------------------------------------------");
printf("\n a%12.2lf d %9.2lf\n b%12.2lf e %9.2lf\n c%12.2lf f %9.2lf",
g_acctInfo.last_year_asset,g_acctInfo.hold,g_acctInfo.asset,g_acctInfo.y_get,g_acctInfo.cash,g_acctInfo.d_get);
printf("\nNo code name price rise numb hold");
printf("\n--------------------------------------------------------------------------------");
for (i = 0; i < g_itemnumb; i++)
{
double dHoldPer = (g_acctInfo.asset>1 ? 10000*g_itemInfo[i].price/g_acctInfo.asset :0);
printf("\n%2d%11s%11s%11.3lf%11.2lf%11d%11.2lf(%.2lf)", g_itemInfo[i].No,g_itemInfo[i].code, g_itemInfo[i].name, g_itemInfo[i].price,g_itemInfo[i].rise,g_itemInfo[i].numb,g_itemInfo[i].hold,dHoldPer);
if (g_itemInfo[i].stop > 0)
printf(" s");
}
printf("\n--------------------------------------------------------------------------------");
g_freshflag = !g_freshflag;
printf("\n<%d>",g_freshflag);
}
void updateAcctAndHold()
{
g_acctInfo.asset = 0;
g_acctInfo.d_get = 0;
g_acctInfo.asset += g_acctInfo.cash;
for (int i = 0; i < g_itemnumb; i++)
{
if (g_itemInfo[i].numb > 0)
{
g_acctInfo.asset += g_itemInfo[i].numb*g_itemInfo[i].price;
}
}
for (int j = 0; j < g_itemnumb; j++)
{
if (g_acctInfo.asset < 1) {g_itemInfo[j].hold = 0; continue;}
g_itemInfo[j].hold = g_itemInfo[j].numb*g_itemInfo[j].price/g_acctInfo.asset*100;
}
if (g_acctInfo.asset > 0)
{
g_acctInfo.hold = (g_acctInfo.asset-g_acctInfo.cash)/g_acctInfo.asset*100;
}
if (g_acctInfo.last_year_asset > 0)
{
g_acctInfo.y_get = (g_acctInfo.asset/g_acctInfo.last_year_asset-1)*100;
}
for (int k = 0; k < g_itemnumb; k++)
{
g_acctInfo.d_get += g_itemInfo[k].rise;
}
if (g_itemnumb) g_acctInfo.d_get /= g_itemnumb;
}
void saveBaseInfo(char* pCfgFile)
{
ofstream outFile(pCfgFile);
outFile <<setiosflags(ios::fixed)<<setprecision(2)<<"last_year_asset="<<g_acctInfo.last_year_asset<<"\n";
outFile <<setiosflags(ios::fixed)<<setprecision(2)<<"cash="<<g_acctInfo.cash<<"\n";
outFile <<"freshtime="<<g_freshtime<<"\n";
for(int i = 0; i < g_itemnumb; i++)
{
outFile <<setiosflags(ios::fixed)<<setprecision(2)<<g_itemInfo[i].code<<"="<<g_itemInfo[i].name<<","<<g_itemInfo[i].numb<<","<<setprecision(3)<<g_itemInfo[i].price<<"\n";
}
}
int GetCurPrice(CInternetSession& session, char* pszName, double& dCurPrice, double& dLastPrice)
{
int nRet = -1;
CHttpFile* pHttpFile = NULL;
char szUrl[128] = {0};
char szPre[] = "http://hq.sinajs.cn/list=";
strncat(szUrl, szPre, strlen(szPre));
strncat(szUrl, pszName, strlen(pszName));
try
{
pHttpFile = (CHttpFile *)session.OpenURL(szUrl, 1, INTERNET_FLAG_TRANSFER_ASCII | INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE);
}
catch (...)
{
return 0;
}
if(!pHttpFile)
{
return 0;
}
DWORD dwHttpStatus = 0;
pHttpFile->QueryInfoStatusCode(dwHttpStatus);
if (dwHttpStatus == HTTP_STATUS_OK)
{
CString strText;
while(pHttpFile->ReadString(strText))
{
int nPos = 0;
int nPos2 = 0;
nPos = strText.Find(",");
nPos = strText.Find(",", nPos+1);
sscanf(LPCTSTR(strText)+nPos+1, "%lf,", &dLastPrice);
nPos = strText.Find(",", nPos+1);
nRet = sscanf(LPCTSTR(strText)+nPos+1, "%lf,", &dCurPrice);
if(nRet == 1)
{
break;
}
}
}
pHttpFile->Close();
delete pHttpFile;
if(nRet == 1)
{
return 1;
}
return 0;
}
int InitCurPrice()
{
CInternetSession session("MySession");
for(int i = 0; i < g_itemnumb; i++)
{
char szCode[NUM64] = {0};
if (strncmp(g_itemInfo[i].code,"6", 1) == 0 || strncmp(g_itemInfo[i].code,"5", 1) == 0)
{
strcpy(szCode, "sh");
}
else
{
strcpy(szCode, "sz");
}
strcpy(szCode+2, g_itemInfo[i].code);
double dLastPrice = 0;
if(!GetCurPrice(session, szCode, g_itemInfo[i].price, dLastPrice))
{
g_itemInfo[i].stop = 1;
g_itemInfo[i].rise = 0;
}
else
{
if (g_itemInfo[i].price < 0.00001) g_itemInfo[i].price = dLastPrice;
g_itemInfo[i].rise = (g_itemInfo[i].price/dLastPrice-1)*100;
g_itemInfo[i].stop = 0;
}
}
session.Close();
return 1;
}
void getCmd(char* szCmd, int nLen)
{
int n = 0;
int ch = 0;
while ((ch=getchar()) != '\n' && n < nLen)
{
*(szCmd+n) = ch;
n++;
}
}
int main(int argc, char* argv[])
{
if (argc < 3) return 0;
char szTitle[64] = {0};
strcpy(szTitle, "title ");
strncpy(szTitle+7, argv[2], 40);
system(szTitle);
init();
loadBaseData(argv[1]);
updateAcctAndHold();
showInfo();
for(;;)
{
InitCurPrice();
updateAcctAndHold();
showInfo();
saveBaseInfo(argv[1]);
Sleep(g_freshtime*1000);
//doCmd("new 000001,xxx5");
//doCmd("mov 5,1");
//doCmd("set a,10000");
//doCmd("mod 1,2000,5.11");
//doCmd("del 1");
//doCmd("adj 1,1");
//doCmd("adj 2,1");
//doCmd("adj 3,1");
//doCmd("adj 4,1");
//doCmd("buy 1,-100");
//doCmd("buy 1,-100");
//doCmd("buy 4,100");
//doCmd("buy 4,100");
//doCmd("mov 1,2");
//doCmd("mov 1,4");
while (kbhit())
{
char szCmd[NUM128] = {0};
getCmd(szCmd, sizeof(szCmd)-1);
doCmd(szCmd);
break;
}
}
return 0;
}
/*
set a,10000
set b,10000
new 6000000,ZSYY
mod 1,100,11.11
mov 1,5
del 1
cfg 1,1
buy 1,-100
*/
void doCmd(char* pszCmd)
{
char para[4][32] = {0};
char* p = pszCmd;
int i = 0;
while(p && *p != 0 && (*p == ',' || *p == ' ')) p++;
while(p && *p != 0 && *p != ',' && *p != ' ' && i < 32)
{
para[0][i] = *p;
p++;
i++;
}
i = 0;
while(p && *p != 0 && (*p == ',' || *p == ' ')) p++;
while(p && *p != 0 && *p != ',' && *p != ' ' && i < 32)
{
para[1][i] = *p;
i++;
p++;
}
i = 0;
while(p && *p != 0 && (*p == ',' || *p == ' ')) p++;
while(p && *p != 0 && *p != ',' && *p != ' ' && i < 32)
{
para[2][i] = *p;
i++;
p++;
}
i = 0;
while(p && *p != 0 && (*p == ',' || *p == ' ')) p++;
while(p && *p != 0 && *p != ',' && *p != ' ' && i < 32)
{
para[3][i] = *p;
i++;
p++;
}
// set a,10000
// set b,10000
if (0== strcmp(para[0], "set"))
{
if (0 == strcmp(para[1], "a"))
{
g_acctInfo.last_year_asset = atof(para[2]);
}
if (0 == strcmp(para[1], "c"))
{
g_acctInfo.cash = atof(para[2]);
}
if (0 == strcmp(para[1], "f"))
{
g_freshtime = atoi(para[2]);
}
}
// new 6000000,ZSYY
else if (0 == strcmp(para[0], "new"))
{
if (g_itemnumb >= NUM99)
{
return ;
}
initItemInfo(g_itemInfo[g_itemnumb]);
strcpy(g_itemInfo[g_itemnumb].code, para[1]);
strcpy(g_itemInfo[g_itemnumb].name, para[2]);
g_itemInfo[g_itemnumb].No = g_itemnumb + 1;
g_itemnumb++;
}
// mod 1,100,11.11
else if (0 == strcmp(para[0], "mod"))
{
int nNo = atoi(para[1]);
if (nNo < 1 || nNo > g_itemnumb) return ;
g_itemInfo[nNo-1].numb = atoi(para[2]);
g_itemInfo[nNo-1].price = atof(para[3]);
}
// mov 3,7 3保存,4567前移1位,7位放3
// mov 7,3 7保存,3456后移1位,3位放7
// 1 2 3 4 5 6 7 8 9
else if (0 == strcmp(para[0], "mov"))
{
int n1 = atoi(para[1]);
int n2 = atoi(para[2]);
if (n1 < 1 || n1 > g_itemnumb || n2 < 1 || n2 > g_itemnumb || n1 == n2) return ;
tItemInfo tmpInfo;
tmpInfo.No = g_itemInfo[n1-1].No;
tmpInfo.price = g_itemInfo[n1-1].price;
tmpInfo.rise = g_itemInfo[n1-1].rise;
tmpInfo.numb = g_itemInfo[n1-1].numb;
tmpInfo.hold = g_itemInfo[n1-1].hold;
tmpInfo.stop = g_itemInfo[n1-1].stop;
strcpy(tmpInfo.code, g_itemInfo[n1-1].code);
strcpy(tmpInfo.name, g_itemInfo[n1-1].name);
if (n1 < n2)
{
for(int i = n1; i < n2; i++)
{
g_itemInfo[i-1].No = g_itemInfo[i].No - 1;
g_itemInfo[i-1].price = g_itemInfo[i].price;
g_itemInfo[i-1].rise = g_itemInfo[i].rise;
g_itemInfo[i-1].numb = g_itemInfo[i].numb;
g_itemInfo[i-1].hold = g_itemInfo[i].hold;
g_itemInfo[i-1].stop = g_itemInfo[i].stop;
strcpy(g_itemInfo[i-1].code, g_itemInfo[i].code);
strcpy(g_itemInfo[i-1].name, g_itemInfo[i].name);
}
}
else
{
for(int i = n1-1; i >= n2; i--)
{
g_itemInfo[i].No = g_itemInfo[i-1].No + 1;
g_itemInfo[i].price = g_itemInfo[i-1].price;
g_itemInfo[i].rise = g_itemInfo[i-1].rise;
g_itemInfo[i].numb = g_itemInfo[i-1].numb;
g_itemInfo[i].hold = g_itemInfo[i-1].hold;
g_itemInfo[i].stop = g_itemInfo[i-1].stop;
strcpy(g_itemInfo[i].code, g_itemInfo[i-1].code);
strcpy(g_itemInfo[i].name, g_itemInfo[i-1].name);
}
}
g_itemInfo[n2-1].price = tmpInfo.price;
g_itemInfo[n2-1].rise = tmpInfo.rise;
g_itemInfo[n2-1].numb = tmpInfo.numb;
g_itemInfo[n2-1].hold = tmpInfo.hold;
g_itemInfo[n2-1].stop = tmpInfo.stop;
strcpy(g_itemInfo[n2-1].code, tmpInfo.code);
strcpy(g_itemInfo[n2-1].name, tmpInfo.name);
}
// del 1
else if (0 == strcmp(para[0], "del"))
{
int nNo = atoi(para[1]);
if (nNo < 1 || nNo > g_itemnumb) return ;
for (int i = nNo; i < g_itemnumb; i++)
{
g_itemInfo[i-1].No = g_itemInfo[i].No - 1;
g_itemInfo[i-1].price = g_itemInfo[i].price;
g_itemInfo[i-1].rise = g_itemInfo[i].rise;
g_itemInfo[i-1].numb = g_itemInfo[i].numb;
g_itemInfo[i-1].hold = g_itemInfo[i].hold;
g_itemInfo[i-1].stop = g_itemInfo[i].stop;
strcpy(g_itemInfo[i-1].code, g_itemInfo[i].code);
strcpy(g_itemInfo[i-1].name, g_itemInfo[i].name);
}
g_itemnumb--;
}
// cfg 1,1
else if (0 == strcmp(para[0], "cfg"))
{
int nNo = atoi(para[1]);
double dHold = atof(para[2]);
if (nNo < 1 || nNo > g_itemnumb) return ;
if (g_itemInfo[nNo-1].stop == 1) return;
int nNumb = g_acctInfo.asset*dHold/100/g_itemInfo[nNo-1].price;
nNumb = ((nNumb+50)/100)*100;
int nBuy = nNumb - g_itemInfo[nNo-1].numb;
int nMax = g_acctInfo.cash / g_itemInfo[nNo-1].price/100;
nMax = nMax*100;
if (nMax < nBuy) nBuy = nMax;
printf("\n buy %d, %d", nNo, nBuy);
printf("\n Y/N: ");
char szCmd[NUM128] = {0};
getCmd(szCmd, sizeof(szCmd)-1);
if (0 == strcmp(szCmd, "Y"))
{
g_itemInfo[nNo-1].numb += nBuy;
g_acctInfo.cash -= nBuy*g_itemInfo[nNo-1].price;
}
}
else if (0 == strcmp(para[0], "buy"))
{
int nNo = atoi(para[1]);
int nBuy = atoi(para[2]);
if (nNo < 1 || nNo > g_itemnumb) return ;
if (nBuy < 0)
{
nBuy = ((nBuy-50)/100)*100;
if (g_itemInfo[nNo-1].numb+nBuy < 0)
{
nBuy = -g_itemInfo[nNo-1].numb;
}
}
else
{
nBuy = ((nBuy+50)/100)*100;
int nMax = g_acctInfo.cash / g_itemInfo[nNo-1].price/100;
nMax = nMax*100;
if (nMax < nBuy) nBuy = nMax;
}
printf("\n buy %d, %d", nNo, nBuy);
printf("\n Y/N: ");
char szCmd[NUM128] = {0};
getCmd(szCmd, sizeof(szCmd)-1);
if (0 == strcmp(szCmd, "Y"))
{
g_itemInfo[nNo-1].numb += nBuy;
g_acctInfo.cash -= nBuy*g_itemInfo[nNo-1].price;
}
}
else if (0 == strcmp(para[0], "exit"))
{
exit(0);
}
}