How to sell on condition OR after n days

It’s really unclear to me how to refer to different points in time in a single Seetu script. I want to add a simple max hold duration as a sell trigger (in this case 14 days). Everything else in this script is working, but positions are often being held beyond 14 days:

// Just starting with the example Rsi script:
Rsi_Value = CalcRSI();  
BuyDate = Day();
SellDate = DateTimeAdd(BuyDate, 14, inDaily);

// Buy if RSI_Value crosses above 30
Buy = Cross(Rsi_Value, 30); 

// Sell if RSI_Value falls under 70 ***or in 14 days***:
Sell = Cross(70, Rsi_Value) OR Day() >= SellDate;

// ... some out-of-scope logic here, then... //
C2TradingSignal(sigBTO, priceType = priceLimit, stopLoss = BuyPrice * 0.9, profitTarget = BuyPrice * 1.1); 
// ...but lawdy, some of my positions are still being held a looonnnnng time!

I’ll bet this is because Day() returns the same value both times it’s called, so Day() >= SellDate is never true. However, it’s not obvious at all to me how to evaluate how long the position has been held when determining whether to sell.

Any ideas?
Thanks!

Hello,

use BarsSince.

Here is an example. (I am using MSFT and different Buy/Sell logic to get more signals.)

BuyLogic =  Cross(CalcMACD(), CalcMACDSignal());
SellLogic = Cross(CalcMACDSignal(), CalcMACD());

Buy = BuyLogic;

Bought_5_bars_ago = BarsSince( BuyLogic ) == 5;

Sell = SellLogic or Bought_5_bars_ago;
    
// Visualise in Exploration 
Filter = Buy or Sell;
AddColumn(Buy, "Buy",1.0);
AddColumn(SellLogic, "SellLogic",1.0);
AddColumn(Bought_5_bars_ago, "Bought_5_bars_ago",1.0);
AddColumn(Sell, "Sell",1.0);

It gives me the following result.
In the first case, SELL was generated by Bought_5_bars_ago logic,
in the second case, SELL was generated by SellLogic. (See dates too.)

obrazek

 

BTW: the Day() function returns a day of the month (1-31). So it is not usable in your case.

Hope it helps.

Thank you, that did the trick!