- ago
I was asked on Discord a question about the new WL8 sample Strategy, and I wanted to post the response here so it's recorded in the forum.

>>What is the general buying and selling theories.<<
It's trying to squeeze one percent during the week out of the triple leveraged TQQQ, taking advantage of the volatility of that ETF.

>>Also since it is run on a daily basis but not intraday, Do the signals buy and sell come during the day on a streaming data or is it a signal for the following day ?<<
Because of the way it's coded, it doesn't generate signals like most Strategies, so for now it's a manual effort to trade, but the rules are simple:

- On the first trading day of the week, observe the open price of TQQQ right at the open and enter a limit order to buy 1% below that price.
- After the close, enter a limit order to sell at 1% above the position entry price.
- At any point during the week, after the market closes, If the open position is sitting at a loss of -0.5% or more, make the limit exit price equal to the entry price (for an attempted breakeven result.)
- At the end of the week, if the position is still open, sell it at market close, so it doesn't hold over the weekend.

1
503
37 Answers

Reply

Bookmark

Sort
- ago
#1
I don't see that day strategy in the Sample folder. Is it supposed to be there?
0
- ago
#2
Yes, are you running the latest WL8 build?

0
- ago
#3
Hi Glitch. So since it doesn't produce signals its algorithm is intended for backtesting and by now it should be trade entirely manually. Wright?
0
- ago
#4
Yes that’s the idea. But I think we can, in a future release, add a new override that can respond to market open and allow a strategy like this to be automated.
1
- ago
#5
I am trying to achieve a similar strategy based on blocks but I think I am missing something, probably the SL placement, I don't know why.
0
- ago
#6
Maybe you can help me to identify what is missing, why I can not get good results as with the original back tested One Percent C-coded strategy, even though it's intended to follow the idea.


0
- ago
#7
I've tried also replacing the SL for a Profit Taking of 0%
0
- ago
#8
The coded strategy places the order one percent below the open price on Monday, the execution day. This isn’t possible using blocks.

The block strategy places a limit order one percent below the open price of Friday, to be executed on Monday.
1
- ago
#9
Thank you @Glitch for this sample code. It is educational about WL8 functions and thought-provoking.

I believe you may have a typo in your code based on your narrative description of the strategy.

Shouldn't
CODE:
            double target = LastOpenPosition.EntryPrice;             if (LastOpenPosition.ProfitAsOf(idx) > -0.5)


Be
CODE:
            double target = LastOpenPosition.EntryPrice;             if (LastOpenPosition.ProfitPctAsOf(idx) > -0.5)


?
1
- ago
#10
Yes, good catch, and changing it to what I intended actually bumped the APR up a couple of percent!
0
- ago
#11
I do have a few questions. On day 2 (Tuesday) it places a limit trade either at a profit target or breakeven, depending on profit at that bar. Will it sell on that bar if either limit is reached, or only if the limit is reached on the next bar, Wednesday? A related question is if a Sell.MarketClose order was also added if the Profit pct< 0.5 would it execute on Tuesday or Wednesday?

I suspect this is a basic question and I know the rule is orders are processed on the next bar. I think the answer is Wednesday for both. I'm getting a little confused because I have been looking into the method AssignAutoStopTargetPrices, which it appears you use in the sample code only to set a flag. My understanding is that limits or stops placed inside this method *will* execute on the same bar, both by broker and backtest.

Any thoughts of adding Sell.CloseAtMarket as a method in AssignAutoStopTargetPrices to allow closing position on same bar?

Also, is ProfitPctAsOf calculated at the close or open of the current bar? If the answer is close, it would be too late to place a Close-at-Market trade for the current day.
0
- ago
#12
I do have a few questions. On day 2 (Tuesday) it places a limit trade either at a profit target or breakeven, depending on profit at that bar. Will it sell on that bar if either limit is reached, or only if the limit is reached on the next bar, Wednesday?<<

Besides the special logic of "peeking ahead" at the open price of Monday, it's a normal strategy. It looks at the profit percent at the end of the day, and places the appropriate order for the following day.

>>A related question is if a Sell.MarketClose order was also added if the Profit pct< 0.5 would it execute on Tuesday or Wednesday?<<

It would execute on the day following whatever day the target was reached.

>>I suspect this is a basic question and I know the rule is orders are processed on the next bar. I think the answer is Wednesday for both. I'm getting a little confused because I have been looking into the method AssignAutoStopTargetPrices, which it appears you use in the sample code only to set a flag. My understanding is that limits or stops placed inside this method *will* execute on the same bar, both by broker and backtest.<<

Yes but this strategy does not employ same-bar exits. The first exit order is calculated after market close of Monday and placed for Tuesday.

