Australian (ASX) Stock Market Forum

Amibroker FAQ

Re: User-definable functions -- precise usage

Hi,
first-time post

Welcome to ASF :)

But I can only get the above example to work if I have the whole function as inline code directly above the call. Clearly this is not the intention. What am I doing wrong? How/where do I save the function so that it is found by a call in another piece of code?

If you want to use that function in other AFL's then you would save it in your "Include" folder (or any other folder but "Include" folder is called by default) then call it using the "include" function in your other AFL. All explained in the link below:

http://www.amibroker.com/guide/afl/afl_view.php?id=1
 
Hi

I just setup AB with IB I want to load in all the symbols i have data for, is there a list anywhere of the all symbols in TWS that i can use to import into AB

Thanks
 
This should work

SetPositionSize( 100, spsPercentOfEquity );
SetBacktestMode( backtestRegular ); //signal-based backtest, redundant signals are removed

BuyPrice = SAR() + 0.01;
SellPrice = SAR() - 0.01;
Buy = Buyprice>=l and Buyprice<=h and cross(C,SAR());
Sell =Sellprice>=l and Sellprice<=h and cross(SAR(), C);
buy = ExRem( buy, sell );
sell = ExRem( sell, buy );

Hi, may I know these:

1. I think C in cross(C,SAR() means using close price to do the cross, if I use cross(Open,SAR()). then I use the open price to compare with the SAR at the open price right? I guess SAR in amibroker would not always use the close SAR even I do the (Open, SAR())?
2. Even I do SetBacktestMode( backtestRegular );, I still need to do the EXRem? What does the EXRem really do?

Thx!
 
This should work

SetPositionSize( 100, spsPercentOfEquity );
SetBacktestMode( backtestRegular ); //signal-based backtest, redundant signals are removed

BuyPrice = SAR() + 0.01;
SellPrice = SAR() - 0.01;
Buy = Buyprice>=l and Buyprice<=h and cross(C,SAR());
Sell =Sellprice>=l and Sellprice<=h and cross(SAR(), C);
buy = ExRem( buy, sell );
sell = ExRem( sell, buy );

I am still very confused that does the "C" in cross(C,SAR()); means current price or close...

If I want to just buy when the SAR cross within the price between day high and low (which is all possible price within that day), does it make sense to have cross(C,SAR());? Is there a way to use the "current price" for this concept?

thx!

- - - Updated - - -

This should work

SetPositionSize( 100, spsPercentOfEquity );
SetBacktestMode( backtestRegular ); //signal-based backtest, redundant signals are removed

BuyPrice = SAR() + 0.01;
SellPrice = SAR() - 0.01;
Buy = Buyprice>=l and Buyprice<=h and cross(C,SAR());
Sell =Sellprice>=l and Sellprice<=h and cross(SAR(), C);
buy = ExRem( buy, sell );
sell = ExRem( sell, buy );

I just backtest these codes..no trades for 15 years data...something wrong....:cry:
 
Hi, may I know these:

1. I think C in cross(C,SAR() means using close price to do the cross, if I use cross(Open,SAR()). then I use the open price to compare with the SAR at the open price right? I guess SAR in amibroker would not always use the close SAR even I do the (Open, SAR())?
2. Even I do SetBacktestMode( backtestRegular );, I still need to do the EXRem? What does the EXRem really do?

Thx!

1. C can mean current price or close.
2. ExRem is not needed in backtest mode. It is only meant for plotting signals and removing redundant ones.
 
I am still very confused that does the "C" in cross(C,SAR()); means current price or close...

If I want to just buy when the SAR cross within the price between day high and low (which is all possible price within that day), does it make sense to have cross(C,SAR());? Is there a way to use the "current price" for this concept?

thx!

- - - Updated - - -



I just backtest these codes..no trades for 15 years data...something wrong....:cry:

My code (above) had an error. I specified Close>SAR(), but also specified a buyprice. So I was asking AB to buy intra-day before the closing price was known. It looked into the future. Just remove the buyprice and sellprice lines.

It's been so long since you've posted I can't remember what it was you were wanting to do.

As joshmi says, C is either the close or the last price (if calculated intra-day).
 
My code (above) had an error. I specified Close>SAR(), but also specified a buyprice. So I was asking AB to buy intra-day before the closing price was known. It looked into the future. Just remove the buyprice and sellprice lines.

It's been so long since you've posted I can't remember what it was you were wanting to do.

As joshmi says, C is either the close or the last price (if calculated intra-day).

hi and thx. I asked you before how to do these:

How to change this AFL codes to
Sell =(SAR() > C) + "only sell if the price is higher than the bought price"?

You told me to try sellPrice>BuyPrice;

Here is my code:

Buy = BuyPrice>=L AND BuyPrice<=H AND Cross(Open,SAR());
Sell =SellPrice>=L AND SellPrice<=H AND Cross(SAR(), Open) AND SellPrice>BuyPrice;
BuyPrice = Open;
SellPrice = Open;
SetPositionSize( 100, spsPercentOfEquity );
SetBacktestMode( backtestRegular );

then it only bought once then never sold in 15 years of data:(
 
hi and thx. I asked you before how to do these:

How to change this AFL codes to
Sell =(SAR() > C) + "only sell if the price is higher than the bought price"?

You told me to try sellPrice>BuyPrice;

Here is my code:

Buy = BuyPrice>=L AND BuyPrice<=H AND Cross(Open,SAR());
Sell =SellPrice>=L AND SellPrice<=H AND Cross(SAR(), Open) AND SellPrice>BuyPrice;
BuyPrice = Open;
SellPrice = Open;
SetPositionSize( 100, spsPercentOfEquity );
SetBacktestMode( backtestRegular );

then it only bought once then never sold in 15 years of data:(


And this has already been answered!
https://www.aussiestockforums.com/f...t=1679&page=92&p=783898&viewfull=1#post783898

If the price at buy has not been reached again of course then you won't see an exit.

If you want an earlier exit then you have to add a stop.

Below is the code again but with Sell shapes and price. Missing is a stop.

Code:
SetPositionSize( 10, spsPercentOfEquity );

acc  = 0.02;
accm = 0.2;
sarc = SAR( acc, accm );

Buy   = Sell = 0;
Short = Cover = 0;
YourBuyConditions = SARc < C;
YourSellConditions = SARc > C;

priceatbuy = Null;
patba = Null; // Price At Buy Array (for plotting only)

for ( i = 0; i < BarCount; i++ )//
{
    SellSignal = YourSellConditions[ i ] AND Close[ i ] > priceatbuy;

    if ( SellSignal AND NOT IsNull( priceatbuy ) )
    {
        Sell[ i ] = 1;
        priceatbuy = Null;
    }

    BuySignal = YourBuyConditions[ i ];

    if ( IsNull( priceatbuy ) AND BuySignal )
    {
        Buy[ i ] = 1;
        priceatbuy = BuyPrice[ i ];
    }

    patba[ i ] = priceatbuy; // to plot you need an ARRAY, not scalar
}

_SECTION_BEGIN("Price");
SetChartOptions( 0, chartShowArrows | chartShowDates | chartWrapTitle );
_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 ) );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle(), 0, 1, 0, 0);
_SECTION_END();


Plot( patba, "priceatbuy", colorGreen, styleDots | styleNoLine, 0, 1, 0, 1 );
Plot( SARc, _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle( "Style", styleDots | styleNoLine, maskDefault | styleDots | styleNoLine ) );

PlotShapes( Sell*shapeDownArrow, colorRed, 0, H, -15 );
PlotShapes( Sell*shapeHollowDownArrow, colorWhite, 0, H, -15 );
PlotShapes( Sell*shapeHollowSmallCircle, colorRed, 0, SellPrice, 0 );
 
Has already been discussed here.

One of million ways:

Code:
SetPositionSize( 10, spsPercentOfEquity );

acc  = 0.02;
accm = 0.2;
sarc = SAR( acc, accm );

Buy   = Sell = 0;
Short = Cover = 0;
YourBuyConditions = SARc < C;
YourSellConditions = SARc > C;

priceatbuy = Null;
patba = Null; // Price At Buy Array (for plotting only)

for( i = 0; i < BarCount; i++ )//
{
  SellSignal = YourSellConditions[ i ] AND Close[ i ] > priceatbuy;

  if( SellSignal AND NOT IsNull( priceatbuy ) )
  {
    Sell[ i ] = 1;
    priceatbuy = Null;
  }

  BuySignal = YourBuyConditions[ i ];

  if( IsNull( priceatbuy ) AND BuySignal )
  {
    Buy[ i ] = 1;
    priceatbuy = BuyPrice[ i ];
  }

  patba[ i ] = priceatbuy; // to plot you need an ARRAY, not scalar
} 

Plot(patba, "priceatbuy", colorGreen, styleLine );
Plot( SARc, _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style", styleDots | styleNoLine, maskDefault | styleDots | styleNoLine ) );



What do you mean? Show an example.

thx. I tried to change the code to use open price, but then it has 80% win instead of 100%, I am still thinking where is the problem:

Code:
SetPositionSize( 100, spsPercentOfEquity );

acc  = 0.02;
accm = 0.2;
sarc = SAR( acc, accm );

Buy   = Sell = 0;
Short = Cover = 0;
YourBuyConditions = SARc < Open;
YourSellConditions = SARc > Open;

priceatbuy = Null;
patba = Null; // Price At Buy Array (for plotting only)

for( i = 0; i < BarCount; i++ )//
{
  SellSignal = YourSellConditions[ i ] AND Open[ i ] > priceatbuy;

  if( SellSignal AND NOT IsNull( priceatbuy ) )
  {
    Sell[ i ] = 1;
    priceatbuy = Null;
  }

  BuySignal = YourBuyConditions[ i ];

  if( IsNull( priceatbuy ) AND BuySignal )
  {
    Buy[ i ] = 1;
    priceatbuy = BuyPrice[ i ];
  }

  patba[ i ] = priceatbuy; // to plot you need an ARRAY, not scalar
} 

BuyPrice = Open;
SellPrice = Open;

Plot(patba, "priceatbuy", colorGreen, styleLine );
Plot( SARc, _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style", styleDots | styleNoLine, maskDefault | styleDots | styleNoLine ) );
 
If I did not make a mistake then it should be this way for entry at next bar's open after signal.

Code:
SetPositionSize( 10, spsPercentOfEquity );

delay = 1;
SetTradeDelays( delay, delay, delay, delay );

acc  = 0.02;
accm = 0.2;
sarc = SAR( acc, accm );

Buy   = Sell = 0;
Short = Cover = 0;
BuyPrice = SellPrice = Open;

YourBuyConditions = SARc < C;
YourSellConditions = SARc > C;

priceatbuy = Null;
patba = Null; // Price At Buy Array (for plotting only)

for ( i = 1; i < BarCount; i++ )//
{
    SellSignal = YourSellConditions[ i - 1 ] AND Close[ i - 1 ] > priceatbuy;

    if ( SellSignal AND NOT IsNull( priceatbuy ) )
    {
        Sell[ i - 1 ] = 1;
        priceatbuy = Null;
    }

    BuySignal = YourBuyConditions[ i - 1 ];

    if ( IsNull( priceatbuy ) AND BuySignal )
    {
        Buy[ i - 1 ] = 1;
        priceatbuy = BuyPrice[ i ];
    }

    patba[ i ] = priceatbuy; // to plot you need an ARRAY, not scalar
}

_SECTION_BEGIN("Price");
SetChartOptions( 0, chartShowArrows | chartShowDates | chartWrapTitle );
_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 ) );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle(), 0, 1, 0, 0);
_SECTION_END();


