Australian (ASX) Stock Market Forum

Amibroker FAQ

Hi to all
I am writing a code .
but ,
I don't know , why my Code for Buy Stop order only work for 1 Day after and dont work for 2 Days or 3Day after,...
if you know my problem, please help me.

Thank you
Code:
BuyStop =  Ref(HHV(High,1),-1); 
Buy=Ref(Close,-1)<Ref(Open,-1) AND Cross(High,BuyStop);
BuyPrice=Max(BuyStop,Low);
  
Sell = MA(C,5)<Close;
Sell=ExRem(Sell,Buy);

View attachment 66117

Greetings --

The first line sets the BuyStop as the highest high for the 1 day period, as of yesterday. If that logic is what you intended, you do not need the HHV function and it would be more straight-forward to write:
BuyStop = Ref(H,-1);

The second line computes the Buy signal.
Buy is set to True when two conditions are both met on the same bar:
1. Yesterday's close is below yesterday's open.
2. Today's High is greater then BuyStop, but it was not yesterday.
Alternative code for rule 2, given the code posted, is:
2. H > Ref(H,-1);

The problem may be in the first line -- BuyStop is reset every day when you intended to have it retain its value for a longer period.

Best,
Howard
 
Greetings --

The first line sets the BuyStop as the highest high for the 1 day period, as of yesterday. If that logic is what you intended, you do not need the HHV function and it would be more straight-forward to write:
BuyStop = Ref(H,-1);

The second line computes the Buy signal.
Buy is set to True when two conditions are both met on the same bar:
1. Yesterday's close is below yesterday's open.
2. Today's High is greater then BuyStop, but it was not yesterday.
Alternative code for rule 2, given the code posted, is:
2. H > Ref(H,-1);

The problem may be in the first line -- BuyStop is reset every day when you intended to have it retain its value for a longer period.
Best,
Howard

Hi Mr.Howard
Thanks for the answers.
My problem is solve with your help.
Code:
BuyStop=Ref(High,-1);
mode1=Ref(Close,-1)<Ref(Open,-1) AND H>Ref(High,-1);
mode2=Ref(Close,-2)<Ref(Open,-2) AND Ref(High,-1)<Ref(High,-2) AND H>Ref(High,-2));
Buy=mode1 OR mode2 ; 
BuyPrice=Max(BuyStop,Low);
Sell=MA(C,8)<Close;
Sell=ExRem(Sell,Buy);

But I have a question, How Can I have Several mode for Buy and Buy Stop in a Code.
for example:
mode1=.....
mode2=.....
mode3=.....
Buy=mode1 OR mode2 OR mode3;
.
.
.
BuyPrice=Max(BuyStop1,Low);
BuyPrice=Max(BuyStop2,Low);
BuyPrice=Max(BuyStop3,Low);
 
But I have a question, How Can I have Several mode for Buy and Buy Stop in a Code.
Ask yourself the question how the hell you can buy exactly at the extremity (e.g. high or low price) of a bar? Gotta be realistic otherwise you are wasting your time. On top of that the idea will not be profitable.
 
Ask yourself the question how the hell you can buy exactly at the extremity (e.g. high or low price) of a bar? Gotta be realistic otherwise you are wasting your time. On top of that the idea will not be profitable.

It is very profitable for me.Because I am learning programming , But it is not profitable for you.Because you know programming.
Regards
 
It is very profitable for me.Because I am learning programming , But it is not profitable for you.Because you know programming.
Regards

Greetings --

Wysiwyg's response is very appropriate.

You do need to become an excellent programmer in the language of whatever trading system development platform you use. That is only part of the skill set you need.

W's comment is not related to learning to program AmiBroker's afl. It is related to the practicalities of trading whatever system might result. Be aware of conditions that test well but are impossible to trade.

One is buying at a price that is not known until the opportunity to trade at the price has passed -- such as buying at the close of trading at the lowest price of the day.

Another is using data that is not yet known as part of the signal generation (known as a "future leak") -- such as including Ref(C,1) in a rule.

Both of these appear to be easily recognized and easily avoided. And in these examples they are. As the programs you write become more complex, the errors will become more subtle and more difficult to detect.

The advice you are hearing is two-fold:
1. Develop your skills as a system's developer.
2. Develop your skills as a programmer.

You need both.

Best,
Howard
 
I am playing around with index filters, is there a way to use an optimization to change filters?

ie something like:

Counter = Optimize("IndexSelection",1,1,2,1);

Index =iif(Counter==1,Foreign( "XAO", "C", True ), Null);
Index =iif(Counter==2,Foreign( "XJO", "C", True ), Null);

Above doesnt work, but might help better explain what I am try to do...

JJZ
 
IIf function is either true or false according to the expression. If true XAO is the Index, if false XJO is the Index.

Counter = 1;

IXAO = Foreign("XAO", "C");
IXJO = Foreign("XJO", "C");

Index = IIf(Counter, IXAO, IXJO);

Note they are very similar indices.
 
It is very profitable for me.Because I am learning programming , But it is not profitable for you.Because you know programming.
Regards
I am only a novice, mate. A novice still because price action is always a little bit different than it was before.
 
IIf function is either true or false according to the expression. If true XAO is the Index, if false XJO is the Index.

Counter = 1;

IXAO = Foreign("XAO", "C");
IXJO = Foreign("XJO", "C");

Index = IIf(Counter, IXAO, IXJO);

Note they are very similar indices.

Thanks, working now. :xyxthumbs Thought it was more like the traditional if/then/else style command.

JJZ
 
Thanks, working now. :xyxthumbs Thought it was more like the traditional if/then/else style command.

