커뮤니티

챗 GPT로 짠 코드인데 한번만 체크 부탁드립니다.

프로필 이미지
한걸음씩
2025-11-27 14:22:48
13
글번호 228457

// ============================================= // DailyPL_Logger : 계좌 일별 손익 로그 전략 // - Account1 전체 평가금액 기준 // - 일별 손익 <= MaxLossToday 이면 로그 1회 기록 // ============================================= // ---- 전역 변수 ---- var StartEquity = 0; // 오늘 시작 기준 계좌 평가금액 var StartDate = 0; // YYYYMMDD 형태 정수 var AlertLogged = false; // 오늘 경고 로그 남겼는지 여부 // ---- 외부변수 기본값 (외부변수 안 쓰면 이 값 사용) ---- // MaxLossToday : 오늘 허용 손실 한도 (음수 값) // LogFileName : 로그 파일 이름 또는 전체 경로 if (typeof MaxLossToday === "undefined") { MaxLossToday = -3000000; // 예: -3,000,000원 } if (typeof LogFileName === "undefined") { // 파일명만 쓰면 Spot\\Export 폴더에 생성됨 (예스스팟 매뉴얼 설명) LogFileName = "DailyLossAlert.log"; } // ---- 날짜 정수(YYYYMMDD) 만들기 ---- function getTodayInt() { var now = new Date(); var y = now.getFullYear(); var m = now.getMonth() + 1; var d = now.getDate(); return y * 10000 + m * 100 + d; } // ---- 로그에 찍을 타임스탬프 문자열 만들기 ---- function getTimestampString() { var now = new Date(); var y = now.getFullYear(); var m = now.getMonth() + 1; var d = now.getDate(); var hh = now.getHours(); var mm = now.getMinutes(); var ss = now.getSeconds(); function pad(n) { return (n < 10 ? "0" : "") + n; } return y + "-" + pad(m) + "-" + pad(d) + " " + pad(hh) + ":" + pad(mm) + ":" + pad(ss); } // ---- 하루 시작 기준값 초기화 ---- function initDailyEquity() { // Account1 전체 잔고 평가금액 합 (0,0 = 전체/전체) // 예스스팟 매뉴얼: Account.GetTotalAmount(nCategory, nTradeKind) :contentReference[oaicite:1]{index=1} StartEquity = Account1.GetTotalAmount(0, 0); StartDate = getTodayInt(); AlertLogged = false; Main.MessageLog("DailyPL_Logger init : StartEquity=" + StartEquity + ", StartDate=" + StartDate); } // ---- 예스스팟 시작 이벤트 ---- function OnStart() { // 하루 기준값 세팅 initDailyEquity(); // 1초(1000ms)마다 OnTimer(1) 호출 Main.SetTimer(1, 1000); } // ---- 타이머 이벤트 ---- function OnTimer(nEventID) { if (nEventID != 1) { return; } // 날짜 바뀌었으면 새로 하루 시작으로 보고 기준값 재설정 var today = getTodayInt(); if (today != StartDate) { initDailyEquity(); } // 현재 계좌 전체 평가금액 var currentEquity = Account1.GetTotalAmount(0, 0); var dailyPL = currentEquity - StartEquity; // 조건: 일별 손익 <= MaxLossToday 이고, 아직 로그 안 남겼을 때 if (!AlertLogged && dailyPL <= MaxLossToday) { var msg = getTimestampString() + " DailyPL=" + dailyPL + " (StartEquity=" + StartEquity + ", CurrentEquity=" + currentEquity + ")"; // 파일에 한 줄 출력 // 예스스팟 문서: Main.PrintOnFile(파일, 메시지...) :contentReference[oaicite:2]{index=2} Main.PrintOnFile(LogFileName, msg); // 디버깅창에도 출력 (테스트용) Main.MessageLog("DailyPL_Logger ALERT : " + msg); AlertLogged = true; // 오늘은 한 번만 알림 } }

답변 0