PureBytes Links
Trading Reference Links
|
Hi,
Try something like the following. The Buy/Sell seems to work. You can add the Short/Cover using the same principles. The code assumes that you are trading at the Close on the day of the signal.
SetTradeDelays(0, 0, 0, 0);
BuyPrice = Close;
SellPrice = Close;
previousClose = Ref(Close, -1);
rsi2 = RSI(2);
Sell = Cross(rsi2, 70);
bull100 = Close > EMA(Close, 200) AND Sum(rsi2 < 25, 2) == 2;
inFirstPos = Flip(bull100, Sell);
bull200 = Close < previousClose AND inFirstPos AND Ref(inFirstPos, -1);
inSecondPos = Flip(bull200, Sell);
bull300 = Close < previousClose AND inSecondPos AND Ref(inSecondPos, -1);
inThirdPos = Flip(bull300, Sell);
bull400 = Close < previousClose AND inThirdPos AND Ref(inThirdPos, -1);
inFourthPos = Flip(bull400, Sell);
firstTrigger = ExRem(inFirstPos, Sell);
secondTrigger = ExRem(inSecondPos, Sell);
thirdTrigger = ExRem(inThirdPos, Sell);
fourthTrigger = ExRem(inFourthPos, Sell);
Buy = firstTrigger + (secondTrigger * sigScaleIn) + (thirdTrigger * sigScaleIn) + (fourthTrigger * sigScaleIn);
SetPositionSize(IIf(firstTrigger, 100, IIf(secondTrigger, 200, IIf(thirdTrigger, 300, 400))), spsShares);
priceColors = IIf(Close > previousClose, colorDarkGreen, IIf(Close < previousClose, colorDarkRed, colorDarkGrey));
buyColors = IIf(firstTrigger, colorGreen, IIf(secondTrigger, colorLime, IIf(thirdTrigger, colorYellow, colorOrange)));
Plot(Close, "Price", priceColors, styleBar);
PlotShapes((Buy > 0) * shapeUpArrow, buyColors, 0, L, -10);
PlotShapes(Sell * shapeDownArrow, colorRed);
Mike
--- In amibroker@xxxxxxxxxxxxxxx, "cvanhaesendonck" <carl.van@xxx> wrote: > > I posted some days ago but got no answer; however this system is powerfull yet simple: it scales in after the initial signal then exit quickly as soon as RSI(2) oberbought. > Problem I struggle with the code below: I can see the multiple buy entry arrows on the chart but backtest will only take the initial signals. > Please help with this - in return you will discover a very interesting system. Here we go: > > The system is simple: > - buy 10% of your position (for exempla, 100 shares) when RSI(2) is below 25 for 2 consecutive bars; close must be above the 200 EMA. > - buy 20% of the position if, any time after the initial buy above, close < ref(close,-1) > - buy 30% of the pôsition if, any time after the buy above, close < ref(close,-1) > - buy 40% of the position if, any time after the buy above, close < ref(close,-1) > > You now own 1,000 shares. Don't buy anymore. > Just wait and sell all when RSI(2 periods) rise above 70. > > Please see the code below and tell me what is wrong - I am struggling with this for days now as the backtest takes only the initial "10%" trade... > > bull= C> EMA(C,200) AND Sum(RSI(2)< 25==2; > bear= C< EMA(C,200) AND Sum(RSI(2)>25==2; > Buy=0; > Short= 0; > > Sell= Cross(RSI(2),70) ; > Cover=Cross(70,RSI(2)) ; > bull200= C<Ref(C,-1) AND BarsSince(Bull)<BarsSince(Sell) AND Ref(Sum(Bull,BarsSince(Sell)),-1)==1; > bear200= C>Ref(C,-1) AND BarsSince(bear)<BarsSince(Cover) AND Ref(Sum(bear,BarsSince(Cover)),-1)==1; > bull300= C<Ref(C,-1) AND Ref(Sum(Bull200,BarsSince(Sell)),-1)==1 ; > bear300= C>Ref(C,-1) AND Ref(Sum(bear200,BarsSince(Cover)),-1)==1 ; > bull400= C<Ref(C,-1) AND Ref(Sum(Bull300,BarsSince(Sell)),-1)==1 ; > bear400= C>Ref(C,-1) AND Ref(Sum(bear300,BarsSince(Cover)),-1)==1 ; > SetPositionSize(IIf(Sum(Buy,BarsSince(Sell))==1, 100,IIf(Sum(Buy,BarsSince(Sell))==2,200,IIf(Sum(Buy,BarsSince(Sell))==3,300,IIf(Sum(Buy,BarsSince(Sell))==4,400,Null)))),4); > SetPositionSize(IIf(Sum(Short,BarsSince(Sell))==1, 100,IIf(Sum(Short,BarsSince(Sell))==2,200,IIf(Sum(Short,BarsSince(Sell))==3,300,IIf(Sum(Short,BarsSince(Sell))==4,400,Null)))),4); > Buy= IIf(Sum(Bull,BarsSince(Sell))<4 AND Sum(Bull200,BarsSince(Sell))<4 AND Sum(Bull300,BarsSince(Sell))<4 AND Sum(Bull400,BarsSince(Sell))<4 ,bull OR bull200 OR bull300 OR bull400,Null); > Short= IIf(Sum(Bear,BarsSince(Sell))<4 AND Sum(Bear200,BarsSince(Sell))<4 AND Sum(Bear300,BarsSince(Sell))<4 AND Sum(Bear400,BarsSince(Sell))<4,bear OR bear200 OR bear300 OR bear400, Null); > Sell=ExRem(Sell,Buy); > Cover=ExRem(Cover,Short); > > PlotShapes(Buy*shapeUpArrow,colorBlue,0,L,-10); > PlotShapes(Short*shapeDownArrow,colorRed,0,H,-10); > PlotShapes(Sell*shapeSmallCircle,colorBlue,0,H,10); > PlotShapes(Cover*shapeSmallCircle,colorRed,0,L,-10); > > Thanks, > Carl >
__._,_.___
**** 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/
__,_._,___
|