>>Any thoughts of adding Sell.CloseAtMarket as a method in AssignAutoStopTargetPrices to allow closing position on same bar?<<

If you want to exit at market close on the same bar you can call PlaceTrade with a Sell of MarketClose right after your PlaceTrade Buy statement. You needn't do it in AssignAutoStop.

>>Also, is ProfitPctAsOf calculated at the close or open of the current bar? If the answer is close, it would be too late to place a Close-at-Market trade for the current day.<<

It's calculated based on the closing price. But the exit order is placed for execution on the following bar.
0
- ago
#13
Thanks for the answers. Getting clearer. One clarification:

QUOTE:
The first exit order is calculated after market close of Monday and placed for Tuesday.

Do you mean the first exit order is calculated after market close on Tuesday and placed for Wednesday?

The logic is that the calculation occurs in

CODE:
         if (HasOpenPosition(bars, PositionType.Long))          { ...... }

which wouldn't be processed until Tuesday when the position executed on Monday is detected, right?

0
- ago
#14
No, check the position list. The entry trades are filled on Mondays. The system places them after market close on Friday.

The first exit is calculated after the end of day Monday and is placed Tuesday.
0
- ago
#15
With today's exit at open it notched a 2.15% gain for the week. The rest of the week can be spent relaxing :)
4
- ago
#16
Is there a way to code this to look at either TQQQ or SQQQ to play both long and short volatility. Backtesting holding TQQQ out performs this strategy, but possibly playing both short and long may be able to beat long term holding of TQQQ? It's just hypothetical question right now.
0
Cone7
- ago
#17
QUOTE:
Backtesting holding TQQQ out performs this strategy
Not if you started any time in the last 5 or 6 years. But sure, triple return from nearly the beginning of a historic bull market, that's tough (impossible?) to beat.
0
- ago
#18
QUOTE:
Because of the way it's coded, it doesn't generate signals like most Strategies

Would you please explain why this strategy doesn't generate signals. It wouldn't generate a signal on Monday either? Does it have something to do with peeking?
0
- ago
#19
It will not generate a signal because it peeks ahead one bar to look at Monday’s open price. You place a buy limit order one percent below the open price on Monday.
0
Cone7
- ago
#20
Here's a version that can give you a Signal for all days except Monday.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript1 { public class MyStrategy : UserStrategyBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) { } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) {          bool isNextBarStartOfWeek = _lastBarofWeek == idx;          bool isNextBarLastDayOfWeek = bars.TomorrowIsLastTradingDayOfWeek(idx);          if (isNextBarLastDayOfWeek)             _lastBarofWeek = idx + 1;          if (isNextBarStartOfWeek)          SetBackgroundColor(bars, idx + 1, WLColor.Silver.SetAlpha(32));          if (isNextBarStartOfWeek)          {             if (idx >= bars.Count - 1)                return;             mondayOpen = bars.Open[idx + 1];             tradedThisWeek = false;          }          if (HasOpenPosition(bars, PositionType.Long))          {          double target = LastOpenPosition.EntryPrice;          if (LastOpenPosition.ProfitPctAsOf(idx) > -0.5)                target = target * 1.01;             PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, target);             if (isNextBarLastDayOfWeek)             {                PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose);             }          }          else          {             if (!Double.IsNaN(mondayOpen) && !tradedThisWeek)             {                double mult = 0.99;                PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, mondayOpen * mult);                if (isNextBarLastDayOfWeek)                {                   PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose);                }             }          } }       //same bar exit public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice) {          //t.AutoProfitTargetPrice = executionPrice * 1.01;          tradedThisWeek = true; } //declare private variables below private double mondayOpen = Double.NaN;       bool tradedThisWeek = false;       int _lastBarofWeek = -1; } }
0
- ago
#21
This is a good strategy!
0
- ago
#22
QUOTE:
It will not generate a signal because it peeks ahead one bar to look at Monday’s open price.

Yes, makes complete sense for real-world simulation.
0
- ago
#23
QUOTE:
Here's a version that can give you a Signal for all days except Monday.

Thank you for that @Cone
0
Cone7
- ago
#24
It's Monday and you know what that means!

Consider this your signal for today :)

