Hi guys,
I've copied and pasted the C# strategy found in the pdf book from Guy Fleury, but it's giving different results when I look at the trades.
Here are the simple rules that he specifies on the pdf:
You buy the open on Monday and set a limit sell order at 7% above the opening price.
If the price gets near your profit target in the next couple of days, cancel that limit
order and reissue a new limit sell order at 8%. If the position is still active by Friday’s
close, liquidate it and take the closing price, whether at a profit or a loss.
But quite a few trades get closed on the following day (Tuesday) for no apparent reason, like on 11/25/24.
I also found this note:
"The program used the ”LastOpenPosition.EntryPrice” as a reference point for its
profit target. I did not notice that initially, but this meant that the price of reference was
not on the current Monday’s opening price but on the entry price of an earlier taken
trade. It could have been a week before, even two or three weeks before, should
some of those past potential trades not have been taken. It is why you see trades
closed before Friday’s close at less profit than the requested 7% profit target. That
you have a profit of less than 7% on Friday’s close is perfectly acceptable. However,
closing a trade before Friday’s close should happen only if the profit target is met."
Not an expert on C#, can someone help me, please?
I've copied and pasted the C# strategy found in the pdf book from Guy Fleury, but it's giving different results when I look at the trades.
Here are the simple rules that he specifies on the pdf:
You buy the open on Monday and set a limit sell order at 7% above the opening price.
If the price gets near your profit target in the next couple of days, cancel that limit
order and reissue a new limit sell order at 8%. If the position is still active by Friday’s
close, liquidate it and take the closing price, whether at a profit or a loss.
But quite a few trades get closed on the following day (Tuesday) for no apparent reason, like on 11/25/24.
I also found this note:
"The program used the ”LastOpenPosition.EntryPrice” as a reference point for its
profit target. I did not notice that initially, but this meant that the price of reference was
not on the current Monday’s opening price but on the entry price of an earlier taken
trade. It could have been a week before, even two or three weeks before, should
some of those past potential trades not have been taken. It is why you see trades
closed before Friday’s close at less profit than the requested 7% profit target. That
you have a profit of less than 7% on Friday’s close is perfectly acceptable. However,
closing a trade before Friday’s close should happen only if the profit target is met."
Not an expert on C#, can someone help me, please?
CODE:
// One Percent Per Week v3 Original version Glitch (Dion) // Mods By Guy Fleury. v5. May 2024 // 7%-8% profit target + positive stance using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript1 { public class OnePercentV3 : UserStrategyBase { public override void Initialize(BarHistory bars) { } public override void Execute(BarHistory bars, int idx) { } public override void ExecuteSessionOpen(BarHistory bars, int idx, double sessionOpenPrice) { bool NextBarIsStartOfWeek = _lastBarofWeek == idx; bool NextBarIsLastDayOfWeek = bars.TomorrowIsLastTradingDayOfWeek(idx); if (NextBarIsLastDayOfWeek) _lastBarofWeek = idx + 1; if (idx - 1 == _lastBarofWeek) SetBackgroundColor(bars, idx, WLColor.Silver.SetAlpha(32)); if (NextBarIsStartOfWeek) { mondayOpen = sessionOpenPrice; tradedThisWeek = false; } if (HasOpenPosition(bars, PositionType.Long)) { double target = LastOpenPosition.EntryPrice; if (LastOpenPosition.ProfitPctAsOf(idx) > 0.0) { target = target * 1.07; if (LastOpenPosition.ProfitPctAsOf(idx) > 0.3) { target = target * 1.011; } } Backtester.CancelationCode = 642; PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, target); Backtester.CancelationCode = 642; if (idx < bars.Count - 1) { if (NextBarIsLastDayOfWeek) PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose); } else { //last bar: if today is the last trading day of the week, signal MOC for the open position if (bars.TomorrowIsLastTradingDayOfWeek(idx)) PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose); } } else { if (!Double.IsNaN(mondayOpen) && !tradedThisWeek) { double mult = 1.001; PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, mondayOpen * mult); if (NextBarIsLastDayOfWeek) { Backtester.CancelationCode = 642; PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose); } } else { //place trade here if } } } //same bar exit public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice) { //t.AutoProfitTargetPrice = executionPrice * 0.99; tradedThisWeek = true; } //declare private variables below private double mondayOpen = Double.NaN; bool tradedThisWeek = false; int _lastBarofWeek = -1; } }
Rename
Some observations:
On Mondays, you have a buy limit order above the opening price by a factor of 1.001. That can be a few pennies above the open. It assures you that you got in the trade. It is like zapping the ask for a few pennies above the ask and limiting the cost. A market order could have swept the ask with, at times, unwanted consequences.
The program will set its exit target the next day. If it sees some red, it chickens out, meaning the profit target will be set at Monday's opening price. This is intentional and was part of Dion's strategy. It took me time to understand the purpose, but it is a good feature. Often, you will see the trade exited at the opening price for this reason. And it is on purpose that it happens. Should you remove that feature, profits will drop. It acts as an immediate stop-loss. I kept it in because it is a nice feature to have.
The entry above Monday's open supersedes other entry methods. It is the one being applied with its 1.001 times the opening price.
As for the 8% profit target, it is more 1.07 times 1.011, giving slightly higher than 8%. Something more like 8.18%.
As for the days during the week when the position is closed before Friday, it's because the strategy saw some red and opted to exit, to chicken out, at the price it can get. It will sometimes be after an opening gap above the opening target reset. At other times, on a rebound, reaching breakeven.
In all, it is quite an interesting construct which can be improved.
On Mondays, you have a buy limit order above the opening price by a factor of 1.001. That can be a few pennies above the open. It assures you that you got in the trade. It is like zapping the ask for a few pennies above the ask and limiting the cost. A market order could have swept the ask with, at times, unwanted consequences.
The program will set its exit target the next day. If it sees some red, it chickens out, meaning the profit target will be set at Monday's opening price. This is intentional and was part of Dion's strategy. It took me time to understand the purpose, but it is a good feature. Often, you will see the trade exited at the opening price for this reason. And it is on purpose that it happens. Should you remove that feature, profits will drop. It acts as an immediate stop-loss. I kept it in because it is a nice feature to have.
The entry above Monday's open supersedes other entry methods. It is the one being applied with its 1.001 times the opening price.
As for the 8% profit target, it is more 1.07 times 1.011, giving slightly higher than 8%. Something more like 8.18%.
As for the days during the week when the position is closed before Friday, it's because the strategy saw some red and opted to exit, to chicken out, at the price it can get. It will sometimes be after an opening gap above the opening target reset. At other times, on a rebound, reaching breakeven.
In all, it is quite an interesting construct which can be improved.
By the way: I just sent an email to ProShares, that they should provide the KID documentation, that we germans can trade the TQQQ ETF too.
Your Response
Post
Edit Post
Login is required