Hi,
thanks for your reply. Adding my code below. And more over I have another problem. The sellprice assigned inside the for loop is not working. Coverprice works. So I have assinged sellprice out of the loop again.
Thanks
function Round2TickSize( aValue ) // for rounding the prices to ticksize
{
TickInv = 1 / TickSize;
return ( round( aValue * TickInv ) / TickInv );
}
function RoundLotSizer( aValue )
{
roundlot = Int( aValue / RoundLotSize ) * RoundLotSize; // Note: RoundLotSize is an array of same values
return roundlot;
}
SetOption( "CommissionMode", 1 );
SetOption( "CommissionAmount", 0.02 );
SetOption( "InitialEquity", 1000000 );
SetOption( "FuturesMode", True );
SetOption( "AllowSameBarExit", True );
SetOption( "UsePrevBarEquityForPosSizing", True );
SetOption( "ReverseSignalForcesExit", True );
// Initial Parameters
Market_end_time = 152500;
DN = DateNum();
TN = TimeNum();
Shares = 10*RoundLotSize;
Market_open_time = ValueWhen( Day() != Ref( Day(), -1 ), TN, 1 );
// Trail Stop
per3 = Optimize( "TSL Multiple (ATR)", 3, 3, 5, 1 );
ATR0 = Ref( atr( 14 ), -1 );
multiRisk = 2;
partialTgtTP = 0.5;
// freeze timing.
rangeh = Ref(HHV( H, 14 ),-1);
rangel = Ref(LLV( L, 14 ),-1);
// Buy sell short cover
Buy0 = Cross( H, rangeh ) ;
//sell0 = cover0 = TN == Market_end_time;
Short0 = Cross( rangel, L );
// Stop management
//initialize buy,sell...
Buy = Sell = Short = Cover = Null;
tslarr = Null;
tslsarr = Null;
profitltgt = profitStgt = Null;
sl = 0;
SellPrice = BuyPrice = ShortPrice = CoverPrice = null;
//flag indicates whether you are in Long,Short position
Bflag = sflag = selltype = longbalance = shortbalance = longentry = shortentry = longexit = shortexit = 0;
//barcount means the number of bars/candles in chart/data being analysed
//i is a counter for indexing arrays
for( i = 4; i < BarCount; i++ )
{
// Long
longbalance[i] = longbalance[i - 1];
//Entry side Long
if( buy0[i] AND sflag == 0 AND bflag == 0 AND TN[i] > Market_open_time[i] AND TN[i] < Market_end_time )
{
Buy[i] = 1;
bflag = 1;
BuyPrice[i] = Max( rangeh[i] + TickSize, Open[i] );
tslarr[i] = round2ticksize( Max( rangel[i], L[i - 1] - ( per3 - 1 ) * ATR0[i] ) );
profitLTgt[i] = round2ticksize( ( BuyPrice[i] - tslarr[i] ) * multiRisk + BuyPrice[i] );
longentry[i] = longbalance[i] = shares;
partialTPDone = 0;
}
// TL Stop
if( bflag == 1 )
{
tslarr[i] = round2ticksize( Max( rangel[i], Max( tslarr[i - 1], H[i - 1] - per3 * ATR0[i] ) ) );
profitLTgt[i] = Max( profitLTgt[i], profitLTgt[i - 1] );
}
// Exit Sell
//Tgt reached
if( High[i] >= profitlTgt[i] AND bflag == 1 AND partialTgtTP > 0 AND partialTPDone == 0 )
{
SellPrice[i] = profitltgt[i] ;
selltype[i] = 1;
partialTPDone = 1;
Buy[i] = sigScaleOut;
longexit[i] = roundlotsizer( longbalance[i] * ( partialTgtTP ) );
longbalance[i] = longbalance[i] - longexit[i];
}
// stop hit
else
if( L[i] <= tslarr[i - 1] AND bflag == 1 )
{
SellPrice[i] = tslarr[i - 1] ;
selltype[i] = 2;
Sell[i] = 1;
bflag = 0;
profittgt[i] = Null;
tslarr[i] = Null;
longexit[i] = longbalance[i];
longbalance[i] = 0;
}
// market closed
else
if( TN[i] == Market_end_time AND bflag == 1 )
{
SellPrice = C[i];
selltype[i] = 1;
Sell[i] = 1;
bflag = 0;
profittgt[i] = Null;
tslarr[i] = Null;
longexit[i] = longbalance[i];
longbalance[i] = 0;
}
////code for short-cover
shortBalance[i] = shortBalance[i - 1];
//Short Entry
if( short0[i] AND sflag == 0 AND bflag ==0 AND TN[i] > Market_open_time[i] AND TN[i] < Market_end_time )
{
Short[i] = 1;
sflag = 1;
ShortPrice[i] = Min( rangel[i] - TickSize, Open[i] );
tslsarr[i] = round2ticksize( min( rangeh[i], H[i - 1] + ( per3 - 1 ) * ATR0[i] ) );
profitSTgt[i] = round2ticksize( ShortPrice[i] - ( tslsarr[i] - ShortPrice[i] ) * multirisk );
shortentry[i] = shortbalance[i] = shares ;
partialTPDone = 0;
}
// Trailing Stop
if( sflag )
{
if( IsNull( tslsarr[i - 1] ) )slval = 999999999;
else slval = tslsarr[i - 1];
tslsarr[i] = round2ticksize( Min( rangeh[i], Min( slval, L[i - 1] + per3 * ATR0[i] ) ) );
profitSTgt[i] = Max( profitSTgt[i - 1], profitSTgt[i] ) ;
}
//Tgt hit
if( L[i] <= ProfitStgt[i] AND sflag == 1 AND partialTgtTP > 0 AND partialTPDone == 0 )
{
CoverPrice[i] = ProfitStgt[i];
partialTPDone = 1;
shortexit[i] = RoundLotSizer( shortBalance[i] * ( partialTgtTP ) );
shortbalance[i] -= shortexit[i];
Short[i] = sigScaleOut;
}
// Stophit
else
if( ( H[i] >= tslsarr[i - 1] ) AND sflag == 1 )
{
CoverPrice[i] = tslsarr[i - 1];
Cover[i] = 1;
sflag = 0;
ProfitStgt[i] = Null;
tslsarr[i] = Null;
shortexit[i] = shortbalance[i];
shortBalance[i] = 0;
}
// Last bar
else
if( TN[i] == Market_end_time AND sflag == 1 )
{
CoverPrice[i] = C[i];
Cover[i] = 1;
sflag = 0;
ProfitStgt[i] = Null;
tslsarr[i] = Null;
shortexit[i] = shortbalance[i];
shortBalance[i] = 0;
}
}
// Sellprice assigned in the loop is not working. the bactester is using bar high for long trades sellprice if the following line is not added.
SellPrice = IIf( selltype == 1, Ref( profitltgt, -1 ), IIf( selltype == 2, Min( Ref( tslarr, -1 ), Open ), Close ) );
SetPositionSize( longentry + shortentry, spsShares );
SetPositionSize( longexit, spsShares * ( Buy == sigScaleOut ) );
SetPositionSize( shortexit, spsShares * ( Short == sigScaleOut ) );
//display
Plot( rangeh, "LongEntryAbove", colorGreen, styleLine );
Plot( rangel, "ShortEntryBelow", colorRed, styleLine );
Plot( C, "Price", colorDefault, styleCandle );
Plot( Ref(tslsarr,-1), "tslsarr", colorAqua, styleDots );
Plot( Ref(tslarr,-1), "tslarr", colorBlueGrey, styleDots );
Plot( profitltgt, "LongPft", colorGold, styleDots | styleNoRescale );
Plot( profitStgt, "ShortPft", colorGold, styleDots | styleNoRescale );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %.2f, Hi %.2f, Lo %.2f, Close %.2f (%.1f%%)\n {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
/* Plot Buy and Sell Signal Arrows */
PlotShapes( IIf( Buy == 1, shapeUpArrow, shapeNone ), colorGreen, 0, L, Offset = -40 );
PlotShapes( IIf( Cover, shapehollowUpArrow, shapeNone ), colorAqua, 0, L, Offset = -45 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, Offset = -45 );
PlotShapes( IIf( Short==1, shapehollowDownArrow, shapeNone ), colorRed, 0, H, Offset = -45 );
PlotShapes( IIf( Buy > 1, shapedownArrow, shapeNone ), colorAqua, 0, H, Offset = -40 );
PlotShapes( IIf( Short > 1, shapehollowUpArrow, shapeNone ), colorBlueGrey, 0, L, Offset = -40 );
Title = Title + StrFormat( "\nLong P: %.f, tSL: %.2f, Buyprice: %.2f, Longbalance: %.f,\n TslShort: %.2f, ShortPrice: %.2f , profitSTgt: %.2f, shortB: %.f", profitLtgt, tslarr, BuyPrice, LongBalance, tslsarr, ShortPrice, profitSTgt, shortbalance );