Plot( patba, "priceatbuy", colorGreen, styleDots | styleNoLine, 0, 1, 0, 1 );
Plot( SARc, _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle( "Style", styleDots | styleNoLine, maskDefault | styleDots | styleNoLine ) );

PlotShapes( Buy*shapeUpArrow, colorGreen, 0, H, -15 );
PlotShapes( Buy*shapeHollowUpArrow, colorWhite, 0, H, -15 );
PlotShapes( Ref(Buy, -1)*shapeHollowSmallCircle, colorGreen, 0, BuyPrice, 0 );

PlotShapes( Sell*shapeDownArrow, colorRed, 0, H, -15 );
PlotShapes( Sell*shapeHollowDownArrow, colorWhite, 0, H, -15 );
PlotShapes( Ref(Sell, -1)*shapeHollowSmallCircle, colorRed, 0, SellPrice, 0 );
 
Hi,

I'm new to AmiBroker and have an AFL related question someone can hopefully help me with (I have searched the forum but could not find an answer to my question).



In addition to the system 'Sell' condition I would like to add a time-based Exit, e.g.,

Sell = ConditionXYZ OR BarsSince(Buy) >= 10;

The problem is that buy signals following entry re-set the 'BarsSince(Buy)' time-frame. I would like to remove the extra buy signals, the problem is that 'Buy = ExRem(Buy,Sell);' does not seem to remove the signals? (i.e., it does remove supefluous arrows on the chart, however when checking backtesting results there are still many trades well in excess of the time-limit).

