I need to calculate the no. of bars from the buy or short signal buy0 and short0 are my systems signals and the counter should turn zero when the sell0 and cover0 signals are generated. I have written the below code but it has 2 issues -
There’s an inbuilt function BarsSince that counts the number of bars from the last time any array was True.
To avoid the count restarting if you have intervening Buy0 signals between the initial Buy0 and Sell0 (which may be what you’re trying to overcome in the loop), you can simply eliminate the extraneous signals with ExRem.
As an aside, the reason your BuyCounter is starting at 2 is you immediately add 1 to it on the very next line after it’s initially set to 1 (perhaps it should initially be 0). You’re also saving BuyCounter as a scalar rather than an array which may be why you are seeing strange results, although it’s hard to tell without seeing the rest of the code.
@sovit, rather use ApplyStop for available proper n-bar stop instead of as suggested BarsSince(). https://www.amibroker.com/guide/afl/applystop.html
Using BarsSince is rather problematic if looking for regular n-bar stop. (It’s problematic because newbies often wonder why it isn’t doing what was expected if being used as nbar stop).
numbars = 50; // or you may use: IIf( Buy, 50, /*if Short then*/ 40 ); // number of bars till exit
ApplyStop( stopTypeNBar, stopModeBars, numbars, 1 );
I suppose u did not get me, I want to know the no. of bars from the buy / short signals so that I can put that no.as range to get the hhv / llv from the buy / short signal. Pls help if possible
I have a profitable strategy but the issue with it is it leaves a lot of earned profits, I need support to write a code that if a threshold percent profit (say 10%) is hit then exit if a certain percent (say 40%) of the maximum potential profit (buy price - the highest high after the buy signal) is lost.
I have tried Applystop, barsince, highestsince, looping but was unable to get the desired result. Can anyone guide in this matter.
Dear Rajendran,
Can u help me with the below mentioned issue:
I have a profitable strategy but the issue with it is it leaves a lot of earned profits, I need support to write a code that if a threshold percent profit (say 10%) is hit then exit if a certain percent (say 40%) of the maximum potential profit (buy price - the highest high after the buy signal) is lost.
I have tried Applystop, barsince, highestsince, looping but was unable to get the desired result.
Have been working on the above as per your recommendation but hv not been able to crack the problem, below is the code I have written, pls identify my mistake.
The problem is that TSL is giving 0 value and TSLLDIFFARR is giving {Empty} value.