Can I get an expert commentary on how I am running my scanner?

Hello,

I would like some general advice for running scanners. Currently I have a script that runs 5 different periods as inputs for each instrument. It’s coded up by copying and pasting the script 5 times with only difference being the periods. a_period = 1, b_period = 2 and so on. Then it gets passed to the condition checker. Buy = a_period_signal OR b_ period_signal … This is then opened by several different instruments. When it gets triggered I have a ShellExecute open a python file that sends me a text message through Twilio.

Is there a better way to program this ? Can I do something like, period = 1 or 2 or 3 … And just have the code once? Any general advice on this would be awesome. Thanks !

I think if you show some of your code one of our more skilled programmers could probably use a loop to accomplish what you want. Otherwise this is an example of what might help you get started in afl.

Buy1 = Cross( Close, MA(Close,10) );
Buy2 = Cross( Close, MA(Close,20) );
Buy3 = Cross( Close, MA(Close,30) );

Buy = buy1 OR Buy2 OR Buy3;


// Exploration
Filter = Buy;
AddColumn( Buy1, "Buy1", 1, colorDefault, IIf( Buy1, colorGreen, colorDefault ) );
AddColumn( Buy2, "Buy2", 1, colorDefault, IIf( Buy2, colorGreen, colorDefault ) );
AddColumn( Buy3, "Buy3", 1, colorDefault, IIf( Buy3, colorGreen, colorDefault ) );


// Charting, lay on top of a standard price chart

PlotShapes(Buy*shapeUpArrow, colorGreen, 0, Low);
PlotShapes(Buy1*shapedigit1, colorGreen, 0, Low,-30);
PlotShapes(Buy2*shapedigit2, colorGreen, 0, Low,-50);
PlotShapes(Buy3*shapedigit3, colorGreen, 0, Low,-75);

Exploration looks like this, and can be modified with fuller code.

Chart can look something like this.

2 Likes

That’s actually exactly how it is. I am away so don’t have access to code now but my main question regards this.

[code]Buy1 = Cross( Close, MA(Close,10) );
Buy2 = Cross( Close, MA(Close,20) );
Buy3 = Cross( Close, MA(Close,30) );

Buy = buy1 OR Buy2 OR Buy3;[/code]

Can I just have -

period = 10 OR 20 OR 30; Buy = Cross( Close, MA(Close, period) );

This basically stops me from having to copy same blocks of code multiple times and reduces scope for error especially when I want to modify the code.

Thanks.

No, that is wrong. “OR” is used for true/false check.

However you may do the following using dynamic variables.

period1 = 10;
period2 = 20;
period3 = 30;

for( i = 1; i < 4; i++ ) {
	period = VarGet( "period" + i );
	VarSet( "Buy" + i, Cross( C, MA(C, period) ) );
}

Buy = Buy1 OR Buy2 OR Buy3;

EDIT: below code by Steve will not be suitable if periods do not have constant increment (in this example 10) i.e., if periods are:

period1 = 15;
period2 = 21;
period3 = 50;
2 Likes

@trunda You should not do this:

period = 10 OR 20 OR 30;

That statement will cause period to be set to 1 (true) because OR is a boolean operator.

However, you could write a for loop like this:

Buy = False;
for (period = 10; period <= 30; period += 10)
    Buy = Buy OR Cross(Close, MA(Close, period));

Hi,

Thanks for your help guys. I followed both your advice Steve and fxshrat and find that the result is not the same when compared to running the periods separately.

As you see I plot a vertical line when I have the Buy signal and comparing that to the original code it doesn’t appear to line up with what I am expecting.

Another thing I would like to get some help on would be how to run ShellExecute on completed bar only once instead of it run every second like it is doing now until the bar is completed.

Thank you !

period1 = 0.001;
period2 = 0.002;
period3 = 0.003;
period4 = 0.004;
period5 = 0.005;

for( i = 1; i < 6; i++ )
{
	a_period = VarGet( "period" + i );
	
	a_1 = cross(H, ma(c,a_period));
	a_2 = H - L > ref(H, -1) - ref(L, -1);
	a_3 = L < ref(L, -1);
	
	a_signal = a_1 AND a_2 AND a_3;
	
	VarSet( "Buy" + i, a_signal );
}

