Sell at second profitable opening

I want to sell in the second profitable opening, but I can't get the code to work, when I add "AND BarsSince(Buy) >= 2" or "&& BarsSince(Buy) >= 2" it doesn't work anywhere....

Where would you put it?

//EXIT WITH FIRST PROFITABLE OPENING bullish
priceatbuy = 0; bi = BarIndex();

for( i = 0; i < BarCount; i++ )
{
if( priceatbuy == 0 && Buy[ i ] ) //First buy
{
priceatbuy = BuyPrice[ i ]; //price buy
stopprice = priceatbuy - priceatbuy*0.05; //stop loss price
EntryBar = bi[ i ]; //BAR WHERE WE BUY
}
if( priceatbuy > 0 && bi[ i ] > EntryBar && Open[ i ] > priceatbuy ) //IF BUY AND First Profit Opening
{
Sell[ i ] = 1 AND BarsSince(Buy) >= 2;
SellPrice[ i ] = Open[i];
priceatbuy = 0;
}
else if( priceatbuy > 0 && Low[ i ] < stopprice ) //IFSTOP LOSS
{
Sell[ i ] = 1;
Buy [ i ] = 0; //IF IT IS TIME TO SELL WE CANNOT BUY
SellPrice[ i ] = stopprice;
priceatbuy = 0;
}
else
Sell[ i ] = 0;
}

  1. It is incorrect because you apply entire array inside loop. Besides using BarsSince is not correct.

  2. You do not need looping for that. Just use ApplyStop function with ValidFrom argument being set to 2 (in you case).

    24

    (Also remember that you can write multiple ApplyStop lines - one per stop type)


Please use code tags if inserting code.

1 Like

Thanks again fxshrat!

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.