예스스탁
예스스탁 답변
2025-09-01 13:57:53
안녕하세요
예스스탁입니다.
문법에 맞춰 변환해 드립니다.
신호가 발생하지 않으시면 수식내 조건등을 살려보셔야 합니다.
Inputs:
UseRealMBO(False), # { True: 실 Bid/Ask 체결량 함수 사용, False: 업틱/다운틱 근사 }
MBOLen(30), # { MBO 누적 길이(분) }
MBOSmooth(10),
ATR_Len(14),
ST_ATRMult(2.0), # { SuperTrend ATR 배수 }
EMA_Len(20), # { 추세평균(EMA) }
RSI_Len(14),
CCI_Len(20),
LearnRate(0.02), # { 학습률 η }
L2Decay(0.001), # { L2 정규화 λ }
ScoreThreshold(0.15),# { 관망 데드존 |score|<=thr }
MaxLots(3), # { 최대 계약수 (미니 기준) }
BaseLots(1), # { 최소 계약수 }
RiskATR_SL(2.0), # { 고정 손절: ATR x N }
RiskATR_Trail(3.0), # { 트레일링: ATR x N }
DayLossLimit_KRW(300000), # { 1일 손실한도 }
DayProfitTarget_KRW(200000),# { 1일 이익목표 }
PointVal(100000), # { 미니선물 1pt=100,000원 }
TickSize(0.05),
CostPerRt_KRW(10000),# { 왕복 비용(수수료+슬리피지) 가정값/계약 }
UseVWAPFilter(True),
UseTimeFilter(True); # { 09:15~09:45, 13:30~14:30만 진입 }
Vars:
#{ --- Intraday VWAP --- }
CumPV(0), CumV(0), VWAP(0),
#{ --- MBO (업틱-다운틱 근사 or 실체결량) --- }
UpV(0), DownV(0), AskV(0), BidV(0), MBO_1m(0), MBO_30(0), MBO_30_S(0),
#{ --- SuperTrend --- }
A(0), basicU(0), basicL(0), finalU(0), finalL(0), STdir(0),
#{ --- 기타 인디케이터 --- }
emav(0), R(0), CC(0),
#{ --- AI 가중치 (features 1..6) --- }
w1(0), w2(0), w3(0), w4(0), w5(0), w6(0),
x1(0), x2(0), x3(0), x4(0), x5(0), x6(0),
score(0), action(0), #{ action ∈ {-1,0,1} }
ret_pt(0), reward_pt(0),
#{ --- P&L/리스크 관리 --- }
DayPnL_KRW(0), LastClose(0),
Lots(0), StopPx(0), TrailPx(0),
canEnter(False), inTime(False),
#{ --- 유틸 --- }
i(0);
/* ========= 0) 일중 리셋 ========= */
If Date <> Date[1] then begin
CumPV = 0; CumV = 0; DayPnL_KRW = 0;
TrailPx = 0; StopPx = 0;
end;
/* ========= 1) VWAP 업데이트 ========= */
A = ATR(ATR_Len);
If CumV = 0 then begin
CumPV = Close * Volume;
CumV = Volume;
end else begin
CumPV = CumPV + Close * Volume;
CumV = CumV + Volume;
end;
If CumV > 0 then VWAP = CumPV / CumV; else VWAP = Close;
/* ========= 2) SuperTrend 계산 (기본형) ========= */
basicU = (High + Low) * 0.5 + ST_ATRMult * A;
basicL = (High + Low) * 0.5 - ST_ATRMult * A;
If CurrentBar == 1 then begin
finalU = basicU;
finalL = basicL;
STdir = 1;
end
else begin
finalU = minlist(basicU, IFF(Close[1] > finalU[1], basicU, finalU[1]));
finalL = maxlist(basicL, IFF(Close[1] < finalL[1], basicL, finalL[1]));
If Close > finalU[1] then STdir = 1;
else if Close < finalL[1] then STdir = -1;
else STdir = STdir[1];
If STdir > 0 then finalL = maxlist(finalL, finalL[1]);
If STdir < 0 then finalU = minlist(finalU, finalU[1]);
end;
/* ========= 3) EMA/RSI/CCI ========= */
emav = Ema(Close, EMA_Len);
R = RSI(RSI_Len);
CC = CCI(CCI_Len);
/* ========= 4) MBO (1분 단위 근사) =========
UseRealMBO = True일 때 AskV/BidV를 실 함수명으로 교체
*/
If UseRealMBO then begin
#{ 예: AskV = AskTradeVol; BidV = BidTradeVol; <-- 플랫폼 함수명으로 교체 }
AskV = 0;
BidV = 0;
MBO_1m = AskV - BidV;
end
else begin
If Close >= Close[1] then UpV = Volume; else UpV = 0;
If Close < Close[1] then DownV = Volume; else DownV = 0;
MBO_1m = UpV - DownV;
end;
MBO_30 = AccumN(MBO_1m, MBOLen);
MBO_30_S = ma(MBO_30, MBOSmooth);
/* ========= 5) 피처 벡터 x =========
안정화를 위해 ATR/Close 등으로 정규화하고, -3~+3으로 클리핑
*/
#{ x1: 모멘텀(수익률/ATR) }
x1 = (Close - Close[1]) / MaxList(A, 0.00001);
#{ x2: RSI 정규화 }
x2 = (R - 50) / 50;
#{ x3: CCI 정규화 }
x3 = CC / 100;
#{ x4: VWAP 위치 (상회=+, 하회=-) }
x4 = (Close - VWAP) / MaxList(A, 0.00001);
#{ x5: 변동성 레짐 (ATR/Close) }
x5 = (A / MaxList(Close, 0.00001));
#{ x6: 수급(MBO 30분, 스무딩) 정규화 }
x6 = MBO_30_S / MaxList(ma(Volume, MBOLen), 1);
#{ 클리핑 }
x1 = MaxList(-3, MinList(3, x1));
x2 = MaxList(-3, MinList(3, x2));
x3 = MaxList(-3, MinList(3, x3));
x4 = MaxList(-3, MinList(3, x4));
x5 = MaxList(-3, MinList(3, x5));
x6 = MaxList(-3, MinList(3, x6));
/* ========= 6) 정책 점수 score = w·x ========= */
score = w1*x1 + w2*x2 + w3*x3 + w4*x4 + w5*x5 + w6*x6;
/* ========= 7) 시간대 필터 ========= */
inTime = True;
If UseTimeFilter then begin
inTime = Time >= 095000 ;
end;
/* ========= 8) 액션 선택 (롱/숏/관망) ========= */
action = 0;
If inTime then begin
If score > ScoreThreshold then action = 1;
If score < -ScoreThreshold then action = -1;
end;
/* ========= 9) 포지션 사이징 (간단 켈리풍; 변동성 억제) ========= */
Lots = BaseLots;
If A > 0 then begin
#{ 위험회피: 변동성 높을수록 수량 축소 }
Lots = BaseLots + IFF(A/Close < 0.007, 1, 0); #{ 대략 0.7% 미만이면 +1 }
Lots = MinList(Lots, MaxLots);
end;
/* ========= 10) 1일 손익 한도 체크 ========= */
#{ 직전 바 대비 손익 업데이트 (보유 포지션 기반) }
If MarketPosition == 1 then DayPnL_KRW = DayPnL_KRW + (Close - Close[1]) * PointVal * CurrentContracts;
else if MarketPosition == -1 then DayPnL_KRW = DayPnL_KRW + (Close[1] - Close) * PointVal * CurrentContracts;
canEnter = (DayPnL_KRW > -DayLossLimit_KRW) and (DayPnL_KRW < DayProfitTarget_KRW);
/* ========= 11) 진입/청산 로직 ========= */
#{ 신호와 반대 포지션 보유 시 우선 청산 }
If MarketPosition == 1 and action <= 0 then ExitLong ("ExitLongSig", atmarket);
If MarketPosition == -1 and action >= 0 then ExitShort ("ExitShortSig", atmarket);
#{ 신규 진입 (한 방향만) }
If canEnter then begin
If MarketPosition <> 1 and action == 1 then begin
If (UseVWAPFilter == False) or (Close > VWAP) then
Buy("AI-Long",atmarket);
end;
If MarketPosition <> -1 and action == -1 then begin
If (UseVWAPFilter == False) or (Close < VWAP) then
Sell("AI-Short",atmarket);
end;
end;
/* ========= 12) 리스크 관리: ATR 손절 + 트레일링 ========= */
If MarketPosition == 1 then begin
StopPx = EntryPrice - RiskATR_SL * A;
TrailPx = MaxList(TrailPx, Close - RiskATR_Trail * A);
If Low <= StopPx then ExitLong("SL-L",atmarket);
else if Low <= TrailPx then ExitLong("TR-L",atmarket);
end;
If MarketPosition == -1 then begin
StopPx = EntryPrice + RiskATR_SL * A;
TrailPx = MinList(TrailPx, Close + RiskATR_Trail * A);
If High >= StopPx then ExitShort("SL-S",atmarket);
else if High >= TrailPx then ExitShort("TR-S",atmarket);
end;
/* ========= 13) 보상 계산(온라인 학습) =========
- 포지션 방향 × (당일 한 바 수익) [포인트 단위]
- 비용 차감(왕복비용을 바 단위로 균등 가정 - 근사)
*/
ret_pt = (Close - Close[1]); #{ 해당 바의 포인트 변화 }
If MarketPosition == 1 then reward_pt = ret_pt;
else if MarketPosition == -1 then reward_pt = -ret_pt;
else reward_pt = 0;
#{ 비용 근사: 포지션 있을 때만 소폭 차감 }
If MarketPosition <> 0 then reward_pt = reward_pt - (CostPerRt_KRW/PointVal) * 0.05;
/* ========= 14) 가중치 업데이트: w <- (1-λ)w + η * reward * x ========= */
w1 = (1 - L2Decay) * w1 + LearnRate * reward_pt * x1;
w2 = (1 - L2Decay) * w2 + LearnRate * reward_pt * x2;
w3 = (1 - L2Decay) * w3 + LearnRate * reward_pt * x3;
w4 = (1 - L2Decay) * w4 + LearnRate * reward_pt * x4;
w5 = (1 - L2Decay) * w5 + LearnRate * reward_pt * x5;
w6 = (1 - L2Decay) * w6 + LearnRate * reward_pt * x6;
즐거운 하루되세요
> 드림365 님이 쓴 글입니다.
> 제목 : 어떻게 고쳐야 할까요?
> Inputs:
UseRealMBO(False), { True: 실 Bid/Ask 체결량 함수 사용, False: 업틱/다운틱 근사 }
MBOLen(30), { MBO 누적 길이(분) }
MBOSmooth(10),
ATR_Len(14),
ST_ATRMult(2.0), { SuperTrend ATR 배수 }
EMA_Len(20), { 추세평균(EMA) }
RSI_Len(14),
CCI_Len(20),
LearnRate(0.02), { 학습률 η }
L2Decay(0.001), { L2 정규화 λ }
ScoreThreshold(0.15), { 관망 데드존 |score|<=thr }
MaxLots(3), { 최대 계약수 (미니 기준) }
BaseLots(1), { 최소 계약수 }
RiskATR_SL(2.0), { 고정 손절: ATR x N }
RiskATR_Trail(3.0), { 트레일링: ATR x N }
DayLossLimit_KRW(300000), { 1일 손실한도 }
DayProfitTarget_KRW(200000), { 1일 이익목표 }
PointValue(100000), { 미니선물 1pt=100,000원 }
TickSize(0.05),
CostPerRt_KRW(10000), { 왕복 비용(수수료+슬리피지) 가정값/계약 }
UseVWAPFilter(True),
UseTimeFilter(True); { 09:15~09:45, 13:30~14:30만 진입 }
Vars:
{ --- Intraday VWAP --- }
CumPV(0), CumV(0), VWAP(0),
{ --- MBO (업틱-다운틱 근사 or 실체결량) --- }
UpV(0), DownV(0), AskV(0), BidV(0), MBO_1m(0), MBO_30(0), MBO_30_S(0),
{ --- SuperTrend --- }
atr(0), basicU(0), basicL(0), finalU(0), finalL(0), STdir(0),
{ --- 기타 인디케이터 --- }
ema(0), rsi(0), cci(0),
{ --- AI 가중치 (features 1..6) --- }
w1(0), w2(0), w3(0), w4(0), w5(0), w6(0),
x1(0), x2(0), x3(0), x4(0), x5(0), x6(0),
score(0), action(0), { action ∈ {-1,0,1} }
ret_pt(0), reward_pt(0),
{ --- P&L/리스크 관리 --- }
DayPnL_KRW(0), LastClose(0),
Lots(0), StopPx(0), TrailPx(0),
canEnter(False), inTime(False),
{ --- 유틸 --- }
i(0);
/* ========= 0) 일중 리셋 ========= */
If Date <> Date[1] then begin
CumPV = 0; CumV = 0; DayPnL_KRW = 0;
TrailPx = 0; StopPx = 0;
end;
/* ========= 1) VWAP 업데이트 ========= */
atr = AvgTrueRange(ATR_Len);
If CumV = 0 then begin
CumPV = Close * Volume;
CumV = Volume;
end else begin
CumPV = CumPV + Close * Volume;
CumV = CumV + Volume;
end;
If CumV > 0 then VWAP = CumPV / CumV else VWAP = Close;
/* ========= 2) SuperTrend 계산 (기본형) ========= */
basicU = (High + Low) * 0.5 + ST_ATRMult * atr;
basicL = (High + Low) * 0.5 - ST_ATRMult * atr;
If CurrentBar = 1 then begin
finalU = basicU;
finalL = basicL;
STdir = 1;
end
else begin
finalU = minlist(basicU, IFF(Close[1] > finalU[1], basicU, finalU[1]));
finalL = maxlist(basicL, IFF(Close[1] < finalL[1], basicL, finalL[1]));
If Close > finalU[1] then STdir = 1
else if Close < finalL[1] then STdir = -1
else STdir = STdir[1];
If STdir > 0 then finalL = maxlist(finalL, finalL[1]);
If STdir < 0 then finalU = minlist(finalU, finalU[1]);
end;
/* ========= 3) EMA/RSI/CCI ========= */
ema = XAverage(Close, EMA_Len);
rsi = RSI(RSI_Len);
cci = CCI(CCI_Len);
/* ========= 4) MBO (1분 단위 근사) =========
UseRealMBO = True일 때 AskV/BidV를 실 함수명으로 교체
*/
If UseRealMBO then begin
{ 예: AskV = AskTradeVol; BidV = BidTradeVol; <-- 플랫폼 함수명으로 교체 }
AskV = 0;
BidV = 0;
MBO_1m = AskV - BidV;
end
else begin
If Close >= Close[1] then UpV = Volume else UpV = 0;
If Close < Close[1] then DownV = Volume else DownV = 0;
MBO_1m = UpV - DownV;
end;
MBO_30 = Summation(MBO_1m, MBOLen);
MBO_30_S = Average(MBO_30, MBOSmooth);
/* ========= 5) 피처 벡터 x =========
안정화를 위해 ATR/Close 등으로 정규화하고, -3~+3으로 클리핑
*/
{ x1: 모멘텀(수익률/ATR) }
x1 = (Close - Close[1]) / MaxList(atr, 0.00001);
{ x2: RSI 정규화 }
x2 = (rsi - 50) / 50;
{ x3: CCI 정규화 }
x3 = cci / 100;
{ x4: VWAP 위치 (상회=+, 하회=-) }
x4 = (Close - VWAP) / MaxList(atr, 0.00001);
{ x5: 변동성 레짐 (ATR/Close) }
x5 = (atr / MaxList(Close, 0.00001));
{ x6: 수급(MBO 30분, 스무딩) 정규화 }
x6 = MBO_30_S / MaxList(Average(Volume, MBOLen), 1);
{ 클리핑 }
x1 = MaxList(-3, MinList(3, x1));
x2 = MaxList(-3, MinList(3, x2));
x3 = MaxList(-3, MinList(3, x3));
x4 = MaxList(-3, MinList(3, x4));
x5 = MaxList(-3, MinList(3, x5));
x6 = MaxList(-3, MinList(3, x6));
/* ========= 6) 정책 점수 score = w·x ========= */
score = w1*x1 + w2*x2 + w3*x3 + w4*x4 + w5*x5 + w6*x6;
/* ========= 7) 시간대 필터 ========= */
inTime = True;
If UseTimeFilter then begin
inTime = (Time >= 0950 ;
end;
/* ========= 8) 액션 선택 (롱/숏/관망) ========= */
action = 0;
If inTime then begin
If score > ScoreThreshold then action = 1;
If score < -ScoreThreshold then action = -1;
end;
/* ========= 9) 포지션 사이징 (간단 켈리풍; 변동성 억제) ========= */
Lots = BaseLots;
If atr > 0 then begin
{ 위험회피: 변동성 높을수록 수량 축소 }
Lots = BaseLots + IFF(atr/Close < 0.007, 1, 0); { 대략 0.7% 미만이면 +1 }
Lots = MinList(Lots, MaxLots);
end;
/* ========= 10) 1일 손익 한도 체크 ========= */
{ 직전 바 대비 손익 업데이트 (보유 포지션 기반) }
If MarketPosition = 1 then DayPnL_KRW = DayPnL_KRW + (Close - Close[1]) * PointValue * CurrentContracts
else if MarketPosition = -1 then DayPnL_KRW = DayPnL_KRW + (Close[1] - Close) * PointValue * CurrentContracts;
canEnter = (DayPnL_KRW > -DayLossLimit_KRW) and (DayPnL_KRW < DayProfitTarget_KRW);
/* ========= 11) 진입/청산 로직 ========= */
{ 신호와 반대 포지션 보유 시 우선 청산 }
If MarketPosition = 1 and action <= 0 then Sell ("ExitLongSig") all contracts next bar at market;
If MarketPosition = -1 and action >= 0 then BuyToCover ("ExitShortSig") all contracts next bar at market;
{ 신규 진입 (한 방향만) }
If canEnter then begin
If MarketPosition <> 1 and action = 1 then begin
If (not UseVWAPFilter) or (Close > VWAP) then
Buy ("AI-Long") Lots contracts next bar at market;
end;
If MarketPosition <> -1 and action = -1 then begin
If (not UseVWAPFilter) or (Close < VWAP) then
SellShort ("AI-Short") Lots contracts next bar at market;
end;
end;
/* ========= 12) 리스크 관리: ATR 손절 + 트레일링 ========= */
If MarketPosition = 1 then begin
StopPx = EntryPrice - RiskATR_SL * atr;
TrailPx = MaxList(TrailPx, Close - RiskATR_Trail * atr);
If Low <= StopPx then Sell ("SL-L") all contracts next bar at market
else if Low <= TrailPx then Sell ("TR-L") all contracts next bar at market;
end;
If MarketPosition = -1 then begin
StopPx = EntryPrice + RiskATR_SL * atr;
TrailPx = MinList(TrailPx, Close + RiskATR_Trail * atr);
If High >= StopPx then BuyToCover ("SL-S") all contracts next bar at market
else if High >= TrailPx then BuyToCover ("TR-S") all contracts next bar at market;
end;
/* ========= 13) 보상 계산(온라인 학습) =========
- 포지션 방향 × (당일 한 바 수익) [포인트 단위]
- 비용 차감(왕복비용을 바 단위로 균등 가정 - 근사)
*/
ret_pt = (Close - Close[1]); { 해당 바의 포인트 변화 }
If MarketPosition = 1 then reward_pt = ret_pt
else if MarketPosition = -1 then reward_pt = -ret_pt
else reward_pt = 0;
{ 비용 근사: 포지션 있을 때만 소폭 차감 }
If MarketPosition <> 0 then reward_pt = reward_pt - (CostPerRt_KRW/PointValue) * 0.05;
/* ========= 14) 가중치 업데이트: w <- (1-λ)w + η * reward * x ========= */
w1 = (1 - L2Decay) * w1 + LearnRate * reward_pt * x1;
w2 = (1 - L2Decay) * w2 + LearnRate * reward_pt * x2;
w3 = (1 - L2Decay) * w3 + LearnRate * reward_pt * x3;
w4 = (1 - L2Decay) * w4 + LearnRate * reward_pt * x4;
w5 = (1 - L2Decay) * w5 + LearnRate * reward_pt * x5;
w6 = (1 - L2Decay) * w6 + LearnRate * reward_pt * x6;
예스시스템 언어로 어떻게 고쳐야 할까요?