Hi Alex,

See the two code samples below. Comment out the one that you don't want to 
test. The first system buys when the high breaks through the Ma(C,20) of the 
previous bar, and the second system buys when the close is higher than the 
Ma(C,20).These contain no 'future leak'. It shows how to nest more than 1 sell 
condition using iif(). It can be used in a backtest. Plot the 3 x DispMA on 
chart and cross check the backtest buy/sell values with chart info. Make sure 
you check "allow same bar exit" in the settings tab. 
Note: The code sample can be cleaned up, improved, modified etc... comments 
welcome. 
Chris.

// BuyStopMaIIF.afl

// Using High and Low

Pre10      = Ref(MA(C,10),-1);
Pre20      = Ref(MA(C,20),-1);
Pre50      = Ref(MA(C,50),-1);
Condition1 = IIf(Pre10>Pre20 AND Pre20>Pre50,1,0);
Condition2 = IIf(O<Pre20 AND H>Pre20,1,0);
Buy        = Condition1 AND Condition2;
Sell       = IIf(H>Pre10,1,IIf(L<Pre50,1,0));
BuyPrice   = Pre20;
SellPrice  = IIf(H>Pre10,Pre10,IIf(L<Pre50,Pre50,0));
SetPositionSize(1000, spsValue); // Buy $1000 worth

/*

// Or Using Close (if closes above Pre10 target)

Pre10      = Ref(MA(C,10),-1);
Pre20      = Ref(MA(C,20),-1);
Pre50      = Ref(MA(C,50),-1);
Condition1 = IIf(Pre10>Pre20 AND Pre20>Pre50,1,0);
Condition2 = IIf(O<Pre20 AND H>Pre20,1,0);
Buy        = Condition1 AND Condition2;
Sell       = IIf(C>Pre10,1,IIf(L<Pre50,1,0));
BuyPrice   = Pre20;
SellPrice  = IIf(C>Pre10,C,IIf(L<Pre50,Pre50,0));
SetPositionSize(1000, spsValue); // Buy $1000 worth

*/

--- In [email protected], "dralexchambers" <dralexchamb...@...> wrote:
>
> Hi,
> 
> How would I put together a backtest for the following. My Buy signal is:
> 
> Buy = C>MA(C,20);
> 
> How do I do a multiple sell signal for the following:
> 
> StopLoss is hit is when C < MA(C,50) Stoploss price is MA50. TakeProfit is 
> when C > MA(C,10), and TP price is MA10.
> 
> I can only see one Sell & Sellprice variable. How do I put both of these 
> conditions into Sell & Sellprice?
> 
> Thanks,
> Alex
>


Reply via email to