AB code for multiple exits required

bharatk8

Active Member
#1
I want to know method of coding multiple exits for BT for amibroker.

For Condition1 exit on next bar open or condition2 exit on next bar open or condition3 exit on next bar open

For condition4 exit immediately,(to be used when stop loss is triggered )

Exits for Sell and Cover are not same.i.e. Exit of buy is not trigger for short.and Exit of Short is not trigger for Buy.

Beside making suitable changes in code to take care of multiple exits,am I supposed to make changes in BT settings too?

Regards

bharat
 

bharatk8

Active Member
#2
I have one code to take care of 2 exits.But it has some bugs.I am not able to fix it.Pl. check out the code and correct it.

////////////////////// HERE WE WRITE YOUR System SIGNALS /////////////////////
// below i wrote 4 condition for buy,sell to fiil them up if you wish ...
//--------------------- condition Long
CondL1 = (C==H) AND (V>Ref(V,-1))

CondL2 = 0;

CondL3 = 0;

CondL4 = 0;

//--------------------- condition SELL
CondSL1 = H < Ref( H, -1 );

CondSL2 = 0;

CondSL3 = 0;

CondSL4 = 0;

//--------------------- condition short
CondS1 = (C==L) AND (V>Ref(V,-1))

CondS2 = 0;

CondS3 = 0;

CondS4 = 0;

//--------------------- condition Cover
CondC1 = L > Ref( L, -1 );;

CondC2 = 0;

CondC3 = 0;

CondC4 = 0;

//--------AND THE FINAL ---BUY / SHORT WE HAVE TO WRITE BELOW HERE-----

buyOK *= Ref( CondL1, -1 );





SellOK = Ref( CondSL1, -1 );

ShortOK = Ref( CondS1, -1 );

CoverOK = Ref( CondC1, -1 );



// -------- DO NOT TOUCH BELOW HERE IF YOU NOT KN/OW/ WHAT ARE YOU DOING ----------------------
//--------------------- trading hours 91500 to 153000
TradeTime = ParamToggle( " backTest with Time?", "No|Yes", 0 ) ;

StartTime = ParamTime( "Start Time", "9:30" );

EndTime = ParamTime( "End Time", "15:15" );

EA = TimeNum() >= starttime AND TimeNum() <= endtime;

EA = TimeNum() >= 093000 AND TimeNum() <= 151500;

if ( TradeTime )
{
//--------------------- Buy, Sell, Short, Cover, Here is with time
Buy = buyOK AND EA ;
BuyPrice = Open;
Sell = SellOK OR EndTime ;
SellPrice = Close;
Short = ShortOK AND EA;
ShortPrice = Open;
Cover = CoverOK OR EndTime ;
CoverPrice = Close;
TitleTradeTime = "\nBacktest Trade With Time = ON";
}
else
{
//--------------------- Buy, Sell, Short, Cover, Here is without time
Buy = buyOK ;
BuyPrice = Open;
Sell = SellOK ;
SellPrice = Open;
Short = ShortOK ;
ShortPrice = Open;
Cover = CoverOK ;
CoverPrice = Open;
TitleTradeTime = "\nBacktest Trade Time is = OFF ";
}

Buy = ExRem( Buy, Sell );

Sell = ExRem( Sell, Buy );
Short = ExRem( Short, Cover );
Cover = ExRem( Cover, Short );


//--------------------- EXPLORATION
lastpriceBuy = ValueWhen( Buy, Open, 1 );
lastpriceSELL = ValueWhen( Sell, Open, 1 );
lastpriceShort = ValueWhen( Short, Open, 1 );
lastpriceCover = ValueWhen( Cover, Open, 1 );
BuyProfit = ( ( lastpriceSELL - lastpriceBuy ) / lastpriceSELL ) * 100; "BuyProfit= "
+ WriteVal( BuyProfit, 1.4 ); // return a single trade BUY-Sell
ShortProfit = ( ( lastpriceShort - lastpriceCover ) / lastpriceCover ) * 100;

//// Gainers/Lossers
monodesLong = Cum( BuyProfit ) ;
monadesShort = Cum( ShortProfit );
total = monodesLong;

