Skip to content

Instantly share code, notes, and snippets.

@igotit-anything
Created October 26, 2024 12:20
Show Gist options
  • Save igotit-anything/4e24b79993a08f694f068fd62c520e80 to your computer and use it in GitHub Desktop.
Save igotit-anything/4e24b79993a08f694f068fd62c520e80 to your computer and use it in GitHub Desktop.
/*
CCy_AP909_TA_DB_Sqlite_1.mqh
2024.10.26
CyEA909 에서 활용되는 SQLite DB CyEA909.sqlite 핸들링.
- 종목별로 다른 정보(예 : 커미션)이면서 mql5 에서 함수 제공안되는 것들 처리위함.
*/
enum CyENUM_COMMISSION_MODE
{
emCMSN_Mode_Zero = 0 // 커미션 없음.
,emCMSN_Mode_InDeal = 1 // 포지션 오픈시에만 수수료 부과
,emCMSN_Mode_InOutDeal = 2 // 포지션 오픈 , 청산시 각각 수수료 부과.
};
enum CyENUM_COMMISSION_VALUETYPE
{
emCMSN_ValueType_NotDefined =0 // emCMSN_Mode_Zero 인 경우.
, emCMSN_ValueType_Percent_Per_TurnOverUSD =1// USD 거래대금의 퍼센트.
, emCMSN_ValueType_USD_Per_VolLot =2// 주문수량 1랏당 USD
};
class CCy_AP909_TA_DB
{
private:
string DB_file_name;
public:
CCy_AP909_TA_DB(string db_file_name);
~CCy_AP909_TA_DB();
private:
void Create_DB();
public:
void Write_Single_Commission(string symbol_meta, CyENUM_COMMISSION_MODE em_commission_mode, CyENUM_COMMISSION_VALUETYPE em_commission_valuetype, string commission_value);
void Read_Single_Commission(string symbol_meta, CyENUM_COMMISSION_MODE & em_commission_mode, CyENUM_COMMISSION_VALUETYPE & em_commission_valuetype, string & commission_value);
};
CCy_AP909_TA_DB::CCy_AP909_TA_DB(string db_file_name)
: DB_file_name(db_file_name)
{
Create_DB();
}
CCy_AP909_TA_DB::~CCy_AP909_TA_DB()
{
}
void CCy_AP909_TA_DB::Create_DB()
{
// DB 오픈, 없다면 새로 생성.
int db = DatabaseOpen(DB_file_name, DATABASE_OPEN_READWRITE | DATABASE_OPEN_CREATE);
if(db == INVALID_HANDLE) {
Print("Database connection failed. Error: ", GetLastError());
return;
}
// T_Single 테이블 생성 SQL 쿼리. 없다면 새로 생성.
string createTableQuery =
"CREATE TABLE IF NOT EXISTS T_Single ("
"SymbolMeta TEXT PRIMARY KEY, "
"Commission_Mode TEXT, " // In Deal, In/Out Deal. Zero commission.
"Commission_ValueType TEXT, " //
"Commission_Value TEXT);";
// 테이블 생성 쿼리 실행
if(DatabaseExecute(db, createTableQuery)) {
Print("Table T_Single created successfully.");
} else {
Print("Failed to create table T_Single. Error: ", GetLastError());
}
// 데이터베이스 연결 종료
DatabaseClose(db);
}
void CCy_AP909_TA_DB::Write_Single_Commission(string symbol_meta, CyENUM_COMMISSION_MODE em_commission_mode, CyENUM_COMMISSION_VALUETYPE em_commission_valuetype, string commission_value)
{
// DB 열기
int db = DatabaseOpen(DB_file_name, DATABASE_OPEN_READWRITE);
if(db == INVALID_HANDLE) {
Print("Failed to open database for writing. Error: ", GetLastError());
return;
}
// SQL 쿼리 작성 (있으면 UPDATE, 없으면 INSERT)
string query = "INSERT OR REPLACE INTO T_Single (SymbolMeta, Commission_Mode, Commission_ValueType, Commission_Value) "
"VALUES ('" + symbol_meta + "', " + IntegerToString((int)em_commission_mode) + ", " + IntegerToString((int)em_commission_valuetype) + ", '" + commission_value + "');";
// SQL 쿼리 실행
if(DatabaseExecute(db, query)) {
Print("Commission data written successfully for symbol: ", symbol_meta);
} else {
Print("Failed to write commission data. Error: ", GetLastError());
}
// DB 연결 닫기
DatabaseClose(db);
}
void CCy_AP909_TA_DB::Read_Single_Commission(string symbol_meta, CyENUM_COMMISSION_MODE &em_commission_mode, CyENUM_COMMISSION_VALUETYPE &em_commission_valuetype, string &commission_value)
{
// DB 열기
int db = DatabaseOpen(DB_file_name, DATABASE_OPEN_READONLY);
if(db == INVALID_HANDLE) {
Print("Failed to open database for reading. Error: ", GetLastError());
return;
}
// SQL 쿼리 작성
string query = "SELECT Commission_Mode, Commission_ValueType, Commission_Value FROM T_Single WHERE SymbolMeta = '" + symbol_meta + "';";
int result = DatabasePrepare(db, query);
if(result == INVALID_HANDLE) {
Print("Failed to prepare query for reading commission data. Error: ", GetLastError());
DatabaseClose(db);
return;
}
// 쿼리 실행 및 결과 읽기
if(DatabaseRead(result)) {
string cmsn_mode;
string cmsn_value_type;
DatabaseColumnText(result, 1, cmsn_mode);
em_commission_mode = (CyENUM_COMMISSION_MODE)StringToInteger(cmsn_mode);
DatabaseColumnText(result, 2, cmsn_value_type);
em_commission_valuetype = (CyENUM_COMMISSION_VALUETYPE)StringToInteger(cmsn_value_type);
DatabaseColumnText(result, 3, commission_value);
Print("Commission data read successfully for symbol: ", symbol_meta);
} else {
Print("No commission data found for symbol: ", symbol_meta);
}
// 쿼리 핸들 및 DB 연결 닫기
DatabaseFinalize(result);
DatabaseClose(db);
}
@igotit-anything
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment