# 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 );
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();
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 )
{
LastEntryPrice = O[i];
lastEntryDay = curDay;
}
if( tradeOn == 1  AND  curDay == lastEntryDay )
{

Sell[i] = 1;
SellPrice = c[i];
}
}
``````

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

...etc.

1 Like

Thanks fxshrat,

I got point & modify code as

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

Sell[i] = 1;
SellPrice = sp;
}
``````

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];
}
}

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