Systems merger - Use SHORT from one symbol signal to go LONG in another symbol


I like to trade leveraged ETF pairs. For example, UWT and DWT. They are inversely related, so when one goes up, the other goes down and vice versa.

I have separate systems for both of those ETF. Each system generates both LONG and SHORT signals. But I don’t like to sell short anything :blush:

When DWT generates a SHORT signal, I would actually like to go LONG UWT. When UWT generates a SHORT signal, I would like to actually go LONG DWT. I would also take all LONG signals generated by each system.

My question is, how would I code something that merges the 2 systems, takes all LONG signals generated by each system, and also uses each system’s SHORT signal to take a LONG position in the opposite ETF?

Thank you in advance for any guidance that you may be able to provide me.



Hi Randy,

A first step would probably be for you to read up on the “Foreign Functions” in the User’s Guide.

I would suggest that Next you make sure you are comfortable with the boolean logic for how to join two (or more) conditions.

condition1 = RSI(14) > 70;
condition2 = V>100000;
condition3 = C>5.25;
S1Buy = condition1 AND condition2 AND condition3;

I hope that this is the Guidance that you need.
Also, Use the Force… um… I mean Use the Search function on this site, and in the User’s Guide.

WHEN you make some progress, and hit your next stumbling block, Show us your code and where you are having difficulty. Then lots of other experienced AFLers would be much more willing to help Guide you to your solution. And you will learn LOTS on the way.



Hi Randy

You’ll first need to define the long and short entry/exit criteria for both symbols using the price array output from the Foreign function.

Once you have that you can use a discriminator in the Buy statement to check which symbol is being processed, and then apply the criteria for that symbol. And repeat for Sell.

Something like this should get you started.

UWTClose = Foreign("UWT", "C");
DWTClose = Foreign("DWT", "C");

UWTLong = Cross(UWTClose, MA(UWTClose, 20));
UWTShort = // Add Short Criteria

DWTLong = Cross(DWTClose, MA(DWTClose, 15));
DWTShort = // Add Short Criteria

Symbol = Name();
Buy = 	(Symbol == "UWT" AND (UWTLong OR DWTShort)) OR
		(Symbol == "DWT" AND (DWTLong OR UWTShort));


IMHO, not an optimal code.


  1. Multiple foreign calls for each applied symbol.
  2. Symbol == “…” is not type array and as such does not require being part of Buy.
  3. Besides other things this leads to multiple rules in one Buy call applied on each symbol with multiple AND/OR operators.
  4. Not efficient and less readability.

Better option(s):

nm = Name();

switch(nm) {
	case "DWT": foreignsym = "UWT"; break;
	case "UWT": foreignsym = "DWT"; break;
	default: foreignsym = ""; break;

if( foreignsym == "" ) {
	ShortSignal = False;
} else {
		ShortSignal = Cross(MA(C, 20), Close );

LongSignal = Cross(C, MA(C, 20));

Buy = Longsignal OR ShortSignal;


nm = Name();

switch(nm) {
	case "DWT": 
			ShortSignal = Cross(MA(C, 20), Close );
	case "UWT": 
			ShortSignal = Cross(MA(C, 20), Close );
	default: ShortSignal = False; break;

LongSignal = Cross(C, MA(C, 20));

Buy = Longsignal OR ShortSignal;


  1. switch statement to add additional options per symbol
  2. one call of foreign per symbol
  3. only one logical operator within Buy variable
  4. better readability
  5. more efficient


Answered already here: