커뮤니티

일봉전략을 5분봉전략으로의 변환 코드 검토 부탁 드립니다. (수정)

프로필 이미지
zapster
2025-10-16 11:18:39.0
172
글번호 226942
답변완료

얼마 전 올렸던 질문(글번호: 194570) 관련 추가질문입니다.


아래와 같이 코드를 변경했는데, 이게 맞나 싶네요... 결과가 너무 달라요.


그리고, 날짜변경시 Daily~로 시작되는 변수들을 업데이트 하도록 했는데,


한국투자증권 나스닥 선물 시간상 한국자정에 날짜가 바뀌는 거 같은데


미국시장 기준으로 바꾸도록 하려면 어떻게 하나요?


검토 좀 부탁 드립니다.


늘 감사 드립니다.


============= 원본코드 =============


// --- 원본 전략 로직 시작 (문법 수정 적용) ---

PcntChange = Close / DayClose(PChng) * 100;

If PcntChange < 100 - OverRange then Begin OS = TRUE; HighBand = Highest( High , Bands ); End;

If PcntChange > 100 + OverRange then Begin OB = TRUE; LowBand = Lowest( Low, Bands); End;

if EntriesToday(sDate) < 1 and NoContract > 0 and TradeAllowed then {

    If OS and MarketPosition <> 1 Then Buy("B", AtStop, HighBand, NoContract);

    If OB and MarketPosition <> -1 then Sell("S", AtStop, LowBand, NoContract);

}

If marketposition == 1 and C > entryprice + profit then ExitLong("LX_Profit", AtMarket);

if marketposition == -1 and C < entryprice - profit then ExitShort("SX_Profit", AtMarket);

if MRO( OS == false, MRO_Length, MRO_Threshold ) == -1 then OS = false;

if MRO( OB == false, MRO_Length, MRO_Threshold ) == -1 then OB = false;

ExitLong("EL_Trail", AtStop, lowest(low,exitperiod));

ExitShort("ES_Trail", AtStop, highest(high,exitperiod));

SetStopLoss(stopPer, PercentStop);

// --- 원본 전략 로직 종료 ---






============= 수정코드 =============


Array:

DailyReturns[100](0),

DailyOpenArray[100](0),

DailyHighArray[100](0),

DailyLowArray[100](0),

DailyCloseArray[100](0);

// ================== 로직 시작 ==================

// 1. 5분봉을 일봉으로 집계

CurrentDate = sDate;

if Index == 0 then begin

PrevDate = CurrentDate;

DailyOpen = Open;

DailyHigh = High;

DailyLow = Low;

DailyBarCount = 0;

end

// 날짜가 바뀌었을 때 (새로운 거래일 시작)

if CurrentDate != PrevDate then begin

// 전일 일봉 데이터를 배열에 저장

For Value1 = 99 DownTo 1 begin

DailyOpenArray[Value1] = DailyOpenArray[Value1-1];

DailyHighArray[Value1] = DailyHighArray[Value1-1];

DailyLowArray[Value1] = DailyLowArray[Value1-1];

DailyCloseArray[Value1] = DailyCloseArray[Value1-1];

end;

DailyOpenArray[0] = PrevDailyOpen;

DailyHighArray[0] = PrevDailyHigh;

DailyLowArray[0] = PrevDailyLow;

DailyCloseArray[0] = PrevDailyClose;

// 새로운 일봉 시작

DailyOpen = Open;

DailyHigh = High;

DailyLow = Low;

DailyBarCount = DailyBarCount + 1;

PrevDate = CurrentDate;

end

// 같은 날 내에서 고가/저가 업데이트

if High > DailyHigh then DailyHigh = High;

if Low < DailyLow then DailyLow = Low;

DailyClose = Close;

// 이전 일봉 데이터 저장

PrevDailyOpen = DailyOpen;

PrevDailyHigh = DailyHigh;

PrevDailyLow = DailyLow;

PrevDailyClose = DailyClose;

.......... 생략 ..........

if CurrentDate != PrevDate and DailyBarCount > 1 then begin

if DailyOpenArray[1] > 0 then

DailyReturn = (DailyOpenArray[0] - DailyOpenArray[1]) / DailyOpenArray[1]

else

DailyReturn = 0;

For Value1 = VaR_Period - 1 DownTo 1 begin

DailyReturns[Value1] = DailyReturns[Value1-1];

end;

DailyReturns[0] = DailyReturn;

end

.......... 생략 ..........

// 5. 원본 전략 로직 (일봉 기준 계산)

if DailyBarCount >= Pchng and DailyCloseArray[Pchng-1] > 0 then

PcntChange = DailyClose / DailyCloseArray[Pchng-1] * 100

else

PcntChange = 100;

If PcntChange < 100 - OverRange then begin

OS = true;

// 최근 Bands개 일봉의 최고가

HighBand = DailyHighArray[0];

For Value1 = 1 to Bands-1 begin

if DailyHighArray[Value1] > HighBand then

HighBand = DailyHighArray[Value1];

end;

end

else

OS = false;

If PcntChange > 100 + OverRange then begin