I'm assuming the way around this involves writing a Loop. My guess at this stage would be something like this:

OpenPos = False;
for(i=0;i<BarCount;i++)
{
IIF(OpenPos,Buy=False,Buy=True);
}

The problem is that AmiBroker does not seem to recognize the 'OpenPos' variable ... although searching the web it seems it should?

Any ideas?
 
Hi,

I'm new to AmiBroker and have an AFL related question someone can hopefully help me with (I have searched the forum but could not find an answer to my question).



In addition to the system 'Sell' condition I would like to add a time-based Exit, e.g.,

Sell = ConditionXYZ OR BarsSince(Buy) >= 10;

The problem is that buy signals following entry re-set the 'BarsSince(Buy)' time-frame. I would like to remove the extra buy signals, the problem is that 'Buy = ExRem(Buy,Sell);' does not seem to remove the signals? (i.e., it does remove supefluous arrows on the chart, however when checking backtesting results there are still many trades well in excess of the time-limit).

...

Any ideas?


Apologies, completely forgot about the stop function https://www.aussiestockforums.com/forums/images/smilies/redface.gif

ApplyStop(StopTypeNBar,StopModeBars,10);
 
Hi,

I'm new to AmiBroker and have an AFL related question someone can hopefully help me with (I have searched the forum but could not find an answer to my question).



