[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Fwd: Buy price variable



PureBytes Links

Trading Reference Links

Hello,
I'm trying to write a system which sometimes opens several positions in the
same bar. So I need to calculate the average entryprice, for  which I used
the buyprice variable.
The trading system itself is not that relevant. My entry criteria is  
to buy if price crosses a resistance level. Then I want to add  
positions as price moves in my favor. The problem I am having is that  
the backtester executes trades at a price which is different from the  
price which is shown by the buyprice variable.
To make sure I was calculating the buyprice correctly I plotted it on a
chart. The value which is displayed on the chart is ok, but when I execute
the backtest the buy price does not match the value I see on the chart ( I'm
plotting the reserved buyprice variable, not a variable created by me).
Please see attached an example of this (bar 11-01-2001 at 19:29).
The code at this time is only applicable to forex pairs with 4 decimal places.

Can you please tell me what am I doing wrong?



PH =TimeFrameGetPrice("H", inDaily, -1);
PL =TimeFrameGetPrice("L", inDaily, -1);
PC =TimeFrameGetPrice("C", inDaily, -1);

PP=(PH+PL+PC)/3;
//R1 = (2 * PP) - PL;
//S1 = (2 * PP) - PH;
//R2 = PP + R1 - S1;
//S2 = PP + S1 - R1;
R3=(PH + (2 * (PP - PL)));
S3=(PL - (2 * (PH - PP) ));
PPcolor=ParamColor("PP",colorYellow);
SRcolor=ParamColor("S/R color",colorSkyblue);
SRstyle=ParamStyle("S/R style",styleDots|styleNoLine|styleNoRescale);
//Plot(PP,"",PPcolor,styleThick);
//Plot(S3,"",SRcolor,SRstyle);
//Plot(R3,"",SRcolor,SRstyle);

// trading system

SetBacktestMode( backtestRegularRawMulti );


intrade=0;
step1=0;
step2=0;
step3=0;
step4=0;
step5=0;
step6=0;
step7=0;
npos=0;
stackplot=0;
entrypriceplot=0;
entryprice=0;
HighestSinceentry=0;
stacks=0;

entry=Cross(H,R3);
//BuyPrice=R3; //entryprice
exit=Cross(pp,L);
SellPrice=PP; //stop loss selling price
Buy=0;

for(i=1 ; i<(BarCount-1) ; i++)
{

      if( entryprice[i]==0 AND entry[i]==1 AND npos[i]==0) // entry bar check
      {

         Buy[i]=1;
         entryprice=R3[i];
         HighestSinceentry=H[i];
         stacks[i]=Min(int((H[i]-entryprice[i])*10000/10),4)+1; //  
inclui mais uma posiçao que é a inicial
  		 npos[i]=stacks[i];
         BuyPrice[i]=(2*entryprice[i]+((stacks[i]-1)*10/10000))/2; //  
retirar um pois esta incluido nos stacks
         intrade=1;
         step1[i]=1;

          if(H[i]>=(entryprice[i]+50/10000)AND intrade[i]) //profit  
target hit on entry bar
	       {

		 	BuyPrice=(2*entryprice[i]+(4*10/10000))/2;
		 	Sell[i]=1;
		 	SellPrice[i]=entryprice[i]+50/10000;
           entryprice=0;
		 	HighestSinceentry=0;
		 	npos[i+1]=0;

	     	step2[i]=2;
         	intrade=0;
           }

       }



	if (H[i]>HighestSinceentry[i] AND intrade[i]) //increase number of positions
	{

	 HighestSinceentry=H[i];

	 stacks[i]=Min(int((H[i]-entryprice[i])*10000/10),4)-npos[i]+1;  
//calculate number of stacks
	 if(stacks[i]<0) stacks[i]=0;
     npos[i]=Min(stacks[i]+npos[i],5); // keep highest number of stacks
     step3[i]=3;
          if(stacks[i]>0 AND npos[i]<=5)  // doesn't buy if stacks is  
zero and doesn't buy if # of positions is > 5
            {
             Buy[i]=1;
	        BuyPrice[i]=(2*entryprice[i]+(stacks[i]*10/10000))/2;
	        step4[i]=4;
             }
    }


       npos[i+1]=Min(npos[i],5);
	   step5[i]=5;


    if(H[i]>=(entryprice[i]+50/10000) AND intrade[i]) //profit target hit
	 {

	 Sell[i]=1;
	 entryprice=0;
	 SellPrice[i]=entryprice[i]+50/10000;
     HighestSinceentry=0;
	 npos[i+1]=0;

	 step6[i]=6;
     intrade=0;
     }

	if(exit[i]==1 AND intrade[i]) //stop loss hit
	{

	Sell[i]=1;
	entryprice=0;
	HighestSinceentry=0;
	npos[i+1]=0;
    intrade=0;
    step7[i]=7;
	}



  stackplot[i]=stacks[i];
  entrypriceplot[i]=entryprice ;

}
Plot(Buy,"buy",colorLime,styleOwnScale);
Plot(step1,"st1",colorLime,styleOwnScale);
Plot(step2,"st2",colorLime,styleOwnScale);
Plot(step3,"st3",colorLime,styleOwnScale);
Plot(step4,"st4",colorLime,styleOwnScale);
Plot(step5,"st5",colorLime,styleOwnScale);
Plot(step6,"st6",colorLime,styleOwnScale);
Plot(step7,"st7",colorLime,styleOwnScale);

Plot(Sell,"sell",colorRed,styleOwnScale);

Plot(BuyPrice,"buyprice",colorViolet,styleOwnScale);
Plot(npos,"npos",colorPink,styleOwnScale);
Plot(stackplot,"stack",colorAqua,styleOwnScale);

Plot(entrypriceplot,"entryprice",colorBlue,styleOwnScale);

SetPositionSize(stackplot,spsShares);

thanks very much,
PP




________________________________________________________________________________
CustoJusto.pt - venda o que quiser a custo zero!
http://www.iol.pt/correio/rodape.php?dst=0905201


------------------------------------

**** 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/

Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/

JPEG image

JPEG image