JJZ
Well it should not work becuase the return will always be XAO with the statement as I put. :D

Really though, what part of index data do you want to optimise for the past?
 
Well it should not work becuase the return will always be XAO with the statement as I put. :D

Really though, what part of index data do you want to optimise for the past?

When you said it was looking for a logical value, I realised what the issue was, I switched to a regular if command.

Not trying to optimize, just using the optimize function to loop through different indexes to see how it effects my codes performance.

Counter = Optimize("Index",1,1,3,1);

if(Counter==1)
{
Index =Foreign( "XAO", "C", True );
}
if(Counter==2)
{
Index = Foreign( "XJO", "C", True ) ;
}

JJZ
 
Hi, could someone guide me to code to be able to reflect stops as follows:

1. The initial stop is just below the last significant trough.

2. Each time the price makes a new high for the trend by rising above
the last peak, the sell stop level for the whole position is trailed up to just
below the last significant confirmed trough

This is shown as per the attached.

Stop level.png

The idea is taken from Colin Nicholson's book Building Wealth in The Stock Market.

Any help will be appreciated.
 
Hi, could someone guide me to code to be able to reflect stops as follows:

1. The initial stop is just below the last significant trough.

2. Each time the price makes a new high for the trend by rising above
the last peak, the sell stop level for the whole position is trailed up to just
below the last significant confirmed trough

This is shown as per the attached.

View attachment 66146

The idea is taken from Colin Nicholson's book Building Wealth in The Stock Market.

Any help will be appreciated.

Search "scaling into positions", because that's what you're trying to do.

Then use this code by EM Pottasch as a start.


// AFL code by Edward Pottasch, Feb 2013

_SECTION_BEGIN("Price1");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
SetChartBkGradientFill(ColorRGB(229,229,229),ColorRGB(229,229,229));
_SECTION_END();



nbar = Param("nbar",5,2,50,1); // number of bars on each side

// define fractals
PHigh = H > Ref(HHV(H,nbar),-1) AND Ref(HHV(H,nbar),nbar) <= H;
PHighPrice = ValueWhen(PHigh,H);

PLow = L < Ref(LLV(L,nbar),-1) AND Ref(LLV(L,nbar),nbar) >= L;
PLowPrice = ValueWhen(PLow,L);

// chart
GraphXSpace = 5;
SetChartOptions(0, chartShowDates);

PriorHigh = ValueWhen(PLow,PHighPrice);
PriorLow = ValueWhen(PHigh,PLowPrice);
PlotShapes(shapeSmallCircle*PLow,colorGreen,0,L,-10);
PlotShapes(shapeSmallCircle*PHigh,colorRed,0,H,10) ;
//Plot( PHigh, "High_flag", colorRed, styleHistogram | styleOwnScale );
//Plot( PLow, "Low_flag", colorGreen, styleHistogram | styleOwnScale );

Plot( PHighPrice, "High",colorBlue );
Plot( PLowPrice, "Low",colorBrown );

//Plot( PriorHigh,"Prior_H",colorGreen );
//Plot( PriorLow,"Prior_L",colorViolet );

Buy = V*C>250000 AND Cross(c, phighprice);
Sell = 0;
 
Search "scaling into positions", because that's what you're trying to do.

Then use this code by EM Pottasch as a start.


Code:
nbar = Param("nbar",5,2,50,1); // number of bars on each side

    // define fractals
    PHigh = H > Ref(HHV(H,nbar),-1) AND Ref(HHV(H,nbar),nbar) <= H;
    PHighPrice = ValueWhen(PHigh,H);

    PLow = L < Ref(LLV(L,nbar),-1) AND Ref(LLV(L,nbar),nbar) >= L;
    PLowPrice = ValueWhen(PLow,L);

This gives me food for thought however, looking at this code the HHV & LLV are defined by a fix parameter, "nbar", in the case of the example code is 5 bars.

The issue is that there can be varying lengths between the last high and the next peak. So having a fixed "nbar" will not pick each peak or trough.

I will try and mull over what you have given to me. Unfortunately my coding ability is limited.
 
I'd like some help to code this please:

HHV since the day's open < open

Using 1 minute data. Assume opening bar is at TimeNum() == 100700;

Thanks.
 
Is there an easy way to adjust a stop based on holding time?

For example, purchase a stock with a 15% stop loss:

ApplyStop( stopTypeLoss, stopModePercent, 15, 2, 0, 0 );

Then after 30 days reduce it to:

ApplyStop( stopTypeLoss, stopModePercent, 10, 2, 0, 0 );

Thanks
JJZ
 
Is there an easy way to adjust a stop based on holding time?

For example, purchase a stock with a 15% stop loss:

ApplyStop( stopTypeLoss, stopModePercent, 15, 2, 0, 0 );

Then after 30 days reduce it to:

ApplyStop( stopTypeLoss, stopModePercent, 10, 2, 0, 0 );

Thanks
JJZ

If you want to tighten things after a certain amount of time, an indicator would be a better option.

SAR tightens up with time. That's its selling point.
 
If you want to tighten things after a certain amount of time, an indicator would be a better option.

SAR tightens up with time. That's its selling point.

Thanks Gringotts Bank, I haven't heard of SAR before, will check it out.

I take it there's no simple way to adjust the stoploss precent on the fly then?


JJZ
 
Is there an easy way to adjust a stop based on holding time?

For example, purchase a stock with a 15% stop loss:

ApplyStop( stopTypeLoss, stopModePercent, 15, 2, 0, 0 );

Then after 30 days reduce it to:

ApplyStop( stopTypeLoss, stopModePercent, 10, 2, 0, 0 );

Thanks
JJZ

If you can use looping it would be pretty easy.
 
Top