### Author Topic: Help with BCFL custom Scans & Coding  (Read 61460 times)

0 Members and 1 Guest are viewing this topic.

#### JohnR ##### Re: FORMULA & INDICATOR SHARED LIBRARY
« Reply #45 on: 26/05/2019, 12:22:03 PM »
Having set up a number of RSI of MACD Histograms, and RSI of MACD.Main and Signal to test and adjust the Code for the RSI to fairly equal the System RSI of the MACD histogram and MACD.Main line; 1,2 and 3.
It seems BCFL won't take more than one RSI in the code, to have a pre qualifier for the trigger signal / alert; like RSIMH1 is greater than 90 with the trigger RSIMH3 crosses 10.
In the code (below) the output indicator produces a flat line when combining two RSIMH's, but they work individually. Is there a better approach when using two RSI's of the MACD histogram? because I would eventually like to have two pre-qualifying RSIMH's (1 & 2 for trigger 3)

{ ---- MACD 1 ---- }
MACD1:= mov(C, 120, E) - mov(C, 260, E);
MACD1sig := mov(MACD1, 90, E);
MACD1hist := (MACD1 - MACD1sig);

{ ---- MACD 3 ---- }
MACD3:= mov(C, 12, E) - mov(C, 50, E);
MACD3sig := mov(MACD3, 25, E);
MACD3hist := (MACD3 - MACD3sig);

{ ---- RSIMH1 ---- }
period := 5;
currentValue := MACD1hist;
previousValue := REF(MACD1hist, -5);
change := currentValue - previousValue;
Gain := if(cum(1) > period, sum(If(change>0, change, 0), period), 0);
Loss := if(cum(1) > period, sum(If(change<0, abs(change), 0), period), 0);
Avg_Gain := Gain / period;
Avg_Loss := Loss / period;
Avg_Change := if(Avg_Loss > 0, Avg_Gain / Avg_Loss, PREV);
RSIMH1 := 100-(100/(1+Avg_Change));

{ ---- RSIMH3 ---- }
period := 5;
currentValue := MACD3hist;
previousValue := REF(MACD3hist, -5);
change := currentValue - previousValue;
Gain := if(cum(1) > period, sum(If(change>0, change, 0), period), 0);
Loss := if(cum(1) > period, sum(If(change<0, abs(change), 0), period), 0);
Avg_Gain := Gain / period;
Avg_Loss := Loss / period;
Avg_Change := if(Avg_Loss > 0, Avg_Gain / Avg_Loss, PREV);
RSIMH3 := 100-(100/(1+Avg_Change));

{ ---- Criteria ---- }
RSIMH1_UP := RSIMH1 >90;
RSIMH3_X10 := cross(RSIMH3,10);

{ ---- Output to Scan Engine ---- }

EDIT
It seems the second RSI is not being read by BCFL engine, only the top one.
I wonder if there is a way to do the following output by having 3 different inputs; MACDhist1,  MACDhist2 and MACDhist3 in one formula without duplicating the entire formula?:

{ ---- MACD 1 ---- }
MACD1:= mov(C, 120, E) - mov(C, 260, E);
MACD1sig := mov(MACD1, 90, E);
MACD1hist := (MACD1 - MACD1sig);

{ ---- MACD 2 ---- }
MACD2:= mov(C, 50, E) - mov(C, 100, E);
MACD2sig := mov(MACD2, 30, E);
MACD2hist := (MACD2 - MACD2sig);

{ ---- MACD 3 ---- }
MACD3:= mov(C, 12, E) - mov(C, 50, E);
MACD3sig := mov(MACD3, 25, E);
MACD3hist := (MACD3 - MACD3sig);

{ ---- RSIM Formula --- }
period := 5;
currentValue := MACD1hist;
previousValue := REF(MACD1hist, -5);
change := currentValue - previousValue;
Gain := if(cum(1) > period, sum(If(change>0, change, 0), period), 0);
Loss := if(cum(1) > period, sum(If(change<0, abs(change), 0), period), 0);
Avg_Gain := Gain / period;
Avg_Loss := Loss / period;
Avg_Change := if(Avg_Loss > 0, Avg_Gain / Avg_Loss, PREV);
RSIMH1 := 100-(100/(1+Avg_Change));
RSIMH2 := 100-(100/(1+Avg_Change));
RSIMH3 := 100-(100/(1+Avg_Change));

« Last Edit: 26/05/2019, 01:14:32 PM by JohnR »

