Australian (ASX) Stock Market Forum

Amibroker FAQ

The common pitfalls found in back tests and the players are only fooling themselves are using future data, trading with zero delay, trading off high or low values, unreal position sizes and fudging commissions.
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    179.7 KB · Views: 27
  • Untitled1.jpg
    Untitled1.jpg
    196.9 KB · Views: 20
Thanks wysiwyg and stillshort for the post.

I am still looking to automate the strategy as that is my long term objective. I am fully aware of those pitfalls :) but it is a good reminders nonetheless..

Hi Howard, does your book provides sample on the automated intraday trading?
 
Thanks wysiwyg and stillshort for the post.

I am still looking to automate the strategy as that is my long term objective. I am fully aware of those pitfalls :) but it is a good reminders nonetheless..

Hi Howard, does your book provides sample on the automated intraday trading?

Hi Viperace --

Sorry, there is nothing in any of my books about automated intraday trading.

Best,
Howard
 
I am still looking to automate the strategy as that is my long term objective. I am fully aware of those pitfalls :) but it is a good reminders nonetheless..

For Amibroker, I cannot find any broker arrangement to route limit/market orders etc. other than Interactive Brokers. I don't believe IB has accurate data feeds and it is limited to 100 symbols. One also cannot switch on and walk away as line dropouts could be costly plus there is the latency issue. One way around this is a VPS. Another could be a true data feed from IQFeed or ESignal to your Amibroker and then fire the orders from that data to IB.
 
For Amibroker, I cannot find any broker arrangement to route limit/market orders etc. other than Interactive Brokers.

Not true. AmiBroker is open architecture. You are able to build your own order routing to other brokers via AmiBroker. Besides that AmiBroker have released the source codes of IBcontroller http://www.amibroker.com/devlog/2013/06/03/ibcontroller-1-3-8-source-codes-released/ There is no "This can not be done" or "This is not possible".

But besides that is there any better or equally good broker as IB out there? I don't know of any. And in the end "everyone" returns to IB anyway calling himself a sinner.

I don't believe IB has accurate data feeds and it is limited to 100 symbols.
Another half true wisdom. The limit can be exceeded by paying a fee. It is called Quote Booster at a cost of $30/mo per pack of 100 quotes, with a maximum of 1100 quotes total. Wait, small peanuts traders and "free" dreamers are gonna cry out loud now. But yeah, IQFeed may be cheaper while having better data.

Another could be a true data feed from IQFeed or ESignal to your Amibroker and then fire the orders from that data to IB.

Yes, it's possible via Alias field in AmiBroker, for example.
 
But yeah, IQFeed may be cheaper while having better data.

...

IQFeed said:
Just a quick bit of additional information. We have options to get you up to 1800 symbols and the cost would only be $155 per month plus exchange fees. Our sales group can get you information and a trial on this package if you need more than 100 symbols.
 
Not true. AmiBroker is open architecture. You are able to build your own order routing to other brokers via AmiBroker. Besides that AmiBroker have released the source codes of IBcontroller http://www.amibroker.com/devlog/2013/06/03/ibcontroller-1-3-8-source-codes-released/ There is no "This can not be done" or "This is not possible".
Being a bit of a ning nong when it comes to language beyond english, I have sought assistance from those in the know. Pi Trading work with Amibroker .afl as well as other languages.

