owned this note
owned this note
Published
Linked with GitHub
## 程式設計期末 - 股票推薦聊天機器人
mission: 串接telegram API
```cpp=
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <sstream>
#define N 31
using namespace std;
class Stock
{
private:
int stockID;
int industry; //會有四種情境:傳產為0,科技為1,泛公股金融為2,民營銀行為3
bool term; //會有兩種可能,短期為0,長期為1
int price; //會有三種價格區段,低價組為0,中價位為1,高價為2
bool risk; //會有兩種風險,低風險為0,高風險為1
int growth; //價值型為0,成長型為1,兩種符合為2
string content;
public:
Stock() {}
Stock(int each_stockID, int each_industry, bool each_term, int each_price, bool each_risk, int each_growth, string each_content)
{
stockID = each_stockID;
industry = each_industry; //會有四種情境:傳產為0,科技為1,泛公股金融為2,民營銀行為3
term = each_term; //會有兩種可能,短期為0,長期為1
price = each_price; //會有三種價格區段,低價組為0,中價位為1,高價為2
risk = each_risk; //會有三種風險,低風險為0,高風險為1
growth = each_growth; //價值型為0,成長型為1,兩種符合為2
content = each_content;
}
~Stock();
int check(int *suggest_metric, bool *preferredIndustry)
{
if (suggest_metric[0] != term)
{ //[0] != 1 就選長期
return false;
}
if (suggest_metric[1] != price)
{
return false;
}
if (suggest_metric[2] < 5 && risk == 1)
{
return false;
}
if (suggest_metric[3] != growth)
{
return false;
}
if (preferredIndustry[industry] == false)
{
return false;
}
if (suggest_metric[3] != growth)
{
return false;
}
return stockID;
}
string get_content()
{
return content;
}
};
string Suggestion(int *suggest_metric, bool *preferredIndustry, Stock **stockList)
{
string result = "";
int select[N] = {0};
int cnt = 0;
for (int i = 0; i < N; i++)
{
int output = stockList[i]->check(suggest_metric, preferredIndustry);
if (output)
{
select[cnt++] = i;
}
}
if (cnt == 0) {
return "目前沒有適合您的股票請您重新查詢";
}
if (cnt == 1) {
result += stockList[select[0]]->get_content() + "\n";
return result;
}
if (cnt == 2) {
result += stockList[select[0]]->get_content() + "\n";
result += stockList[select[1]]->get_content() + "\n";
return result;
}
int idx = rand() % cnt;
result += stockList[select[idx]]->get_content() + "\n";
int tmp = 0;
while (tmp == idx) tmp = rand() % cnt;
result += stockList[select[tmp]]->get_content() + "\n";
return result;
}
int main(int argc, char const *argv[])
{
srand(time(0));
int choice = 0;
int suggest_metric[4] = {0}; // 4種分數:可投資期間/可承受價格/可承受風險/成長型或價值型,後面根據這個分數看要設定哪個區間得什麼股票
bool preferredIndustry[4] = {0}; //分三種產業,第一個為傳產,第二個為科技,第三個是泛公股金融,第四個是民營銀行,若不分產業就標定四者;若不分金融股就標定[2][3]
bool terminate = false;
typedef Stock *stock; //再補股票數
stock stockList[N];
int cnt = 0;
fstream input_file("stockList.txt");
int stock_cnt = N;
while (stock_cnt--)
{
int each_stockID, each_industry, each_term, each_price, each_risk, each_growth;
string each_content;
string tmp;
getline(input_file, tmp);
stringstream ss(tmp);
ss >> each_stockID >> each_industry >> each_term >> each_price >> each_risk >> each_growth;
ss >> each_content;
// cout << each_content << endl;;
stockList[cnt] = new Stock(each_stockID, each_industry, each_term, each_price, each_risk, each_growth, each_content);
++cnt;
}
// for (int i = 0; i < N; i++) {
// cout << stockList[i]->stockID << " " << stockList[i]->industry << endl;
// }
// Age
cout << "請選擇年齡:(請輸入號碼)" << endl;
cout << "1. 20以下"
<< "\n"
<< "2. 20-29"
<< "\n"
<< "3. 30-39"
<< "\n"
<< "4. 40-49"
<< "\n"
<< "5. 50-59"
<< "\n"
<< "6. 60以上" << endl;
cin >> choice;
if (choice == 1)
{
terminate = true;
}
//
if (terminate)
{
cout << "小朋友快回家";
std::exit(1);
}
else
{
// 行業:
cout << "-> 請選擇台股50中,所偏好行業:(請輸入號碼)" << endl; //只會有幾個行業而已,可以給三個選項就好
cout << "1. 傳統產業"
<< "\n"
<< "2. 科技產業"
<< "\n"
<< "3. 金融業"
<< "\n"
<< "4. 不分產業" << endl;
cin >> choice; //若選擇特定產業,只會推薦該產業股票;不分產業就隨機推薦。至多兩支。
if (choice == 1 || choice == 2)
{
preferredIndustry[choice - 1] = true;
}
else if (choice == 4)
{
preferredIndustry[0] = true;
preferredIndustry[1] = true;
preferredIndustry[2] = true;
preferredIndustry[3] = true;
}
else
{
cout << "-> 請選擇偏好哪一種金融股:(請輸入號碼)" << endl; //只會有幾個行業而已,可以給三個選項就好
cout << "1. 泛公股"
<< "\n"
<< "2. 民營銀行"
<< "\n"
<< "3. 沒有類別偏好" << endl;
cin >> choice;
if (choice < 3)
{
preferredIndustry[choice + 1] = true;
}
else
{
preferredIndustry[2] = true;
preferredIndustry[3] = true;
}
}
cout << "\n";
// 長短期:
int termPts = 0;
cout << "-> 通常您儲蓄時,會選擇哪一種定存:(請輸入號碼) " << endl;
cout << "1. 三年內無法提領的定存,年報酬率2%"
<< "\n"
<< "2. 十年內無法提領的定存,年報酬率5%"
<< "\n"
<< "3. 我都不要!我要活存!" << endl;
cin >> choice;
if (choice == 2)
{
termPts += 1;
}
cout << "\n";
cout << "\n"
<< "您期望的投資期間?(請輸入號碼)" << endl;
cout << "1. 1年以內"
<< "\n"
<< "2. 1-3年"
<< "\n"
<< "3. 3-10年"
<< "\n"
<< "4. 10年以上 " << endl;
cin >> choice;
if (choice >= 3)
{
termPts++;
}
cout << "\n";
suggest_metric[0] = termPts; // 算完可投資期間分數後存到suggest_metric,越大代表越長期
// 要推薦的股價範圍:
int pricePts = 0;
// 年收
cout << "請問目前的年收入?(請輸入號碼)" << endl;
cout << "1. 50萬以下"
<< "\n"
<< "2. 50-100萬"
<< "\n"
<< "3. 100萬以上" << endl;
cin >> choice;
cout << "\n";
// 詢問要投入多少
cout << "您目前預計投入多少資金?(請輸入號碼)" << endl;
cout << "1. 100萬以下"
<< "\n"
<< "2. 100-200萬"
<< "\n"
<< "3. 200-300萬"
<< "\n"
<< "4. 300萬以上" << endl;
cin >> choice;
if (choice == 2 || choice == 3)
{
pricePts += 1;
}
else if (choice == 4)
{
pricePts += 2;
}
cout << "\n";
suggest_metric[1] = pricePts; // 算完可投資金額分數後存到suggest_metric,越大代表越可以推薦高價股
// 風險承受判斷
int riskPts = 0; // higher means higher risk tolerance
cout << "您所希望一年的投資報酬率,以及相對應的損失風險?(請輸入號碼)" << endl;
cout << "1. 低於5%"
<< "\n"
<< "2. 5-10%"
<< "\n"
<< "3. 10-20"
<< "\n"
<< "4. 20-30"
<< "\n"
<< "5. 30以上" << endl;
cin >> choice; // 希望一年的投資報酬率,以及相對應的損失風險,前兩個選項0分,中兩個1分,最後一個2分
if (choice == 3 || choice == 4)
{
riskPts += 1;
}
else if (choice == 5)
{
riskPts += 2;
}
cout << "\n";
cout << "風險和報酬間您會如何取捨?(請輸入號碼)" << endl;
cout << "1. 風險考量的比重高於報酬"
<< "\n"
<< "2. 風險和報酬考量的比重一樣"
<< "\n"
<< "3. 報酬考量的比重高於風險" << endl;
cin >> choice;
riskPts += (choice - 1); //最高加2分
cout << "\n";
cout << "若投資100萬,發生突發事件的一個禮拜內,您可以容忍的損失金額為何?(請輸入號碼)" << endl;
cout << "1. 5萬以下"
<< "\n"
<< "2. 5~15萬"
<< "\n"
<< "3. 15~25萬"
<< "\n"
<< "4. 25~40萬"
<< "\n"
<< "5. 40萬以上" << endl;
cin >> choice;
riskPts += (choice - 1); //最高加4分
cout << "\n";
cout << "請問目前有無須償還之貸款?(請輸入號碼)" << endl;
cout << "1. 無"
<< "\n"
<< "2. 每月1萬以下"
<< "\n"
<< "3. 每月1~3萬"
<< "\n"
<< "4. 每月3~5萬"
<< "\n"
<< "5. 每月5萬以上" << endl;
cin >> choice;
riskPts += (choice - 1); //最高加4分
cout << "\n";
suggest_metric[2] = riskPts; // 算完可承擔風險分數後存到suggest_metric,越大代表越能承受風險
// 用 suggest_metric跟preferredIndustry,可以給下一個階段做歸類哪種股票
cout << "您比較偏好下列哪種選擇?" << endl;
cout << "1. 價值股:價值被低估但風險較低的公司"
<< "\n"
<< "2. 成長股:市場評估有潛力,但尚未開始獲利的新創公司"
<< "\n"
<< "3. 期望同時符合價值股跟成長股" << endl;
cin >> choice;
suggest_metric[3] = (choice - 1); //若選1,[3]是0,選價值型;選2,[3]是1,選成長型;選3,[3]是2,給符合兩種指標的;不符合成長股或價值股的就丟掉
cout << "\n";
cout << Suggestion(suggest_metric, preferredIndustry, stockList);
return 0;
}
}
```
文字檔
```
1101 0 0 0 0 2 台泥:2019年前三季合併稅後淨利177億元、成長11%,EPS3.24元、再創新高
1102 0 0 0 0 2 亞泥:法人估,今年兩岸水泥市場都好,亞泥全年獲利至少會有160億元以上,EPS可達5元
1216 0 0 1 0 1 鴻海:本益比9.49倍,股價淨值比1.02倍,EPS連續4季較去年同期成長。
1326 0 0 1 0 0 台化:台化副董事長洪福源表示,11月營收為239.35億元,年減28.7%,減幅為台塑四寶最高,月減3.8%,不過最主要仍是受到美中貿易紛爭不確定因素及大陸新產能投產
2105 0 0 0 0 1 正新:1.當季EPS創歷史新低2.當季EPS創8季新低。"
2207 0 0 2 1 1 和泰車:EPS連續3季成長、當季EPS創歷史新高。
2633 0 0 0 0 1 台灣高鐵:現金股利殖利率2.9%,低於52%的公司,負債比83.88%
2912 0 1 2 0 1 統一超:1.毛利率連續2季較去年同期成長。根據統計:這類股票未來90天股價平均漲幅比其他股票多2.22%2.當季毛利率是近6季新高。根據統計:這類股票未來90天股價平均漲幅比其他股票多2.35%3.負債比減少1.08%。根據統計:這類股票未來90天股價平均漲幅比其他股票多1.56%4.大股東持股比例增加"
9904 0 0 0 0 2 寶成:連3季EPS較去年同期成長,大股東持比增加,但當季營業亦用率創8季新低,且Q3獲利主要來自業外。NIKE鞋類開出佳績,供應鏈寶成業績看俏
9910 0 1 2 0 1 豐泰:當季EPS創歷史新高,連續4季EPS較去年同期成'長。NIKE鞋類開出佳績,供應鏈豐泰業績看俏
2317 1 1 1 0 1 鴻海:本益比9.49倍,股價淨值比1.02倍,EPS連續4季較去年同期成長。
2330 1 1 2 0 1 台積電:第四季創下股價新高,外資持續看好
2357 1 0 2 0 2 華碩:營收年增近8%,連續五個月營收較去年同期成長,手機和筆電事業初步顯示轉型成功
2395 1 1 2 0 1 研華:營收創新高,可望突破500億元,且近5年營收年複合成長率達9.7%,毛利率及營益率長期維持在40%、15%,ROE則保持在20%以上,營收成長率及獲利結構遠優於同業,本益比將維持在20_30倍。
2474 1 0 2 0 1 可成:自iPhone6以來,受惠執行力優異,市占不斷擴大,然而這項趨勢將從2020年新款iPhone開始反轉,原因在於iPhone機殼毛利率降低,與可成經營團隊強調的改善獲利策略相牴觸
3008 1 1 2 0 1 大立光:看好多鏡頭趨勢,大立光匯百億元資金回台擴充產能
3045 1 1 2 0 1 台灣大:EPS連續兩季成長,連續五年高股利
3711 1 0 1 0 2 日月光:EPS連續兩季成長,大股東持股比例增加,與子公司日月暘電子股份有限公司通過簡易合併案
4938 1 0 1 0 2 和碩:董監持股比例上升,EPS連續兩季成長,不過2019Q1獲利多來自業外,且五年內有三年現金流為負。預計貧果明年出IPHONE新機,和碩可受惠
5880 2 0 0 0 1 合庫金(泛公股銀行):EPS連續3季成長,金管會核准合庫銀申設美國境外休士頓分行
2892 2 0 0 0 1 第一金(泛公股銀行):EPS與營業利益率連續三季成長,且連四季比去年同期高,雖負債比高,股價成長有限,仍可持續獲利;經綜合評估,股利發放、殖利率與年增率表現良好,適合作為成長型投資標的
2823 3 0 0 1 2 中壽:EPS與營業利益率連續三季成長,且連兩季比去年同期高,雖負債比高,股價成長有限,仍可持續獲利
2881 3 1 0 0 2 富邦金:EPS與營業利益率連續三季成長,且EPS連兩季比去年同期高,十月每股純利高於其他金融股
2882 3 1 0 1 0 國泰金:獲利能力好,EPS連三季成長
2883 3 0 0 0 1 開發金:EPS連三季成長,近日股價下跌
2884 3 0 0 0 1 玉山金:EPS與營業利益率連四季較去年同期成長,適合偏好短期成長者投資
2885 3 0 0 1 1 元大金:近期積極推動金融科技,EPS與營業利益率連續三季成長,且連兩季比去年同期高,適合偏好短期成長者投資
2887 3 1 0 0 1 台新金:台新金積極拓展海內外事業,10月自結稅後純益12.63億元,月增約33%,累計前十月稅後純益131.80億元,年增10%,每股純益1.09元
2891 3 1 0 0 1 中信金:營業利益率連續三季成長,且連兩季比去年同期高,但現金流為負,較適合長期持有
5871 3 0 2 1 1 中租-KY:重要子公司MyLeasing(Mauritius)Corp.董事會決議辦理現金增資美金9,000仟元,須注意子公司有無營運危機"
5876 3 1 1 1 2 上海商銀:金管會擬鬆綁法令,上海商銀樂漲
```
之後增進方向:
* 嫁接API
* 優化UI設計