#### JohnR ##### Re: FORMULA & INDICATOR SHARED LIBRARY
« Reply #46 on: 29/05/2019, 12:33:59 AM »
I have a work-around for now simply substituting the RSIMH1 trend with the MACD1 Histogram or even just the MACD.Main > Signal
But clearly it isn't optimised due to the RSI code output. I don't mind the current RSIMH3 absolute extension into the O/B O/S where the system RSI of the macd histogram doesn't always extend to.

period := 5;
currentValue := MACDH3;
previousValue := REF(MACDH3, - 6);

settings adjusted to extend as high into the 90 zone as possible to eliminate flat spots at the same time limiting lag by having longer Previous Values.

Currently only using a single RSIMH3 for the trigger crossing 10 and 90 line for testing but have introduce 'longer trend' qualifier zones as per above MACDH1 rising filter.
until I examine the non trending zone triggers (of which some very oversold stocks can prove to be a profitable trades based on the RSIMH3 triggers alone).

The current RSIMH3 does trigger closer to the highs and lows when looking back over the chart of recent SPA3T triggered stocks.
Maybe too many close to each other signals, and some unnecessary ones too.

It's a bit hit and miss, but might improve when optimised by a better RSI code and inclusion of other RSIMH's in the code.

Cyan and Fuchsia diamond above and below are the trigger symbols.
Have also added candle conditions for a 'heads up' on the Moving Average scans I am now doing as they are proving to be not to shabby!

I'm waiting to see just how many chart overlays i can put onto the chart before memory limits are reached!

#### Campbell Sinclair

•     • Posts: 249 ##### Re: FORMULA & INDICATOR SHARED LIBRARY
« Reply #47 on: 29/05/2019, 02:57:18 PM »
Hi John,

With the 1st formula from this post (pre-edit): http://www.sharewealthsystems.com/forum/index.php/topic,3526.msg16036.html#msg16036

The condition is that the 1st RSI has to be greater than 90 AND the 2nd RSI is crossing the value of 10 on the same bar. You are correct that the RSIs both work, but it looks like they oscilate between 0 and 1.

So a small tweak in the criteria gets the formula firing:

{ ---- Criteria ---- }
RSIMH1_UP := RSIMH1 >.9;
RSIMH3_X10 := cross(RSIMH3,.10);

You may have worked this out already it looks like.

With the formula duplication question in the edit - due to the way BCFL is the answer is no. You can put common elements in their OWN formula and then get the result of that formula as part of another.

ie this formula 1 called MACD1 in the left hand side Formula Explorer

{ ---- MACD 1 ---- }
MACD1:= mov(C, 120, E) - mov(C, 260, E);
MACD1sig := mov(MACD1, 90, E);
MACD1hist := (MACD1 - MACD1sig);

{ ---- RSIM Formula --- }
period := 5;
currentValue := MACD1hist;
previousValue := REF(MACD1hist, -5);
change := currentValue - previousValue;
Gain := if(cum(1) > period, sum(If(change>0, change, 0), period), 0);
Loss := if(cum(1) > period, sum(If(change<0, abs(change), 0), period), 0);
Avg_Gain := Gain / period;
Avg_Loss := Loss / period;
Avg_Change := if(Avg_Loss > 0, Avg_Gain / Avg_Loss, PREV);
RSIMH1 := 100-(100/(1+Avg_Change));
RSIMH1;

This is formula 2 called: MACD2

{ ---- MACD 2 ---- }
MACD2:= mov(C, 50, E) - mov(C, 100, E);
MACD2sig := mov(MACD2, 30, E);
MACD2hist := (MACD2 - MACD2sig);

{ ---- RSIM Formula --- }
period := 5;
currentValue := MACD2hist ;
previousValue := REF(MACD2hist, -5);
change := currentValue - previousValue;
Gain := if(cum(1) > period, sum(If(change>0, change, 0), period), 0);
Loss := if(cum(1) > period, sum(If(change<0, abs(change), 0), period), 0);
Avg_Gain := Gain / period;
Avg_Loss := Loss / period;
Avg_Change := if(Avg_Loss > 0, Avg_Gain / Avg_Loss, PREV);
RSIMH2 := 100-(100/(1+Avg_Change));
RSIMH2;

This is another formula that gets both of these * individual * formulas and plots their calculations.

{ New Formula }
RSI1 := FML("MACD1");
RSI2 := FML("MACD2");

RSI1;
RSI2;

The implications of this may be useful for using common calculations to shortern specific formulas (or multiple ones) and you only need to perform edits in a single place.

