oppz
November 6, 2021, 4:32pm
#1
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;
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;
I have tried different variants of ExRem, but without success..
Any suggestion ?
fxshrat
November 6, 2021, 6:18pm
#2
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.
Here is another picture using code of this thread
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:
ApplyStop function by default requires us to provide stop amount (expressed in either dollar or percentage distance from entry price). Therefore, if we want to place stop at certain price level, then we need to calculate the corresponding stop amount...
No, do not use ExRem. Just recent thread:
You did not read the documentation of Applystop carefully or completely.
You do not need ExRem.
Please read doc page from top to bottom including comments:
https://www.amibroker.com/guide/afl/applystop.html
So call Buy Sell after Equity call in chart.
Also knowledge base has example for plotting lines after AppyStop.
Besides forum has examples of plotting profit and loss target lines if using ApplyStop already.
Please use forum search feature.
3 Likes
oppz
November 7, 2021, 11:28am
#3
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.
system
Closed
February 15, 2022, 11:29am
#4
This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.