Good day all,
I'm having problem coding loop trailing stop to align with the ApplyStop. I tried searching the forum but did not find the answer I need, unless I don't know where to look.
The trailing stop varies based on the market condition. If the Index closes above its 10 period moving average, the trailing stop's exit percentage will be 20%, otherwise will be 10%. I have found code that replicates how the ApplyStop behaves. However, I wish to use loop instead for further charting purposes.
As shown below picture, I need the Red Line to be the same as the Orange Line.
Any pointers will be greater appreciated!
Thanks,
SetOption( "InitialEquity", 200000 );
SetOption( "CommissionMode", 2 );
SetOption( "CommissionAmount", 9.50 );
SetOption( "AllowSameBarExit", False );
SetOption( "AllowPositionShrinking", False );
SetOption( "MaxOpenPositions", 20 );
SetPositionSize( 10000, spsValue );
SetTradeDelays( 1, 1, 1, 1 );
//--------------------- Buy Signal --------------------- //
Buy = C > EMA(C,70);
//--------------------- Index Filter ---------------------//
SetForeign( "$XAO" );
Index_Up = C > MA( C, 10 );
RestorePriceArrays();
//--------------------- Sell Trailing Stop --------------------- //
Sell = 0;
Exit_Percent = IIf( Index_Up, 20, 10 );
ApplyStop( stopTypeTrailing, stopModePercent, Exit_Percent, ExitAtStop = 2 );
trailARRAY = Null;
OnLastTwoBarsOfInactiveSecurity = trailstop = 0;
StopLevel = Null;
/*
for( i = 1; i < BarCount; i++ )
{
if(Index_Up[i] == True)
{
StopLevel[i] = 1 - 20/100;
}
if(Index_Up[i] == False)
{
StopLevel[i] = 1 - 10/100;
}
if( trailstop == 0 AND Buy[ i ] )
{
trailstop = High[ i ] * StopLevel[i];
}
else Buy[ i ] = 0; // remove excess buy signals
if( trailstop > 0 AND Low[ i ] < trailstop )
{
Sell[ i ] = 1;
SellPrice[ i ] = trailstop;
trailstop = 0;
SellType[i] = 1;
}
if( trailstop > 0)
{
if(Index_Up[i] != Index_Up[i-1]){
trailstop = High[ i ] * StopLevel[i];
trailARRAY[ i ] = trailstop;
}else{
trailstop = Max( High[ i ] * StopLevel[i], trailstop);
trailARRAY[ i ] = trailstop;
}
}
}
*/
for( i = 1; i < BarCount; i++ )
{
if( Index_Up[ i ] == True )
{
StopLevel = 1 - 20/100;
}
else StopLevel =1 - 10/100;
if( trailstop == 0 AND Buy[ i ] )
{
trailstop = High[ i ] * StopLevel;
}
else Buy[ i ] = 0; // remove excess buy signals
if( trailstop > 0 AND Low[ i ] < trailstop OR OnLastTwoBarsOfInactiveSecurity [ i ] == 1)
{
Sell[ i ] = 1;
SellPrice[ i ] = trailstop;
trailstop = 0;
}
if( trailstop > 0 )
{
trailstop = Max( High[ i ] * StopLevel, trailstop );
trailARRAY[ i ] = trailstop;
}
}
//Plot Looping Trail
Plot( trailARRAY,"trailing stop level", colorRed );
//---------------------------------- Charting ------------------------------------- //
Plot( C, "Price", colorDefault, styleBar|styleThick|styleNoTitle );
PlotShapes( Buy*shapeUpTriangle, colorGreen, 0, Low, -20 );
PlotShapes( ( Sell > 0 ) * shapeDownTriangle, colorRed, 0, High, -40 );
RibbonColor = IIf( Index_Up, colorGreen, colorRed ); // If Index Up is TRUE the ribbon is GREEN, If Index Up is FALSE the ribbon is Red
Plot( 1, "", RibbonColor, styleArea | styleOwnScale | styleNoLabel, -0.0001, 190 );
//Plot ApplyStop Trail
Equity( 1, 0 );
InTrade = Flip( Buy, Sell );
SetOption("EveryBarNullCheck", True );
stopline = IIf( InTrade, HighestSince( Buy, High ) * ( 1 - 0.01 * Exit_Percent ), Null );
Plot( stopline, "trailing stop line", colorOrange );