ScaleOut 3 times if target1 .. target3 hits and exit once target4 hits

Hi Friends,

I want to scaleout position 3 times when target1, target2, target3 hits and finally if target4 hits want to exit from position.

I'm just testing for one ticker but getting error, details logs shows wrong position/ insufficient balance.
I'm also interested to know how the trade entry will look like one trade for all four partial trades and what would be entry and exit price etc.

Please help me

Thanks

// code for auto backtesting. 
inTrade=False; 
stopLossLine=0; 
SignalType=0;
TradeSignal=0;

exit =0;


idr		=abs(SLLineF-EntryLine); 
target1 =0; 
target2	=0; 
target3	=0; 
target4	=0; 
bp=0;
sp=0;

Buy=Null; 
Sell=Null; 
Short=Null; 
Cover=Null; 

BuyPrice=0; 
SellPrice=0; 
ShortPrice=0; 
CoverPrice=0; 

for( i = 0; i < BarCount; i++ )
{
	if(newDay[i]==True)
	{
		inTrade=False;
		exit	=0; 
		target1 =0; 
		target2	=0; 
		target3	=0; 
		target4	=0; 
		stopLossLine=0;
		bp=0;
		sp=0;
	}
	
	if(Buy_cond_F[i]==True AND inTrade ==False AND TradeSignal==0)
	{
		SignalType=9;
	}
	else if(Short_cond_F[i]==True AND inTrade ==False AND TradeSignal==0)
	{
		SignalType=19; 
	}
	// buy code here 
	if(SignalType==9 AND High[i] >EntryLine[i] AND TradeSignal==0 AND exit==0 AND inTrade==False)
	{
		Buy[i]=True; 
		bp=BuyPrice[i]=EntryLine[i];
		
		inTrade=True; 
		TradeSignal=10; 
		
		idr_temp=idr[i]; 
		target1 =bp+idr_temp; 
		target2	=bp+(idr_temp*2); 
		target3	=bp+(idr_temp*3); 
		target4	=bp+(idr_temp*4); 
		stopLossLine =SLLineF[i]; 
	}
	else if (exit==0 AND TradeSignal==10 AND High[i] >= target1)
	{
		exit = 1;
        Buy[ i ] = sigScaleOut;
        BuyPrice[i]=target1;
	}
	else if (exit==1 AND TradeSignal==10 AND High[i] >= target2)
	{
		exit = 2;
        Buy[ i ] = sigScaleOut;
        BuyPrice[i]=target2;
	}
	else if (exit==2 AND TradeSignal==10 AND High[i] >= target3)
	{
		exit = 3;
        Buy[ i ] = sigScaleOut;
        BuyPrice[i]=target3;
	}
	else if (exit==3 AND TradeSignal==10 AND High[i] >= target4)
	{
		exit = 4;
        Buy[ i ] = 0;
        Sell[i]=exit+1;
        SellPrice[i]=target4; 
	}
	else if (TradeSignal==10)
	{
		if (Low[i] < stopLossLine AND exit==0 AND TradeSignal==10 )
		{
			Sell[i] =exit+1; 
			SellPrice[i]=stopLossLine;
			TradeSignal=0;
		}
		else if (Low [i] <bp AND exit >=1 AND TradeSignal ==10)
		{
			Sell[i] =exit+1; 
			SellPrice[i]=bp;
			TradeSignal=0;
		}
	}
	//buy code end here 
	//sell code
	if(SignalType==19 AND Low[i] <EntryLine[i] AND TradeSignal==0 AND exit==0 AND inTrade==False)
	{
		Short[i]=True; 
		sp=ShortPrice[i]=EntryLine[i];
		
		inTrade=True; 
		TradeSignal=20; 
		
		idr_temp=idr[i]; 
		target1 =sp-idr_temp; 
		target2	=sp-(idr_temp*2); 
		target3	=sp-(idr_temp*3); 
		target4	=sp-(idr_temp*4); 
		stopLossLine =SLLineF[i]; 
	}
	else if (exit==0 AND TradeSignal==20 AND Low[i] <= target1)
	{
		exit = 1;
        Short[ i ] = sigScaleOut;
        ShortPrice[i]= target1; 
	}
	else if (exit==1 AND TradeSignal==20 AND Low[i] <= target2)
	{
		exit = 2;
        Short[ i ] = sigScaleOut;
        ShortPrice[i]= target2; 
	}
	else if (exit==2 AND TradeSignal==20 AND Low[i] <= target3)
	{
		exit = 3;
        Short[ i ] = sigScaleOut;
        ShortPrice[i]= target3; 
	}
	else if (exit==3 AND TradeSignal==20 AND Low[i] <= target4)
	{
		exit = 4;
        Short[ i ] = 0;
        Cover[i]=exit+1;
        CoverPrice[i]=target4; 
	}
	else if (TradeSignal==20)
	{
		if (High[i] > stopLossLine AND exit==0 AND TradeSignal==20 )
		{
			Cover[i] =exit+1; 
			CoverPrice[i]=stopLossLine;
			TradeSignal=0;
		}
		else if (High [i] >sp AND exit >=1 AND TradeSignal ==20)
		{
			Cover[i] =exit+1; 
			CoverPrice[i]=sp;
			TradeSignal=0;
		}
	}
}

sl_amount =1000; 

position_size = IIf(Buy==1 OR Short==1 , floor(sl_amount/idr), Null) ; 
SetPositionSize( position_size, spsShares );

SetPositionSize( 25, spsPercentOfPosition * ( Buy == sigScaleOut OR Short ==sigScaleOut) ); // scale out 25% of position

Error msg.
Capture

Debug logs

Capture2

Hi @Tomasz ,

I have observed , the reqEntryPosSize=-975 for all possible trades.

what's wrong with my code.

sl_amount =1000; 

position_size = IIf(Buy==1 OR Short==1 , floor(sl_amount/idr), Null) ; 
//SetPositionSize( position_size, spsShares );
SetPositionSize( position_size*BuyPrice, spsValue );
SetPositionSize( 25, spsPercentOfPosition * ( Buy == sigScaleOut OR Short ==sigScaleOut) ); // scale out 25% of position

Capture3

Array value
Capture4

Please help me here .
Thanks

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.