Without trying to understand your code I see several issues: Start by putting these two lines AFTER the loop.
Buy=Buytemp; Sell=Selltemp; The whole point of looping is to control things bar by bar and you have simply executed the entire backtest on your 3rd and 4th lines. Then, towards the end of your code you have this line: SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); You can scaleout, but Buy == tests the equivalence of these two lines. You want Buy = Any you need your actual Buy and Sell statements at the end which you will need to combine with your sigScaleOut since that is assigned to Buy if you're scaling and the actual buy (Buytemp possibly) needs to be assigned as well. In fact, I'm unclear on exactly how that works. Better check the user guide on ScaleOut and SetPositionSize. Last point: you have many variables in your loop without subscripts[i]. Just make sure those are numeric variables and not arrays. -- Terry -----Original Message----- From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of matrix10014 Sent: Monday, August 28, 2006 15:09 To: [email protected] Subject: [amibroker] Looping discrepencies Hi all, I am still working on my looping skills and just when I thought I had it down,I run into a brick AFL looping wall...here is the code I worked with from the users manual for the stock NUHC,backtested from 6/20/06-8/25 --------------------------------------------------------------------- Buytemp = Cross(C, MA( C,21) ); Selltemp = Cross(MA(C,200),C); //Buy=ExRem(Buy,Sell); // my code change //Sell=ExRem(Sell,Buy);// my code change Filter=1; //exploration AddColumn(BuyTemp,"All Buy Signals");//exploration AddColumn(Buy,"BUY SIGNAL");//exploration AddColumn(SellTemp,"All Sell Signals");//exploration AddColumn(Sell,"SELL SIGNAL");//exploration FirstProfitTarget = 50; // profit ..purposely left high TrailingStop = 34; // also in percent Maxstop=9;//also in percent //FirstProfitTarget = Optimize("FirstProfitTarget",15,5, 50,5); //TrailingStop = Optimize("TrailingStop",34,5,25,1); //Maxstop = Optimize("maxstop",8,2,15,1); priceatbuy=0; highsincebuy = 0; exit = 0; for( i = 0; i < BarCount; i++ ) { if( priceatbuy == 0 AND Buy[ i ] ) { priceatbuy = BuyPrice[ i ]; } if( priceatbuy > 0 ) { highsincebuy = Max( High[ i ], highsincebuy ); if( exit == 0 AND High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy ) { //first profit target hit - scale-out exit = 1; Buy[ i ] = sigScaleOut; } if(Low[ i ] <= ( 1 - TrailingStop * 0.01 ) * highsincebuy ) { // trailing stop hit - exit exit = 2; SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop * 0.01 ) * highsincebuy ); } if (Low[i]<=(1-Maxstop*.01)*priceatbuy) { exit = 3; SellPrice[i]=Min(Open[i],(1-Maxstop*.01)*priceatbuy); //My attempt to code in a Max stop loss-appears to work as coded } if( exit >= 2 ) { Buy[ i ] = 0; Sell[ i ] = exit + 1; // mark appropriate exit code exit = 0; priceatbuy = 0; // reset price highsincebuy = 0; } } } SetPositionSize( 50, spsPercentOfEquity ); SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); ------------------------------------------------------------------- On 6/20/06, NUHC crossed above its 21 day moving average.When I code a simple formula in AFL Buy=Cross(C, MA( C,21) ); Everything works as expected.I can set the BuyPrice and delay in settings and entrydate may change,but it still signals a BUY.This holds true regardless of any stops I apply... Here is the problem. If I run the above looping code example and set the BuyPrice to Close, Delays to 0 ,trailing stop of 35 or higher OR maxstop 10 or greater,everything tests as expected.I get the buy signal,and the arrows for raw signals and trades are as expected..The important thing is I am using a trailing stop of 35 or greater OR a maxstop of 10 or greater..I do verify the signals in explorer as well. If I should keep those exact same settings,but change the Trailing stop to 34(from 35),or max stop to 9 or less,the 6/20 MA21 is ignored and the system doesnt show a buy till 8/09.Not only that,but when I look at the raw arrows the Buy arrow that I had on 6/20 is now a SELL arrow.If I look at the arrow for actual trades,there are no arrows.What is more interesting is that if I run EXPLORE,All Buy Signals and Buy signals show a 1 for 6/20/2006,yet I am getting a sell arrow on 6/20/2006 for raw signals.Is there something unusual about raw signals that would cause a buy arrow to become a sell arrow??What could I be missing?? Am I correct that if i dont use Exrem,the first all signals will fire as soon as they occur?? I have to assume it must be something in the stop levels,but i can not verify it.I dont understand how a clear buy signal would be ignored and show up with a sell arrow for raw signals..Can anyone please assist me on this?? Thanks in advance Allan Please note that this group is for discussion between users only. To get support from AmiBroker please send an e-mail directly to SUPPORT {at} amibroker.com For other support material please check also: http://www.amibroker.com/support.html Yahoo! Groups Links Please note that this group is for discussion between users only. To get support from AmiBroker please send an e-mail directly to SUPPORT {at} amibroker.com For other support material please check also: http://www.amibroker.com/support.html Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/amibroker/ <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/