if ( Status( "Action" ) == actionScan OR Status( "Action" ) == actionExplore )
{
Width60 = 60;
Filter = Buy OR Sell OR Short OR Cover;

colorBuyProfit = IIf( BuyProfit > 0, colorBrightGreen, colorRed );
ColorShortProfit = IIf( ShortProfit > 0, colorBrightGreen, colorRed );

SetOption( "NoDefaultColumns", True );
AddTextColumn( Name(), "Symbol", 77, 1, colorDefault, 70 );
AddColumn( DateTime(), "Date", formatDateTime, 1, colorDefault, 120 );
// these 5 linea are for Long trades
AddColumn( IIf( Buy, Asc( "B", 0 ), Asc( "S" ) ), "PosL", formatChar );
AddColumn( Buy, "Buy", 1 );
AddColumn( IIf( Buy, lastpriceBuy, Null ) , "BUY", 1.4, colorDefault, colorDefault, 60 );
AddColumn( IIf( Sell, lastpriceSELL, Null ), "SELL", 1.4, colorDefault, colorDefault, 60 );
AddColumn( a1 = IIf( Sell, lastpriceSELL - Ref( lastpriceBuy, -1 ), Null ), "Long-Pip", 1.4, colorDefault, colorBuyProfit , 60 );

// these 5 linea are for Short trades
AddColumn( IIf( Short, Asc( "S", 0 ), Asc( "C" ) ), "PosS", formatChar );
AddColumn( Sell, "Short", 1 );
AddColumn( IIf( Short, lastpriceShort, Null ) , "Short", 1.4, colorDefault, colorDefault, 70 );
AddColumn( IIf( Cover, lastpriceCover, Null ), "Cover", 1.4, colorDefault, colorDefault, 70 );
AddColumn( b1 = IIf( Cover, Ref( lastpriceshort, -1 ) - lastpriceCover, Null ), "ShortPip", 1.4, colorDefault, ColorShortProfit , 60 );



a3 = IIf( Sell, a1, IIf( Cover, b1 , Null ) ); // tell me the points of every trade
Mycolor = IIf( a3 > 0 , colorGreen, colorRed ); //is it correct??????no ap3ut it after a3 line this line
AddColumn( a3, "Gain/Loss", 1.4, Mycolor, colorBuyProfit , 80 ); // contains wining trades of every 2 lines
Percentange = IIf( Sell, ( ( lastpriceSELL - lastpriceBuy ) / lastpriceBuy ) * 100, IIf( Cover, ( ( lastpriceShort - lastpriceCover ) / lastpriceCover ) * 100, Null ) );

//AddColumn(a3,"Gain/Loss",1.4,colorDefault,colorBuyProfit ,80); // contains wining trades of every 2 lines
// all above are corect for gia BUY SELL sort cover

AddSummaryRows( 1, 1.4, 4, 7, 9, 12, 13, 15 );
} // end actionExplore


