Australian (ASX) Stock Market Forum

AmiBroker - XIV Code

Joined
9 September 2015
Posts
2
Reactions
0
Hi there,

I am trying to set up some code to trade XIV, which is the VelocitySHares Daily Inverse VIX Short. In real basic terms, here is what I am trying to achieve:

1. Buy XIV tomorrow at tomorrow's Close, when today's Low is > yesterday's Low
2. Sell XIV tomorrow at tomorrow's Close, when today's Low is < yesterday's Low

Basically, if you look at a chart, I want to buy the day after XIV has made a higher low. Inversely, I want to get out of that position the day after the XIV made a lower low than the day before.

Here is what I have coded so far but can't make it work. Seems simple but for the life of me I can't get it to act correctly. Anyone on this board able to help - is it a simple thing I missed or am I approaching it all wrong?

Code:
/////* Strategy : basic higher lows
/////* 1.Buy tomorrow at tomorrow's Close, when today's Low is > yesterday's Low
/////* 2.Sell tomorrow at tomorrow's Close, when today's Low is < yesterday's Low

SetFormulaName("HigherLowsStrategy"); /*name it for backtest report identification*/
SetOption( "initialequity", 5000 ); /* starting capital */
SetOption( "MaxOpenPositions", 1 );
SetOption( "CommissionMode", 3 ); /* set commissions AND costs as $ per trade */
SetOption( "CommissionAmount", 0.005 ); /* commissions AND cost */
//SetTradeDelays(1,1,1,1); /* Buy and sell on next bar - will be close due to BuyPrice=SellPrice = Close */

YesterdaysLow = Ref( L, -1 );
TodaysLow = L;

Buy = Ref(ROC(L, 1 ) > 0, -1 );
Sell = TodaysLow < YesterdaysLow;

BuyPrice = SellPrice = Close;

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
 
Hi there,

I am trying to set up some code to trade XIV, which is the VelocitySHares Daily Inverse VIX Short. In real basic terms, here is what I am trying to achieve:

1. Buy XIV tomorrow at tomorrow's Close, when today's Low is > yesterday's Low
2. Sell XIV tomorrow at tomorrow's Close, when today's Low is < yesterday's Low

Basically, if you look at a chart, I want to buy the day after XIV has made a higher low. Inversely, I want to get out of that position the day after the XIV made a lower low than the day before.

Here is what I have coded so far but can't make it work. Seems simple but for the life of me I can't get it to act correctly. Anyone on this board able to help - is it a simple thing I missed or am I approaching it all wrong?

Code:
/////* Strategy : basic higher lows
/////* 1.Buy tomorrow at tomorrow's Close, when today's Low is > yesterday's Low
/////* 2.Sell tomorrow at tomorrow's Close, when today's Low is < yesterday's Low

SetFormulaName("HigherLowsStrategy"); /*name it for backtest report identification*/
SetOption( "initialequity", 5000 ); /* starting capital */
SetOption( "MaxOpenPositions", 1 );
SetOption( "CommissionMode", 3 ); /* set commissions AND costs as $ per trade */
SetOption( "CommissionAmount", 0.005 ); /* commissions AND cost */
//SetTradeDelays(1,1,1,1); /* Buy and sell on next bar - will be close due to BuyPrice=SellPrice = Close */

YesterdaysLow = Ref( L, -1 );
TodaysLow = L;

Buy = Ref(ROC(L, 1 ) > 0, -1 );
Sell = TodaysLow < YesterdaysLow;

BuyPrice = SellPrice = Close;

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);


Your sell rule is not correct

Either you do

Code:
...
....

YesterdaysLow = Ref( L, -1 );
TodaysLow = L;

Buy = Ref(ROC(L, 1 ) > 0, -1 );
Sell = Ref( TodaysLow < YesterdaysLow, -1 );

BuyPrice = SellPrice = Close;

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);


or

Code:
....
....

Buy = Ref( ROC(L, 1 ) > 0, -1 );
Sell = Ref( ROC(L, 1 ) < 0, -1 );

BuyPrice = SellPrice = Close;

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);


or


Code:
...
....

SetTradeDelays(1,1,1,1); /* Buy and sell on next bar - will be close due to BuyPrice=SellPrice = Close */

Buy = ROC(L, 1 ) > 0;
Sell = ROC(L, 1 ) < 0;

BuyPrice = SellPrice = Close;

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

or

....
 
As aside it is better practice to call a function just one time.

How to do that? We store it to a variable.
Why are we doing that? We do it in order to keep our code operating as fast as possible and for better maintenance.

Code:
SetTradeDelays(1,1,1,1); /* Buy and sell on next bar - will be close due to BuyPrice=SellPrice = Close */

myRoc = ROC( L, 1 );

Buy = myROC > 0;
Sell = myROC < 0;

BuyPrice = SellPrice = Close;

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

Now in above one it is just called two times but you will see big differences once when your codes will become hundreds/thousands/... of lines long. So better get into the habit of doing things the "pro way" as early as possible because in the end it is YOU who will benefit from it.
 
Thank you so much for the help - and going the extra mile on coding it more efficiently. I don't have much coding in my background, so this is very helpful. Was able to run this in AB - ok return but the drawdown is high. Will see if I can work on that to improve it.

Thanks again.
 
Top