Get price at Crossover and add TP to value

Hello,

I want to set a specific take profit for each trade i take at crossover.
eg when ever there's a cross over a buy signal is generated i want the sellsignal to be + 5 points to the buysignal
( sellsignal = buysignal + 5points )

i am using ValueWhen function is it correct or should i use a different function

here's my afl

mystochasticD = StochD(5,3,3);
mystochasticK = StochK(5,3);
Plot(mystochasticD,"stochastic",colorWhite,styleLine);
Plot(mystochasticK,"stochastic",colorRed,styleLine);

buysig = Cross(mystochasticK,mystochasticD);
Buy_arrow = buysig * shapeUpArrow;
PlotShapes(Buy_arrow,colorYellow);

sellsig = ValueWhen(buysig,C,0)+ 2;
sell_arrow = sellsig * shapeUpArrow;
PlotShapes(sell_arrow,colorPink);

Thanks

ValueWhen() with 0, ie. lookahead or future peek itself is risky and depends on what you are doing.

Another thing is a new signal in Buy will change the original value returned by ValueWhen() and your computation will be erroneous.

Instead try ApplyStop() https://www.amibroker.com/guide/afl/applystop.html as you can target different exit types and trail as well.

1 Like

Hi,

Thanks i am using this code, however i see that the sell signal is being generated all the time.

sellsig = ApplyStop(stopTypeProfit ,stopModePoint,5,1,False);

Here's
Entire Code

mystochasticD = StochD(5,3,3);
mystochasticK = StochK(5,3);
Plot(mystochasticD,"stochastic",colorWhite,styleLine);
Plot(mystochasticK,"stochastic",colorRed,styleLine);

buysig = Cross(mystochasticK,mystochasticD);
Buy_arrow = buysig * shapeUpArrow;
PlotShapes(Buy_arrow,colorYellow);
Buy = buysig;
Sell = 0;
sellsig = ApplyStop(stopTypeProfit ,stopModePoint,5,1,False);
sellsigs = ExRem(Buy,sellsig);
sell_arrow = sellsigs * shapeDownArrow;
PlotShapes(sell_arrow,colorPink);

if ( LastValue(Buy) == True )
{
	placeOrderUsingParams(AT_EXCHANGE, AT_SYMBOL, "BUY", AT_ORDER_TYPE, AT_QUANTITY, buyPrice, defaultTriggerPrice(), 1);
}

if ( LastValue(Sell) == False )
{
	placeOrderUsingParams(AT_EXCHANGE, AT_SYMBOL, "SELL", AT_ORDER_TYPE, AT_QUANTITY, sellsigs, defaultTriggerPrice(), 1);
}

Read here about why ValueWhen is not proper way of applying stops.


Because it is wrong usage. Why don't you follow exactly the documentation on ApplyStop reference page? You can clearly see there that it is written at the top that the function returns nothing. See void type. Also the examples there do not use any assignments. Also as mentioned at the other forum link there are enough examples on KB of AmiBroker already (stop example codes via ApplyStop or looping).

And if you want to plot stops generated by ApplyStop in chart then you have to evaluate them via Equity() function first. Carefully read upper links (and not just half of it but all of it).

2 Likes