Help with Coding XIV / VXX Strategy

Love Amibroker and slowly learning how to program (very little experience with this). I am currently working on some code to buy either XIV or VXX. Here are the rules I want to backtest:

Buy XIV if $VXV >= $VIX
Buy VXX if $VXV < $VIX

Right now I am able to make it work so I can buy and sell XIV, but I am trying to get the second rule to work so that I sell XIV and buy VXX when the signal says to, and visa versa as per the rules. I am running this with a watchlist set up with just XIV - I can add one with XIV and VXX as well of course.

Here is the code I have started with - are you able to help me with how to make this work for the rule as stated above?:


//Buy XIV if $VXV >= $VIX
//Buy VXX if $VXV < $VIX

#include_once "Formulas\Norgate Data\Norgate Data Functions.afl"

// Portfolio Settings
SetOption( "InitialEquity", 75000 ); /* starting capital */
SetOption( "CommissionMode", 3 ); /* set commissions AND costs as $ per share */
SetOption( "CommissionAmount", 0.005 ); /* commissions AND cost */
SetOption("MaxOpenPositions", 1);
SetPositionSize(100, spsPercentOfEquity);
SetTradeDelays(1,1,1,1);

BuyPrice = Open;
SellPrice = Open;
 
VXV = Foreign( "$VXV", "C" );
VIX = Foreign( "$VIX", "C" );
XIV = Foreign( "XIV", "C" );
VXX = Foreign( "VXX", "C" );

BuyXIV = VXV >= VIX;
SellXIV = VXV < VIX;
//BuyVXX = VXV < VIX;
//SellVXX = VXV > VIX;

Buy = BuyXIV;
Sell = SellXIV;

To have different buy/sell rules for different symbols, you can add IF statements like this:

if (Name() == "XIV")
{
    Buy = ...
    Sell = ....
}
else if (Name() == "VXX")
{
    Buy = ...
    Sell = ....
}

2 Likes

Thanks for the help - that did the trick.

Hello Jeremy,
i am a newbie to amibroker too - may i please request you paste the new code that worked?
would apprecaite it and it will help me understand more and better.

thanks
NR