SellPrice Issue

Simple strategy. EOD
Buy tomorrow on open and Sell on Close same day, whatever profit or loss.
My Code is

sig = Cross( RSI( 2 ), RSI( 4 ) );
Buy = Ref( sig , -1 );
BuyPrice = O;
Sell =1;
SellPrice = C;

This Produce my intended result, but when I try to use 'for Loop ’ to achieve same result, things not work.

My new code is

Buy = 0;
Sell = 0;

days = DayOfYear();
tradeOn = 0;
lastEntryDay = 0;


buyArray  = Cross( RSI( 2 ), RSI( 4 ) );
sig = Ref( buyArray , -1 );


for( i = 1; i < BarCount ; i++ )
{
    curDay = days[i];

    if( sig[i] == 1   AND tradeOn == 0 AND lastEntryDay != curDay )
    {
        Buy[i] = 1;
        tradeOn = 1;
        LastEntryPrice = O[i];
        BuyPrice[i] = LastEntryPrice;
        lastEntryDay = curDay; 
    }
    if( tradeOn == 1  AND  curDay == lastEntryDay )
    {

        Sell[i] = 1;
        SellPrice = c[i];
        tradeOn = 0;
    }
}

In new code BuyPrice is OK (Open Price) but SellPrice is not a Close but Low.
What is wrong in this code … Please.
Here I also try to only one trade per day.

SellPrice is an ARRAY!

SellPrice[i] = C[i];

If you don't know return type of a variable then use typeof()
https://www.amibroker.com/guide/keyword/typeof.html

printf( typeof(Sellprice) );

or

_TRACE( typeof(Sellprice) );

or debugger

https://www.amibroker.com/guide/h_debugger.html

1

...etc.

1 Like

Thanks fxshrat,

I got point & modify code as

 if( tradeOn == 1  AND  curDay == lastEntryDay )
    {

        Sell[i] = 1;
        sp = ValueWhen(Buy[i],C);
        SellPrice = sp;
        tradeOn = 0;
    }

Works Ok. Thanks

Your code is still incorrect. Correct code just:

// [..UNCHANGED PART REMOVED..  ]
    if( tradeOn == 1  AND  curDay == lastEntryDay )
    {
        Sell[i] = 1;
        // REMOVE THIS SellPrice = c[i];
        tradeOn = 0;
    }
}

BuyPrice = Open; // always open
SellPrice = Close; // set sell price outside the loop 

as you just exit on CLOSE (there is NO condition here, the sell price is always close)

Inside the loop you only need to do things that depend on bar-by-bar condition. Anything else should be outside the loop and done as single array assignment. The same with BuyPrice. It is always open.

Also I see no point in using the loop as array based code that you used in first place is correct and works. Why do you make things more error prone when you already got code that was 20x more efficient, faster and shorter.

1 Like