sirolf2009

ANN Strategy v2

7653 51 935
This is version 2 of my ANN strategy. This version will not repaint, but requires some settings configuration.
The reason why the old version repainted, is because it used the daily OHLC/4 which kept changing, causing lower timeframes ro repaint.
You can now specify which timeframes to use. The bigger timeframe is set in the settings (e.g. 1 day). You set the smaller settings by opening a certain chart (e.g. a 4h chart).
The timeframes are used to calculate the percentual difference, which is fed to the ANN as input

Link to version 1:
ANN Strategy
Remove from Favorite Scripts Add to Favorite Scripts
//@version=2
strategy("ANN Strategy v2")

threshold = input(title="Threshold", type=float, defval=0.0000, step=0.0001)
timeframe = input(title="Timeframe", type=resolution, defval='1D' )

getDiff() =>
    yesterday=security(tickerid, timeframe, ohlc4[1])
    today=ohlc4
    delta=today-yesterday
    percentage=delta/yesterday

PineActivationFunctionLinear(v) => v
PineActivationFunctionTanh(v) => (exp(v) - exp(-v))/(exp(v) + exp(-v))


l0_0 = PineActivationFunctionLinear(getDiff())

l1_0 = PineActivationFunctionTanh(l0_0*0.8446488687)
l1_1 = PineActivationFunctionTanh(l0_0*-0.5674069006)
l1_2 = PineActivationFunctionTanh(l0_0*0.8676766445)
l1_3 = PineActivationFunctionTanh(l0_0*0.5200611473)
l1_4 = PineActivationFunctionTanh(l0_0*-0.2215499554)

l2_0 = PineActivationFunctionTanh(l1_0*0.3341657935 + l1_1*-2.0060003664 + l1_2*0.8606354375 + l1_3*0.9184846912 + l1_4*-0.8531172267)
l2_1 = PineActivationFunctionTanh(l1_0*-0.0394076437 + l1_1*-0.4720374911 + l1_2*0.2900968524 + l1_3*1.0653326022 + l1_4*0.3000188806)
l2_2 = PineActivationFunctionTanh(l1_0*-0.559307785 + l1_1*-0.9353655177 + l1_2*1.2133832962 + l1_3*0.1952686024 + l1_4*0.8552068166)
l2_3 = PineActivationFunctionTanh(l1_0*-0.4293220754 + l1_1*0.8484259409 + l1_2*-0.7154087313 + l1_3*0.1102971055 + l1_4*0.2279392724)
l2_4 = PineActivationFunctionTanh(l1_0*0.9111779155 + l1_1*0.2801691115 + l1_2*0.0039982713 + l1_3*-0.5648257117 + l1_4*0.3281705155)
l2_5 = PineActivationFunctionTanh(l1_0*-0.2963954503 + l1_1*0.4046532178 + l1_2*0.2460580977 + l1_3*0.6608675819 + l1_4*-0.8732022547)
l2_6 = PineActivationFunctionTanh(l1_0*0.8810811932 + l1_1*0.6903706878 + l1_2*-0.5953059103 + l1_3*-0.3084040686 + l1_4*-0.4038498853)
l2_7 = PineActivationFunctionTanh(l1_0*-0.5687101164 + l1_1*0.2736758588 + l1_2*-0.2217360382 + l1_3*0.8742950972 + l1_4*0.2997583987)
l2_8 = PineActivationFunctionTanh(l1_0*0.0708459913 + l1_1*0.8221730616 + l1_2*-0.7213265567 + l1_3*-0.3810462836 + l1_4*0.0503867753)
l2_9 = PineActivationFunctionTanh(l1_0*0.4880140595 + l1_1*0.9466627196 + l1_2*1.0163097961 + l1_3*-0.9500386514 + l1_4*-0.6341709382)
l2_10 = PineActivationFunctionTanh(l1_0*1.3402207103 + l1_1*0.0013395288 + l1_2*3.4813009133 + l1_3*-0.8636814677 + l1_4*41.3171047132)
l2_11 = PineActivationFunctionTanh(l1_0*1.2388217292 + l1_1*-0.6520886912 + l1_2*0.3508321737 + l1_3*0.6640560714 + l1_4*1.5936220597)
l2_12 = PineActivationFunctionTanh(l1_0*-0.1800525171 + l1_1*-0.2620989752 + l1_2*0.056675277 + l1_3*-0.5045395315 + l1_4*0.2732553554)
l2_13 = PineActivationFunctionTanh(l1_0*-0.7776331454 + l1_1*0.1895231137 + l1_2*0.5384918862 + l1_3*0.093711904 + l1_4*-0.3725627758)
l2_14 = PineActivationFunctionTanh(l1_0*-0.3181583022 + l1_1*0.2467979854 + l1_2*0.4341718676 + l1_3*-0.7277619935 + l1_4*0.1799381758)
l2_15 = PineActivationFunctionTanh(l1_0*-0.5558227731 + l1_1*0.3666152536 + l1_2*0.1538243225 + l1_3*-0.8915928174 + l1_4*-0.7659355684)
l2_16 = PineActivationFunctionTanh(l1_0*0.6111516061 + l1_1*-0.5459495224 + l1_2*-0.5724238425 + l1_3*-0.8553500765 + l1_4*-0.8696190472)
l2_17 = PineActivationFunctionTanh(l1_0*0.6843667454 + l1_1*0.408652181 + l1_2*-0.8830470112 + l1_3*-0.8602324935 + l1_4*0.1135462621)
l2_18 = PineActivationFunctionTanh(l1_0*-0.1569048216 + l1_1*-1.4643247888 + l1_2*0.5557152813 + l1_3*1.0482791924 + l1_4*1.4523116833)
l2_19 = PineActivationFunctionTanh(l1_0*0.5207514017 + l1_1*-0.2734444192 + l1_2*-0.3328660936 + l1_3*-0.7941515963 + l1_4*-0.3536051491)
l2_20 = PineActivationFunctionTanh(l1_0*-0.4097807954 + l1_1*0.3198619826 + l1_2*0.461681627 + l1_3*-0.1135575498 + l1_4*0.7103339851)
l2_21 = PineActivationFunctionTanh(l1_0*-0.8725014237 + l1_1*-1.0312091401 + l1_2*0.2267643037 + l1_3*-0.6814258121 + l1_4*0.7524828703)
l2_22 = PineActivationFunctionTanh(l1_0*-0.3986855003 + l1_1*0.4962556631 + l1_2*-0.7330224516 + l1_3*0.7355772164 + l1_4*0.3180141739)
l2_23 = PineActivationFunctionTanh(l1_0*-1.083080442 + l1_1*1.8752543187 + l1_2*0.3623326265 + l1_3*-0.348145191 + l1_4*0.1977935038)
l2_24 = PineActivationFunctionTanh(l1_0*-0.0291290625 + l1_1*0.0612906199 + l1_2*0.1219696687 + l1_3*-1.0273685429 + l1_4*0.0872219768)
l2_25 = PineActivationFunctionTanh(l1_0*0.931791094 + l1_1*-0.313753684 + l1_2*-0.3028724837 + l1_3*0.7387076712 + l1_4*0.3806140391)
l2_26 = PineActivationFunctionTanh(l1_0*0.2630619402 + l1_1*-1.9827996702 + l1_2*-0.7741413496 + l1_3*0.1262957444 + l1_4*0.2248777886)
l2_27 = PineActivationFunctionTanh(l1_0*-0.2666322362 + l1_1*-1.124654664 + l1_2*0.7288282621 + l1_3*-0.1384289204 + l1_4*0.2395966188)
l2_28 = PineActivationFunctionTanh(l1_0*0.6611845175 + l1_1*0.0466048937 + l1_2*-0.1980999993 + l1_3*0.8152350927 + l1_4*0.0032723211)
l2_29 = PineActivationFunctionTanh(l1_0*-0.3150344751 + l1_1*0.1391754608 + l1_2*0.5462816249 + l1_3*-0.7952302364 + l1_4*-0.7520712378)
l2_30 = PineActivationFunctionTanh(l1_0*-0.0576916066 + l1_1*0.3678415302 + l1_2*0.6802537378 + l1_3*1.1437036331 + l1_4*-0.8637405666)
l2_31 = PineActivationFunctionTanh(l1_0*0.7016273068 + l1_1*0.3978601709 + l1_2*0.3157049654 + l1_3*-0.2528455662 + l1_4*-0.8614146703)
l2_32 = PineActivationFunctionTanh(l1_0*1.1741126834 + l1_1*-1.4046408959 + l1_2*1.2914477803 + l1_3*0.9904052964 + l1_4*-0.6980155826)

l3_0 = PineActivationFunctionTanh(l2_0*-0.1366382003 + l2_1*0.8161960822 + l2_2*-0.9458773183 + l2_3*0.4692969576 + l2_4*0.0126710629 + l2_5*-0.0403001012 + l2_6*-0.0116244898 + l2_7*-0.4874816289 + l2_8*-0.6392241448 + l2_9*-0.410338398 + l2_10*-0.1181027081 + l2_11*0.1075562037 + l2_12*-0.5948728252 + l2_13*0.5593677345 + l2_14*-0.3642935247 + l2_15*-0.2867603217 + l2_16*0.142250271 + l2_17*-0.0535698019 + l2_18*-0.034007685 + l2_19*-0.3594532426 + l2_20*0.2551095195 + l2_21*0.4214344983 + l2_22*0.8941621336 + l2_23*0.6283377368 + l2_24*-0.7138020667 + l2_25*-0.1426738249 + l2_26*0.172671223 + l2_27*0.0714824385 + l2_28*-0.3268182144 + l2_29*-0.0078989755 + l2_30*-0.2032828145 + l2_31*-0.0260631534 + l2_32*0.4918037012)

buying = l3_0 > 0 ? true : l3_0 < -0 ? false : buying[1]

hline(0, title="base line")
//bgcolor(l3_0 > 0.0014 ? green : l3_0 < -0.0014 ? red : gray, transp=20)
bgcolor(buying ? green : red, transp=20)
plot(l3_0, color=silver, style=area, transp=75)
plot(l3_0, color=aqua, title="prediction")

longCondition = buying
if (longCondition)
    strategy.entry("Long", strategy.long)

shortCondition = buying != true
if (shortCondition)
    strategy.entry("Short", strategy.short)
Looks fantastic!!!!!!!!!!!!!!!!!!!!. Thanks for sharing!!!! This is really a great effort, appreciate it. Will check tomorrow when market is on.
Reply
UrbanNiche
a year ago
Thanks for this looking forward to testing!! :)
Reply
TASAVANT
a year ago
TYwill test it
Reply
UrbanNiche
a year ago
Not sure if I'm missing something but when I change the threshold it's not updating?
Reply
On smaller time frame of 10 min it gives error "Order limit of 2000 was reached" can you increase this order limit?
+5 Reply
pcmourao
a year ago
I was using the first version with SPX500, EURUSD, USOIL ... I was finding higher profitability rates on Version 1 than I am on 2 .... why is that? V2 Gives me under 40% profitability while version 1 gives me over 80% .... doesn't make sense to use V2 in this case, right?
+6 Reply
metmet
a year ago
just tested it in realtime.
it does repaint!!!
i think you should work on ohlc, only ohlc cause repaint.
Reply
metmet metmet
a year ago
"today=ohlc4"
Reply
metmet metmet
a year ago
today=ohlc4(a bar ago, im not able to write square brackets)
Reply
sirolf2009 PRO metmet
a year ago
Only open would not repaint, but that would be misusing the network as it was not trained to do this.

I tested this real quick and netto profit went down from 2653$ to 1426$
Reply
metmet sirolf2009
a year ago
i use it with 15' and 1' calculated on the last COMPLETE bar.
i love this code.
thank you a lot.
(point=square bracket)

