PSAR 및 EMA를 기반으로 한 양적 전략

PSAR EMA IGC IRC
생성 날짜: 2024-05-28 11:00:40 마지막으로 수정됨: 2024-05-28 11:00:40
복사: 7 클릭수: 681
avatar of ChaoZhang ChaoZhang
1
집중하다
1617
수행원

PSAR 및 EMA를 기반으로 한 양적 전략

개요

이 양적 전략은 주로 평행선 SAR 지표 ((PSAR) 와 지수 이동 평균 ((EMA) 의 교차 신호를 사용하여 여러 사용자 정의 조건과 결합하여 구매 및 판매 신호를 생성합니다. 전략의 주요 아이디어는: PSAR이 아래에서 EMA를 깨고 특정 조건을 충족하면 구매 신호를 생성합니다.

전략 원칙

  1. PSAR와 30주기 EMA를 계산하는 방법
  2. PSAR와 EMA의 교차 관계를 판단하고, 그에 따른 지표 지점을 설정
  3. PSAR와 EMA의 위치관계, K선 색상 등의 조건을 결합하여 IGC ((Ideal Green Candle) 와 IRC ((Ideal Red Candle) 를 정의한다.
  4. IGC와 IRC의 출현을 통해 구매 및 판매 신호를 판단합니다.
  5. 스톱과 스톱로스를 설정, 스톱은 매입 가격의 8%, 16%, 32%이며, 스톱로스는 매입 가격의 16%, 매매 가격의 8%, 16%, 32%이며, 스톱로스는 매매 가격의 16%입니다.
  6. 거래시기와 포지션 상태에 따라 구매, 판매 또는 청산 작업을 수행합니다.

전략적 이점

  1. 여러 지표와 조건을 결합하여 신호의 신뢰성을 향상시킵니다.
  2. 리스크와 수익을 유연하게 제어할 수 있는 여러 스톱 포스트와 스톱 손실 포스트를 설정합니다.
  3. 다양한 시장 상황에 맞게 구매 및 판매 필터링 조건을 설정하여 전략의 적응성을 향상시킵니다.
  4. 코드가 모듈화되어 있고, 이해하기 쉽고 수정하기 쉽습니다.

전략적 위험

  1. 전략의 매개 변수 설정은 모든 시장 환경에 적합하지 않을 수 있으며 실제 상황에 따라 조정해야합니다.
  2. 이 전략은 불안한 시장에서 거래 신호가 자주 발생하여 거래 비용이 증가할 수 있습니다.
  3. 이 전략은 시장의 추세에 대한 판단이 부족하여 강세를 보이는 시장에서 기회를 놓칠 수 있습니다.
  4. 정지금의 설정은 극단적인 상황의 위험을 완전히 피할 수 없습니다.

전략 최적화 방향

  1. 더 많은 기술 지표 또는 시장 감정 지표를 도입하여 신호의 정확성과 신뢰성을 향상시킵니다.
  2. 정지 및 정지 지점의 설정을 최적화하여 동적 정지 또는 변동율에 기반한 정지 정지를 도입하는 것을 고려할 수 있습니다.
  3. 다양한 시장 상황에 맞게 다른 거래 매개 변수와 규칙을 설정하여 전략의 적응력을 높여줍니다.
  4. 자산 관리 모듈을 추가하여 계정 주식 비율 균형과 같은 요소에 따라 위치 및 위험 노출을 동적으로 조정합니다.

요약하다

이 양적 전략은 PSAR 및 EMA 지표를 기반으로 여러 사용자 정의 조건과 규칙을 통해 구매 및 판매 신호를 생성합니다. 전략은 약간의 적응력과 유연성을 가지고 있으며, 위험을 제어하기 위해 스톱스트로드를 설정합니다. 그러나, 전략의 매개 변수 설정 및 위험 제어 측면에서 최적화 할 수있는 공간이 있습니다.

전략 소스 코드
/*backtest
start: 2024-04-01 00:00:00
end: 2024-04-30 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Binance","currency":"BTC_USDT"}]
*/

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SwapnilRaykar

//@version=5
strategy("aj sir second project", overlay=true, margin_long=100, margin_short=100)

start=input("0915-1515","session time")
st11=time(timeframe.period,start)
st=st11>0
et= not st 

psar=ta.sar(0.02,0.02,0.2)
emared=ta.ema(close,30)
//plot(psar,"psar",color.yellow,style = plot.style_cross)
//plot(emared,"emared",color.red)
var crodownflag=0
var croupflag=0

var igcflag=0

var ircflag=0

cdown1=ta.crossunder(psar,emared)  and not (psar<close and psar[1]>close[1])
cup1=ta.crossover(psar,emared) and not (psar>close and psar[1]<close[1])

cdown=ta.crossunder(psar,emared) 
cup=ta.crossover(psar,emared)


green_candle=close>open
red_candle=close<open

if ta.crossunder(psar,emared) and crodownflag==0  and not (psar<close and psar[1]>close[1])
    crodownflag:=1
else if cdown and crodownflag==1
    crodownflag:=0



if crodownflag==1 and green_candle and igcflag==0
    igcflag:=1
else if cdown and igcflag==1
    igcflag:=0

//plot(igcflag,"igcflag",color.lime)

if ta.crossover(psar,emared) and croupflag==0 and not (psar>close and psar[1]<close[1])
    croupflag:=1
else if cdown and croupflag==1
    croupflag:=0

//plot(crodownflag,"crodownflag",color.white)
irc_cond=croupflag==1 or cup

if (croupflag==1 and red_candle and ircflag==0)
    ircflag:=1
else if cup and croupflag==1
    ircflag:=0

igc_candle1=(igcflag==1 and igcflag[1]==0) or (cdown1 and green_candle)
irc_candle1=(ircflag==1 and ircflag[1]==0) or (cup1 and red_candle)
///////////////////////////
dm=dayofmonth(time)
newday=dm!=dm[1]
dmc=dm==ta.valuewhen(bar_index==last_bar_index,dm,0)

///////////////////////////////////////////
var irc_there=0

if irc_candle1[1] and irc_there==0
    irc_there:=1
else if cdown and irc_there==1
    irc_there:=0

irc_candle=irc_candle1 and irc_there==0// and dmc

var igc_there=0

if igc_candle1[1] and igc_there==0
    igc_there:=1
else if cup and igc_there ==1
    igc_there:=0

igc_candle=igc_candle1 and igc_there==0// and dmc
/////////// to get rid of irc being valid even after crossdown
var valid_igc_low=0
var valid_irc_high=0

if irc_candle[1] and valid_irc_high==0
    valid_irc_high:=1
else if igc_candle and valid_irc_high==1
    valid_irc_high:=0

if igc_candle and valid_igc_low==0
    valid_igc_low:=1
else if irc_candle and valid_igc_low==1
    valid_igc_low:=0


igc_low=ta.valuewhen(igc_candle,low,0)
irc_high=ta.valuewhen(irc_candle,high,0)
//////////////////////////////
//plot(irc_high,"irc_high",color.red)

//plot(valid_irc_high,"valid_irc_high",color.purple)

buy12=ta.crossunder(close,igc_low) and valid_igc_low==1
buy1=buy12[1]

short12=ta.crossover(close,irc_high) and valid_irc_high==1
short1=short12[1]
//plotshape(short12,"short12",shape.arrowdown,color=color.purple)

// plotshape(igc_candle,"igc_candle",shape.arrowdown,color=color.green)
// plotshape(irc_candle,"irc_candle",shape.arrowdown,color=color.red)
//plotshape((psar<close and psar[1]>close[1]) ,"croup",shape.arrowdown,color=color.red)
//plotshape(cup ,"croup",shape.arrowdown,color=color.orange)

buyprice=ta.valuewhen(buy1 and strategy.position_size[1]==0,open,0)
shortprice=ta.valuewhen(short1 and strategy.position_size[1]==0,open,0)

btarget1=buyprice+(buyprice*0.08)
btarget2=buyprice+(buyprice*0.16)
btarget3=buyprice+(buyprice*0.32)
bstoploss=buyprice-(buyprice*0.16)

starget1=shortprice-(shortprice*0.08)
starget2=shortprice-(shortprice*0.16)
starget3=shortprice-(shortprice*0.32)
sstoploss=shortprice+(shortprice*0.16)

if buy12 and strategy.position_size==0 and st11
    strategy.entry("buy",strategy.long)

if strategy.position_size >0
    strategy.exit("sell",from_entry = "buy",stop=bstoploss,limit=btarget3)

if short12 and strategy.position_size==0 and st11
    strategy.entry("short",strategy.short)

if strategy.position_size<0
    strategy.exit("cover",from_entry = "short",stop = sstoploss,limit = starget3)

if et
    strategy.close_all(comment = "timeover")

plot(strategy.position_size>0?buyprice:na,"buyprice",color.white, style=plot.style_circles )
plot(strategy.position_size>0?bstoploss:na,"bstoploss",color.red, style=plot.style_circles )
plot(strategy.position_size>0?btarget1:na,"btarget1",color.green, style=plot.style_circles )
plot(strategy.position_size>0?btarget2:na,"btarget2",color.green, style=plot.style_circles )
plot(strategy.position_size>0?btarget3:na,"btarget3",color.green, style=plot.style_circles )

plot(strategy.position_size<0?shortprice:na,"shortprice",color.white, style=plot.style_circles )
plot(strategy.position_size<0?sstoploss:na,"sstoploss",color.red, style=plot.style_circles )
plot(strategy.position_size<0?starget1:na,"starget1",color.green, style=plot.style_circles )
plot(strategy.position_size<0?starget2:na,"starget2",color.green, style=plot.style_circles )
plot(strategy.position_size<0?starget3:na,"starget3",color.green, style=plot.style_circles )