Hi Tuzo,
Here is the code I managed to build (with your help):
Buy = RSI (14)<30;
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 = 25; // in percent
TrailingStop = 15; // also in percent
isInTrade = False;
priceatbuy=0;
highsincebuy = 0;
exit = 0;
for( i = 0; i < BarCount; i++ )
{
if ( NOT isInTrade AND Buy[ i ] )
{
isInTrade = True;
priceatbuy = BuyPrice[ i ];
}
if( isInTrade )
{
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, spsPercentOfPosition * ( Buy == sigScaleOut )); //
scale out 50% of position
Short=0;
Cover=0;
///
///
///
///
///
For some reason, it does not work very well, as I don't see when the program
gets out of 50% of the position on the equity curve. I just see, as an
example, that on the graph the buy is at 62 and the sell at 75 but I see
only 9% and a sell at let's say 65 in the report.
Moreover, I don't know how to add other selling rules to that (where to put
those rules in the code) and I don't understand the meaning of this part Sell[
i ] = exit + 1; // mark appropriate exit code
If you can help me, as always it would be greatly appreciated!
Thanks,
Louis
2008/2/23, tuzo_wilson <[EMAIL PROTECTED]>:
>
> --- In [email protected] <amibroker%40yahoogroups.com>, "Louis
> Pr�fontaine" <[EMAIL PROTECTED]>
> wrote:
>
> > I now understand that for each bar "i", the program now looks if the bar
> > value is less than the barcount and then apply the formula below.
> But what
> > I don't understand there is how can "priceatbuy" differ from 0 since
> it was
> > defined as 0 just over (first line of the code). How could
> "priceatbuy",
> > which was given a value of 0 could have a value over 0 then?
> >
> > I understand that the bar in which a buy was issued is represented
> as Buy
> > [i], but why the "priceatbuy ==0" before?
>
> priceatbuy is set to 0 before the loop but the the value is also set
> within the loop. If priceatbuy is 0 and Buy[i] is non-zero then
> priceatbuy is set to the BuyPrice[i]. i.e. if there is a buy signal
> and we haven't set the buyatprice then set the buyatprice to be the
> Buy Price for that bar.
>
> Basically, priceatbuy lets you know if you are in a trade. If it's
> zero then you aren't in a trade but if it's non-zero then you are in a
> trade so you need to check the profit target exit criteria.
>
> > 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)
>
> Tuzo
>
>
>