Frank,
 
Make sure to adapt the code to your needs. This is an example for an entry on a 
5-day high -- you'll have to add your own entry/exit rules:
 
BuySignal = Close == HHV(Close,5);

PositionSize = -10;

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

if (BuySignal[i]) {

// 3 Percent profit, 2 Percen loss

Buy[i] = 1;

BuyPrice[i] = Close[i];

wPct = 3;

lPct = 2;

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

if (Low[j] <= (1 - lPct/100)*BuyPrice[i]) {

Sell[j] = 1;

SellPrice[j] = Min(Open[j], (1-lPct/100)*BuyPrice[i]);

i = j;

j = BarCount; // Terminate loop

}

else if (High[j] >= (1 + wPct/100)*BuyPrice[i]) {

Sell[j] = 1;

SellPrice[j] = Max(Open[j], (1+wPct/100)*BuyPrice[i]);

i = j;

j = BarCount; // Terminate loop

}

}

}

}

 

Regards,

Amit Ehrenreich

Market Testing Wizards

www.markettestingwizards.com

 

________________________________

From: [email protected] on behalf of cstdc5588
Sent: Thu 3/27/2008 12:07 AM
To: [email protected]
Subject: [amibroker] Re: Back-testing a simple trading system



Hi Amit Ehrenreich, 

Thank you so much. But I can not use this AFL program. Something 
wrong in this formula that I don't know how to fix it. I need more 
help.

Cond = Cross( MA (Close, 20), MA(Close, 50) );
Cond1 = Cross( MA (Close, 50), MA(Close, 20) );
MA20 = MA( Close, 20 );
MA50 = MA( Close, 50 );
Crosses = Cross( MA20, MA50 ) OR Cross( MA50, MA20 ) ;
Buy = Cond;
Sell = Cond1;
SetOption("MaxOpenPositions",10);
PositionSize = -10;
for (i = 1; i <= BarCount-1; i++) {
if (Buy[i]) {
// 3 Percent profit, 2 Percen loss
wPct = 3;
lPct = 2;
for (j = i; j <= BarCount-1; j++) {
if (Low[j] <= (1 - lPct/100)*BuyPrice[i]) {
Sell[j] = 1;
SellPrice[j] = Min(Open[j], (1-lPct/100)*BuyPrice[i]);
i = j;
j = BarCount; // Terminate loop
}
if (High[j] >= (1 + wPct/100)*BuyPrice[i]) {
Sell[j] = 1;
SellPrice[j] = Max(Open[j], (1+wPct/100)*BuyPrice[i]);
i = j;
j = BarCount; // Terminate loop
}
}
}
}

Regards,

Frank

--- In [email protected] <mailto:amibroker%40yahoogroups.com> , "Amit 
Ehrenreich" <[EMAIL PROTECTED]> wrote:
>
> Frank,
> 
> Say you want to sell after a 3% rise, and have a stop-loss after a 
2% drop. The following code should point you in the right direction. 
> 
> for (i = 1; i <= Barcount-1; i++) {
> if (Buy[i]) {
> // 3 Percent profit, 2 Percen loss
> wPct = 3;
> lPct = 2;
> for (j = i; j <= Barcount-1; j++) {
> if (Low[j] <= (1 - lPct/100)*BuyPrice[i]) { 
> Sell[j] = 1;
> SellPrice[j] = Min(Open[j], (1-lPct/100)
*BuyPrice[i]);
> i = j;
> j = Barcount; // Terminate loop
> }
> if (High[j] >= (1 + wPct/100)*BuyPrice[i]) {
> Sell[j] = 1;
> SellPrice[j] = Max(Open[j], (1+wPct/100)
*BuyPrice[i]);
> i = j;
> j = Barcount; // Terminate loop
> }
> }
> }
> }
> 
> 
> Regards,
> 
> Amit Ehrenreich
> Market Testing Wizards
> www.markettestingwizards.com
> 
> 
> ________________________________
> 
> From: [email protected] <mailto:amibroker%40yahoogroups.com>  on 
> behalf of cstdc5588
> Sent: Wed 3/26/2008 7:01 PM
> To: [email protected] <mailto:amibroker%40yahoogroups.com> 
> Subject: [amibroker] Back-testing a simple trading system
> 
> 
> 
> I want to back-testing a simple trading system, the formula is as 
> follows: 
> 
> Cond = Cross( MA (Close, 20), MA(Close, 50) );
> Cond1 = Cross( MA (Close, 50), MA(Close, 20) );
> MA20 = MA( Close, 20 );
> MA50 = MA( Close, 50 );
> Crosses = Cross( MA20, MA50 ) OR Cross( MA50, MA20 ) ;
> Buy = Cond;
> Sell = Cond1;
> 
> I'm going to add buy(or short) and sell(or cover), as well as stop-
> loss conditions. Such as: when to buy or short a stock, if it has a 
> profit of 3%, should sell(or cover) to close this position. If the 
> loss occurred, when the loss is 2% to sell(or cover) to stop loss. 
In 
> addition, total capital is $100,000, per transaction for a 10% of 
the 
> capital, while a maximum of 10 positions, not considering 
> commissions. I don't know how to write this formula, any help would 
> be appreciated!
> 
> Frank
>



 

<<winmail.dat>>

Reply via email to