SellPrice doesn't work with IIF ?

#1
dear all,

I'm achieving weird results assegning Sellprice by IIF function:

I want to sell at the close of the day before and the following script work very well:
SetTradeDelays (0,1,1,1);
SellPrice=Ref(C,-1);


But if there is a gap, and next day open is below the before day close, I want to sell at next day open. The following script works very well
SetTradeDelays (0,1,1,1);
SellPrice=O;


But putting together the 2 above scripts for Sellprice in a IIF instruction:
SellPrice= IIf ( Ref(O, 1) < C, O, Ref(C,-1));

I achieve weird results, selling, now and then, at Open also when next day open is higher than before day close. :confused:

please test the IIF string with your data and let me know.

thank you

cippo
 

trash

Well-Known Member
#2
It does not work because of your mistakes.
Do you realize that Ref(O, 1 ) looks into future??

Besides why using IIf if there is Min?

SellPrice = Min( O, Ref(C, -1 ) );
 
Last edited:

trash

Well-Known Member
#3
Don't you rather want this?

Code:
SetTradeDelays( 0, 0, 0, 0 ); // zero delays as we use Ref() to shift sell signal.
SetOption( "PriceBoundChecking", False ); 

Buy = Cross( C, MA( C, 50 ) );
BuyPrice = Close; 

Sell = Cross( MA( C, 50 ), C );
RefPrice = Ref( Close, -1 );
Sell = Ref( Sell, -1 ) AND H > RefPrice;
SellPrice = Max(Open, RefPrice);
 
#4
It does not work because of your mistakes.
Do you realize that Ref(O, 1 ) looks into future??

Besides why using IIf if there is Min?

SellPrice = Min( O, Ref(C, -1 ) );
hello trash, thank you for your answer, you are my angel teacher!

I thought that when sellprice instruction is processed we are still in the selling bar and we are defining the sellPrice looking in the future to compare the open value of next day with current bar close.
But I see you are right, in fact thinking to be already in next bar, that is the one in which happens the actual selling, the script works very well:

SetTradeDelays (0,1,1,1)
SellPrice= IIf (O < Ref(C,-1), O, Ref(C,-1));

thank you again ... I have been trying to solve this problem all over the Sunday ...
 
#5
Don't you rather want this?

Code:
SetTradeDelays( 0, 0, 0, 0 ); // zero delays as we use Ref() to shift sell signal.
SetOption( "PriceBoundChecking", False ); 

Buy = Cross( C, MA( C, 50 ) );
BuyPrice = Close; 

Sell = Cross( MA( C, 50 ), C );
RefPrice = Ref( Close, -1 );
Sell = Ref( Sell, -1 ) AND H > RefPrice;
SellPrice = Max(Open, RefPrice);
My whole selling script is the following:
SetTradeDelays (0,1,1,1);
Sell = C < MA(C, 50) AND Ref(L,1)<Min(C,O) ;
SellPrice= IIf (O < Ref(C,-1), O, Ref(C,-1)); //or =Min(Ref(C,-1), O) as you suggest


so I look in the future to check if the next bar has a low lower than min(C,O) of the current bar. This cause in real life I put a selling order under these values.

yes, I should have used zero delay too. May be I'd dealed better with the SeelPrice. This is the script with zero delay. It gives the same results of the above script:
SetTradeDelays (0,0,1,1);
Sell = ref(C,-1) < ref(MA(C, 50),-1) AND L<ref(Min(C,O),-1) ;
SellPrice= IIf (O < Ref(C,-1), O, Ref(C,-1));


The strange thing is that with delay zero or delay 1 the sellPrice string keeps just the same. ???
 
Last edited:

trash

Well-Known Member
#6
If you set settradedelays to zero but instead use Ref(.., -delay) then you still have a trade delay! Read the manual, man.

Besides the only strange thing are your code lines.
 
#7
If you set settradedelays to zero but instead use Ref(.., -delay) then you still have a trade delay! Read the manual, man.

Besides the only strange thing are your code lines.
yes, trash, scold your pupil, I really deserved it!
Anyway manual is not so reach of info and examples about sellprice.
In the meantime I understood why sellprice row is the same independently ofdelay. It is cause settradedelays is only relative to sell instruction and not sellprice instruction. And, as you say, using ref in the delay zero I produced the same delay of settradedelays (0,1,).

thank you again

cippo
 

trash

Well-Known Member
#8
yes, trash, scold your pupil, I really deserved it!
Anyway manual is not so reach of info and examples about sellprice.
In the meantime I understood why sellprice row is the same independently ofdelay. It is cause settradedelays is only relative to sell instruction and not sellprice instruction. And, as you say, using ref in the delay zero I produced the same delay of settradedelays (0,1,).

thank you again

cippo
What is there to understand about Sellprice??
Sellprice is the price at which you sell. Price means that it is not boolean but ... well ... a price variable. Hallelujah. So what else is there to understand about it?

Besides function reference of settradedelays in the manual clearly illustrates that it is referring to buy, sell, short, cover but not to prices. Quote from help file:

SetTradeDelays
- allows to control trade delays applied by the backtester Trading system toolbox
(AFL 2.1)


SYNTAX SetTradeDelays( buydelay, selldelay, shortdelay, coverdelay )
RETURNS nothing
FUNCTION Sets trade delays applied by the backtester. This function allows you to override trade delays from the "Settings" page. It is important do understand what trade delays really do. They in fact internally apply the following:

Buy = Ref( Buy, -buydelay );
Sell = Ref( Sell, -selldelay );
Short = Ref( Short, -shortdelay );
Cover = Ref( Cover, -coverdelay );

inside backtester after your formula is executed but before backtester starts trade simulation. It is functionally equivalent to having above 4 lines at the end of your formula. Note that NO OTHER variables are affected by trade delays, therefore for example if your position sizing depends on values found in buy/sell/short/cover variables *and* if you are using non-zero trade delays you need to account for that in your code.
EXAMPLE settradedelays( 1, 1, 1, 1 )
I think it can not be written more clearly but mostly is just not being read carefully.
 
#9
What is there to understand about Sellprice??
Sellprice is the price at which you sell. Price means that it is not boolean but ... well ... a price variable. Hallelujah. So what else is there to understand about it?

I think it can not be written more clearly but mostly is just not being read carefully.
I read several times this info and it was all quite clear about sell delay, but I was complaining about Sellprice instruction that are not clear.
If NO OTHER variables are affected by trade delays, so why if I want a sellprice at the value of the close of the bar in which sell signal comes (but that will be executed in the next bar cause the delay) I have to write sellprice = ref(c,-1)? shouldn't I just write Sellprice=C?
It seems that Sellprice instruction is read and executed in the day of the actual selling, so writing Sellprice=O it means next day Open for delay (1) but same bar for the delay (0).

Anyway, Now I know it works in this way and I'll write my script following this guideline.

thank you again ... and ... may I ask your suggestion for this other trouble too?
http://www.traderji.com/amibroker/96603-peak-instruction-doesnt-allow-change-array.html


thank you
cippo
 
Last edited:

trash

Well-Known Member
#10
If your trade delay is zero then sellprice is at same bar so sellprice = c; and if settradedelays is >0 then trade entry/exit is x bars later with tradeprice i.e. sellprice = o; or sellprice = c; or… .Tradedelay affects trade signals not prices.

And looking into the future is just wrong since you can not look into the future in live tradeing too.