ダイバージェンスストラテジー ver.1

自分へのメモ用に残しておきます🤔

RSIは14

利食い損切りは適当に設定してある

lot計算部分は上手く動作していないので改善が必要

 

//@version=4

strategy(title="RSI Divergence Indicator", overlay=false, pyramiding=1, default_qty_value=1, default_qty_type=strategy.fixed, initial_capital=30000, currency=currency.JPY)

 

//変数定義

len = input(title="RSI Period", minval=1, defval=14)

src = input(title="RSI Source", defval=close)

lbR = input(title="Pivot Lookback Right", defval=2)

lbL = input(title="Pivot Lookback Left", defval=5)

takeProfitRSILevel = input(title="Take Profit at RSI Level", minval=70, defval=75)
rangeUpper = input(title="Max of Lookback Range", defval=70)

rangeLower = input(title="Min of Lookback Range", defval=5)

plotBull = input(title="Plot Buli", defval=true)

plotHiddenBull = input(title="Plot Hidden Bull", defval=true)

plotBear = input(title="Plot Bear", defval=true)

plotHiddenBear = input(title="Plot Hidden Bear", defval=true)


//資金管理//

strategy.initial_capital → スタート時の資金 

// strategy.netprofit → 累計損益

lot = input(defval=10000) 

unit_val_pct = input(defval=0.01)

balance = strategy.initial_capital + strategy.netprofit

atr = ema (tr ,20)atr_pct = 0.5 * 100 * lot

unit = balance * unit_val_pct

amount = round(unit/ atr_pct) * lot

 

//利確・損切り

inpTakeProfit = input(defval=30, title="Take Profit", minval=0)

inpStopLoss = input(defval=10, title="Stop Loss", minval=0)

inpTrailStop = input(defval=10, title="Trailing Stop Loss", minval=0)

inpTrailOffset = input(defval=0, title="Trailing Stop Loss Offset", minval=0) 

 

useTakeProfit = inpTakeProfit >= 1 ? inpTakeProfit : na

useStopLoss = inpStopLoss >= 1 ? inpStopLoss : na

useTrailStop = inpTrailStop >= 1 ? inpTrailStop : na

useTrailOffset = inpTrailOffset >= 1 ? inpTrailOffset : na 

bearColor = color.purple

bullColor = color.green

hiddenBullColor = color.new(color.green, 80)

hiddenBearColor = color.new(color.red, 80)

textColor = color.white

noneColor = color.new(color.white, 100)


osc = rsi(src, len)


plot(osc, title="RSI", linewidth=2, color=#8D1699)

hline(50, title="Middle Line", linestyle=hline.style_dotted)

obLevel = hline(70, title="Overbought", linestyle=hline.style_dotted)

osLevel = hline(30, title="Oversold", linestyle=hline.style_dotted)

fill(obLevel, osLevel, title="Background", color=#9915FF, transp=90)

plFound = na(pivotlow(osc, lbL, lbR)) ? false : true

phFound = na(pivothigh(osc, lbL, lbR)) ? false : true


_inRange(cond) =>

    bars = barssince(cond == true)

    rangeLower <= bars and bars <= rangeUpper
//------------------------------------------------------------------------------

// Regular Bullish
// Osc: Higher Low

oscHL = osc[lbR] > valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

// Price: Lower Low

priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1)

bullCond = plotBull and priceLL and oscHL and plFound
plot(

plFound ? osc[lbR] : na,

offset=-lbR,

title="Regular Bullish",

linewidth=2,

color=(bullCond ? bullColor : noneColor), transp=0

)

plotshape(

bullCond ? osc[lbR] : na,

offset=-lbR,

title="Regular Bullish Label",

text=" Bull ",

style=shape.labelup,

location=location.absolute,

color=bullColor,

textcolor=textColor,

transp=0

)
//------------------------------------------------------------------------------

// Hidden Bullish
// Osc: Lower Low

oscLL = osc[lbR] < valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])


// Price: Higher Low

priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1)
hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound


plot(

plFound ? osc[lbR] : na,

offset=-lbR,

title="Hidden Bullish",

linewidth=2,

color=(hiddenBullCond ? hiddenBullColor : noneColor),

transp=0

)


plotshape(

hiddenBullCond ? osc[lbR] : na,

offset=-lbR,

title="Hidden Bullish Label",

text=" H Bull ",

style=shape.labelup,

location=location.absolute,

color=bullColor,

textcolor=textColor,

transp=0

)
longCondition=bullCond or hiddenBullCond

//? osc[lbR] : na

//hiddenBullCond

 

//------------------------------------------------------------------------------

// Regular Bearish
// Osc: Lower High

oscLH = osc[lbR] < valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])


// Price: Higher HighpriceHH = high[lbR] > valuewhen(phFound, high[lbR], 1)
bearCond = plotBear and priceHH and oscLH and phFound
plot(

phFound ? osc[lbR] : na,

offset=-lbR,

title="Regular Bearish",

linewidth=2,

color=(bearCond ? bearColor : noneColor),

transp=0

)
plotshape(

bearCond ? osc[lbR] : na,

offset=-lbR,

title="Regular Bearish Label",

text=" Bear ",

style=shape.labeldown,

location=location.absolute,

color=bearColor,

textcolor=textColor,

transp=0

)
//------------------------------------------------------------------------------

// Hidden Bearish
// Osc: Higher High

oscHH = osc[lbR] > valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

// Price: Lower HighpriceLH = high[lbR] < valuewhen(phFound, high[lbR], 1)
hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound
plot(

phFound ? osc[lbR] : na,

offset=-lbR,

title="Hidden Bearish",

linewidth=2,

color=(hiddenBearCond ? hiddenBearColor : noneColor),

transp=0

)
plotshape(

hiddenBearCond ? osc[lbR] : na,

offset=-lbR,

title="Hidden Bearish Label",

text=" H Bear ",

style=shape.labeldown,

location=location.absolute,

color=bearColor,

textcolor=textColor,

transp=0

)

longCloseCondition=crossover(osc,takeProfitRSILevel) or bearCond

 

//エントリー

strategy.entry(id="RSIDivLE", qty=10, long=true,when=longCondition)

strategy.exit(id="RSIDivLE", loss = useStopLoss, profit = useTakeProfit, trail_points=useTrailStop)

strategy.entry(id="RSIDivSE", qty=10, long=true, when=longCloseCondition)

strategy.exit(id="RSIDivSE", loss = useStopLoss, profit = useTakeProfit, trail_points=useTrailStop)