To use margin on via code you
must also adjust positionSize according to your margin. Below I
have included a file I use for most of my AFL's that does that and
sets most other options as well. I discovered this
by watching what the backtester did when Settings
were used to adjust margin.
The trick is in these lines
of code:
Margin =
Param ( "Margin"
,100,10, 100,10 ) ; //Use this instead of my code
below to vary margin in any amount
leverage =
100/margin; //Will = 2 when margin =
50
PositionSize = PositionSize *
leverage;
/***************************************
Settings for Operation
***************************************/
when =
ParamToggle
( "Trade Day:"
, "Same Day|Next
Day", 0);
SetTradeDelays
(when,when,when,when);
OC =
ParamField ( "Trade price"
, field = 3 );
BuyPrice = SellPrice = ShortPrice = CoverPrice = OC;
//Ref(OC,when); Don't do
this, Amib
roker b acktest shifts the
dates of the signals to the next day to pick up the right
prices!
//Print ab ove
settings to Interpretation window:
"Trade " + WriteIf (when, "Next Day
" , "Same Day
") + WriteIf(BuyPrice == C ,"Close" ,WriteIf( BuyPrice == O,"Open" , "Other than Open or
Close"));
//Margin is set
next based on user selection---> margin = 50 *
ParamToggle("Margin","50,100",1) + 50; //False = 50 * 0 + 50 = 50
//True = 50 * 1 + 50 = 100
//Set trading
mode and commissions
_N (mode = ParamList ( "Trading Mode"
, "Stocks 1:1,Margin 2:1,No
Commission Funds 1:1,Futures xx:1", 0));
if (StrLeft (mode, 1 ) == "S" ) {fm = False; cm =
3 ; margin =
100 ; myCommission = .005;}
//$0.005 per share
IB
if (StrLeft (mode, 1 ) == "M" ) {fm = False; cm =
3 ; margin =
50 ; myCommission = .005;}
//$0.005 per share
IB
if (StrLeft (mode, 1 ) == "N" ) {fm = False; cm =
2 ; margin =
100 ; myCommission =
0 ;} //No commission ProFunds or
Rydex
if (StrLeft (mode, 1 ) == "F" ) {fm = True; cm =
3 ; margin =
100 ; myCommission =
2.4 ;} //$2.40 per contract each
way
SetOption ("FuturesMode"
,fm); //Requires True or False.
NOTE: This is insufficient. You must also check Futures mode in
AA-Settings.
SetOption ("CommissionMode"
, cm); /* Modes explained next...
0 - use portfolio manager commission tab le
1 - percent of trade
2 - $ per trade
3 - $ per share/contract */
SetOption ("CommissionAmount"
, myCommission);
//Used to allow for slippage
and commissions.
SetOption ( "AllowSameBarExit"
,False);
SetOption ( "ActivateStopsImmediately"
,False);
SetOption ( "AllowPositionShrinking"
,True);
SetOption ( "InitialEquity"
, Param ( "Initial
Equity"
,10000 , 10000 , 1000000 , 5000));
SetOption ( "InterestRate"
,0 );
SetOption ( "MarginRequirement"
,margin);
SetOption ( "MaxOpenPositions"
, Param ( "Max Open
Positions"
,1, 1 , 10 , 1));
SetOption ( "MinShares"
,1 );
SetOption ( "NoDefaultColumns"
,False);
SetOption ( "PriceBoundChecking"
,True);
SetOption ( "ReverseSignalForcesExit"
,True); //Normally True if not
trading Long and Short simultaneously
SetOption ( "UsePrevBarEquityForPosSizing"
,False);
PositionSize = Param ( "Position Size - see notes
in SetOptions.afl"
,- 100,- 100 , 1000000, 1 );
leverage =
100/margin; //defines leverage for stop
loss and gain calcs b ased on selected margin.
PositionSize = PositionSize *
leverage;
RoundLotSize = 1 ; //Overrides the Settings
page. Set as desired.
SetChartOptions (0, chartShowDates);
//The following
4 lines for ApplyStop are here to prevent AA Settings from
over-riding this code. Change as desired.
ApplyStop ( stopTypeLoss,stopModeDisa ble
, 100, 1, False,0 );
ApplyStop ( stopTypeTrailing,stopModeDisa ble
, 100, 1, False,0 );
ApplyStop ( stopTypeNBar,stopModeDisa ble
, 100, 1, False,0 );
ApplyStop(
stopTypeProfit,stopModeDisab
le, 100,
1 ,False ,0
);
/***********************************
END Settings for Operation
***************************************/
--
Terry
-----Original
Message-----
From: amibroker@xxxxxxxxxxxxxxx [mailto:
amibroker@xxxxxxxxxxxxxxx] On Behalf Of jimmyzee1975
Sent:
Thursday, January 19, 2006 21:06
To: amibroker@xxxxxxxxxxxxxxx
Subject:
[amibroker] scaling out help
Hello, has anyone else
managed to use scaling out successfully WITH
margin??
Not sure what i am doing
wrong, just using example code (see below),
backtest against eg IBM with
margin at 50 and cant get it to work...
Buy = Cross( MA( C, 10 ), MA(
C, 50 ) );
Sell = 0;
// the system will exit
// 50% of position if FIRST
PROFIT TARGET stop is hit
// 50% of position is SECOND
PROFIT TARGET stop is hit
// 100% of position if
TRAILING STOP is hit
FirstProfitTarget = 10; //
profit
SecondProfitTarget = 20; //
in percent
TrailingStop = 10; // also in
percent
priceatbuy=0;
highsincebuy = 0;
exit = 0;
for( i = 0; i < BarCount;
i++ )
{
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;
}
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 );
}
if( 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
>= 2 )
{
Buy[ i ] = 0;
Sell[ i ] = exit + 1; // mark appropriate exit code
exit = 0;
priceatbuy = 0; // reset price
highsincebuy = 0;
}
}
}
SetPositionSize( 50,
spsPercentOfEquity );
SetPositionSize( 50,
spsPercentOfPosition * ( Buy == sigScaleOut ) );
// scale out 50% of
position
------------------------
Yahoo! Groups Sponsor --------------------~-->
Try Online Currency Trading
with GFT. Free 50K Demo. Trade
24 Hours. Commission-Free.
http://us.click.yahoo.com/RvFikB/9M2KAA/U1CZAA/GHeqlB/TM
--------------------------------------------------------------------~->
Please note that this group
is for discussion between users only.
To get support from AmiBroker
please send an e-mail directly to
SUPPORT {at} amibroker.com
For other support material
please check also:
http://www.amibroker.com/support.html
Yahoo! Groups
Links
<*> To visit your group
on the web, go to:
http://groups.yahoo.com/group/amibroker/
<*> 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/