Vix Timing Strategy

cole10925

New member
I am currently working on a variation of the vix timing strategy in TOS. I want to add trailing stops to the code but when i do, a new order is added after the trailing stop closes the position due to the condition still being met. Does anyone have any idea how I could rework this? It is driving me crazy that TD does not have a dedicated support team.

I attached my code below.

Thanks,

Code:
input PriceType = FundamentalType.LOW;
input EmaLength = 10;
input TrendLength = 11;
input Period1 = AggregationPeriod.FIFTEEN_MIN;

def priceVix = Fundamental(PriceType, "VIX", Period1);
def emaVix = ExpAverage(priceVix, EmaLength);

AddOrder(OrderType.BUY_AUTO, Sum(priceVix < emaVix, TrendLength)[1] == TrendLength, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "Long");

AddOrder(OrderType.SELL_AUTO, Sum(priceVix > emaVix, TrendLength)[1] == TrendLength, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "Short");
 

rad14733

Well-known member
VIP
Are you working on a backtesting strategy...??? Remember, TOS doesn't do automated trading... You can, however, create Conditional Orders just as long as your logic isn't complicated because Conditional Orders don't do aggregation...
 

cole10925

New member
Are you working on a backtesting strategy...??? Remember, TOS doesn't do automated trading... You can, however, create Conditional Orders just as long as your logic isn't complicated because Conditional Orders don't do aggregation...
Yes, I'm backtesting a strategy but it still can be used as a buy or sell signal for manual trading
 

cole10925

New member
Have you checked the ATRTrailingStop study that comes with TOS...??? You can probably scavenge what you need for that Study...
yes i did, the problem is that when the trailing stop triggers to close the order, my code immediately opens the same position back up because my condition is still met. It does not wait for the next buy/sell signal.
 

rad14733

Well-known member
VIP
yes i did, the problem is that when the trailing stop triggers to close the order, my code immediately opens the same position back up because my condition is still met. It does not wait for the next buy/sell signal.
You need to add another conditions so that doesn't happen... I usually define a variable with my Buy and Sell criteria and then when doing a Sell Addorder() I add "and !buy" or whatever variable you define as your buy signal... You do that for both the Buy and Sell to eliminate the issue you described... Something like "sell and !buy" and "buy and !sell"... Something to that effect... Adjust your logic to where you can't have a Buy and Sell on the same bar...
 

cole10925

New member
You need to add another conditions so that doesn't happen... I usually define a variable with my Buy and Sell criteria and then when doing a Sell Addorder() I add "and !buy" or whatever variable you define as your buy signal... You do that for both the Buy and Sell to eliminate the issue you described... Something like "sell and !buy" and "buy and !sell"... Something to that effect... Adjust your logic to where you can't have a Buy and Sell on the same bar...
I have tried that but am still having trouble with the short side trailing stop. I will attach the code below, I have been struggling with this for a while.

Code:
input PriceType = FundamentalType.LOW;
input EmaLength = 10;
input TrendLength = 11;
input Period1 = AggregationPeriod.FIFTEEN_MIN;

def priceVix = Fundamental(PriceType, "VIX", Period1);
def emaVix = ExpAverage(priceVix, EmaLength);

def buy = Sum(priceVix < emaVix, TrendLength)[1] == TrendLength;

def buyconditional = Sum(priceVix < emaVix, TrendLength+1)[1] == TrendLength+1;

def sell = Sum(priceVix > emaVix, TrendLength)[1] == TrendLength;

def sellconditional = Sum(priceVix > emaVix, TrendLength+1)[1] == TrendLength+1;


AddOrder(OrderType.BUY_AUTO, buy and !buyconditional , tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "Long");



AddOrder(OrderType.SELL_AUTO, sell and !sellconditional , tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "Short");

#Trailing Stop Code

input trailStop = 10.0;
input offsetType = {default percent, value, tick};

def entryPrice = entryPrice();
def price = if IsNaN(entryPrice[1]) then entryPrice else if !IsNaN(entryPrice) then Max(close, price[1]) else entryPrice;

def price2 = if IsNaN(entryPrice[1]) then entryPrice else if !IsNaN(entryPrice) then Min(close, price2[1]) else entryPrice;

def mult;
switch (offsetType) {
case percent:
    mult = price / 100;
case value:
    mult = 1;
case tick:
    mult = tickSize();
}

def trailStopPrice = price - trailStop * mult;
def TSS = close < trailStopPrice;

def trailStopPrice2 = price2 + trailStop * mult;
def TSB = close >= trailStopPrice2;

addOrder(OrderType.SELL_TO_CLOSE, TSS and !TSB , tickColor = GetColor(0), arrowColor = GetColor(0), name = "TrailStop");

addOrder(OrderType.BUY_TO_CLOSE, TSB and !TSS, tickColor = GetColor(9), arrowColor = GetColor(9), name = "TrailStop");
 

rad14733

Well-known member
VIP
Without knowing exactly what you are trying to do, from my initial perusal of your code, the buy and sell logic is flawed... For example, the buy code just doesn't appear to semantically accurate...

Ruby:
Sum(priceVix < emaVix, TrendLength)[1]

Explain what your interpretation of that code is... Are you trying to assess the previous number of true values...??? That's how I read it and I don't think that is your intent... If that is the case then all of your conditionals are incorrect...
 

cole10925

New member
Without knowing exactly what you are trying to do, from my initial perusal of your code, the buy and sell logic is flawed... For example, the buy code just doesn't appear to semantically accurate...

Ruby:
Sum(priceVix < emaVix, TrendLength)[1]

Explain what your interpretation of that code is... Are you trying to assess the previous number of true values...??? That's how I read it and I don't think that is your intent... If that is the case then all of your conditionals are incorrect...
i pulled that from an existing strategy on TOS called Vix Timing. Basically I am trying to get a signal every time the vix is above its ema for a certain number of bars.
 

Similar threads

Top