- ago
Need Help Converting OR building with Blocks, Thanks in advance

CODE:
//@version=4 study("Rule Number 1 Signals [ALERTS]", overlay=true) //This strategy is based off of the Book Rule #1 by Phil Town which teaches Long Term GARP/Value investing. There are three signals, MACD, Stochastics, and Moving Averages, //that are all used in conjunction to signal a LONG entry. It is better used on Daily and above time periods as the book as about long term investing. Remember, the book, //is about fundamental business analysis, to see if its a wonderful company, as the focus, and technical analysis is just a trigger. I built this as an experiment to see what //would happen if the signals were applied as per the book //MACD fastLength = input(8) slowlength = input(17) MACDLength = input(9) MACD = ema(close, fastLength) - ema(close, slowlength) aMACD = ema(MACD, MACDLength) macdDelta = MACD - aMACD //SMA signal smaSignal = input(10) sma = sma(close, smaSignal) //Stochastics 14-5 (Slow) stochasticLength = input(14, minval=1) stochasticOverBought = input(80) stochasticOverSold = input(20) smoothK = 5 smoothD = 5 k = sma(stoch(close, high, low, stochasticLength), smoothK) d = sma(k, smoothD) //Buy and close conditions smaCrossOver = crossover(close, sma) stochasticCrossOver = crossover(k, d) macdCrossOver = crossover(macdDelta, 0) smaBullish = crossover(close, sma) or close >= sma and abs(close - sma) > abs(close[1]) - sma[1] macdBullish = crossover(macdDelta, 0) or macdDelta >= 0 and abs(MACD - aMACD) > abs(MACD[1] - aMACD[1]) stochasticBullish = crossover(k, d) or k >= d and abs(k - d) > abs(k[1] - d[1]) macdCrossUnder = crossunder(macdDelta, 0) stochasticCrossUnder = crossunder(k, d) smaCrossUnder = crossunder(close, sma(close, smaSignal)) smaBearish = crossunder(close, sma) or close <= sma and abs(close - sma) > abs(close[1]) - sma[1] macdBearish = crossunder(macdDelta, 0) or macdDelta >= 0 and abs(MACD - aMACD) > abs(MACD[1] - aMACD[1]) stochasticBearish = crossunder(k, d) or k <= d and abs(k - d) > abs(k[1] - d[1]) //Set up the premises for the strategy le = macdCrossOver and smaBullish and stochasticBullish or macdBullish and smaCrossOver and stochasticBullish or macdBullish and smaBullish and stochasticCrossOver se = macdCrossUnder and smaBearish and stochasticBearish or macdBearish and smaCrossUnder and stochasticBearish or macdBearish and smaBearish and stochasticCrossUnder //Strategy to go long or to close out long term positions. // if long // strategy.entry("buy", strategy.long, 10, comment="BUY") // if closeLong // strategy.close("buy") le_filt = le and not se se_filt = se and not le prev = 0 prev := se_filt ? 1 : le_filt ? -1 : prev[1] se_final = se_filt and prev[1] == -1 le_final = le_filt and prev[1] == 1 plotshape(le_final, style=shape.arrowup, location=location.belowbar, text="buy", color=color(#fdf6e3), textcolor=color(#fdf6e3)) plotshape(se_final, style=shape.arrowdown, location=location.abovebar, text="sell" , color=(#ffa726), textcolor=color(#ffa726))
0
1,158
Solved
12 Replies

Reply

Bookmark

Sort
- ago
#1
How's your evaluation going? You asked for Money Flow oscillator two months ago:
https://www.wealth-lab.com/Discussion/The-Money-Flow-Oscillator-Apirine-6267
0
- ago
#2
Hey Eugene,
I did not max my old evaluation and did not get help, I download it again to my new PC i was hopping to get help and fully evaluate this time, hope you can help me to convert this script, Thanks in advance
0
- ago
#3
Hi Uriel,

Our logs tell that you used the demo for two weeks straight back in May, staying pretty active almost daily.

If you like Wealth-Lab 7 kindly consider subscribing (e.g. to a monthly plan for starters) and then we can look into some custom programming. Thanks in advance.
0
- ago
#4
Thanks Eugene, Sure I can do that, Done!
0
- ago
#5
You're welcome. Here goes your code!
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript2 { public class urielted20210722 : UserStrategyBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) {          //MACD          int fastLength = 8, slowlength = 17, MACDLength = 9;          macd = MACD.Series(bars.Close, fastLength, slowlength);          aMACD = EMA.Series(macd, MACDLength);          macdDelta = macd - aMACD;          //SMA signal          int smaSignal = 10;          sma = SMA.Series(bars.Close, smaSignal);          //Stochastics 14-5 (Slow)          int stochasticLength = 14, stochasticOverBought = 80, stochasticOverSold = 20, smoothK = 5, smoothD = 5;          k = SMA.Series(StochK.Series(bars, stochasticLength), smoothK);          d = StochD.Series(bars, smoothK, smoothD);          prev = new TimeSeries(bars.DateTimes, 0);          PlotTimeSeriesLine(macd, "macd", "MACD", Color.Brown);          PlotTimeSeriesLine(aMACD, "macd signal", "MACD", Color.DarkGreen);          PlotTimeSeries(macdDelta, "macd delta", "MACD", Color.Blue, PlotStyles.ThickHistogram);                    PlotTimeSeriesLine(sma, "sma", "Price", Color.Blue);          PlotTimeSeriesLine(k, "stochK", "Stoch", Color.Red);          PlotTimeSeriesLine(d, "stochD", "Stoch", Color.Blue); } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) {          if (idx > 0)          {             //Buy and close conditions             bool smaCrossOver = bars.Close.CrossesOver(sma, idx);             bool stochasticCrossOver = k.CrossesOver(d, idx);             bool macdCrossOver = macdDelta.CrossesOver(0, idx);             bool smaBullish = smaCrossOver || (bars.Close[idx] >= sma[idx] && Math.Abs(bars.Close[idx] - sma[idx]) > Math.Abs(bars.Close[idx - 1] - sma[idx - 1]));             bool macdBullish = macdCrossOver || macdDelta[idx] >= 0 && Math.Abs(macdDelta[idx]) > Math.Abs(macdDelta[idx - 1]);             bool stochasticBullish = stochasticCrossOver || k[idx] >= d[idx] && Math.Abs(k[idx] -d[idx]) > Math.Abs(k[idx - 1] - d[idx - 1]);             bool smaCrossUnder = bars.Close.CrossesUnder(sma, idx);             bool macdCrossUnder = macdDelta.CrossesUnder(0, idx);             bool stochasticCrossUnder = k.CrossesUnder(d, idx);             bool smaBearish = smaCrossUnder || (bars.Close[idx] <= sma[idx] && Math.Abs(bars.Close[idx] - sma[idx]) > Math.Abs(bars.Close[idx - 1] - sma[idx - 1]));             bool macdBearish = macdCrossUnder || macdDelta[idx] >= 0 && Math.Abs(macdDelta[idx]) > Math.Abs(macdDelta[idx - 1]);             bool stochasticBearish = stochasticCrossUnder || k[idx] <= d[idx] && Math.Abs(k[idx] -d[idx]) > Math.Abs(k[idx - 1] - d[idx - 1]);             //Set up the premises for the strategy             bool le = (macdCrossOver && smaBullish && stochasticBullish) || (macdBullish && smaCrossOver && stochasticBullish) || (macdBullish && smaBullish && stochasticCrossOver);             bool se = (macdCrossUnder && smaBearish && stochasticBearish) || (macdBearish && smaCrossUnder && stochasticBearish) || (macdBearish && smaBearish && stochasticCrossUnder);             bool le_filt = le && !se;             bool se_filt = se && !le;             if(idx > 0)                prev[idx] = se_filt ? 1 : le_filt ? -1 : prev[idx -1];             else                prev[idx] = 0;             bool se_final = idx > 0 ? se_filt && prev[idx -1] == -1 : false;             bool le_final = idx > 0 ? le_filt && prev[idx -1] == 1 : false;             if (!HasOpenPosition(bars, PositionType.Long))             {                //code your buy conditions here                if(le_final)                   PlaceTrade(bars, TransactionType.Buy, OrderType.Market);             }             else             {                //code your sell conditions here                if(se_final)                   PlaceTrade(bars, TransactionType.Sell, OrderType.Market);             }          }           }       //declare private variables below       TimeSeries macd, aMACD, macdDelta, k, d, sma, prev;    } }
0
Best Answer
- ago
#6
Thank you
0
- ago
#7
Eugene,
The strategy build on Rule#1 book the info for the strategy layout here:

http://docshare04.docshare.tips/files/26904/269043119.pdf

Please advice if the code match setting

Thanks in advance
0
- ago
#8
Buy methodology in regards to the Three Tools:
1. Latest closing price above 30- period moving average;
2. %K line of slow stochastic oscillator above %D line; and
3. MACD histogram above centerline (greater than zero).

Sell rules in regards to the Three Tools are:
1. %K line of slow stochastic falls below %D line; and
2. MACD histogram falls below centerline (is less than zero);
and
3. The stock is trading sideways.


Three Greens You’re In, Two
Reds You’re Out
0
- ago
#9
The script contains a lot more logic that your rules in Post #8. I've provided a literal translation of the script in #1.
0
- ago
#10
Eugene,
Can this be build with blocks ?
0
- ago
#11
Uriel, it's unlikely that the logic in #1 could be programmed in Blocks. However, the rules in Post #8 can certainly be:

Buy rules:
1. Latest closing price above 30- period moving average; => Use "Indicator Compare to Indicator", Close and SMA
2. %K line of slow stochastic oscillator above %D line; and => Use "Indicator Compare to Indicator", StochK and StochD
3. MACD histtogram above centerline (greater than zero). => Use "Indicator Compare to Value", MACDHist

Sell rules in regards
1. %K line of slow stochastic falls below %D line; and => Use "Indicator Compare to Indicator", StochK and StochD
2. MACD histogram falls below centerline (is less than zero); => Use "Indicator Compare to Value", MACDHist
3. The stock is trading sideways. => Use "Indicator Compare to Value", ADX < 25


* Helpful resources:
https://wl6.wealth-lab.com/Forum/Posts/Rules-WL6-gt-Building-Blocks-WL7-40617/
https://www.wealth-lab.com/Discussion/Quick-WL6-9-to-WL7-Translation-Guide-5548
0
- ago
#12
Please advice the correct setting: image attach

0

Reply

Bookmark

Sort