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

Re: [amibroker] Re: Changing the stop loss depending on the stock price?



PureBytes Links

Trading Reference Links

Is it possible that there is an error in the code?


isInTrade = False;
priceatbuy = 0;
highsincebuy = 0;
exit = 0;

for ( i = 0; i < BarCount; i++ )
{
    // if we are not in a trade and we have a buy signal then set the buy price
    if ( NOT isInTrade AND Buy[ i ] )
    {
        // now we are in a trade so set the buy price
        isInTrade = True;
        priceatbuy = BuyPrice[ i ];
    }

    // if we are in a trade then check to see if we hit the profit target
    if ( isInTrade )
    {
                highsincebuy = Max( High[ i ], highsincebuy );
                if( exit == 0 AND
                    High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy)

                //reset isInTrade = False;
    }

I get an error message...

Louis


2008/2/23, tuzo_wilson <j.tuzo.wilson@xxxxxxxxx>:

--- In amibroker@xxxxxxxxxxxxxxx, "Louis Pr�fontaine" <rockprog80@xxx> wrote:

> Wouldn't it be possible to simply write
>
> if( Buy[ i ] )
> {
> priceatbuy = BuyPrice[ i ];
> }
>
> instead of
>
>
> if( priceatbuy == 0 AND Buy[ i ] )
> {
> priceatbuy = BuyPrice[ i ];
> }

The Buy array may have multiple buy signals (i.e. non zero entries) so you can't just use the Buy array.

> if the priceatbuy value was already set to zero before the "for" command? I
> mean: we already know that the only possible value of priceatbuy at this
> point was zero, so why add the if (priceatbuy == 0... ? and not simply if

That's not true.  priceatbuy is set within the for loop.  As its name indicates priceatbuy is used to hold the price when the buy signal was issued.  Plus it is also being used to let you know if you are not in a trade (priceatbuy == 0) so from a logical point of view it is holding more than just the buy price information.

You could rewrite it differently:

isInTrade = False;
priceatbuy = 0;
highsincebuy = 0;
exit = 0;

for ( i = 0; i < BarCount; i++ )
{
    // if we are not in a trade and we have a buy signal then set the buy price
    if ( NOT isInTrade AND Buy[ i ] )
    {
        // now we are in a trade so set the buy price
        isInTrade = True;
        priceatbuy = BuyPrice[ i ];
    }

    // if we are in a trade then check to see if we hit the profit target
    if ( isInTrade )
    {
     
           highsincebuy = Max( High[ i ], highsincebuy );
                if( exit == 0 AND
                    High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy)

                // reset isInTrade = False;
    }
}


The logic is is the same but maybe expressing it that way makes more sense to you?


Tuzo


__._,_.___

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 NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html




Your email settings: Individual Email|Traditional
Change settings via the Web (Yahoo! ID required)
Change settings via email: Switch delivery to Daily Digest | Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | Unsubscribe

__,_._,___