#### JohnR ##### Re: FORMULA & INDICATOR SHARED LIBRARY
« Reply #48 on: 30/05/2019, 06:39:10 PM »
Linking formulas certainly broadens the scope for BCFL scanning etc., I was vaguely aware of this just have not put it into practice yet and something I will be looking into.

The RSI code implemented for the macd histogram is still giving me trouble; this time I was looking at getting a closer fit to the system 'RSI of macd histogram' and also see what a bar highlight colours would look like so started with two separate formulas for:
RSIMH1 > ref(RSIMH1, -1); and also
RSIMH1 < ref(RSIMH1, -1);

The above is for the coded RSI:
period := 5;
currentValue := MACDH1;
previousValue := REF(MACDH1, -1);

Assuming the System RSI has a *PreviousValue of -1 so have used that for this test.
MACDH1 used above is the histogram of MACD 120,260,90.

The results plotted as indicator don't look right at all, and seem to only fire when the RSIMH1 moves up slightly and then reversed down, firing in a binary fashion, unlike the system RSI of the macd histogram which has varying indexed levels. Although I am unable to test this due to not having a dotted line type to see where the points are located in the coded and system RSI lines, or being able to do a system profile of RSI of MACD 1 histogram.

As you mention before the coded RSI "it looks like they oscillate between 0 and 1".
The question remains why, and what can be done to fix the coded RSI to more resemble the System one?

« Last Edit: 30/05/2019, 09:35:19 PM by JohnR »

#### Campbell Sinclair

•     • Posts: 249 ##### Re: FORMULA & INDICATOR SHARED LIBRARY
« Reply #49 on: 31/05/2019, 03:38:00 PM »
It's because of how the RSI is calculated and the constraints of how the language works.

BCFL is great for going one bar at a time and building indicators based on calculations, but the RSI is itself a 2 step calculation. You find the average up and down movements over the initial time period parameter, but then for each bar after the first say 14 days, you are recalculating both the up and down movements in different ways depending on the movement of that individual bar.

For instance say the initial averages have been done (step1 of the RSI formula).

On day 15 it is an up day:
Up movement average = method A: (Up Movements *  Period  ) + (Todays close - Yesterday close ) / Period
Down movement average = method B: (Down movements * Period) / Period

On day 16 it is a down day:
Up movement average = method B on this set
Down movement average = method A on this set

You cannot do this in BCFL as you cannot edit multiple variables based on what happens. You may be able to provide some very extensive IF conditioning to get there, but it is a departure in itself from how the first days are handled. BCFL has to repeat the script on every bar, which is where the implementation difficulties come in again because you can seperate the data involved in step 1 and step 2.

The full composition is outlined here: https://www.investopedia.com/terms/r/rsi.asp

#### JohnR ##### Re: Help with BCFL custom Scans & Coding
« Reply #50 on: 03/06/2019, 01:18:21 PM »
What about this pine script, I understand you have indicated short comings within BCFL in regards to the RSI, but if I can't get the rsi of macd's working here I may as well just use Trading View which does, with the addition of intra-day time frames.

Is there any hope of using a better code base for the RSI than the one you supplied, or is it simply a limitation of BCFL full stop.

I persist with the RSI of MACD Histogram and Main (RSIMH, RSIMM) because it doesn't produce over signalling like the MACD Histogram does for the faster period settings and the reaction time of the RSIM's is excellent for major turning points, can be related to O/B O/S indexed levels, and the RSIMH and RSIMM could very well be deployed as a System in BCFL. But until the RSI issues are resolved it remains hit and miss which is not good for an additional alert setup for optimised real money trading.

If BCFL had reference in Library for these built-ins could the RSI work? I.O.W. are the BCFL Library items within the C# area of the program?

Pine Script:
hist1 = macd histogram 1
rma, max, change & -min are Pine built-in functions

// ============== RSIMH 1 histogram ============== //

src1 = hist1, len = input(5, minval=1, title="Length")
up1 = rma(max(change(src1), 0), len)
down1 = rma(-min(change(src1), 0), len)
RSIMH1 = down1 == 0 ? 100 : up1 == 0 ? 0 : 100 - (100 / (1 + up1 / down1))

#### Campbell Sinclair

•     • Posts: 249 ##### Re: Help with BCFL custom Scans & Coding
« Reply #51 on: 04/06/2019, 03:42:23 PM »
We can expand RSI to take parameters as I outlined initially, so that you can choose which data sets the BCFL RSI function is applied to instead of just the close - although this will be down the track once the development team completes their current business projects.