On 7/04/2014 9:10 PM, Raghav Bihani wrote:
>> 1) What is possible?
>
> Removing the option of porting the code, you can use a relay application to exchange signals and orders between the client (Amibroker) and the server (IB).
>> 2) How much for a Trading Automation .afl?
>
> Unfortunately, due to current commitments and extended workload, we are not accepting new programming projects until 3rd Quarter 2014.
>
> Any further questions or comments, please do not hesitate to contact us.
>
> Regards,
>
> -- Raghav Bihani
> -- Senior Engineer
> -- Pi Trading Corporation
So although being a peanut trader (prefer Nobby's premium mixed at $5.20 per 375grams) I do look forward to any future possibilities.
 
Re: Amibroker FAQ - Stop Loss Coding Question

Hi Group,

I'm not sure that this is a FAQ but there certainly seems to be a lot of brain power here!

I'm wondering can help me out or possibly point me in the right direction. Any suggestions and advice on how to approach this would be very much appreciated.

I've left some comments in the code as I have been working through it but I'm not even sure I am on the right track - so even some advice to say "try this" or "read that" would be great.

I've been working on the AFL below to implement a dynamic stop loss that changes based on the condition of an Index Filter (broader market barometer).

In summary
- After entering a position an initial stop loss is set at 20% below the high of the buy bar
- If the index filter (in this case the the close of the $SPX) is above the 10MA the index filter is up continue to trail the position at the initial stop loss level of 20%
- If the index filter is false/ down, tighten the stop to 5%
- While the index filter is false/ down, continue to trail at 5%
- If the index filter turns up again the stop can be widened (to a maximum of 20%, the original level)as the stock price increases.

Thanks again for any help/ suggestions.

PS - I understand I can use the applystop function but from what I have read I wont be able to plot this on my charts... now thats no fun...

ApplyStop( stopTypeTrailing, stopModePercent, IIf( indexup, 40, 10), 0, True );



Mike

//------------------------------------------//
// Index Filter //
//------------------------------------------//

indexFilter = Foreign("$spx","C"); //use the $SPX as an index filter
indexUp = indexFilter > MA( index, 10); //if the index is trading above the 30 day MA of the index, indexUp is true
ribbonColor = IIf( indexUp , colorGreen, colorRed ); // color the ribbon. If indexUp is true, colorGreen, if not true, Red
Plot( 2, "", ribbonColor, styleArea|styleOwnScale, 0, 100); // plot the ribbon

//------------------------------------------//
// Buy/Sell Rules //
//------------------------------------------//

Buy = Cross( MACD(), Signal() );
Sell = 0;

//------------------------------------------//
// Trailing Stop Loss //
//------------------------------------------//

// Initial stop loss is set at 20% below the high of the bar the stock is purchased
// While the index filter is up/ true the 20% stop loss level is maintained
// If the index filter is down/ false the stop loss is tightened to 5%
// If the price then moves higher while the index is down the stock is trailed
// at 5%. If the prices moves high while the index filter is up/ true the stop
// is increased to a maximum of the original 20%

InitialStopLevel = 1 - Param("Initial Trailing Stop",20,1,40,1)/100; //set initial stop level to 20%
IndexDownStopLevel = 1 - Param("Initial Trailing Stop",5,1,40,1)/100; //set secondary stop level to 5%. Used when index filter is flase/down


trailARRAY = Null;
trailstop = 0;

for( i = 1; i < BarCount; i++ )
{

if( trailstop == 0 AND Buy[ i ] )
{
trailstop = High[ i ] * InitialStopLevel;
}
else Buy[ i ] = 0; // remove excess buy signals

//If IndexUp is fales change the trailstop to IndexDownStopLevel;



if( trailstop > 0 AND Low[ i ] < trailstop )
{
Sell[ i ] = 1;
SellPrice[ i ] = trailstop;
trailstop = 0;
}

if( trailstop > 0 )
{
trailstop = Max( High[ i ] * InitialStopLevel, trailstop );

//trailstop = Max (IIF(IndexUp, InitialStopLevel,IndexDownStopLevel), (high * IIf(IndexUp, InitialStopLevel,



trailARRAY[ i ] = trailstop;
}

}

PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);

Plot( Close,"Price",colorBlack,styleBar);
Plot( trailARRAY,"trailing stop level", colorRed );
 
Re: Amibroker FAQ - Stop Loss Coding Question

PS - I understand I can use the applystop function but from what I have read I wont be able to plot this on my charts... now thats no fun...

ApplyStop( stopTypeTrailing, stopModePercent, IIf( indexup, 40, 10), 0, True );


Wrong, you can plot Applystop. Where have you read that you can't? In Multicharts forum? You just have to look in AB help file (AFL function reference for example) or AB Knowledge base.
 
Re: Amibroker FAQ - Stop Loss Coding Question

Wrong, you can plot Applystop. Where have you read that you can't? In Multicharts forum? You just have to look in AB help file (AFL function reference for example) or AB Knowledge base.

Hi Trash,

Thanks for pointing that out. I didn't read it in the Multicharts Forum.

I took the advice of AB Support who who implied plotting the ApplyStop would not be the best way forward as I would need to use the EQUITY() Function and some additional code anyway. With this in mind I decided using looping code would be the best option.

Thank you very much for your e-mail. You could potentially trigger ApplyStop and write back to Sell/Cover arrays if you used EQUITY() function, but that will require some additional code for calculating the stop position anyway.

On the other hand - for more detailed control over the stop-line plotting – you would need to use FOR loop – see the examples at: http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/


Thanks again for your help. I'll make sure I don't use the Multicharts forum for AB questions.

Cheers

Mike
 
Look, this charts plots an ApplyStop version in first pane and a loop version in second pane. Both plot the same.
image.png
Your message was that you couldn't plot Applystop.
Yes, in ApplyStop version you would have to use Equity() function to evaluate stops.
Yes, if you want to have more control over custom stops then loop provides that.
But I haven't denied that. I have only corrected something.

I didn't read it in the Multicharts Forum.

