@happyFace typically members of this forum like to see that you've put some effort into finding a solution rather than just asking someone else to spend their valuable time writing free code for you. Here are a few of the AmiBroker functions that you will need to create your solution:
LLV()
Ref()
MA()
Write yourself a bit of code, and then test it using an Exploration, for which you will also need to learn about the following:
Filter
AddColumn()
If your AFL is not working as expected, come back and show us your code, and describe how the output is different than what you hoped.
rallyDays = llvbars( Low, 30); // Why does this return a array? From documentation it should just return a //integer?
if(rallyDays >= 4) //Trying to check if days since lowest low is bigger than 3.
{
//Check if close is > +2%.
percentChange = Close/Ref(Close, -1);
if(percentChange > 1.02) //
{
//Buy Signal!
Buy = True;
PlotShapes(Buy*shapeUpArrow, colorGreen,0);
}
}
I'm very confused by all this. Help would be appreciated!
What kind of documentation have you read since your claim of documentation telling different things than what is returned by formula is far from reality? Do not close your eyes while reading.
Thank you both! I admit I was wrong.
Now I have a new problem..
cond1 = llvbars( Low, 30 ) >= 4;
cond2 = ( Close * 1.01 ) > ( Ref( Close, -1 ) );
Buy = cond1 AND cond2;
lastBuyDay = BarsSince(Buy); // <---- Will mostly be 0 :/
lastBuyDayLow = ref( Low, -lastBuyDay ); //I want lastBuyDayLow to have the value of the low of the bar/day the buy-signal was triggered.
cond3 = Low < lastBuyDayLow;
Sell = cond3;
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
BarsSince(Buy) seem to be triggered alot, the value is mostly 0. I think it might work if BarsSince(Buy) is loaded after doing the ExRem method. But then Buy and Sell will not be defined yet..
Do anyone know how to make this work?
Thanks, but.. The problem is a Buy signal is generated very often. But it is not a real buy because you need a sell signal before a buy:
"Buy1" -> "Buy2" //"Buy2" is not a real buy.
Sell -> Buy //Real buy.
Buy -> Buy
ValueWhen(Buy,Low); // Gives the low of a false buy signal.
"Sell1" -> "Buy1" -> "Buy2" //Need to get the value of the real signal which is "Buy1"
// more examples you may need
buy1 = cross( close, ema(close,9) ) ; //Gives a "1" or true on the day that ARRAY1 crosses above ARRAY2
Buy = buy1; //put YOUR final CONTITION here
LowOfBuyBar= ValueWhen(Buy,Low); // when the Signal BUY occur give the Low
LastBuyDayLow = LastValue(LowOfBuyBar);
cond1 = llvbars( Low, 30 ) >= 4;
cond2 = ( Close * 1.01 ) > ( Ref( Close, -1 ) );
Buy = cond1 AND cond2; //Buy generated very often. But can only really buy (after a Sell) on "Buy1": Sell1 -> Buy1 -> Buy2.
lastBuyDay = BarsSince(Buy); //Will point to BarsSince("Buy2"), I want it to point to the real "Buy1".
lastBuyDayLow = ref( Low, -lastBuyDay ); //Will get wrong value, since "lastBuyDay" is "Buy2" which is not the real buy day.
cond3 = Low < lastBuyDayLow; //"lastBuyDayLow" is wrong value.
Sell = cond3;
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
cond1 = llvbars( Low, 30 ) >= 4;
cond2 = ( Close ) > (( Ref( Close, -1 ) )* 1.01);
Buy = cond1 AND cond2; //Buy generated very often. But can only really buy (after a Sell) on "Buy1": Sell1 -> Buy1 -> Buy2.
lastBuyDay = BarsSince(Buy); //Will point to BarsSince("Buy2"), I want it to point to the real "Buy1".
lastBuyDayLow = ref( Low, -lastBuyDay ); //Will get wrong value, since "lastBuyDay" is "Buy2" which is not the real buy day.
cond3 = Low < lastBuyDayLow; //"lastBuyDayLow" is wrong value.
Sell = cond3;
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
So this doesn't work in Amibroker?
Can I make a big loop instead?
@happyFace, I think you need to CAREFULLY define what you want to do.
If you specify day by day what you want to happen, you might discover how to code it.
Can you define your buy conditions so they are unique?
Are you using an Exploration and tracking the items that you think make up your buy conditions so you can see what the system is telling you. Then maybe you can alter them to get the results you want.
Or describe a situation with OHLC values and show us what values you want to use.
People are trying to help, but we don't understand what you are asking.
The topic for this thread could be answered with the Ref function, but I am assuming you have looked at the Ref function already. If not check it out.
@happyFace you have a Sell condition that is dependent on the Buy condition, and no way to remove extra Buy signals using array logic until the Sell has been defined. A loop is one way to solve this problem. Another would be to use ApplyStop, which is how you would typically implement an exit which is dependent on the entry.
As @snoopy.pa30 pointed out, it might be worthwhile to create an exploration so that you can confirm that your code is really producing the results that you expect, and also to see if there might be additional logic that might make it possible to ExRem your Buy signals so that you don't have to resort to looping.
@happyFace unless this is just an exercise in code writing, don't you think you need to reassess your system's logic? I have not followed this whole discussion so forgive me if I am mistaken but you want your Sell logic to be "exit below the Low of Long Entry Bar"???
@happyFace, This diagram is helpful. First issue I see is that you want to buy on the bar that you get the buy signal. Can't be done - well actually can't be done systematically in real life.... So, you get the signal on the bar 4 of your diagram, and can make the buy on bar 5. So then you need to decide what buy price you want to use. Typically it would be the open on the next bar - or bar 5 here. Then you need to decide what is the Stop price that you want to use. Since you are on bar 5 for the bar, it could be the Close of bar 4, which you can easily get using REF function. Then you need to control your system to not issue a buy signal until you have had a sell. Again, this is code that you can write.
Since @mradtke in post 2 suggested the functions you might need, and I suggested an exploration so you can see the various variables as you move forward, I would suggest you alter your code to be an exploration and see what you are getting in more detail. Then post it here with your observations or issues.
It is kind of basic.. there has already been alot of input into this by members of the community. Please
use the suggestions and / or study the relevant sections to your particular issue. Everything is
in the help file along with examples.