Hello, purchased AB a couple months ago and learning to code. I am new to coding so I am still trying to figure out a lot of the terms. I have been able to code a few simple strategies but having a real hard time with this one.
The code should always check if the price of SPY is above the 20MA first. if this is true than buy SPY and exit when the price of SPY is below the 20MA. If the price of SPY is not above the 20MA, Then TLT should be checked. If the price of TLT is above its 14MA then buy TLT and exit when the price of TLT is below 14MA.. I tried to use SetForeign, but I don't think that's the right command to select the symbol to trade. Here is the code I came up with and I know it's very wrong due to my results. I would appreciate any help, thank you for your time.
SetOption("AllowSameBarExit",False);
SetOption( "MaxOpenPositions", 1 );
SetTradeDelays(1,1,0,0);
spyClose = Foreign( "SPY", "C" );
tltClose = Foreign( "TLT", "C" );
spyAboveMA = spyClose > MA( spyClose, 20 );
if( spyAboveMA = True )
{
SetForeign ("SPY");
movingAv20 = MA(C, 20);
movingAvAbove20 = C > movingAv20 ;
movingAvBelow20 = C < movingAv20 ;
BuyLevel = llV(movingAvAbove20,1);
SellLevel = llV(movingAvBelow20,1);
BuySignal = BuyLevel;
Buy = BuySignal;
BuyPrice = Close;
Sell = SellLevel;
SellPrice = Close;
}
else
if( spyAboveMA = False )
{
SetForeign ("TLT");
movingAv14 = MA(C,14);
movingAvAbove14 = C > movingAv14 ;
movingAvBelow14 = C < movingAv14 ;
BuyLevel = llV(movingAvAbove14,1);
SellLevel = llV(movingAvBelow14,1);
BuySignal = BuyLevel ;
Buy = BuySignal;
BuyPrice = Close;
Sell = SellLevel;
SellPrice = Close;
}