• Australian (ASX) Stock Market Forum

Discussion in 'Software and Data' started by mscir, Jan 21, 2015.

1. ### mscir

Posts:
9
0
Joined:
Jan 21, 2015
I wrote my first Amibroker formula to find buy and sell points based on a couple of indicators, and I am able to run the optimizer on it, Amibroker is really impressive. For backtesting I'd like to draw a green line at each buy and a red line at each sell, and I can't sort it. Any help would be appreciated.

2. ### mscir

Posts:
9
0
Joined:
Jan 21, 2015
I found code to draw vertical lines on the price pane:
Plot(Buy ,"vertical line", colorGreen, styleHistogram|styleOwnScale, 0, 1);
Plot(Sell, "vertical line", colorRed, styleHistogram|styleOwnScale, 0, 1);

I'd like to know how to only draw one Buy line after a Buy condition has been met and a buy line has been drawn, and only start drawing Buy lines again after a Sell line has been drawn. Right now I have multiple buy lines drawn as the function is correctly identifying multiple buy conditions.

3. ### mscir

Posts:
9
0
Joined:
Jan 21, 2015
This page (below) clued me into the fact that Buy and Sell are arrays, and showed me how to take out redundant buy or sell signals.

for (i = 0; i < BarCount; i++) // Loop over all bars in the chart
{
if (OpenPos) // If have an open position
{
if (Sell) // If have sell signal on this bar
OpenPos = False; // No longer have open position
}
else // Else if don't have open position
{
Sell = False; // Remove any surplus sell signals
OpenPos = True; // Now have an open position
}
}

// PLOT VERTICAL LINES ON PRICE CHART
Plot(Buy ,"vertical line", colorGreen, styleHistogram|styleOwnScale, 0, 1); // price pane
Plot(Sell, "vertical line", colorRed, styleHistogram|styleOwnScale, 0, 1); // price pane

http://www.amibrokerforum.com/index.php?topic=50.0

4. ### trash

Posts:
412
17
Joined:
Mar 31, 2012
There is no loop required. Simply use ExRem() function. Check out the AB manual.
In Analysis ExRem is not required as regular backtester takes care of redundant signals.
But there are other backtest modes that can be activated. See SetBacktestMode() procedure.

5. ### trash

Posts:
412
17
Joined:
Mar 31, 2012
Here is an example

Code:
```period = 20; // number of averaging periods
m = MA( Close, period ); // simple moving average
Buy = Close > m; // buy when close is ABOVE moving average
Sell = m > Close; // sell when close is BELOW moving average
Short = Cover = 0;

if( Status( "action" ) == actionIndicator )
{
// remove excessive signals
Sell = ExRem( Sell, Buy );
Short = ExRem( Short, Cover );
Cover = ExRem( Cover, Short );

SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle );

Plot( C, "Price", colorDefault, GetPriceStyle() | styleNoTitle );

// PLOT VERTICAL LINES ON PRICE CHART
style = styleHistogram|styleOwnScale|styleNoLabel|styleNoTitle;
Plot(Buy ,"vertical line", colorGreen, style, 0, 1); // price pane
Plot(Sell, "vertical line", colorRed, style, 0, 1); // price pane

_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} - {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%), Vol %g {{VALUES}}",
O, H, L, C, SelectedValue( ROC( C, 1 ) ), V ) );
}```

6. ### mscir

Posts:
9
0
Joined:
Jan 21, 2015
Thanks I'll check that stuff out. Much appreciated!

7. ### mscir

Posts:
9
0
Joined:
Jan 21, 2015
I found that I can drag and drop the AFL file which contains this code onto the price chart from the left pane (on the Charts TAB in the Custom folder) the lines will be drawn on the price chart, really convenient tool.

Thanks again!
Mike

8. ### mscir

Posts:
9
0
Joined:
Jan 21, 2015
Now that I've got one AFL file that creates an indicator with red and green lines, and one AFL file that draws to the price chart, when I drag and drop it on the price chart, I'm wondering if I can write to the price chart from the indicator AFL. Is there a way to tell the code where to plot the line? Is there a pane array or some other way to control it?

TIA,
MIke

9. ### trash

Posts:
412
17
Joined:
Mar 31, 2012
See static variables (StaticVarSet, StaticVarGet,...)

10. ### mscir

Posts:
9
0
Joined:
Jan 21, 2015
Thanks for your reply. I looked at the help files for both commands, e.g.

// start of the formula:
temp = StaticVarGet("mystaticarray" );
// now perform all necessary calculations using temp variable
temp = Nz(temp) + C/2;
...
// at the end of the formula store to static
StaticVarSet("mystaticarray", temp );

I'm not seeing how this can be used to write to the indicator pane (below the price pane) or to the price pane. I'd like to do both from one AFL file, so I can add the code to write vertical lines on the price pane to the AFL file that draws the indicator lines. Sorry if I wasn't clear about that.

11. ### trash

Posts:
412
17
Joined:
Mar 31, 2012
Are you the guy who is the experienced one or is it me? When I say static variables will do that sharing then it does not matter what you see or don't see. What matters are facts.

You store your variables of one pane via staticvarset and call them via staticvarget in the other pane.

12. ### mscir

Posts:
9
0
Joined:
Jan 21, 2015
NO disrespect intended, I was just trying to say I couldn't connect the dots. I was hoping to use a single file to draw to both panes, but if I understand what you're saying then that isn't possible, at least using StaticVarSet and StaticVarGet.
I am an abject beginner and just trying to see what's possible with this platform.

13. ### trash

Posts:
412
17
Joined:
Mar 31, 2012
But that's what static variables will achieve. If you change parameters of variables in master chart pane i.e. you set different trade rules (Buy/Sell/...) and in addition trade variables are stored via staticvarset then it's not required to change the same rules in the slave chart pane as slave pane is programmatically receiving those modified buy/sell rules at the same time after saving master pane's AFL to updated state. So slave pane is receiving new info in real-time without the need to do the same modifications there.

But you can't send some info by some magic and expect that other panes act like A.I. and expect them to know what your brain wants them to do while your brain sends out some beams.

If you wanna send something then you need something to receive from sender.

14. ### mscir

Posts:
9
0
Joined:
Jan 21, 2015
That all makes sense, I appreciate you explaining it.

Best Regards,
Mike

15. ### OldGrantonian

Posts:
2
0
Joined:
Jun 4, 2013
The following code plots arrows. Maybe it can be modified.

adFastMA = MA(Close, 5 ) ;