The BCFL Library are the pre-built functions, so it is the RSI in here that would be expanded.

#### JohnR ##### Re: Help with BCFL custom Scans & Coding
« Reply #52 on: 05/06/2019, 12:50:56 AM »
OK, I'm thinking more along the lines of going long on some of the stronger looking scans and judging the state of the trend for BC daily data within TradingView's intra day time frame; 1 hour seems to work. This works much like a weekly data chart (long entry, exit) with taking signals on the daily data charts (short entry, exit).

The RSI of MACD histograms work well on the intra day charts, so will go with this for now, and it makes picking swing tops and bottoms much easier than relying on basing trades on signals generated on the previous days data, if the signal happened yesterday morning and the TM trade action takes place today at some point, the risk is the sell side on some stocks might look too thin today to place a direct price trade, forcing to sell down several price points to get the bulk of the order filled, or sit on the sell side with fingers crossed. Maybe I can pick the turning point clearer on the 1 hour chart data (see example images).

It will be a juggle to organise rapid turnover that SPA3-T, as well as my own scans, and to manage it all through the portfolio manager, but I've already started to go long on some of the New Entries and other scans ignoring system signals where they occur in strong trends to ride the trend until Moving Average, MACD2 and MACD1 loses momentum.

Re the formula for:
>mentioned in a few posts back was the short form formula:
"This is another formula that gets both of these * individual * formulas and plots their calculations."

{ New Formula }
RSI1 := FML("MACD1");
RSI2 := FML("MACD2");

RSI1;
RSI2;

If the MACD has 3 outputs (as indicator), how do you reference one or two of those outputs where;
saved formula title = MACD1:

{ ---- MACD 1 ---- }
MACDM1:= mov(C, 120, E) - mov(C, 260, E);
MACDS1 := mov(MACDM1, 90, E);
MACDH1 := (MACDM1 - MACDS1);
MACDM1;
MACDS1;
MACDH1;

In short form when calling the title name:

MACD1 := FML("MACD1");

but want a qualifier for MACDH1, MACDS1 and or MACDM1; does th short form automatically read all the outputs to put into the qualifying parameters?

QF1 := MACDM1 > MACDS1;
TRIG_UP := MACDH2 > REF(MACDH2, -1) and REF(MACDH2,-1) < REF(MACDH2,-2);

Thanks.
« Last Edit: 05/06/2019, 01:10:43 AM by JohnR »

#### Campbell Sinclair

•     • Posts: 249 ##### Re: Help with BCFL custom Scans & Coding
« Reply #53 on: 05/06/2019, 02:51:34 PM »
As long as you have a process outlined!

Volume depth for orders or liqudity at certain price points and the importance should be reconciled with the risk of waiting on the sidelines. We believe it is better to follow the defined exit signals for peace of mind alone than chasing price points, as any system with sufficient edge will reveal itself over time via execution of the process. The process being following the defined entry and exit signals that build towards that reseached edge.

As for the formula, you want the fmrvar() function.

input_A := fmlvar("MACD1", "MACDH1");

and the syntax is:

input_A := fmlvar("Formula Name", "Variable Name in Formula");

#### JohnR ##### Re: Help with BCFL custom Scans & Coding
« Reply #54 on: 06/06/2019, 12:53:52 PM »
Thanks for the formula.

Quote
Volume depth for orders or liqudity at certain price points and the importance should be reconciled with the risk of waiting on the sidelines.
Yes, am beginning to see this through practical experience; held IAG for this very reason (in fact a few stocks with thin buy sides), This is what is prompting me to use Trading View's intra-day time frames to spot any turning points, although it multiplies my workload.

I guess this is what happens when you get multiple trading systems all triggering sells at the same time = a rush for the exit doors! the herd following rules can alone move a stock considerably downward. Although IAG still in underlying trend I do expect from being so severely oversold a correction of sorts.