Buy = Buy1 OR Buy2 OR Buy3 OR Buy4 OR Buy5;

if(LastValue( Buy ))
{
	ShellExecute("E:\\eusignal.py", "", "" );
}

Plot(Buy,"Period", colorBlue, styleHistogram| styleThick, 0,1);

Nonsense!

If applying my sample code in this further (extended) example

SetPositionSize( 1, spsShares );

period1 = 10;
period2 = 20;
period3 = 30;

for( i = 1; i < 4; i++ ) {
	period = VarGet( "period" + i );
	VarSet( "Buy" + i, Cross( C, MA(C, period) ) );
}

Buy = Buy1 OR Buy2 OR Buy3;

Sell = 0;
Short = Cover = 0;

bars = 40; // exit after 40 bars
ApplyStop( stopTypeNBar, stopModeBars, bars, True );

Then the results are the exactly the same ones as doing it without VarSet/VarGet:

SetPositionSize( 1, spsShares );

period1 = 10;
period2 = 20;
period3 = 30;

Buy1 = Cross( C, MA(C, period1) );
Buy2 = Cross( C, MA(C, period2) );
Buy3 = Cross( C, MA(C, period3) );

Buy = Buy1 OR Buy2 OR Buy3;

Sell = 0;
Short = Cover = 0;

bars = 40; // exit after 40 bars
ApplyStop( stopTypeNBar, stopModeBars, bars, True );

The further nonsense I’m seeing in your code is that you are using fractional periods. MA’s periods are bar periods.

Is

bi = BarIndex();
x = bi - Ref( bi, -1 );

fractional?

I just used MA as an example. Fractional periods are for my strategy that uses zigzag indicator with period as inputs.

1 (filling min 20char)

You post some nonsense code and don’t even post the code that you have actually compared it with. No one knows what you are actually doing.

Re-read my last post. The examples given by Steve and me are working. Steve’s example does work too but as mentioned just with constant incrementing periods ( 10, 20, 30 or 5, 10, 15, 20 etc).

So far for claiming things being far away from truth.

Have a nice day. I’m out of this thread… I do not want to waste more time with non-productive posts leading to no where.

EDIT:

This code

SetPositionSize( 1, spsShares );

period1 = 10;
period2 = 20;
period3 = 30;
period4 = 40;
period5 = 50;

for( i = 1; i < 6; i++ )
{
    a_period = VarGet( "period" + i );
    a_1 = Cross( H, Ma( C, a_period ) );
    a_2 = (H - L) > Ref( H - L, -1 );
    a_3 = L < Ref( L, -1 );
    a_signal = a_1 AND a_2 AND a_3;
    VarSet( "Buy" + i, a_signal );
}

Buy = Buy1 OR Buy2 OR Buy3 OR Buy4 OR Buy5;

Sell = 0;
Short = Cover = 0;

bars = 60; // exit after 60 bars
ApplyStop( stopTypeNBar, stopModeBars, bars, True );

outputs same results as this code

SetPositionSize( 1, spsShares );

period1 = 10;
period2 = 20;
period3 = 30;
period4 = 40;
period5 = 50;

a_2 = ( H - L ) > Ref( H - L, -1 );
a_3 = L < Ref( L, -1 );

Buy1 = Cross( H, Ma( C, period1 ) ) AND a_2 AND a_3;
Buy2 = Cross( H, Ma( C, period2 ) ) AND a_2 AND a_3;
Buy3 = Cross( H, Ma( C, period3 ) ) AND a_2 AND a_3;
Buy4 = Cross( H, Ma( C, period4 ) ) AND a_2 AND a_3;
Buy5 = Cross( H, Ma( C, period5 ) ) AND a_2 AND a_3;

Buy = Buy1 OR Buy2 OR Buy3 OR Buy4 OR Buy5;

Sell = 0;
Short = Cover = 0;

bars = 60; // exit after 60 bars
ApplyStop( stopTypeNBar, stopModeBars, bars, True );

Quod erat demonstrandum!

1 Like

You are right. I made a small mistake in my code. This works great !