# Help with system trading a pair

Hi all, I'm having trouble getting my "long/short pairs" trading system to work and am reaching out for some help. I've looked at it for some time now and wonder if I'm missing something fundamental. The code is attached, in a nutshell, it is meant to create and plot a MACD and signal for each of the two stocks, and then plot arrows for the trades. Sell/Cover trades are should be generated when a defined profit% is made.

It's still work in progress, but the main problem I've having is that I am getting trade arrows to plot for the stock, MA, but I can't get them to display for the second stock, V. My intended logic is for them to always occur together. I wonder whether there is something fundamentally wrong with my approach that I have not twigged!

``````SetBarsRequired(sbrAll,0);

//----------------------------------------------------------------------------------------------------

_SECTION_BEGIN("MACD for two stocks");

// Pairs-specific variables....
Stock1Ticker="MA";
Stock2Ticker="V";

Stock1C 		= Foreign(Stock1Ticker,"C",1);
Stock2C 		= Foreign(Stock2Ticker,"C",1);

BaselineBeginBar= -100;	// how far back to start the origin
BaselineMA_Period=  25; // ma period used for baseline

ma1				=	MA(Stock1C,BaselineMA_Period);
ma2				=	MA(Stock2C,BaselineMA_Period);
ma1Baseline		=	Ref(ma1,BaselineBeginBar);
ma2Baseline		=	Ref(ma2,BaselineBeginBar);
originFactor	=	Lastvalue(ma1Baseline/ma2Baseline);

r1 = Param( "Fast avg", 12, 2, 200, 1 );
r2 = Param( "Slow avg", 26, 2, 200, 1 );
r3 = Param( "Signal avg", 9, 2, 200, 1 );

S1_macdLine 	= EMA(Stock1C,r1)-EMA(Stock1C,r2);
S1_signalLine 	= EMA(S1_macdLine,r3);

S2_macdLine 	= EMA(Stock2C,r1)-EMA(Stock2C,r2);
S2_signalLine 	= EMA(S2_macdLine,r3);

// MACD plot - TOP
offset=2;
Plot( S2_ml = S2_macdline+offset, StrFormat(_SECTION_NAME()+"(%g,%g)", r1, r2),colorlightGrey,styleLine);
Plot( S2_sl = S2_SignalLine+offset, "Signal" + _PARAM_VALUES(),colorGrey50,styleLine);
//Plot( S2_sl = S2_SignalLine+offset, "Signal" + _PARAM_VALUES(), ParamColor("Signal color", colorBlueGrey ), ParamStyle("Signal style") );
//Plot( S2_ml-S2_sl+offset, "MACD Histogram", ParamColor("Histogram color", colorDefault ), styleNoTitle | ParamStyle("Histogram style", styleHistogram | styleNoLabel, maskHistogram ) );

// MACD plot - BOTTOM
Plot( S1_ml = S1_macdline, StrFormat(_SECTION_NAME()+"(%g,%g)", r1, r2),colorlightGrey,styleLine);
Plot( S1_sl = S1_SignalLine, "Signal" + _PARAM_VALUES(),colorGrey50,styleLine);
//Plot( S1_ml-S1_sl, "MACD Histogram", ParamColor("Histogram color", colorDefault ), styleNoTitle | ParamStyle("Histogram style", styleHistogram | styleNoLabel, maskHistogram ) );

_SECTION_END();

//----------------------------------------------------------------------------------------------------

StockATR		=	ATR(10);
Sell 			=	0;
Cover			=	0;
Short			=	0;
minProfit		=	0.01;

AND S1_MACDLine - S1_SignalLine >= minBuyGap 				// This stock's MACD line is above it's signal line
AND S2_SignalLine - S2_MACDLine >0)								// The other stock has its signal above the MACD line
OR
(Name()==Stock2Ticker
AND S2_MACDLine - S2_SignalLine >= minBuyGap 				// This stock's MACD line is above it's signal line
AND S1_SignalLine - S1_MACDLine >0);							// The other stock has its signal above the MACD line

ShortSetup	=	(Name()==Stock2Ticker
AND S1_MACDLine - S1_SignalLine >= minBuyGap 				// The other stock's MACD line is above it's signal line
AND S2_SignalLine - S2_MACDLine >0)								// This stock has its signal above the MACD line
OR
(Name()==Stock1Ticker
AND S2_MACDLine - S2_SignalLine >= minBuyGap 				// The other stock's MACD line is above it's signal line
AND S1_SignalLine - S1_MACDLine >0);						// This stock has its signal above the MACD line

// set open positions
for (j = 1; j < BarCount; j++)
{
{
}

{
PriceAtBuy[j]	=	Open[j+1]; // tomorrow's open
}
else
{
Short[j]	=	1;
PriceAtShort[j]=	Open[j+1]; // tomorrow's open
ShortPrice[j]=	Open[j+1];
}
else
if ((Name()==Stock1Ticker OR Name()==Stock2Ticker) AND (BarsInTrade[j-1]>0 AND (C[j] / PriceAtBuy[j]) -1 > minProfit))
{

Sell[j]		=	1;
SellPrice[j]=	O[j+1];

Cover[j]	=	1;
CoverPrice[j]=	O[j+1];

}
} // for loop

//Position sizing inputs
AccBalance = Param("Account Balance",25000,1,1000000,1);
PcntRisk = Param("Explorer: % Equity",13.5,1,100,0.01);
Shares = round((AccBalance*PcntRisk/100)/Ref(O,1));

_SECTION_END();

//----------------------------------------------------------------------------------------------------

PlotShapes(shapeDownArrow*Sell,colorRed);
PlotShapes(shapeHollowUpArrow*Cover,colorBrightGreen,0,Graph0,-20);
PlotShapes(shapeHollowDownArrow*Short,colorRed,0,Graph0,-20);

_SECTION_END();

//----------------------------------------------------------------------------------------------------

_SECTION_BEGIN("Interpretation Windows Output");

printf("\n\n"+"Current Stock: "+name()+"\n");
printf("S1 MACD Line: "+NumToStr(S1_MACDLine,1.2)+"\n");
printf("S1 Signal Line: "+NumToStr(S1_SignalLine,1.2)+"\n");
printf("S2 MACD Line: "+NumToStr(S2_MACDLine,1.2)+"\n");
printf("S2 Signal Line: "+NumToStr(S2_SignalLine,1.2)+"\n");