yesterday=security(tickerid, timeframe, ohlc4.1.)
today=ohlc4.1.
Reply
metmet metmet
a year ago
in this way no repaint at all
Reply
sirolf2009 PRO metmet
a year ago
You may want to change the first ohlc4 to ohlc4.2.
What you're doing now is calculating the difference between yesterday and yesterday. Which is and always will be 0, rendering the indicator useless
Reply
sirolf2009 PRO sirolf2009
a year ago
oh no wait, nevermind I somehow forgot how to read code :)
This is interesting though, it took the netto profit from 2653$ to 4553$
Reply
metmet sirolf2009
a year ago
it's the difference between 15' and 1'
:)
Reply
UrbanNiche metmet
a year ago
I've changed the code but what timeframe is your chart and what inputs do you have in the setting?
Reply
cristian.d metmet
a year ago
Can you repost the code with the new addition that does not repaint? Thanks a lot
+1 Reply
Speran49 PRO metmet
8 months ago
What do you mean 15' and 1' Where? 15 minute and 1 minute timeframe?
Reply
olmodelcaz sirolf2009
a year ago
Sorry sirolf2009, just a question: Could you make a strategy from an existent indicator in here? Would you be able to add in it a buy/sell/stop/profit condition?
Reply
thimo.blom sirolf2009
13 days ago
@sirolf2009, Can you post the new code? That would be awesome! :)
Reply
cjszbd metmet
4 months ago
Could you post the script that does not repaint?
Reply
pcmourao
a year ago
Hello Sirolf2009. Great coding on version 1 of this strategy and the repaint doesn't really bother as this doesn't seem to be a strategy for day trading but works really well on the 4H timeframe, which is more my timeframe for trades. I do have one thing I wanted to ask and that is what is the base for the strategy.... I mean how does it calculate the BUY and SELL signals, what conditions have to be met? I guess the question is if this strategy wasn't coded, what are the conditions that have to be met to generate the signals....almost like if I was looking for the signals "manually" instead of what he script does.
Reply
fxcandle pcmourao
a year ago
This is a question i also want to learn ?
Reply
sirolf2009 PRO fxcandle
a year ago
This is not an easy question, if you look at the source, you'll see a ton of random number in the center. These numbers are all pulled through some functions, then they're added up together and then they're pulled through some functions again. If you were to calculate it by hand, you'll probably spend around 10 minutes calculating the final value. From there it's easy, if the final value is more than 0, the indicator is bullish, if the value is less than 0, the indicator is bearish. If it switches between bullish and bearish, a signal is put out
Reply
Coinhike
a year ago
With the Heinkin Ashi candles this script give better profits. More stable I think.
Reply
G_Man Coinhike
a year ago
HA is problematic on tradingview, the backtest uses the wrong data
+1 Reply
zmm20 Coinhike
a year ago
Any time you use a Heikin Ashi chart with a strategy, the results you get are not correct. It's using the HA open price as the trade entry/exit price, and those prices are not the "actual" price at the time of the trade. Sorry to rain on your parade lol
+1 Reply
Coinhike
a year ago
Hi Sirolf, buying variable should use the l3_0 value of the last bar with : l3_0. Otherwise it is repainted.
Reply
sirolf2009 PRO Coinhike
a year ago
Hey man, I know but that's not how the indicator should be used. You're actually supposed to wait until the end of the day to make your trades, you can trade halfway through the day, but you'll risk the repainting (which is like a breakeven point)
+1 Reply
pengxianyao sirolf2009
2 months ago
Hey sirolf, what do you mean by the end of the day? because the market is open 24/5 all week weekdays, and we're all on different timezones. Thanks in advance!
Reply
Hi Sirolf, thank you for this wonderful strategy! Just a question: if I would like to use it with a 15min chart, should I just open a 15min chart and apply the v2 strategy, or should i apply some more settings? Thanks a lot. Alessandro
+3 Reply
zmm20
a year ago
Cool strategy, unfortunately any time you use a longer resolution than your current tick, TradingView gives you the OHLC values from the END of that interval. You must specify OPEN price (because that will be correct since it's from the start of the interval) but all the other values will be using "future" data. That's why your signals repaint. Want to see my 100% accurate multi-timeframe strategy? It's only 1 line of code. Lol.
Reply
zmm20
a year ago
I would warn anyone who uses this or any other strategy that uses a custom resolution to STOP USING IT or make sure you specify OPEN price. THE RESULTS ARE NOT REAL PEOPLE..............
+1 Reply
zmm20
a year ago
yesterday would be ohlc running with daily candles on 60 minute ticks, today would be ohlc OHLC values don't get locked in until the end of the daily period, which is 24 hours long.

+1 Reply
zmm20 zmm20
a year ago
Ok that's weird, the brackets got deleted from my message. It should say - ohlc brackets 48 and ohlc brackets 24. Why don't brackets come through in messages?
Reply
zmm20 zmm20
a year ago
https://imgur.com/r4jVnzn here is my multi-timeframe indicator. Too bad it's not real! Yours is doing the same thing.
Reply
sirolf2009 PRO zmm20
a year ago
I'm fully aware of this and currently working on different methods to fix it. I've spoken with a few people who are trading with this indicator, and they handle the repainting as a breakeven point. I've tried using the open price as input, but the ANN simply was not trained to be used this way and I got very poor results out of it.
Reply
Vincent121 sirolf2009
a year ago
what's your final thoughts? should we wait for an update?
Reply
sirolf2009 PRO Vincent121
a year ago
ANN still seems profitable if you trade every repaint, which is a notion that has been confirmed by multiple people. Currently I'm looking at different ways to use ANN which may or may not result in new publications in the future
Reply
Vincent121 sirolf2009
a year ago
Okay, can you please keep up updated?
Reply
O4karitO sirolf2009
9 months ago
what if we wait till the very end of the current candle formation(but before it actually ends) and act only then.
Isn't it pretty much the way it's done during backtesting? And this should help to eliminate repainting problem
Reply
ChartArt
9 months ago
Hi, does this use the OHLC4 close of the time-frame the user is currently on?

To me it looks like this line "today=ohlc4" means that if the user is on a 5min chart that it compares the ohlc4 of the daily with the current ohlc4 value of whatever time-frame the trader is on.

Therefore if the user is on a hourly chart this might work better than on a minute chart.
Reply
ChartArt ChartArt
9 months ago
I wanted to say: I understand this code that it does the following:

If the user is on a 5min chart that it compares the ohlc4 of the daily from one day ago with the current ohlc4 value of the 5min chart. Therefore comparing a daily bar with a 5min bar.
Reply
sirolf2009 PRO ChartArt
9 months ago
Yeah, it uses the timeframe you're currently on. But it was trained using daily data which is why I chose the somewhat weird names for my variables
Reply
thomas.gigure
9 months ago
Hey man try your strategy out with Renko charts. Granted it was taking results from 1995, but AMD gave a profit factor of 43! AKS gave a profit factor of 29 too! That's the way to use this, not with regular candles.
Reply
sirolf2009 PRO thomas.gigure
9 months ago
We checked this out on both Renko and Heikin Ashi. However, the trades tradingview makes are based on the Renko closes, not the real closes. Meaning that the trades it makes are impossible to replicate in the "real" world. We checked how it performed when it calculates using Renko and performed trades on regular candles and it's actually worse than performing calculations on regular candles
Reply
clubie
8 months ago
Thanks for the updates. how to turn back the "Buy" and "sell" signals as it shows in v1?
Reply
Hello,

How do I get this to send me an alert when it changes from green to red? thank you
Reply
harryexe TheCryptoTrader
2 months ago
Hmm is it even fixed yet =/
Reply
FadeThem PRO
a month ago
Equity curve caught my eye. Unfortunately the data it uses is 'unknowable' and results could not be reproduced in real time since one does not know the current OHLC. Given that it is looking forward on higher time frame the impossible results should be better than they are. Somewhat interesting code but unfortunately fantasy result.
Reply
launell
15 days ago
Hello, is it possible to get a sound alert when a trade is triggered. If so how?
Reply
Ideas Scripts Chart
United States
United Kingdom
India
España
Italia
Brasil
Россия
Türkiye
日本
한국
Home Stock Screener Economic Calendar How It Works Chart Features House Rules Moderators For the WEB Widgets Stock Charting Library Priority Support Feature Request Blog & News FAQ Help & Wiki Twitter
Private Messages Chat Ideas Published Followers Following Priority Support Public Profile Profile Settings Billing Sign Out