PureBytes Links
Trading Reference Links
|
Barry,
You cannot reliably do what you are attempting. The only way to truely know whether or not you are in a position is via iterating through the open position list within custom backtester code. The backtester employs numerous rules that affect whether or not a position is actually taken, despite the signals given (e.g. availability of funds, max positions, limits on number of positions per symbol, etc.).
In answer to your other questions, BarsSince is entirely dependent upon the state of the arrays at the time that you call it. So, of course, it matters a great deal where you place the call in your code.
To illustrate the problem, here is code that will do what you want. But, you will notice that it is wrong since it allows simultaneous positions of both long and short, does not take into consideration availability of funds, etc. all of which only the custom backtester would be able to filter reliably.
MA1 = MA(Close, 5); MA2 = MA(Close, 25); MA3 = MA(Close, 7); MA4 = MA(Close, 35);
Buy = Cross(MA1, MA2); Sell = Cross(MA2, MA1); AmLong = Flip(Buy, Sell);
Short = Cross(MA4, MA3); Cover = Cross(MA3, MA4); AmShort = Flip(Short, Cover);
State = IIF(AmLong, 1, IIF(AmShort, 2, 0));
Buy = ExRem(Buy, Sell); Sell = ExRem(Sell, Buy); Short = ExRem(Short, Cover); Cover = ExRem(Cover, Short);
Plot(Close, "Close", colorLightGrey, styleBar); Plot(State, "State", colorRed, styleStairCase | styleOwnScale); PlotShapes(Buy * shapeUpArrow, colorGreen); PlotShapes(Sell * shapeDownArrow, colorRed); PlotShapes(Short * shapeHollowDownArrow, colorRed); PlotShapes(Cover * shapeHollowUpArrow, colorGreen);
Mike
--- In amibroker@xxxxxxxxxxxxxxx, "Barry Scarborough" <razzbarry@xxx> wrote: > > I am trying to set states based on whether I have a position and the position type. If AFL has a getPositions function I can't find it. I tried to create states using the following code but barssince does not return a valid value. > > // buy control > BarsSB = Nz(BarsSince(Buy), 0); > BarsSSl = Nz(BarsSince(Sell), 0); > BarsSSh = Nz(BarsSince(Short), 0); > BarsSC = Nz(BarsSince(Cover), 0); > > state = 0; > // 0 = no position, 1 = buy last, 2 = short last; > state = IIf(BarsSB == BarsSSh, 0, IIf(BarsSB < BarsSSh, 1, 2)); > // if sell after buy we are flat > state = IIf(state == 1 AND BarsSSl < BarsSB, 0, state); > // if cover after short we are flat > state = IIf(state == 2 AND BarsSC < BarsSSh, 0, state); > > If I don't use NZ then BarsSince returns Empty. > When I use NZ then I can see trades using plot shapes. But all the barsSince returns 0 so state is always 0. What am I doing wrong? > > The really interesting thing is that I can put the barsince lines below the logic where I set buy, sell, short and cover, then I get correct values returned. Why does the position matter? > > I can use static vars to keep the state but when I do that AutoAnalysis will not work and the shapes don't show properly. So I am tryng to find a way that allows auto analysis to work. I tried to use switch and found it only works with numbers and not arrays. > > Thanks, > Barry >
__._,_.___
**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.
TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com
TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
__,_._,___
|