Yea, I figured this was some catch 22 situation. I guess I need a course in weird array manifestations because I really don't understand why the array logic in AFL gets all screwed up under these conditions. Once the buy array has valid data, not nulls, then I don't understand why barssince can't see 0 or 1 and work correctly. I tried ref to keep it from checking a bar that also sets a buy but that didn't change a thing.
I am trying to convert a C++ trading program to AFL native, array only processing. I really don't want to use loops because the target for this code is an auto trading program that will loop through the indicator every tick. For an EOD program using loops would be OK but not when it will run the loop many times a second. Unless AFL has changed a lot, going through a loop every tick will really slow it down and it could miss ticks. I believe Ami will only process the array elements necessary for the chart rather than the real number of bars in BarCount. The array I am processing is really large so processing the entire array would be prohibitive. So I am not sure processing only enough bars to ensure correct data would help much. If I have to I will code it that way and trace it to see what damage running the loops with real time data would cause. I would really like to do this with array logic only. Where would you suggest I look to understand the array ripple effect and why barssince can't see the values in the buy and sell arrays? Or can you simply explain now to circumvent this error in array mode? Thanks, Barry --- In [email protected], "Bruce" <bru...@...> wrote: > > Barry - > > I thought I'd take this one since I'm here. I'm not going to answer your > BarsSince() question directly, but rather address your goal from your first > paragraph since BarsSince appears to be kind of a detour. > > But, briefly, you have described a situation where there is a "ripple > effect". For example, if you are on a buy, whether or not you take the stop > and where you take it affects the stop for the subsequent sell. And then, it > affects the subsequent buy ... and so on. IOW, it ripples through. > > There are a couple of approaches to handling this, but the easiest to see is > looping over bars. This ripple is problematic for basic array logic. > Assuming that you just want a model for a solution and not a lengthy > explanation of the array considerations, I suggest that you take a look at > the following link to the Amibroker site members area. Tomasz posted a code > example for June Stock & Commodities that included a "fixed percentage" > trailing stop. Interesting approach also in handling the system state in the > "trailstop" variable - > > http://www.amibroker.com/members/traders/06-2009.html > > I think that this is what you want. Hope this helps, > BruceR > > > --- In [email protected], "Barry" <razzbarry@> wrote: > > > > I am trying to buy when the price rises a certain amount above the lowest > > close since the last sell or sell when price has fallen a certain amount > > below the highest close since the last buy. > > > > I am trying to use barssince to find the number of bars since the last buy > > or sell. But before there has been a buy or sell I want to use Lowest(C) > > or Highest(c) then switch to HHV and LLV once there has been a trade. > > > > So I wrote a test program to see how barssince works. > > > > periods = Param( "Periods", 14, 2, 30, 1 ); > > fCCI = CCI( periods ); > > > > Buy = Sell = 0; > > > > bsBuy = Nz(BarsSince(Buy), -1); > > bsSell = Nz(BarsSince(Sell), -1); > > printf(StrFormat("Bars since buy = %g \nBars since sell = %g", bsBuy, > > bsSell)); > > > > theLo = IIf(bsSell == -1, Lowest(C), LLV(C, bsSell)); // lowest close > > since last sell > > theHi = IIf(bsBuy == -1, Highest(C), HHV(C, bsBuy)); // hignest > > close since last buy > > > > Buy = Cross(fCCi, -50); > > Sell = Cross( 50, fcci); > > > > When I put the buy and sell assignments below the barssince it always > > returns -1. Even after there is a buy or sell it still returns -1. If I > > move the buy and sell assignments above the bsBuy and bsSell it works fine. > > But in my program the buy and sell must be after barssince. > > > > What I don't understand is why barssince never sees that a buy or sell has > > occurred? Shouldn't it see the buy or sell after the CCI cross and start > > returning the number of bars rather than -1? I am using shapes to see the > > buy and sell points but after that barssince still returns -1. What is > > going on? > > > > The system does not use indicators so I can't set the buy or sell before > > the CCI cross as in this test. I have to use Lowest or Highest before the > > first trade then switch to HHV and LLV after that. How do I do that? > > > > Thanks, > > Barry > > >
