Hi everyone,
I tried to combine buy and short with cover and sell. here the detail :
I have difficulty because the short signal appears not as I expected
Short Signal should appear after Buy Signal occurs. (does not appear if there is NO Buy Signal ) and finished after Sell / Cover occurs. I hope you all can help me.
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
procedure BuyProfitLossStop(pab1, pab2, pab3 ) {
/// code sources:
/// @link https://forum.amibroker.com/t/sell-price-buying-price-x/1358/29
/// @link https://tinyurl.com/y74e8k56
/// derived from:
/// @link https://www.amibroker.com/kb/
/// (commercial use prohibited!)
global Buy, Sell, Short, Cover, BuyPrice, SellPrice, ShortPrice, CoverPrice;
local i, tgt_level, stp_level, Short_level;
local BuyPrice_array1, SellSignal1, BuyPrice_array2, SellSignal2,BuyPrice_array3, Short_Signal1 ;
tgt_level = stp_level = Short_level=Cover_Level1=Cover_level2= 0;
BuyPrice_array1 = BuyPrice_array2 = BuyPrice_array3 =Short_array4=Short_Array5= Null;
for ( i = 0; i < BarCount; i++ ) {
//
if ( Buy[ i ] && tgt_level == 0 && stp_level == 0) {
tgt_level = BuyPrice[ i ] * 1.03;
stp_level = BuyPrice[ i ] * 0.97;
Short_level = BuyPrice[ i ] * 0.99;
} else Buy[ i ] = 0;
//
if ( tgt_level > 0 )
BuyPrice_array1[ i ] = tgt_level;
if ( stp_level > 0 )
BuyPrice_array2[ i ] = stp_level;
if ( Short_level > 0 )
BuyPrice_array3[ i ] = Short_level ;
//
Short_Signal1 = L[ i ] < Short_level;
if ( Short_Signal1 && Short_level > 0 ) {
Short[ i ] = 1;
ShortPrice[ i ] = Min(O[ i ], Short_level);
Short_level = 0;
}
//
SellSignal2 = Cover_signal2 = L[ i ] < stp_level;
if ( SellSignal2 && Cover_signal2 && stp_level > 0 ) {
Sell[ i ] = Cover[ i ] = 1;
SellPrice[ i ] = Cover[ i ] = Min(O[ i ], stp_level);
tgt_level = stp_level = 0;
}
//
SellSignal1 =Cover_signal1 = H[ i ] > tgt_level;
if ( SellSignal1 && Cover_signal1&& tgt_level > 0 ) {
Sell[ i ] = Cover[ i ] = 1;
SellPrice[ i ] = Cover[ i ] = Max(O[ i ],tgt_level);
tgt_level = stp_level = 0;
}
//
}
VarSet(pab1, BuyPrice_array1);
VarSet(pab2, BuyPrice_array2);
VarSet(pab3, BuyPrice_array3);
}
Buy = Cross( MACD(), Signal() );
BuyPrice = Close;
Sell = Short = Cover = 0;
BuyProfitLossStop("tgt_arr", "stp_arr", "Short_x");
Plot( Short_x, "Short_x", colorYellow );
Plot( tgt_arr, "tgt_arr", colorgreen );
Plot( stp_arr, "stp_arr", colorRed );
color1 = IIf( Buy, colorWhite, colorYellow );
y = IIf( Buy, BuyPrice, SellPrice );
PlotShapes( (Buy + Sell) * shapeSmallCircle, color1, layer = 0, y, offset = 0);
PlotShapes(IIf(short, shapeSmallCircle, shapeNone), colorBlue, 0, c, 0)
Thank you for your attention