hi, I agree with Helixtrader, it helps to visualize the trades.
Below another example. The system enter 300 shares and has 2 targets where it exits 100 shares at each target (if reached). If stopped out it sells the entire open position. It can also be backtested in a portfolio type system.
_SECTION_BEGIN( "Pivots" );
per = Param( "per", 20, 1, 150, 1 );
mult = Param( "mult", 2, 1, 4, 0.05 );
fact = Param( "Chart Time Frame Factor", 3, 1, 10, 1 );
_SECTION_END();
tfrm = Interval() * fact;
TimeFrameSet( tfrm );
sup = C - mult * ATR( per );
res = C + mult * ATR( per );
trailARRAY = Null;
trailstop = 0;
for( i = 1; i < BarCount; i++ )
{
if( C[ i ] > trailstop AND C[ i - 1 ] > trailstop )
trailstop = Max( trailstop, sup[ i ] );
else
if( C[ i ] < trailstop AND C[ i - 1 ] < trailstop )
trailstop = Min( trailstop, res[ i ] );
else
trailstop = IIf( C[ i ] > trailstop, sup[ i ], res[ i ] );
trailARRAY[ i ] = trailstop;
}
ts = IIf( trailArray > C, trailArray, Null );
tl = IIf( trailArray < C, trailArray, Null );
TimeFrameRestore();
ts = TimeFrameExpand( ts, tfrm, expandLast );
tl = TimeFrameExpand( tl, tfrm, expandLast );
Buy = IsEmpty( ts ) AND !isempty( Ref( ts, -1 ) ) AND !IsEmpty( tl );
Sell = 0;
FirstProfitTarget = Null;
flag1 = 0;
SecondProfitTarget = Null;
flag2 = 0;
slip = 0.00;
for( i = 0; i < BarCount; i++ )
{
if( Buy[ i ] )
{
BuyPrice[i] = C[i];
FirstProfitTarget[i] = BuyPrice[i] + ( BuyPrice[i] - tl[i] );
SecondProfitTarget[i] = BuyPrice[i] + ( BuyPrice[i] - tl[i] ) * 2;
for( j = i + 1; j < BarCount; j++ )
{
if( flag1 == 0 ) FirstProfitTarget[j] = FirstProfitTarget[i];
if( flag2 == 0 AND flag1 == 1 ) SecondProfitTarget[j] = SecondProfitTarget[i];
if( High[ j ] >= FirstProfitTarget[j] AND !IsEmpty( tl[j] ) AND flag1 == 0 )
{
Buy[ j ] = sigScaleOut;
BuyPrice[j] = Max( O[j], FirstProfitTarget[j] );
flag1 = 1;
}
if( High[ j ] >= SecondProfitTarget[j] AND !IsEmpty( tl[j] ) AND flag2 == 0 )
{
Buy[ j ] = sigScaleOut;
BuyPrice[j] = Max( O[j], SecondProfitTarget[j] );
flag2 = 1;
}
if( IsEmpty( tl[j] ) AND !IsEmpty( ts[j] ) )
{
Sell[j] = 1;
SellPrice[ j ] = C[j] - slip;
i = j;
flag1 = 0;
flag2 = 0;
break;
}
if( j == BarCount - 1 )
{
i = BarCount;
flag1 = 0;
flag2 = 0;
break;
}
}
}
}
SetOption( "InitialEquity", 200000 );
SetOption( "CommissionMode", 3 );
SetOption( "CommissionAmount", 0.006 );
SetOption( "MarginRequirement", 100 );
SetTradeDelays( 0, 0, 0, 0 );
Qty = 20;
SetOption( "maxopenpositions", Qty );
SetChartBkColor( colorBlack );
SetChartOptions( 0, chartShowDates );
GraphXSpace = 10;
SetBarFillColor( IIf( C > O, ColorRGB( 0, 75, 0 ), IIf( C <= O, ColorRGB( 75, 0, 0 ), colorLightGrey ) ) );
Plot( C, "", IIf( C > O, ColorRGB( 0, 255, 0 ), IIf( C <= O, ColorRGB( 255, 0, 0 ), colorLightGrey ) ), 64, Null, Null, 0, 0, 1 );
Plot( ts, "", colorRed, styleLine, Null, Null, 0, 0, 1 );
Plot( tl, "", colorBlue, styleLine, Null, Null, 0, 0, 1 );
SetPositionSize( 300, spsShares );
SetPositionSize( 100, spsShares * ( Buy == sigScaleOut ) );
PlotShapes( IIf( Buy == 1, shapeUpArrow, shapeNone ), colorDarkGreen, 0, L, -15 );
PlotShapes( IIf( Buy == 1, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
//PlotShapes( IIf( Buy == sigscalein, shapeUpArrow, shapeNone ), colorlightblue, 0, L, -15 );
//PlotShapes( IIf( Buy == sigscalein, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Buy == sigscaleout, shapeDownArrow, shapeNone ), colorOrange, 0, H, -15 );
PlotShapes( IIf( Buy == sigscaleout, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, -15 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );
Plot( FirstProfitTarget, "", colorYellow, styleDashed, Null, Null, 0, 0, 1 );
Plot( SecondProfitTarget, "", colorLightBlue, styleDashed, Null, Null, 0, 0, 1 );
Title = Name() +
" | " + Now( 2 ) +
" | " + "Timeframe Factor: " + EncodeColor( ColorRGB( 147, 197, 114 ) ) + " " + fact + " " + EncodeColor( colorWhite ) + " times the Base Time Frame";