I have a question in regards to porting the Hull MACD over to BCFL and was wondering if the same RSI limitation in BCFL might effect this indicator (p.s. don't spend too much time on this):
Where round and sqrt = attached images. And obviously minus the plot lines.

{ ---- HULL MACD 1 ---- }
f_hma(_src, _length)=>
_return = wma((2 * wma(_src, _length / 2)) - wma(_src, _length), round(sqrt(_length)))

len_fast1 = input(120, "Fast MA", minval=1)
len_slow1=input(260, "Slow MA", minval=1)
len_sig1=input(90, "Signal Length", minval=1)

fast_ma1 = f_hma(source, len_fast1)
slow_ma1 = f_hma(source, len_slow1)

MACDM1 = fast_ma1 - slow_ma1
MACDS1 = wma(MACDM1, len_sig1)
MACDH1 = MACDM1 - MACDS1

plot(MACDH1, style=histogram, linewidth=2,color=#2b77f2, transp=10)
plot(MACDM1, color=blue, linewidth=2)
plot(MACDS1, color=red, linewidth=2)

#### Campbell Sinclair

•     • Posts: 249 ##### Re: Help with BCFL custom Scans & Coding
« Reply #55 on: 06/06/2019, 03:51:54 PM »
No not really - as the RSI is in effect a 2 part calculation, and inputs to the final calculation are updated differently depending on how the price action moves. This cannot be easily replicated in the style of language that BCFL is.

SQRT() and ROUND() are both Math functions that can be accessed in BCFL, which will provide senisble output given the input in to either is a number.

ie. SQRT(Round(20.5)); will give back 4.47.

#### JohnR ##### Re: Help with BCFL custom Scans & Coding
« Reply #56 on: 12/06/2019, 12:27:55 PM »
Hi Campbell,
If the RSI is broken for BCFL I am wondering about using another indicator to take the MACD histograms data.

Are you able to list the BC indicators that will take another indicators values like the RSI and MACD to be used in BCFL?

Thanks.

#### Campbell Sinclair

•     • Posts: 249 ##### Re: Help with BCFL custom Scans & Coding
« Reply #57 on: 12/06/2019, 03:06:33 PM »
Hi John,

The base language it is based off does not have this capability either, the functionality you are looking for just goes a little bit beyond what these languages do until we can naturally extend its base capabilities.

For what you have asked - look at the right hand side BCFL Library. This is a list of all the functions in BCFL and there's a dedicated indicator section.

Highlight the top indicator, then open the quick reference section (the book icon with the thunderbolt) at the top of the panel. This is the documentation for the indicators and it gives examples of what each function requires to operate.

An indicator with the DATA ARRAY syntax can take other indicators as an input to the calculation. This is also a great place to look to see how functions work when you come back to formulas in the future.

#### JohnR ##### Re: Help with BCFL custom Scans & Coding
« Reply #58 on: 14/06/2019, 05:47:14 PM »
Is it possible to apply an output with: line, or histogram, or colour to an output entity?

#### JohnR ##### Re: Help with BCFL custom Scans & Coding
« Reply #59 on: 18/06/2019, 12:31:20 PM »
Hi Campbell, when you get some spares, could you give an idea if adding the MACD histogram to the ATR-TS input would be possible. As there is no BCFL ATR-TS code to review for DATA ARRAY syntax.

I'm thinking instead of routing the MACD histogram through the RSI for an indexed oscillator, it might be better to go directly onto price chart as an atr-ts type oscillator. The ATR-TS would signal when the MACD histogram long turns down from rising and visa-versa, or same done with the main macd line if the histogram was too sensitive, although the long 120, 260, 90 histogram should be ok.

I'm aware you don't like inputs, so can the first two lines be restructured to take the macd histogram (MACDH1) as the data source?

{MACDH1}
MACDH1:= fmlvar("MACD1", "MACDH1");

{ATR-TS}
atrper:=Input("ATR period :",1,100,14);
atrfact:=Input("ATR multiplication :",1,10,4.5);
loss:=atrfact*ATR(atrper);
ATR_TS_L :=
If(C>PREV AND Ref(C,-1)>PREV,
Max(PREV,C-loss),
If(C<PREV AND Ref(C,-1)<PREV,
Min(PREV,C+loss),
If(C>PREV,C-loss,C+loss)));
ATR_TS_L;

Or in fact the ATR-TS you posted earlier:

{ Trailing_Stop SIMPLE SMOOTHING }
ATR_smoother := MOV(ATR(1), 14, S);
up_atrfact:= 3;
dn_atrfact := 3;
up_loss:=up_atrfact*ATR_smoother;
dn_loss:=dn_atrfact*ATR_smoother;
trail:=
If(L>PREV AND Ref(L,-1)>PREV,
Max(PREV,L-dn_loss),
If(L<PREV AND Ref(L,-1)<PREV,
Min(PREV,L+up_loss),
If(L>PREV,L-dn_loss,L+up_loss)));
trail;

« Last Edit: 18/06/2019, 12:46:45 PM by JohnR »