답변완료
추세선 상하대칭
input : Period(250);
Var:j(0),T(0);
Var: date11(0),date12(0),time11(0),time12(0),
date21(0),date22(0),time21(0),time22(0),
date31(0),date32(0),time31(0),time32(0);
Array:HiVal[20](0),LoVal[20](0),HiBar[20](0),LoBar[20](0);
var : btx1(0),btx2(0),btx11(0),btx12(0),btx21(0),btx22(0);
Plot1(value12);
For j = 0 To 19
{
HiBar[j] = HiBar[j] + 1;
LoBar[j] = LoBar[j] + 1;
}
if crossup(c,highest(H,Period)[1]) Then
T = 1;
if CrossDown(c,Lowest(L,Period)[1]) Then
T = -1;
If T == -1 Then
{
If T[1] != -1 Then
{
For j = 18 DownTo 0
{
LoVal[j+1] = LoVal[j];
LoBar[j+1] = LoBar[j];
}
LoVal[0] = L;
LoBar[0] = 0;
date11 = date[HiBar[0]];
time11 = stime[HiBar[0]];
Value11 = HiVal[0];
date12 = date[LoBar[0]];
time12 = stime[LoBar[0]];
Value12 = LoVal[0];
date21 = date[HiBar[0]];
time21 = stime[HiBar[0]];
date22 = date[0];
time22 = stime[0];
if hival[0] > 0 and Loval[0] > 0 Then
{
btx11 = Text_New(date12,time12,Value11+abs(Hival[0]-loval[0])*1.618,"■");
btx12 = Text_New(date12,time12,Value11+abs(Hival[0]-loval[0])*0.618,"■");
btx1 = Text_New(date12,time12,Value11+abs(Hival[0]-loval[0]),"■");
btx2 = Text_New(date12,time12,Value12-abs(Hival[0]-loval[0]),"■");
btx21 = Text_New(date12,time12,Value12-abs(Hival[0]-loval[0])*1.618,"■");
btx22 = Text_New(date12,time12,Value12-abs(Hival[0]-loval[0])*0.618,"■");
Text_SetStyle(btx11,2,2);
Text_SetStyle(btx12,2,2);
Text_SetStyle(btx1,2,2);
Text_SetStyle(btx2,2,2);
Text_SetStyle(btx21,2,2);
Text_SetStyle(btx22,2,2);
Text_SetColor(btx11,Lime);
Text_SetColor(btx12,Lime);
Text_SetColor(btx1,Blue);
Text_SetColor(btx2,Blue);
Text_SetColor(btx21,Lime);
Text_SetColor(btx22,Lime);
}
}
If LoVal[0] > L Then
{
LoVal[0] = L;
LoBar[0] = 0;
date12 = date[LoBar[0]];
time12 = stime[LoBar[0]];
Value12 = LoVal[0];
date22 = date[0];
time22 = stime[0];
Text_SetLocation(btx11, date12,time12,Value11+abs(Hival[0]-loval[0])*1.618);
Text_SetLocation(btx12, date12,time12,Value11+abs(Hival[0]-loval[0])*0.618);
Text_SetLocation(btx1, date12,time12,Value11+abs(Hival[0]-loval[0]));
Text_SetLocation(btx2, date12,time12,Value12-abs(Hival[0]-loval[0]));
Text_SetLocation(btx21, date12,time12,Value12-abs(Hival[0]-loval[0])*1.618);
Text_SetLocation(btx22, date12,time12,Value12-abs(Hival[0]-loval[0])*0.618);
}
}
If T == 1 Then
{
If T[1] != 1 Then
{
For j = 18 DownTo 0
{
HiVal[j+1] = HiVal[j];
HiBar[j+1] = HiBar[j];
}
HiVal[0] = H;
HiBar[0] = 0;
date11 = date[LoBar[0]];
time11 = stime[LoBar[0]];
Value11 = LoVal[0];
date12 = date[HiBar[0]];
time12 = stime[HiBar[0]];
Value12 = HiVal[0];
date31 = date[LoBar[0]];
time31 = stime[LoBar[0]];
date32 = date[0];
time32 = stime[0];
if hival[0] > 0 and Loval[0] > 0 Then
{
btx11 = Text_New(date12,time12,Value12+abs(Hival[0]-loval[0])*1.618,"■");
btx12 = Text_New(date12,time12,Value12+abs(Hival[0]-loval[0])*0.618,"■");
btx1 = Text_New(date12,time12,Value12+abs(Hival[0]-loval[0]),"■");
btx2 = Text_New(date12,time12,Value11-abs(Hival[0]-loval[0]),"■");
btx21 = Text_New(date12,time12,Value11-abs(Hival[0]-loval[0])*1.618,"■");
btx22 = Text_New(date12,time12,Value11-abs(Hival[0]-loval[0])*0.618,"■");
Text_SetStyle(btx11,2,2);
Text_SetStyle(btx12,2,2);
Text_SetStyle(btx1,2,2);
Text_SetStyle(btx2,2,2);
Text_SetStyle(btx21,2,2);
Text_SetStyle(btx22,2,2);
Text_SetColor(btx11,Lime);
Text_SetColor(btx12,Lime);
Text_SetColor(btx1,Red);
Text_SetColor(btx2,Red);
Text_SetColor(btx21,Lime);
Text_SetColor(btx22,Lime);
}
}
If HiVal[0] < H Then
{
HiVal[0] = H;
HiBar[0] = 0;
date12 = date[HiBar[0]];
time12 = stime[HiBar[0]];
Value12 = HiVal[0];
date32 = date[0];
time32 = stime[0];
Text_SetLocation(btx11, date12,time12,Value12+abs(Hival[0]-loval[0])*1.618);
Text_SetLocation(btx12, date12,time12,Value12+abs(Hival[0]-loval[0])*0.618);
Text_SetLocation(btx1, date12,time12,Value12+abs(Hival[0]-loval[0]));
Text_SetLocation(btx2, date12,time12,Value11-abs(Hival[0]-loval[0]));
Text_SetLocation(btx21, date12,time12,Value11-abs(Hival[0]-loval[0])*1.618);
Text_SetLocation(btx22, date12,time12,Value11-abs(Hival[0]-loval[0])*0.618);
}
}
네모 표시를, 추세선의 크기가 작으면 무시하고 1P 이상일 때만 표시되게 수정 부탁드립니다.
2023-06-11
1656
글번호 169658
지표
답변완료
TS이지랭귀지를 예스랭귀지로 변환하고 싶습니다.
예스랭귀지로 변환해서 거래하고 싶습니다. 양이 좀 많아서 죄송합니다.
잘부탁드립니다.
1. 기울기관련수식
{ period = length of exponential trend
filter = whole percent trend change to give signal
lag = enter after a lag of "lag"
}
input: period(20), filter(0), lag(0);
vars: signal(0);
{ Linear regression slope }
signal = LRSsignal(period,filter,lag);
if signal = 1 then Buy This Bar on close;
if signal = -1 then Sell Short This Bar on close;
2. 모멘텀 관련 수식
{ period = length of calculation }
input: period(20), usefutures(false), printfile(true);
vars: nhigh(0), nlow(0), adate(" "),
totalPL(0), dailyPL(0), psignal(0), ATR(0), ATRperiod(20), size(0),
futuresinvestment(25000), stockinvestment(10000);;
if usefutures then begin
size = futuresinvestment/(avgtruerange(ATRperiod)*bigpointvalue);
end
else size = stockinvestment/close[1];
{ 매수, 매도 수식 }
if close > close[period] then begin
buy to cover all contracts this bar on close;
Buy size contracts This Bar on close;
end;
if close < close[period] then begin
sell all contracts this bar on close;
Sell Short size contracts This Bar on close;
end;
totalpl = netprofit + openpositionprofit;
dailyPL = psignal*currentshares*(close - close[1]);
psignal = marketposition;
end;
3. 변동성 시스템
inputs: period(20), volfactor(2.0), usefutures(false);
vars: ATR(0), change(0), sizeper(20), size(0),
stockinvestment(10000), futuresinvestment(25000);
ATR = avgtruerange(period);
change = close - close[1];
// size for new positions
if usefutures then begin
size = futuresinvestment/(avgtruerange(sizeper)*bigpointvalue);
end
else begin size = stockinvestment/close;
end;
if marketposition <= 0 and change > volfactor*ATR[1] then
buy size contracts next bar on open
else if marketposition >= 0 and change < -volfactor*ATR[1] then
sell short size contracts next bar on open;
4. 볼린저밴드관련 수식
inputs: period(20), factor(2.0);
vars: alpha(0), mt(0), ut(0), dt(0), mt2(0), ut2(0), dt2(0), but(0), blt(0);
{ smoothing constant }
alpha = 2/(period + 1);
mt = alpha*close + (1 - alpha)*mt;
ut = alpha*mt + (1 - alpha)*ut;
dt = ((2 - alpha)*mt - ut)/(1 - alpha);
mt2 = alpha*absvalue(close - dt) + (1 - alpha)*mt2;
ut2 = alpha*mt2 + (1 - alpha)*ut2;
dt2 = ((2 - alpha)*mt2 - ut2)/(1 - alpha);
but = dt + factor*dt2;
blt = dt - factor*dt2;
종가가 볼린저밴드 상단 돌파시 매수, 종가가 볼린저밴드 하단 돌파시 매도
5.선형회귀 밴드
inputs: period(20), bandwidth(2.0);
vars: slope(0), lrvalue(0), resid(0), sdresid(0), angle(0),
intercept(0), upperband(0), lowerband(0), size(0), investment(25000);
Value1 = LinearReg( close, period, currentbar, slope, angle, intercept,
lrvalue ) ;
{ slope = LinearRegSlope(close,period);
lrvalue = linearRegValue(close,period,currentbar);}
resid = Close - lrvalue;
If Currentbar > period then begin
{ position size }
size = investment / (avgtruerange(30)*bigpointvalue);
size = maxlist(size,1);
sdresid = stddev(close,period);
Upperband = lrvalue + slope + bandwidth*sdresid;
Lowerband = lrvalue + slope - bandwidth*sdresid;
{ plot1(upperband,"UB");
Plot2(lowerband,"LB");
} plot3(lrvalue,"LR");
{ If Close > upperband[1] then buy size contracts this bar on close
Else if Close < lowerband[1] then sell short size contracts this bar on close; }
end;
6. 스윙인덱스 관련 수식
Inputs for TSM SWING:
type = 0, normal price
= 1, 3-month rate
= 2, bond price at par
swing = price swing in whole % (e.g., 2% = 2.0) }
input: type(0), swing(2.5), usefutures(false), printfile(true);
vars: pcswing(0), lastswing(0), curhigh(0), curlow(0), swhigh(0), swlow(0) , highbar(0),
lowbar(0), chighbar(0), clowbar(0), lowp(0), highp(0), xclose(0), xhigh(0),
xlow(0), divide(4), par(800), prevhigh(0), prevlow(0), adate(" "),
totalPL(0), dailyPL(0), psignal(0), ATR(0), ATRperiod(20), size(0),
futuresinvestment(25000), stockinvestment(10000);
pcswing = swing / 100.;
if usefutures then begin
size = futuresinvestment/(avgtruerange(ATRperiod)*bigpointvalue);
end
else size = stockinvestment/close[1];
if type = 0 then begin
xhigh = high;
xlow = low;
xclose = close;
end
else if type = 1 then begin
xhigh = 100 - low;
xlow = 100 - high;
xclose = 100 - close;
end
else if type = 2 then begin
xhigh = par / low;
xlow = par / high;
xclose = par / close;
end;
{ INITIALIZE MOST RECENT HIGH AND LOW }
if currentbar = 1 then begin
curhigh = xclose; { current high }
curlow = xclose; { current low }
end;
{ Buy or sell signals }
if type = 0 then begin
if marketposition <= 0 and xhigh > swhigh then begin
Buy to cover all contracts this bar on close;
Buy size contracts This Bar on close;
end
else if marketposition >= 0 and xlow < swlow then begin
sell all contracts this bar on close;
Sell Short size contracts This Bar on close;
end;
end
else begin
if marketposition >= 0 and xhigh > swhigh then begin
sell all contracts this bar on close;
Sell Short size contracts This Bar on close;
end
else if marketposition <= 0 and xlow < swlow then begin
Buy to cover all contracts This Bar on close;
buy size contracts this bar on close;
end;
end;
{ SEARCH FOR A NEW HIGH -- favor reversals }
if lastswing<>1 then begin
{ REVERSE FROM HIGH IF MINIMUM % SWING }
if xlow < curhigh - curhigh*pcswing then begin
lastswing = 1; { lastswing high fixed }
swhigh = curhigh; { new verified high }
highbar = chighbar;
curlow = xlow; { initialize new lows }
lowp = xlow; { swing low for plot }
clowbar = currentbar;
end
else begin
if xhigh > curhigh then begin
curhigh = xhigh; { new current high }
chighbar = currentbar;
end;
end;
end;
{ SEARCH FOR A NEW LOW - favor reversal }
if lastswing <> -1 then begin
{ REVERSAL FROM LOW IF MINIMUM % SWING }
if xhigh > curlow + curlow*pcswing then begin
lastswing = -1;
swlow = curlow;
lowbar = clowbar;
curhigh = xhigh; { initialize current high }
highp = xhigh; { swing high for plot }
chighbar = currentbar;
end
else begin
if xlow < curlow then begin
curlow = xlow;
clowbar = currentbar;
end;
end;
end;
totalpl = netprofit + openpositionprofit;
dailyPL = psignal*currentshares*(close - close[1]);
psignal = marketposition;
end;
7. 표준편차 이동평균 관련 수식
inputs: period(40);
vars: SD(0), SDV(0), stdavg(0);
SD = stddev(close,period);
SDV = (SD - SD[1])/SD;
StdAvg = Average(close,period/2) + .05*SDV;
plot1(stdavg,"StdAvg");
8. 추세추종 시스템 관련 수식
{ period = length of calculaton
entryoption = 0 buy/sell on current close
= 1 buy/sell on next open
= 2 buy/sell next close
crossoption = 0 using trendline,
= 1 use price crossing trendline
longonly true then only long positions
high-vol filter
}
input: usemovingaverage(true), usebreakout(false), useslope(false), useexponential(false), period(80),
entryoption(0), crossoption(0), longonly(false), exitvol(0), reentervol(0), stoploss(0),
usestocks(true),usefutures(false),variablesize(true), reinvest(false), futuresinvestment(25000),
stockinvestment(10000), printfiles(false);
vars: signal(0), price(0), size(1), investment(1000), slope(0), ATRper(20), smooth(1.0),
atrange(0), MA(0), adate(" "), equity(0), PL(0), PLlong(0), PLshort(0), exposure(0),
expsmooth(0), tradeentry(0), tradeentrybar(0), tradeprice(0),dailyreturn(0), NAV(100),
volper(20), returns(0), vol(0), exitvolOK(true), reentervolOK(true), exitonstop(0),
bestprice(0);
If Currentbar = 1 then begin
if usestocks then begin
investment = stockinvestment;
if variablesize = false then size = 100;
end
else begin
investment = futuresinvestment;
if variablesize = false then size = 1;
end;
if useexponential then smooth = 2/(period + 1);
end;
if variablesize then begin
if usefutures then begin
atrange = avgtruerange(ATRper);
if atrange = 0 then
size = 1
else
if atrange <> 0 then size = investment/(atrange*bigpointvalue);
end;
if usestocks then begin
if close[1] <> 0 then size = investment/close[1];
end;
end;
if usemovingaverage then begin
MA = average(close,period);
if crossoption = 0 then begin
if MA > MA[1] then signal = 1
else if MA < MA[1] then signal = -1;
end
else if crossoption = 1 then begin
if close >= MA then signal = 1
else signal = -1;
end;
end
else if usebreakout then begin
if high > highest(high[1],period) then signal = 1
else if low < lowest(low[1],period) then signal = -1;
end
else if useslope then begin
slope = linearregslope(close,period);
if slope > 0 then signal = 1
else if slope < 0 then signal = -1;
end
else if useexponential then begin
if currentbar = 1 then
expsmooth = close
else begin
expsmooth = expsmooth + smooth*(close - expsmooth);
end;
if expsmooth > expsmooth[1] then signal = 1
else if expsmooth < expsmooth[1] then signal = -1;
end;
// reset exit on stop
if exitonstop <> 0 then begin
if exitonstop > 0 and signal < 0 then exitonstop = 0
else if exitonstop < 0 and signal > 0 then exitonstop = 0;
end;
// exit percentage trailing stop-loss
if stoploss <> 0 and marketposition <> 0 then begin
// return on trade as of close
// PL = marketposition*currentcontracts*(close - entryprice);
if marketposition > 0 then begin
// bestprice = maxlist(bestprice,high);
// if close/bestprice - 1 < -stoploss then begin
if close/entryprice - 1 < -stoploss then begin
sell ("XLstop") all contracts next bar on open;
exitonstop = marketposition;
end;
end
else if marketposition < 0 then begin
bestprice = minlist(bestprice,low);
if bestprice/close - 1 < -stoploss then begin
buy to cover ("XSstop") all contracts next bar on open;
exitonstop = marketposition;
end;
end;
end;
// exit volatility filter
if exitvol <> 0 then begin
returns = close/close[1] - 1;
vol = stddev(returns,volper)*squareroot(252.);
if reentervol <> 0 and vol < reentervol then exitvolOK = true;
if vol > exitvol then exitvolOK = false;
// exit on volatility filter
if marketposition <> 0 and exitvolOK = false then begin
if marketposition > 0 then sell ("XLvol") all contracts next bar on open
else if marketposition < 0 then buy to cover ("XSvol") all contracts next bar on open;
reentervolOK = false;
end;
end;
if marketposition = 0 and reentervolOK = false and vol < reentervol
then reentervolOK = true;
{ entry and entry options, always reverse position }
If entryoption = 0 and exitvolOK and exitonstop = 0 then begin
if signal = 1 and marketposition <> 1 then begin
if usemovingaverage then Buy ("BMA") size contracts this bar on close
else if usebreakout then Buy ("BBO") size contracts this bar on close
else if useslope then buy ("BLRS") size contracts this bar on close
else if useexponential then buy ("BEXP") size contracts this bar on close;
// adate = ELdatetostring(tradeentry);
// print(file("c:₩tradestation₩Trend_trade_length.csv"), adate, ",", currentbar - tradeentrybar:5:0,
// ",", size*(close - tradeprice):8:2);
tradeentry = date;
tradeentrybar = currentbar;
tradeprice = close;
bestprice = close;
end
else if signal = -1 and marketposition <> -1 then begin
if longonly then begin
if usemovingaverage then sell ("XMA") all contracts this bar on close
else if usebreakout then sell ("XBO") all contracts this bar on close
else if useslope then sell ("XLRS") all contracts this bar on close
else if useexponential then sell ("XEXP") all contracts this bar on close;
// adate = ELdatetostring(tradeentry);
// print(file("c:₩tradestation₩Trend_trade_length.csv"), adate, ",", currentbar - tradeentrybar:5:0,
// ",", size*(close - tradeprice):8:2);
tradeentry = date;
tradeentrybar = currentbar;
tradeprice = close;
bestprice = close;
end
else begin
if usemovingaverage then sell short ("SMA") size contracts this bar on close
else if usebreakout then sell short ("SBO") size contracts this bar on close
else if useslope then sell short ("SLRS") size contracts this bar on close
else if useexponential then sell short ("SEXP") size contracts this bar on close;
// adate = ELdatetostring(tradeentry);
// print(file("c:₩tradestation₩Trend_trade_length.csv"), adate, ",", currentbar - tradeentrybar:5:0,
// ",", size*(close - tradeprice):8:2);
tradeentry = date;
tradeentrybar = currentbar;
tradeprice = close;
bestprice = close;
end;
end;
end
else if entryoption = 1 and exitvolOK then begin
If signal = 1 and marketposition <> 1 then buy size contracts next bar on open
Else if signal = -1 and marketposition <> -1 then begin
if longonly then sell all contracts next bar on open
else sell short size contracts next bar on open;
end;
end
else if entryoption = 2 and exitvolOK then begin
If signal[1] = 1 and marketposition <> 1 then buy size contracts this bar on close
Else if signal[1] = -1 and marketposition <> -1 then begin
if longonly then sell all contracts next bar on open
else sell short size contracts next bar on open;
end;
end;
if lastbaronchart then
if marketposition > 0 then sell all contracts this bar on close
else if marketposition < 0 then buy to cover all contracts this bar on close;
equity = netprofit + openpositionprofit;
PL = equity - equity[1];
dailyreturn = 0;
if investment <> 0 then dailyreturn = PL/investment;
NAV = NAV*(1 + dailyreturn);
if marketposition > 0 then
PLlong = PLlong + PL
else if marketposition < 0 then
PLshort = PLshort + PL;
if reinvest then investment = stockinvestment + equity;
If printfiles then begin
adate = ELdatetostring(date);
// if currentbar = 1 then begin
// print(file("c:₩tradestation₩Trend_trade_length.csv"), "Enter,Days,PL");
// end;
// if usemovingaverage then begin
If Currentbar = 1 then begin
print(file("c:₩tradestation₩Trend_PL.csv"),"Date,TotalPL");
print(file("c:₩tradestation₩Trend_Detail.csv"),
"Date,Open,High,Low,Close,MA,size,marketposition,PLtoday,PLlong,",
"PLshort,TotalPL,Return,NAV");
end;
print(file("c:₩tradestation₩Trend_PL.csv"),adate, ",", PL:8:4);
print(file("c:₩tradestation₩Trend_Detail.csv"),adate, ",", open:8:4, ",", high:8:4, ",",
low:8:4, ",", close:8:4, ",", MA:8:4, ",", currentcontracts:6:2, ",",
marketposition:6:2, ",", PL:8:4, ",", PLlong:8:4, ",",
PLshort:8:4, ",", equity:8:4, ",", dailyreturn:6:5, ",", NAV:10:3);
// end
// else if usebreakout then begin
// If Currentbar = 1 then print(file("c:₩tradestation₩BO_PL.csv"), "Date,size,netPL,PLlong,PLshort,PLtoday");
// print(file("c:₩tradestation₩BO_PL.csv"),adate, ",", size:8:3, ",", equity:8:4, ",", PLlong:8:4, ",",
// PLshort:8:4, ",", PL:8:4);
// end
// else if useslope then begin
// If Currentbar = 1 then print(file("c:₩tradestation₩LRS_PL.csv"), "Date,size,netPL,PLlong,PLshort,PLtoday");
// print(file("c:₩tradestation₩LRS_PL.csv"),adate, ",", size:8:3, ",", equity:8:4, ",", PLlong:8:4, ",",
// PLshort:8:4, ",", PL:8:4);
// end;
end;
9.표준편차 청산수식
{ inputs: length = average true range calculation period
(normally 30 for intraday, 20 for daily)
factor = standard deviation value for stop }
inputs: length(20), factor1(2.06), factor2(3.2);
vars: up1(0), up2(0), dn1(0), dn2(0);
up1 = highest(high,length) - TSMStdevStop(length, factor1);
up2 = highest(high, length) - TSMStdevStop(length, factor2);
dn1 = lowest(low, length) + TSMStdevStop(length, factor1);
dn2 = lowest(low, length) + TSMStdevStop(length, factor2);
plot1(up1,"TSMup1");
plot2(up2,"TSMup2");
plot3(dn1,"TSMdn1");
plot4(dn2,"TSMdn2");
10. 효율비 관련 수식
input: period(20);
vars: change(0), noise(0), diff(0), ratio(0), signal(0), sumratio(0), avgratio(0);
ratio = 0;
diff = AbsValue(close - close[1]);
if currentbar > period then begin
change = close - close[period];
signal = AbsValue(change);
noise = summation(diff,period);
ratio = 0;
if noise <> 0 then ratio = signal/noise;
sumratio = sumratio + ratio;
end;
if lastbaronchart then begin
avgratio = sumratio/(currentbar - period);
2023-06-10
1478
글번호 169656
시스템