简体   繁体   中英

Code hourly Stochastic RSI strategy in pine script

I am trying to code a stochastic rsi strategy in pine script to go long when the hourly K is above D and K is below, 80 as per below but it returns almost no trades when I backtested it unless I change from just the < to <= which tells me something is not right. Can someone help please.

kH = security(syminfo.tickerid, "60", sma(stoch(close, high, low, stochLength), 3))
dH = sma(kH, 3)

stochConditionH = kH < dH and kH < 80

To obtain that condition correctly it has to be done within the context of the security call. Otherwise you aren't going to obtain the correct calculation for the one hour timeframe. Let's say your chart is the 15 minute chart.

dh = sma(kH, 3)

Will give you the average of kH over the last 3 15 minute bars. If you need to perform operations on the 60 minute timeframe, you need to wrap everything in function and pass that to the security call.

f_stochConditionH(_close, _high, _low, _len) =>
    _kH = sma(stoch(_close, _high, _low, _len), 3)
    _dH = sma(_kH, 3)
    _stochConditionH = _kH < _dH and _kH < 80
    _stochConditionH


stochConditionH = security(syminfo.tickerid, "60", f_stochConditionH(close, high, low, stochLength))

This allows the operation to be done correctly on the one hour timeframe. So now _dh = sma(_kH,3) inside the function is now the average of the last three one hour values of _kH when used within the security call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM