Hello friend,
i want to try to instantly price trigger buy / short. so i try the cross function. but not getting result in my side. i know my mistak.
please help any one.
dn = DateNum();
new_day = dn != Ref(dn, -1);
Plot( new_day, "", colorBlue, styleHistogram | styleOwnScale | styleDashed, 0, 1 );
SetTradeDelays( 0, 0, 0, 0 );
// In your case to disable H-L price bound checking use following option.
// https://forum.amibroker.com/t/overriding-amibroker-backtester-settings-generating-unrealistic-results/7374
SetOption( "PriceBoundChecking", False );
// https://forum.amibroker.com/t/incremental-loop-to-add-columns/10703/2
selected_dn = Status("rangetodate");
last_day = dn == selected_dn;
_Buy = Cross( Close, BuyValue ) OR Cross( BuyValue, Close );
_Sell = Cross( SellStopLevel, Close );
_Short = Cross( ShortLevel, Close ) OR Cross( Close, ShortLevel );
_Cover = Cross( Close, CoverStopLevel );
// https://forum.amibroker.com/t/sumsince-vs-highestsince-lowestsince/15233/6
function SumSinceInclusive( condition, array )
{
return Sum( condition, array ) + ValueWhen( condition, array );
}
// trades limit
N = 2;
// modify Buy array and allow only first N signals
Buy = Buy AND SumSinceInclusive( Buy, BarsSince( new_Day) +1 ) <= N;
// modify Short array and allow only first N signals
Short = Short AND SumSinceInclusive( Short, BarsSince( new_Day) +1 ) <= N;
// Initialize variables
Buy = Sell = Short = Cover = Null;
LongFlag = ShortFlag = 0; // Flags to record trade state
for( i = 0; i < BarCount; i++ )
{
// Long Positions
if( _Buy[ i ] && !LongFlag && last_day[i] )
{
Buy[ i ] = 1;
LongFlag = 1; // To record that we are in Long position
}
if( _Short[ i ] && LongFlag )
{
Sell[ i ] = 1; // Selling-off the Long position
Short[ i ] = 1; // Shorting-off the Long position
LongFlag = 0; // Reseting LongFlag back to False, to denote that we are no longer in "Long" position
}
// Short Positions
if( _Short[ i ] && !ShortFlag && last_day[i] )
{
Short[ i ] = 1;
ShortFlag = 1; // To record that we are in Short position
}
if( _Buy[ i ] && ShortFlag )
{
Cover[ i ] = 1; // Covering the Short position
Buy[ i ] = 1; // Buying the Short position
ShortFlag = 0; // Reseting ShortFlag back to False, to denote that we are no longer in "Short" position
}
}
Plot( BuyValue , "Buy", colorGreen);
Plot( SellStopLevel, "Sell", colorRed);
Plot( ShortLevel, "Short", colorRed);
Plot( CoverStopLevel , "Cover", colorGreen);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-25);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-35);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-30);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=25);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=35);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-30);