OB = true;

// 최근 Bands개 일봉의 최저가

LowBand = DailyLowArray[0];

For Value1 = 1 to Bands-1 begin

if DailyLowArray[Value1] < LowBand then

LowBand = DailyLowArray[Value1];

end;

end

else

OB = false;

// 6. 주문 실행 (5분봉에서 즉시 실행)

if EntriesToday(sDate) < 1 and NoContract > 0 and TradeAllowed then begin

If OS and MarketPosition <> 1 then

Buy("B", AtStop, HighBand, NoContract);

If OB and MarketPosition <> -1 then

Sell("S", AtStop, LowBand, NoContract);

end

If MarketPosition == 1 and C > EntryPrice + profit then

ExitLong("LX_Profit", AtMarket);

if MarketPosition == -1 and C < EntryPrice - profit then

ExitShort("SX_Profit", AtMarket);

if MRO(OS == false, MRO_Length, MRO_Threshold) == -1 then OS = false;

if MRO(OB == false, MRO_Length, MRO_Threshold) == -1 then OB = false;

// exitperiod도 일봉 기준으로 계산

Var: ExitLowBand(99999), ExitHighBand(0);

ExitLowBand = DailyLowArray[0];

ExitHighBand = DailyHighArray[0];

For Value1 = 1 to exitperiod-1 begin

if DailyLowArray[Value1] < ExitLowBand then

ExitLowBand = DailyLowArray[Value1];

if DailyHighArray[Value1] > ExitHighBand then

ExitHighBand = DailyHighArray[Value1];

end;

ExitLong("EL_Trail", AtStop, ExitLowBand);

ExitShort("ES_Trail", AtStop, ExitHighBand);

SetStopLoss(stopPer, PercentStop);


시스템
답변 1
프로필 이미지

예스스탁 예스스탁 답변

2025-10-16 17:37:18.0

안녕하세요
예스스탁입니다. atstop과 atlimit이 봉완성시 값을 셋팅하고 다음봉 현재가와 비교해 신호가 발생합니다. 기존식이 일봉에 적용되는 내용이므로 분봉에서 동작하게 하시려면 모든 if조건은 전일 마지막봉에서 일봉상값으로 조건을 체크하게 하셔야 합니다. 분봉상 당일 마지막봉은 if NextBarBdate != Bdate 조건으로 체크하시면 됩니다. 아래식 참고하시기 바랍니다.

input : PChng(1),OverRange(1),Bands(20),NoContract(1),TradeAllowed(true); input : profit(1),exitperiod(10),stopPer(5),MRO_Length(20),MRO_Threshold(-1); var : PcntChange(0); var : HighBand(0),lowBand(0),i(0); var : HH(0),LL(0),OScnt(0),OSMRO(0),OBcnt(0),OBMRO(0); array : OS[50](False),OB[50](False); if NextBarBdate != Bdate Then { PcntChange = Close / DayClose(PChng) * 100; for i = 49 DownTo 1 { OS[i] = OS[i-1]; OB[i] = OB[i-1]; } If PcntChange < 100 - OverRange then { OS[0] = TRUE; HighBand = 0; For i = 0 to Bands-1 { if HighBand == 0 or (HighBand > 0 and DayHigh(i) > HighBand) Then HighBand = DayHigh(i); } } If PcntChange > 100 + OverRange then { OB[0] = TRUE; LowBand = 0; For i = 0 to Bands-1 { if LowBand == 0 or (LowBand > 0 and DayLow(i) < LowBand) Then LowBand = DayLow(i); } } } if NoContract > 0 and TradeAllowed then { If OS[0] == true and MarketPosition <> 1 and DayHigh < HighBand Then Buy("B", AtStop, HighBand, NoContract); If OB[0] == true and MarketPosition <> -1 and DayLow > lowBand then Sell("S", AtStop, LowBand, NoContract); } If marketposition == 1 and C > entryprice + profit then ExitLong("LX_Profit", AtMarket); if marketposition == -1 and C < entryprice - profit then ExitShort("SX_Profit", AtMarket); if NextBarBdate != Bdate[1] Then { OScnt = 0; OSMRO = -1; OBcnt = 0; OBMRO = -1; For i = 0 to MRO_Length-1 { if OS[i] == False Then { OScnt = OScnt+1; if OScnt == MRO_Threshold Then OSMRO = i; } if OB[i] == False Then { OBcnt = OBcnt+1; if OBcnt == MRO_Threshold Then OBMRO = i; } } if OSMRO == -1 then OS[0] = false; if OBMRO == -1 then OB[0] = false; } HH = 0; LL = 0; For i = 0 to exitperiod-1 { if HH == 0 or (HH > 0 and DayHigh(i) > HH) Then HH = DayHigh(i); if LL == 0 or (LL > 0 and DayLow(i) < LL) Then LL = DayLow(i); } if MarketPosition == 1 Then ExitLong("EL_Trail", AtStop, LL); if MarketPosition == -1 Then ExitShort("ES_Trail", AtStop, HH);
SetStopLoss(stopPer, PercentStop);


즐거운 하루되세요