Hello friends,
I am a newbie in Amibroker. Presently, I am trying to develop an
intraday trading system for futures (15% margin). Based on EMA crossover
it will buy and scale out the trade when in 0.5% profit (50%) and 1%
(full exit). Trailing stop loss is 0.5%. The code is given below.
Original code I got from the web, which I adapted for my use.
SetOption("InitialEquity", 100000);
//SetOption("MinShares", 100);
//SetOption("NoDefaultColumns", True );
//SetOption("AllowSameBarExit", True );
//SetOption("ActivateStopsImmediately" , True );
SetOption("FuturesMode", True );
SetOption("InterestRate", 0);
SetOption("CommissionMode", 1); //$$ per trade
SetOption("CommissionAmount", 0.031); // commission is accounted for in
skid
SetOption("MarginRequirement", 15);
SetTradeDelays(0,0,0,0);
RoundLotSize = 50;
TickSize = 0.05;
timestore = TimeNum();
Buy = TimeNum() <= 150000 AND Cross( EMA( C, 4 ), EMA( C, 32 ) );
Sell = 0;
FirstProfitTarget = 0.5; // profit
SecondProfitTarget = 1.0; // in percent
TrailingStop = 0.5; // also in percent
priceatbuy=0;
highsincebuy = 0;
exit = 0;
for( i = 0; i < BarCount; i++ )
{
SellPrice[i]=0;
if( priceatbuy == 0 AND Buy[i] ) {
priceatbuy = BuyPrice[i];
}
if( priceatbuy > 0 ) {
highsincebuy = Max( High[i], highsincebuy );
if( exit == 0 AND High[i] >= ( 1 + FirstProfitTarget * 0.01 ) *
priceatbuy ) {
// first profit target hit - scale-out
exit = 1;
Buy[i] = sigScaleOut;
BuyPrice[i] = Max( Open[i], ( 1 + FirstProfitTarget * 0.01 ) *
priceatbuy );
}
else { // Added ELSE here for Initial entry trail stop
if( exit == 0 AND ( timestore[i] > 152000 OR Low[ i ] <= ( 1 -
TrailingStop * 0.01 ) * highsincebuy ) ) {
// trailing stop hit - exit
exit = 3;
SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop * 0.01
) * highsincebuy );
}
}
if( exit == 1 AND High[i] >= ( 1 + SecondProfitTarget * 0.01 ) *
priceatbuy ) {
// second profit target hit - exit
exit = 2;
SellPrice[i] = Max( Open[i], ( 1 + SecondProfitTarget * 0.01 )
* priceatbuy );
}
else { // Added ELSE here for trail stop on scale out parcel
if(exit == 1 AND ( timestore[i] > 152000 OR Low[ i ] <= ( 1 -
TrailingStop * 0.01 ) * highsincebuy ) ) {
exit = 3;
SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop * 0.01
) * highsincebuy );
}
}
if( exit >= 2 ) {
Buy[i] = 0;
Sell[i] = exit + 1; // mark appropriate exit code
exit = 0;
priceatbuy = 0; // reset price
highsincebuy = 0;
}
}
}
PlotShapes( Buy * shapeUpArrow + Sell * shapeDownArrow , IIf( Buy,
colorGreen, colorRed ), 0, IIf( Buy, Low, High ));
//SetPositionSize( 50, spsPercentOfEquity) ; // 2% of equity
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) );
Unfortunately, I am not able to solve the sigscaleout portion of the
code. Whenever, scale out is taking place the cash is becoming negative
and afterwards no trade is taking place due to this.
The detailed log in AA is pasted here:
Date Information
8/6/2008 1:34:55 PM
Entry signals(score):
Exit signals:
0 Open Positions: ,Equity: 100000, Cash: 100000
8/6/2008 1:39:55 PM
Entrysignals(score):NIFTY_F1=Buy(1),
Exit signals:
Enter Long, NIFTY_F1, Price: 4579.9,Shares: 100, Commission:
141.977, Rank: 1, Equity 99716, Margin Loan: 389412,Fx rate: 1
1 Open Positions: ,NIFTY_F1 (+100), Equity: 99716, Cash:
31280.2
8/6/2008 2:04:55 PM
Entry signals(score):
Exit signals:NIFTY_F1=Sell,
Exit Long, NIFTY_F1, Price: 4570.25,(Avg. exit pr. 4570.25),
Shares: 100, Commission: 141.678, (Total comm.:283.655), Profit:
-1248.64 (-0.27 %), Entry rank:1, Equity: 98751.4, Fx rate: 1
0 Open Positions: ,Equity: 98751.4, Cash: 98751.4
8/7/2008 12:29:55 PM
Entrysignals(score):NIFTY_F1=Buy(1),
Exit signals:
Enter Long, NIFTY_F1, Price: 4558.1,Shares: 100, Commission:
141.301, Rank: 1, Equity 98468.8, Margin Loan: 387559,Fx rate: 1
1 Open Positions: ,NIFTY_F1 (+100), Equity: 98468.8, Cash:
30358.7
8/7/2008 1:14:55 PM
Entry signals(score):
Exit signals:NIFTY_F1=Scale-Out,
Scale-Out Long NIFTY_F1, Price4580.89, Shares 50, Fx Rate 1,
Number of shares - Current: 50, Exited: 50, Max:100, Avg. Entry Price
4558.1, Avg. Exit Price 4580.89, Avg Fx. Rate Entry 1,Exit 1,
Entry+scaling commission 212.305
1 Open Positions: ,NIFTY_F1 (+50), Equity: 100803, Cash:
64704.7
8/7/2008 2:14:55 PM
Entry signals(score):
Exit signals:NIFTY_F1=Sell,
Exit Long, NIFTY_F1, Price: 4575.71,(Avg. exit pr. 4578.3),
Shares: 50, Commission: 70.9235, (Total comm.:283.228), Profit: 1736.62
(0.38 %), Entry rank:1, Equity: 101022, Fx rate: 1
0 Open Positions: ,Equity: 100488, Cash: -288767
I must be going wrong in some place. Can anybody help me!!!
Regards,
SG