Edit - If you wanted to Auto-Place the order on Monday, just use this script on 1-minute bars in the Strategy Monitor. It's only job is to place a limit order "Monday today" after the first bar ends at 99% of the opening price.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; namespace WealthScript123 { public class BuyOnePercentDownFromOpenOnMonday : UserStrategyBase { public override void Initialize(BarHistory bars) { } public override void Execute(BarHistory bars, int idx) {          if (bars.DateTimes[idx].Date != today || bars.DateTimes[idx].DayOfWeek != DayOfWeek.Monday)             return;         if (bars.IsFirstBarOfDay(idx)) // Ensure Filter Pre/Post Market Data is checked!             PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, bars.Open[idx] * 0.99); }       DateTime today = DateTime.Now.Date; } }
0
- ago
#25
This question is perhaps very basic, but important. I was live monitoring market data and TQQQ opened 33,65 at 09:30, but when looking at Morningstar it says Open=33.14, what open shoould I take in order to be aligned with the backtested strategy? Is it RTH open or Pre-market open?

Thank you in advance.
0
Cone7
- ago
#26
RTH open.
1
- ago
#27
And don’t be afraid, like I did today, to exit early. In this market it’s never a bad call to take some profits!
1
- ago
#28
Indeed, also I am trading covered calls
0
- ago
#29
Hi I have a question regarding this strategy > Say if i a buy the TQQQ 1% below the open, and the TQQQ hits 1% on the same day am i selling according to the strategy rules OR am i selling on Tuesday at the open ?

I noticied on the backtesting in WL , that the selling occured the following day. Just want to clarify the sale . Many thanks for your time
0
Cone7
- ago
#30
The strategy isn't programmed to make a same-bar exit, and it would be unrealistic to do that using only Daily bars.

If you want to trade the way the strategy backtests, then you hold and submit the limit order GTC, regular session, after today's close. If it doesn't hit by the end of the week, then you sell MOC on Friday.
1
- ago
#31
Thank you Cone
1
- ago
#32
How could I alter this code for this a weighted strategy to only submit a limit order for the lower rsi between SQQQ and TQQQ if I made those two their own dataset?
0
Cone7
- ago
#33
You're not going to like the results, but at least you backtested it and found it to be far worse ;)

Run in Single symbol mode on TQQQ. SQQQ is hard-coded as the secondary symbol.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript6 { public class MyStrategy : UserStrategyBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) {          // assumes run on TQQQ          _rsiT = RSI.Series(bars.Close, 14);          _sqqq = GetHistory(bars, "SQQQ");          PlotBarHistory(_sqqq, "SQQQ");                    _rsiS = RSI.Series(_sqqq.Close, 14);          _tradeBars = bars; } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) {          bool isNextBarStartOfWeek = _lastBarofWeek == idx;          bool isNextBarLastDayOfWeek = bars.TomorrowIsLastTradingDayOfWeek(idx);          if (isNextBarLastDayOfWeek)             _lastBarofWeek = idx + 1;          if (isNextBarStartOfWeek)          SetBackgroundColor(bars, idx + 1, WLColor.Silver.SetAlpha(32));          if (isNextBarStartOfWeek)          {             if (idx >= bars.Count - 1)                return;             _tradeBars = _rsiT[idx] < _rsiS[idx] ? bars : _sqqq;             mondayOpen = _tradeBars.Open[idx + 1];             tradedThisWeek = false;          }          Position pos = FindOpenPositionAllSymbols(42);          if (pos != null)          {             double target = pos.EntryPrice;             if (pos.ProfitPctAsOf(idx) > -0.5)                target = target * 1.01;             PlaceTrade(_tradeBars, TransactionType.Sell, OrderType.Limit, target);             if (isNextBarLastDayOfWeek)             {                PlaceTrade(_tradeBars, TransactionType.Sell, OrderType.MarketClose);             }          }          else          {                if (!Double.IsNaN(mondayOpen) && !tradedThisWeek)             {                double mult = 0.99;                PlaceTrade(_tradeBars, TransactionType.Buy, OrderType.Limit, mondayOpen * mult, 42);                if (isNextBarLastDayOfWeek)                {                   PlaceTrade(_tradeBars, TransactionType.Sell, OrderType.MarketClose);                }             }          } }       //same bar exit public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice) {          //t.AutoProfitTargetPrice = executionPrice * 1.01;          tradedThisWeek = true; } //declare private variables below private double mondayOpen = Double.NaN;       bool tradedThisWeek = false;       int _lastBarofWeek = -1;       RSI _rsiT;       RSI _rsiS;       BarHistory _sqqq;       BarHistory _tradeBars; } }
1
- ago
#34
Thanks cone, was worth a shot to backtest it and rule it out!
0
- ago
#35
Can the method TomorrowIsLastTradingDayOfWeek() be used for real-time trading in addition to backtest? That is, does this function require "peeking" or does it use a source outside the history bars to generate its bool value?
0
Cone7
- ago
#36
Yes, it can be used. It uses real-world date rules, which isn't "peeking" in the backtesting sense.
0
- ago
#37
Thank you for confirming
0

Reply

Bookmark

Sort