//--------------------- appearance of various signals of long / short in the chart
if ( Status( "Action" ) == actionIndicator )
{
SetChartBkColor( ParamColor( "backround ", colorBlack ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
strWeekday = StrMid( "SunMonTueWedThuFriSat", SelectedValue( DayOfWeek() ) * 3, 3 );
Title1 = Name() + "," + Date() + "," + " TimeFrame: " + Interval( 2 ) + "\n Open " + O + ", High " + H + ", Low " + L + ", Close " + C + ", " + "(" + WriteVal( ROC( C, 1 ), 1.2 ) + "%)" ;

Display_Bars = ParamToggle( "Display Bars", "No|Yes", 1 );

if ( Display_Bars )
Plot( C, "" , colorWhite, styleBar );

if ( TradeTime )
Plot( IIf( EA, 6, -1e10 ), "", colorBlueGrey , styleOwnScale | styleArea | styleNoLabel, -1, 90 );

////////////////////// Arrows
belakiaBS = ParamToggle( "Arrows Buy-Sell", "HIDE|SHOW", 1 );

if ( belakiaBS )
{
PlotShapes( IIf ( Buy, shapeUpArrow, shapeNone ), colorGreen, 0, L, -30 );
PlotShapes( IIf ( Buy, shapeSmallCircle, shapeNone ), colorGreen, 0, O, 0 );
PlotShapes( IIf ( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, -40 );
PlotShapes( IIf ( Sell, shapeSmallCircle, shapeNone ), colorRed, 0, O, 0 );
}

belakiaSC = ParamToggle( "Arrows Short-Cover", "HIDE|SHOW", 1 );

if ( belakiaSC )
{
PlotShapes( IIf( Short, shapeSmallDownTriangle, shapeNone ), colorRed, 0, H, -30 );
PlotShapes( IIf ( Short, shapeSmallCircle, shapeNone ), colorRed, 0, O, 0 );
PlotShapes( IIf( Cover, shapeSmallUpTriangle, shapeNone ), colorOrange, 0, L, -40 );
PlotShapes( IIf ( Cover, shapeSmallCircle, shapeNone ), colorOrange, 0, O, 0 );
}

////////////////////// Sum Total buy and short positions

// Value of one lot of Nifty is approx. Rs.250,000 for which Rs.25000 margin is required.
// commission for 1 lot=Rs250 Stop=12
TotalPosBuy = Cum( Buy );

x1 = Cum( BuyProfit );

x2 = Sum( ShortProfit, 98 );

NumContracts = Param( "NumContracts ", 1, 1, 100, 1 );

Commission = TickSize * Param( "Commission", 250, 1, 450, 1 ); // 250 per lot

TotalCommisionTrade = TotalPosBuy * NumContracts * Commission ;

TitleSynolo = " Entry Long Trades = " + TotalPosBuy + "\n Num Contracts " + NumContracts + "\nTotal Commision Of Long Trades RS = " + TotalCommisionTrade +
"\n";



EventNum = TotalPosBuy;

FirstVisibleBar = Status( "FirstVisibleBar" );

Lastvisiblebar = Status( "LastVisibleBar" );

for ( b = Firstvisiblebar; b < Lastvisiblebar AND b < BarCount; b++ )
{
dist = TickSize * 9;
colorBuyProfit = IIf( BuyProfit > 0, colorGreen, colorRed );
ColorShortProfit = IIf( ShortProfit > 0, colorGreen, colorRed );
////////////////////// PlotText Total Pos Buy appear
TextEventNum = ParamToggle( "Event TotalPosBuy", "HIDE|SHOW", 0 );

if ( TextEventNum )
{
if ( Buy )
PlotText( "\n\n\n\nEvent\n" + NumToStr( EventNum, 1.0, False ), b, L, colorWhite );

if ( Sell )
PlotText( "Event\n" + NumToStr( EventNum, 1.0, False ), b, H, colorWhite );
}

////////////////////// PlotText we appear gainers / lossers
TextPipsBS = ParamToggle( "Plot Buy Pips", "HIDE|SHOW", 0 );

if ( TextPipsBS )
{
if ( Buy )
PlotText( "Buy\n\n" + lastpriceBuy , b, L - dist, colorBrightGreen ); //ColorShortProfit

if ( Sell )
PlotText( "Sell\n" + lastpriceSELL + "\n" + WriteVal( BuyProfit , 1.4 ) , b, H + dist, colorBlack, colorBuyProfit );
}

TextPipsSC = ParamToggle( "Plot Short Pips", "HIDE|SHOW", 0 );

if ( TextPipsSC )
{
if ( Short )
PlotText( "\n\n\nShort\n" + lastpriceShort, b, H + dist, colorOrange );

if ( Cover )
PlotText( "\n\n\nCover\n" + lastpriceCover + "\n" + WriteVal( ShortProfit , 1.4 ), b, L - dist, colorBlack, ColorShortProfit );
}

///////// END ///////////// PlotText we appear gainers / lossers

////////////////////// PlotText we appear the Time of the Trade
myDateTime = DateTime();

TextDateOnChart = ParamToggle( "Plot Text Date Time", "SHOW TextDate|HIDE TextDate", 0 );

if ( TextDateOnChart )
{
if ( Buy )
PlotText( "\n\n\n" + NumToStr( myDateTime, formatDateTime ) + "", b, H[ b ] - dist, colorLime );

if ( Sell )
PlotText( "\n\n\n" + NumToStr( myDateTime, formatDateTime ) + "" , b, L[ b ] + dist, colorLime );
}

//////// END ////////////// PlotText we appear the Time of the Trade

} // end FirstVisibleBar

Eq = Equity( 1 );

if ( ParamToggle( "Equity", "HIDE|SHOW", 0 ) )
Plot( Eq, "", colorYellow, 1 | styleOwnScale );

Title = " # " + strWeekday + " # " + Title1 + TitleTradeTime + "\n\n" + TitleSynolo ;

} // end actionIndicator


_SECTION_END();////////////////////// Sum Total buy AND Short positions



// Value of one lot of Nifty is approx.Rs.300,000 for which Rs.10000 margin is required.


// commission for 1 lot=Rs250; Stop=12


TotalPosBuy = Cum( Buy );

x1 = Cum( BuyProfit );

x2 = Sum( ShortProfit, 98 );


NumContracts = Param( "NumContracts ", 1, 1, 100, 1 );


Commission = TickSize * Param( "Commission", 250, 1, 450, 1 ); // 250 per lot


SumCommisionBuyTrades = TotalPosBuy * NumContracts * Commission ;




TotalPosShort = Cum( Short );

SumCommisionShortTrades = TotalPosShort * NumContracts * Commission ;




TotalCommisionTrade = 0;




TitleSynolo = " Entry Long Trades = " + TotalPosBuy + " Entry
Short Trades = " + TotalPosShort + "\n Num Contracts " + NumContracts
+ "\nTotal Commision Of Long
Trades RS = " + SumCommisionBuyTrades +


"\nTotal Commision Of Short
Trades RS = " + SumCommisionShortTrades
+
 
Last edited:

Similar threads