Stop loss for multiple positions

Hi,

I am trying to setup a simple Option strategy where 2 legs are sold at market open and closed at market close (After selecting right strikes / expiry etc). This is the part I have been able to achieve.

I also want to add a stop loss that is a combined stop loss for both the positions (trying to achieve this through a loop so that I can have other conditions later in the same loop).

For instance,
If Position A was bought for 100/- and Position B was bought for 100/- too and I decide a Stop loss to be kept at 50 total points, how will I be able to achieve it.
To make it clear, in the above case I want to exit if :

  1. A = 75 and B = 75 (Total loss = 50)
  2. A = 100 and B = 50 (Total loss = 50)
    and so on..

Here is what I have been able to code so far.

This code is inside a for loop.

if (starttime[i] AND position_taken >= 0 AND position_taken < 2 AND get_expiry == 1)
	{	
		if(Name() == CEstring) {
			SetPositionSize(20,spsShares);
			Short[i] = 1;
			CEshortprice = CEClosearr[i];
			position_taken = position_taken + 1;
			}
		
		if(Name() == PEstring) {
			SetPositionSize(20,spsShares);
			Short[i] = 1;
			PEshortprice = PEClosearr[i];
			position_taken = position_taken + 1;
			}
		Combinedshortprice = CEshortprice + PEshortprice;
		
	}
if (position_taken == 2 AND inbetweentime[i])
	{
		//Check combined total
		Currentcombined = CEClosearr[i] + PEClosearr[i];
		
		if ((Currentcombined - Combinedshortprice) > 50)
		{
			
			if(Name() == CEstring)
			{
				Cover[i] = 1;
				position_taken = position_taken - 1;
			}
			if(Name() == PEstring)
			{
				Cover[i] = 1;
				position_taken = position_taken - 1;
			}
		}
		
		
	}

The inbetween time variable is set as : inbetweentime = IIf((TimeNum()>091500) AND (TimeNum() < 151500) , 1, 0);

The arrays are set when the right strikes are identified :
CEClosearr = Foreign(CEstring,"Close");
PEClosearr = Foreign(PEstring,"Close");

Have tried to add as much info as possible, please let me know if any other information is required.

Thanks in advance.

Siddhant Kankani

I got this working, there was an error in the logic for one of the loops. Thanks.