In addition to the system 'Sell' condition I would like to add a time-based Exit, e.g.,

Sell = ConditionXYZ OR BarsSince(Buy) >= 10;

The problem is that buy signals following entry re-set the 'BarsSince(Buy)' time-frame. I would like to remove the extra buy signals, the problem is that 'Buy = ExRem(Buy,Sell);' does not seem to remove the signals? (i.e., it does remove supefluous arrows on the chart, however when checking backtesting results there are still many trades well in excess of the time-limit).

I'm assuming the way around this involves writing a Loop.

No, there is no problem. And it can be done without loop and without ApplyStop.

In your case
Code:
HoldBars = 10;
Sell = ConditionXYZ OR ExRemSpan( Ref( Buy, -HoldBars ), HoldBars );


But Tomasz recommends using Applystop because:

Hello,

It can be done without loops too.
However ApplyStop is recommended as it handles all cases and works fine together with other stops.

Best regards,
Tomasz Janeczko
amibroker.com



ExRem(); is only needed for removing excessive signals in charts or exploration/scan. Backtester takes care of it itself. In addition there you have function SetBacktestMode available.
 
No, there is no problem. And it can be done without loop and without ApplyStop.

In your case
Code:
HoldBars = 10;
Sell = ConditionXYZ OR ExRemSpan( Ref( Buy, -HoldBars ), HoldBars );

But Tomasz recommends using Applystop

ExRem(); is only needed for removing excessive signals in charts or exploration/scan. Backtester takes care of it itself. In addition there you have function SetBacktestMode available.



Excellent! Many thanks.
 
Top