Both BuyPrice and SellPrice are arrays. You must use an index into them when assigning values, else the same value will be set for all bars.
e.g. SellPrice[i] = stopLoss; Also, have a look at the ApplyStop function. Your entire example can probably be written in 3 or 4 lines using ApplyStop instead of doing all that looping. http://www.amibroker.com/guide/afl/afl_view.php?id=20 Mike --- In [email protected], "michaels_musings" <michaels_musi...@...> wrote: > > Hi all, > > I'm a long time coder (30+ years), but I'm apparently not understanding AFL > correctly. I'm trying to get the testing code below to specifically open and > close positions at set prices, and I thought I had dug through enough manuals > to have that happen, but it doesn't? > > The systems is junk, but it should return fixed exit points... (Opens are > correct) > > A few examples on FAS 1-minute bars over the last 180 days : > > Open Close % Profit > 25 25.9167 3.67% > 25 23.7033 -5.19% > 25 26.22 4.88% > > Anyone tell me if I've missed something? (Are there a bunch of switches I've > missed and I haven't coded correctly?) > > Thanks, > Michael > > > /*------------------------------------- > firstSystem.afl > > Buy = Buy when price has showed upward momentum > by moving from TradeActivationPrice to BuyPriceFixed. > Sell = Sell at SellPrice. > StopLoss = Sell at stopLoss price. > > Notes: > Fixed trigger price, therefor only one trade open at any one time. > > -------------------------------------*/ > // optimize( "description", default, min , max, step ) > PctChange = Optimize( "PctChg", .05, .01, .10, .01 ); > BuyPriceFixed = 25; > SellPriceFixed = BuyPriceFixed * ( 1 + PctChange ); > SellStop = BuyPriceFixed * ( 1 - PctChange ); > stopLoss = BuyPriceFixed * ( 1 - PctChange ); > TradeActivationPrice = BuyPriceFixed * ( 1 - PctChange ); > > // RoundLotSize = 1; > // SetOption( "field", value ); > SetOption( "InitialEquity", 1000000000 ); > SetOption( "ActivateStopsImmediately", True ); > SetOption( "AllowSameBarExit", False ); > SetOption( "CommissionMode", 3 ); > SetOption( "CommissionAmount", .005 ); > SetOption( "GenerateReport", 1 ); > SetOption( "ReverseSignalForcesExit", False ); > SetOption( "SeparateLongShortRank", True ); > SetOption( "RefreshWhenCompleted", True ); > SetOption( "RequireDeclarations", False ); > SetOption( "PortfolioReportMode", 0 ); > SetOption("ExtraColumnsLocation", 1 ); > //SetOption( "field", value ); > > SetPositionSize( 10000, spsValue ); > SetTradeDelays(0,0,0,0); > > // Start this Pig... > Buy = False; // Confused why I have to initialize "Buy" array.... > Sell = False; > OpenPos = False; // No open position to start with > LastLow = TradeActivationPrice; // Initialize LastLow for Buy Check > > for ( i = 0; i < BarCount; i++ ) // Loop over all bars in the chart > { > if ( OpenPos ) // Have an open position, check to sell it > { > if ( Low[i] < stopLoss ) // Check for Stops > { > SellPrice = stopLoss; > Sell[i] = True; > } > else > { > if ( High[i] > SellPriceFixed ) // Check for Profits > { > SellPrice = SellPriceFixed; > Sell[i] = True; > } > } > > // Selling Clean up > Buy[i] = False; // Remove any surplus buy signals > if ( Sell[i] ) // If have sell signal on this bar > { > OpenPos = False; // No longer have open position > LastLow = TradeActivationPrice; // ReSet LastLow for Buy Check > } > } > else // Nothing open, see if we can buy something > { > if ( ( LastLow < TradeActivationPrice ) > AND ( High[i] > BuyPriceFixed ) > // AND ( BuyPriceFixed > Low[i] ) > ) > { > BuyPrice = BuyPriceFixed; > Buy[i] = True; > } > > // Buying Clean up > Sell[i] = False; // Remove any surplus sell signals > if ( Buy[i] ) // If have a buy signal on this bar > { > OpenPos = True; // Now have an open position > } > if ( LastLow > Low[i] ) > { > LastLow = Low[i]; // Set LastLow for Buy Check > } > } > } >
