Australian (ASX) Stock Market Forum

AmiBroker Plugin for Trailing Stop Plots

Thanks R0n1n
I was trying to rename it after I downloaded, not before. All good now..

And thankyou GP for all the work you have posted here, awesome..
 
Hello GP,

Thank you so much for your contributions on this forum.

I have been attempting to code up some systems in Amibroker, I think I'm OK at native AFL, but have not been game to tackle looping. I have now discovered that I need loops to do what I need to do for my system...

Your code sample in post 35 was a great help to illustrate how to incorporate your great DLL function into my own system. I have been toying around with it.

Basically I have a fairly simple system for long entries and short entries, the difference with it that I am finding a challenge is that it is a 'stop loss and reverse' type system.

Basically, I want to get it to exit at the countback line or ATR level that I define as my stoploss. But I ALSO want to place a reverse when it is stopped out. So, say I have opened a long trade, I want the system to exit at the stop (sell) and also place another sell (to open) trade.

So basically, my entries are both Short and long and are defined, but my exits and subsequent trades are defined by the stops.

I also want to define that this happens a limited number of times (but I'd be happy to get the first bit working for now...).

A bit tricky I've found...

I have attempted to arrange it so it is a long and short system as below, but then I am not sure where to turn...

Code:
Buy = My defined Buy signal;
Short = My defined Short Signal;

cback = Param("Countback?", 20, 1, 100, 1);

cblLineL = GP_CountBack(cback, 1);
CblLineS = GP_CountBack(Cback, 0);
StopL = GP_CreateStop(cblLineL, Close, Buy, Null, "", -1, -2, 1);
StopS = GP_CreateStop(cblLineS, Close, Short, Null, "", -1, -2, 0);

Sell = <My defined Sell Signal> || StopL < Ref(StopL, -1);
Cover = <My defined Sell Signal> || StopS > Ref(StopS, -1);


Could your stops be used in a stop loss and reverse system like this?

I would appreciate your help if you have a chance.

Thanks
Sam
 
Sam,

You can use the stops DLL to create a stop-and-reverse system using the stops as triggers, but it is a little more complicated if you want to do it properly.

The simplest, but least accurate, way would be to just create long and short auto-resetting stops and use those as the stop-loss lines for every trade. However, that line won't always match the stop-loss line you'd get if you started it properly on a particular entry date.

If you want to actually start the stop properly from each entry date, but use the stop-loss as the only exit signal, then it's more complicated and slower. The latest DLL version allows you to pass entry and exit arrays, but I haven't allowed for the stop itself to be the only exit (something I might have to consider adding, but it would be complicated in a SAR system where both long and short stops would need to be used consecutively).

I think I tried this myself some time ago and had to use a loop, where inside the loop I'd create a new stop-loss plot from each entry date to figure out the exit date. If there are a lot of bars and only short periods between reversals, so a lot of entry and exit signals, then the calculation can be quite slow due to the number of calls to the stop calculation function. Ultimately it might be quicker to do the whole thing just in a loop in AFL. The stop/loss function is fairly trivial in AFL, but the CBL one isn't so easy if that's the stop you're using.

With regard to looping, there's a document I wrote called "Looping in AmiBroker AFL.pdf". I think I posted it here somewhere, but also on the AmiBroker Yahoo forum in the files area there. If you can't find it, send me a PM with an email address and I'll email it to you.

Cheers,
GP
 
Thanks GP,

I will read up on looping and give it a go. I will post what I come up with once I have it.

I really appreciate your help and for taking the time to write a document on looping, and for your contributions here.

Thanks!
Sam
 
Thanks GP for this code,
I'm just getting into trading and AmiBroker - just starting to realise that exits are MUCH harder then entries!
It's really useful to have the stop loss referencing your entry postion via the CSV files.
However I was wondering how to also plot just a horizontal line showing my buy price - do you have any pointers on how to do this?

Cheers,
dman
 
dman,

There's a function called "GP_GetBuyPrice" to obtain the purchase price of a stock in a portfolio. If you have multiple purchases, then it gives a weighted average price over all the purchases.

The sample AFL code already has that in there for proper stop-loss plots, in the form:

BuyPrice = GP_GetBuyPrice(sname, pfId);

Since BuyPrice is a pre-defined array, that will set the whole array to the purchase price of the stock (can't remember now if I used that name on purpose or not). So you should just be able to plot that array, eg:

Plot(BuyPrice, "BuyPrice", colorOrange, styleLine|styleThick);

Put that statement at the end of the block where the "BuyPrice = " statement is (ie. after the plot of dummy2).

Cheers,
GP
 
Thanks GP,
Just what I was wanting (except that I would prefer that the line was under the current price for more of my stocks :banghead:)

Cheers,
DMan
 
Great Pig,
I am new to share trading and new to Amibroker. Your AFL code and plugin have greatly helped me understand AFL. Thanks very much for sharing.

I have some programming experience (just a little) and have recently bought ms visual studio (when I found that AFL is not able to handle string arrays). I was wondering if you could also share your program code for your GP_Stops.dll so that I can learn how "traditional programming languages" interact with AFL. What language is GP_Stops written in ?

Thanks and Regards,
KP6



I've developed a plugin DLL for AmiBroker to generate trailing stop plots from any type of data. It calculates a proper trailing stop array from a specified buy date, where the buy date can either come from a portfolio-like file or be passed from the AFL code. With the sample AFL code provided, the latter is used when a date range has been set, making the beginning of the range the buy date.

The DLL also includes a Guppy Count Back Line function to allow that to be used for trailing stops (there's no Count Back entry function though).

The attached ZIP file includes the following files:

GP_Stops.dll - the plugin code
GP_ATRStop.afl - AFL code for an ATR trailing stop indicator
GP_CBStop.afl - AFL code for a Guppy Count Back trailing stop indicator
GP_EMAStop.afl - AFL code for a simple EMA trailing stop indicator
GP Stops DLL.pdf - an instruction sheet
Pfolio1.csv - a sample portfolio file

See the PDF file for instructions.

These files can be freely distributed and modified. The only thing I ask is that if you modify the AFL code and pass it on to someone else, make it clear that it's not my original code by changing the filename or adding a comment to that effect.

Let me know if you have any problems with it or any comments.

Cheers,
GP
 
kp6,

The DLL is written in C/C++ using MS Visual Studio 6. To develop plugins, you need to download the ADK (development kit) from the AmiBroker website. It used to be restricted to members only, but I see that was changed a while ago to be unrestricted, so I can now make this information available.

The complete project has a number of files, most of which haven't significantly changed from what comes in the ADK. The one file with all the function code in it is called "Functions.cpp", which I've attached here with a different name (and ending in ".txt" so that it's possible to attach it here).

I did a quick clean-up of the file to remove some old code and stuff, but haven't tried to pretty it up for presentation too much. Don't bet the farm on all the comments being exactly correct - like most programmers, I sometimes change code and forget to update comments accordingly. They should mostly be okay though.

For a beginner, the most difficult part will likely be all the file handling for the portfolio. That involves setting up a watch folder in Windows and running a separate thread, so requires some knowledge of Windows programming. It also uses linked lists, a common programming construct. The code for the functions called from AFL should be more straight-forward though.

As you are probably aware already, programming code is usually viewed with a non-proportional font (like Courier New). If you use a proportional one, some of the code mightn't line up properly, as I use spaces for indenting rather than hard tabs (except where my editor occasionlly whacks in a hard tab just to annoy me... :D).

Cheers,
GP
 

Attachments

  • Stops DLL Functions.cpp.txt
    25.5 KB · Views: 199
GP,

Thanks VERY much for your reply and the attachment. It will take me a while to understand the program and will, very much, be a very good learning exercise for me.

So, thanks again for your prompt reply, the clean-up and your various advice in your email.

Regards,
KP6
 
GP

Thanks for your plugin, I've been using it purely for the Count Back Line function, and it has been great.
Just built a new PC with XP64 and 64 bit Amibroker. One problem, 32 bit dll does not go. When I found your recent post with the code I thought I would be right, but the ADK is 32 bit.
I'm out of my depth here, but what I think I need is a complier that will compile your code as a 64 bit dll, then I treat it just as I did with your original in the 32 bit version.

Is this a simple thing to do, or will it be like the 64 bit speech engines which just don't seem to be developed yet?

Thanks in advance.
 
Sorry, 64-bit machines and software are things I've had no experience with yet. I'm guessing the 32-bit plugin won't work with the 64-bit Amibroker, but I don't have a 64-bit machine or compiler, so have no idea what's involved in building a 64-bit plugin.

You could maybe try asking Amibroker support or on the Yahoo forum.

Cheers,
GP
 
GP

Thanks for the prompt reply. Yes, they definitely don't work. 64 bit machine wont read 32 bit dll's, when installed as part of the main (64bit) application.
Not all is lost, as you can run 32 bit applications on 64 bit machines, which is what I am doing with Amibroker at present.
I suspect its a significant job, as the 64 bit Amibroker plugin to read Metastock data is still pending. If it was just a simple recompile I'm sure it would have been bundled with the application.

cheers
Tipene
 
GP.
Very fine code that you are sharing with us.
I believe the ATR system is ultimate solution toward proper stop-loss setting.
alot thx indeed

cheers
wiitch:banghead:
 
Thanks for contributing this snippet GP. I was glad I found this post without having to search through your entire post history (forum search function worked well for once!) Remember seeing a it a while back and never downloaded it because I was running fcharts. Finally purchased amibroker a few months ago.

edit - did they reset our post counts here on asf?
 
Thanks for contributing this snippet GP. I was glad I found this post without having to search through your entire post history (forum search function worked well for once!) Remember seeing a it a while back and never downloaded it because I was running fcharts. Finally purchased amibroker a few months ago.

edit - did they reset our post counts here on asf?
I am new to Ami. too and I don't know the AFL very well. With the trailing stops is it just a matter of pasting the code anywhere into a formula?

No post count reset. Have you posted before?
 
I am new to Ami. too and I don't know the AFL very well. With the trailing stops is it just a matter of pasting the code anywhere into a formula?

No post count reset. Have you posted before?

Hey Wysiwyg, I haven't messed around with AFL to much but I do come from a programming background(more specifically object orientated languages like VB and java script). I've pretty much printed out the amibroker manual to get an idea of the functions and conditional statements tonight. I can send you a PM when I attempt running GP's stop plot code with instructions to implement it if you like.

Yeah I've posted before, even entered the competition a few times. That's one bonus I guess I can meet the daily post count again to enter it.

GP seems to have explained it fairly simply in the PDF file but let me know if you need hand with it.
 
Hey Wysiwyg, I haven't messed around with AFL to much but I do come from a programming background(more specifically object orientated languages like VB and java script). I've pretty much printed out the amibroker manual to get an idea of the functions and conditional statements tonight. I can send you a PM when I attempt running GP's stop plot code with instructions to implement it if you like.

Yeah I've posted before, even entered the competition a few times. That's one bonus I guess I can meet the daily post count again to enter it.

I tried backtesting trade formulas from the Ami. site but throwing darts would give better backtest results. :cool: I plan to grow into AFL formulas with Ami. so in no hurry.

Need not send the PM matey, thanks anyway. Strange how your post count has disappeared but your actual posts will still be on the site. Remember any threads ?
 
I tried backtesting trade formulas from the Ami. site but throwing darts would give better backtest results. :cool: I plan to grow into AFL formulas with Ami. so in no hurry.

Need not send the PM matey, thanks anyway. Strange how your post count has disappeared but your actual posts will still be on the site. Remember any threads ?

I think I probably forgot my orginal username I guess though I tend to use the same for most forums I have joined(it would of had 82au on the end because I not that original and tend to use it a lot).

Haven't had a chance to mess with the stop plot yet but I imagine it will involve putting the dll file in the amibroker plugin folder and in the formula editor loading the .afl indicator from the zip file you want. Then applying the indicator. If you already have a few formulas I don't see any problems in pasting it in the same window as long as there isn't a function with an identical name. Setting the variables for the plot is a bit beyond me at the moment. I've really only started looking at backtesting so you are one step ahead of me.
 
Top