was just a joke because MC guys post a lot of junk about other vendors.
 

Attachments

  • image.png
    image.png
    19.8 KB · Views: 7
Hi Guys,

Can anyone help out with some advice on how to program fixed fractional position sizing?

I've been going through the manual and found this code:

TrailStopAmount = 2 * ATR( 20 );
Capital = 100000; /* IMPORTANT: Set it also in the Settings: Initial Equity */

Risk = 0.01*Capital;
PositionSize = (Risk/TrailStopAmount)*BuyPrice;
ApplyStop( 2, 2, TrailStopAmount, 1 )

But from what I can tell, this wouldn't dynamically adjust the risk based on the portfolio equity. I've found a few other posts on using the custom back tester but not sure if this would be the simplest approach as they were quite old.

Thanks for any advice you can offer.

Mike
 
Hi Guys,

Can anyone help out with some advice on how to program fixed fractional position sizing?

I've been going through the manual and found this code:

TrailStopAmount = 2 * ATR( 20 );
Capital = 100000; /* IMPORTANT: Set it also in the Settings: Initial Equity */

Risk = 0.01*Capital;
PositionSize = (Risk/TrailStopAmount)*BuyPrice;
ApplyStop( 2, 2, TrailStopAmount, 1 )

But from what I can tell, this wouldn't dynamically adjust the risk based on the portfolio equity. I've found a few other posts on using the custom back tester but not sure if this would be the simplest approach as they were quite old.

Thanks for any advice you can offer.

Mike


Code:
RiskInPoints = 2.0 * ATR( 20 );
StaticVarSet( "Volatility" + Name(), RiskInPoints );

SetCustomBacktestProc( "" );
if ( Status( "action" ) == actionPortfolio )
{
    // retrieve the interface to portfolio backtester
    bo = GetBacktesterObject();
    bo.PreProcess();

    for ( bar = 0; bar < BarCount; bar++ )
    {
        // this retrieves current value of portfolio-level equity
        Eq = bo.Equity;

        // this for loop iterates through all trade signals and adjust pos size
        for ( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar ) )
        {
            Risk = 0.01 * Eq;
            Vola = StaticVarGet( "Volatility" + sig.Symbol );
            sig.PosSize = Risk/Vola[bar]  * sig.Price;
        }
        bo.ProcessTradeSignals( bar );
    }

    bo.PostProcess();
}
 
Hi Trash,

Thanks for helping me out. I can't say I entirely understand the code you have provided but I can see you are using the Custom Back Tester (a new concept for me) so I will do some more reading on the hows and whys of using it.

Thanks!
 
Hi Trash,

Thanks for helping me out. I can't say I entirely understand the code you have provided but I can see you are using the Custom Back Tester (a new concept for me) so I will do some more reading on the hows and whys of using it.

Thanks!

Well, actually in regards to what you want to achieve you don't need to use CBT.
I was just trying to show that in order to access portfolio equity (to calculate more comprehensive stuff) you need to use CBT.

The following one will do the same as the previous CBT procedure.
Code:
Risk = 1 / 100;
Stop = 2 * ATR( 20 ) / BuyPrice;
SetPositionSize( 100 * Risk / Stop, spsPercentOfEquity );

Multiple roads lead to Rome.

Rome == AmiBroker
 
Well, actually in regards to what you want to achieve you don't need to use CBT.
I was just trying to show that in order to access portfolio equity (to calculate more comprehensive stuff) you need to use CBT.

The following one will do the same as the previous CBT procedure.
Code:
Risk = 1 / 100;
Stop = 2 * ATR( 20 ) / BuyPrice;
SetPositionSize( 100 * Risk / Stop, spsPercentOfEquity );

Multiple roads lead to Rome.

Rome == AmiBroker


Thanks Trash... I'm definitely on the slow boat to Rome but loving the learning experience. I'm finding more and more with AmiBroker the answer is "Yes", now what would you like. One small victory at a time the formula comes together.

One other thing I have been pondering is a workflow to ensure the code that is programmed is doing what one intended. I guess this is a debugging process? In my limited programming experience (10 years ago) with multimedia applications (Macromedia Director, Flash etc) it was a very visual process and it was quite obvious (from a visual perspective) if the program was operating as expected. Can you make any suggestions on workflow or overall approach to programming in AmiBroker?
 
It's my first post, so I want say Hello, from Poland ;)

I have a little question. Is possible set position size of 10% equity by not less than 1600? (damn high provision :mad: )

e.g
Initial Equity: 10000
max open positions: 5 or whatever

In my code I set:
SetPositionSize( 10, spsPercentOfEquity ); thats great but how set no less than 1600?
 
Top