Problem with ValueWhen and Buy

Hi

Trying to make a code that will sell at a previous highest high, and my code looks like this;

initial = 100000;  
SetOption( "InitialEquity", initial );  
  
lev1 =  1;
lev = 1 / lev1 ;  
MarginDeposit = 100000 * lev;  
  
SetOption( "PriceBoundChecking", False );  
SetOption( "ActivateStopsImmediately", True );
SetBacktestMode( backtestRegular );
  
SetPositionSize( initial  , spsValue );  
  
SetOption( "CommissionMode", 3 );  
SetOption( "CommissionAmount", 0 );  
spread = 2;  
  
sl_m = 	10;	
ixxV_t = 	150;		
ixxV_t2 = 200;

HHV1 = Ref( HHV( H, ixxV_t ), -1 );  
HHV2 = Ref(HHV( H, ixxV_t2 ),-1 );  

SetTradeDelays( 0, 0, 0, 0 );  

Buy =  H >= HHV1 AND  O < HHV1;
BuyPrice = HHV1 + spread;
SellTarget = ValueWhen(Buy == 1, HHV2,1);

Sell = H > SellTarget AND L < SellTarget;
SellPrice =  IIf( O > SellTarget , O, SellTarget);

ApplyStop( stopTypeLoss, stopModePercent, SL_m , 1, 1, 1, 0 );  

Whats happened is as in this picture;
Amibroker_1

It will exit the position when a new buy signal (in theory) is created (and I assume a new ValueWhen is created). But I assume that Amibroker did not create a new Buy signal until a sell has been created (backtest mode regular) . So why do valueWhen change from my original Buy, to something new, when no new Buy should be created? What I want is as in this picture;
amibroroker 2

I have tried different variants of ExRem, but without success..

Any suggestion ?

You have to use ApplyStop but not Sell = H > ValueWhen...

ValueWhen(Buy, L)

or

ValueWhen(Buy, H)

create new line with every new buy signal.

32

Here is another picture using code of this thread
551

Just plot ValueWhen and you will see

Plot( ValueWhen(Buy, Close), "ValueWhen(Buy,Close)", colorGreen );

For proper stop loss and/or profit target stop you have to use ApplyStop or looping!

There are even examples at Knowledge base of AmiBroker.
E.g. this one:


No, do not use ExRem. Just recent thread:

3 Likes

Hi

Many thanks, it works perfectly with ApplyStop, should have thought about that earlier.... So a Buy is still a "Buy" even before a Sell during BacktestModeRegular, hence a ValueWhen will not work? And as stated in other posts, ApplyStop is the function to use, so I fully understands that now.

To help others with the same problem, my code now is;

initial = 100000;  
SetOption( "InitialEquity", initial );  
  
lev1 =  1;
lev = 1 / lev1 ;  
MarginDeposit = 100000 * lev;  
  
SetOption( "PriceBoundChecking", False );  
SetOption( "ActivateStopsImmediately", True );
SetBacktestMode( backtestRegular );
  
SetPositionSize( initial  , spsValue );  
  
SetOption( "CommissionMode", 3 );  
SetOption( "CommissionAmount", 0 );  
spread = 2;  
  
sl_m = 10;				
ixxV_t = 150;		
ixxV_t2 =  200;

HHV1 = Ref( HHV( H, ixxV_t ), -1 );  
HHV2 = HHV( H, ixxV_t2 );  

SetTradeDelays( 0, 0, 0, 0 );  

BuyPrice = HHV1 + spread;
Buy =  H >= HHV1 AND  O < HHV1;

SellPip = Max((HHV2 - BuyPrice),1);
Sell = 0;

ApplyStop(stopTypeProfit,stopModePoint,SellPip,1,False,1,1);
ApplyStop( stopTypeLoss, stopModePercent, SL_m , 1, 1, 1, 0 );  

But that leads me to another question, why must SellPip be > 0 ? It makes of course sense from a trading perspective if I want to make money, but it would be interesting to learn from a coding perspective.

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