Hi team.
Although I am aware that some functions in WL work only for backtests, I am very surprised that this function does not work properly in (automatic) trading.
Might this be a broker related issue or what is the reason that new orders are placed although there is an existing position at the broker?
Please have a look at it.
Although I am aware that some functions in WL work only for backtests, I am very surprised that this function does not work properly in (automatic) trading.
Might this be a broker related issue or what is the reason that new orders are placed although there is an existing position at the broker?
Please have a look at it.
Rename
Strategies ALWAYS operate in a hypothetical backtest mode. With the Portfolio Sync function, its not a problem for exiting positions (orders are ignored if a live Position doesn't exist). However, if you already have a live position and you don't want to add to it, then you need to make your hypothetical strategy aware of it.
Vote:
https://www.wealth-lab.com/Discussion/Access-Accounts-data-from-broker-programmatically-7914
Vote:
https://www.wealth-lab.com/Discussion/Access-Accounts-data-from-broker-programmatically-7914
Ok, thanks for your feedback. Here is my opinion on that.
If you offer an automated trading mode in your software, it must be prepared to provide the same functionality for both modes, at least in terms of basic functionality. You can certainly find this function in 95% of all strategies.
If one uses again and again functions, which one maps for real trading with a workaround the use is lost.
From my point of view, once again not well thought out.
I have already voted for the feature, but I did not think of such elementary basics to control the automated trading.
If you offer an automated trading mode in your software, it must be prepared to provide the same functionality for both modes, at least in terms of basic functionality. You can certainly find this function in 95% of all strategies.
If one uses again and again functions, which one maps for real trading with a workaround the use is lost.
From my point of view, once again not well thought out.
I have already voted for the feature, but I did not think of such elementary basics to control the automated trading.
Can you please explain how the behaviour of this code is while trading
I don't know what you are referring to, can you please give me another hint.
CODE:
if (!HasOpenPosition(bars, PositionType.Long)) { // 1. When is this code executed in trading?! // 2. How must this look like, so that it is functionally identical in the backtest and in trading? // 3. What is the point of writing code (block, self-implemented) that must be different and modified for trading? } else { // do something }
QUOTE:
... With the Portfolio Sync function, its not a problem for exiting positions ...
I don't know what you are referring to, can you please give me another hint.
The point is to keep the integrity of the state of the trading system. Cone already answered, WL strategies are always based on hypothetical positions.
Once exit signals are issued, they can then be synchronized to actual positions held in a broker account.
Once exit signals are issued, they can then be synchronized to actual positions held in a broker account.
QUOTE:
I don't know what you are referring to, can you please give me another hint.
The built-in Help is the best resource to refer as another hint. Type in "portfolio sync" in Filter and please read the "Trading Preferences" chapter where it's been described in detail with embedded Youtube links.
QUOTE:
The point is to keep the integrity of the state of the trading system. Cone already answered, WL strategies are always based on hypothetical positions.
I understood that it is based on hypothetical positions of a backtest.
But this does not answer what the code is doing when trading!
Sorry Glitch, now a very basic and simple question. I has nothing to do with synchronization of the positions.
When trading, is this branch of code always, never, systematically or randomly executed?
You can't sugarcoat this point. There can be a lot more calculations that are not directly related to place a trade and to sync with the broker. Everything that is coded within this block is affected. You just have to know how the function behaves, i.e. how the return values are generated, otherwise controlling the branch and its contents is impossible!
@Eugene Thanks for the reference.
HasOpenPosition checks to see if the simulation has an open position at the point in time it is called. It doesn’t have anything to do with live trading.
In a live trading scenario, we’re only concerned with the last bar of data, the one that generates signals. If there is a theoretical position open in the backtest as of the last bar of data, then that code will execute.
Hope that clears things up 🙂
In a live trading scenario, we’re only concerned with the last bar of data, the one that generates signals. If there is a theoretical position open in the backtest as of the last bar of data, then that code will execute.
Hope that clears things up 🙂
So on the last bar of execution, will HasOpenPosition check if there is a real position in my brokerage account?
To check if there is a real position in your brokerage account, you can use C# code solution referenced in the topic in @Cone's Post #1.
This topic has aged. There is a new Trading Preference, Live Positions, that changes things a bit. See the User Guide for details.
In short, in a Streaming Strategy Window or in the Strategy Monitor with the Live Positions option enabled, HasOpenPosition essentially checks for a real position in a brokerage account. Otherwise, it does not.
In short, in a Streaming Strategy Window or in the Strategy Monitor with the Live Positions option enabled, HasOpenPosition essentially checks for a real position in a brokerage account. Otherwise, it does not.
I have a manually purchased position like TQQQ. Would the HasOpenPosition function pick that up if the Live Position is check-marked? If so, I can't seem to get it to sell my existing position.
Strategy Monitor. As far as I can tell, it looks like everything is connected.
I've included everything to give you some context. I was running this code using 15-minute data, but for the sake of forcing it to do something, I used 1-minute data.
The image I copied was just after closing today. Previously, I accidentally deleted the strategy from the monitor and had to re-add it to get the image.
I've included everything to give you some context. I was running this code using 15-minute data, but for the sake of forcing it to do something, I used 1-minute data.
The image I copied was just after closing today. Previously, I accidentally deleted the strategy from the monitor and had to re-add it to get the image.
CODE:
/* This code has been modified using the One Percent A Week Strategy. Assign TQQQ to a Market that opens at 09:29 EST (Tools > Markets & Symbols) Use a 15-minute data scale. */ using WealthLab.Backtest; using System; using WealthLab.Core; namespace WealthScript1 { public class RunAnyDayStrategy : UserStrategyBase { public override void Initialize(BarHistory bars) { PlotStopsAndLimits(3); } public override void Execute(BarHistory bars, int idx) { if (idx >= bars.Count - 1) return; // FIRST TRADING DAY OF WEEK LOGIC if (TodayIsFirstSessionOfTheWeek(bars, bars.DateTimes[idx])) { // Ensure Filter Pre/Post Market Data is checked! if (!HasOpenPosition(bars, PositionType.Long)) { int firstBarToday = idx - bars.IntradayBarNumber(idx); if (bars.DateTimes[firstBarToday].TimeOfDay == ts0930) // the first bar was the premarket bar ending at 0930 { if (idx > firstBarToday) { _limit = bars.Open[firstBarToday + 1] * 0.99; } else { // use the close of the last premarket bar _limit = bars.Close[firstBarToday] * 0.99; } } else // the first bar was not a premarket bar _limit = bars.Open[firstBarToday] * 0.99; PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, _limit); return; } SetBackgroundColor(bars, idx, WLColor.FromArgb(40, WLColor.Red)); return; } else { SetBackgroundColor(bars, idx, WLColor.FromArgb(40, WLColor.Red)); // Ensure Filter Pre/Post Market Data is checked! if (HasOpenPosition(bars, PositionType.Long)) { SetBackgroundColor(bars, idx, WLColor.FromArgb(40, WLColor.Orange)); target = LastOpenPosition.EntryPrice; pct_profit_or_loss = LastOpenPosition.ProfitPctAsOf(idx); target = LastOpenPosition.EntryPrice; if (pct_profit_or_loss > -0.5) { target *= 1.01; WLHost.Instance.AddLogItem("Sell", "Trying to sell @ " + target.ToString(), WLColor.Red); PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, target); } // Sell TQQQ if the loss < 15 percent (temporarily changed it zero to force a trade) else if (pct_profit_or_loss < 0.0) { WLHost.Instance.AddLogItem("Sell", "Trying to sell @ " + bars.Close[idx].ToString(), WLColor.Orange); PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, bars.Close[idx]); } } } } // pass the current date to check with the bars public bool TodayIsFirstSessionOfTheWeek(BarHistory bars, DateTime dte) { if (!bars.Market.IsTradingDay(dte.Date)) return false; DayOfWeek dow = dte.DayOfWeek; do { dte = dte.AddDays(-1); if (bars.Market.IsTradingDay(dte.Date)) return dow < dte.DayOfWeek; } while (dte.Year > 1900); return false; } TimeSpan ts0930 = new TimeSpan(9, 30, 0); double _limit = 0; private double pct_profit_or_loss = 0.0; private double target = 0.0; } }
QUOTE:Yes, but I've found a bug...
I have a manually purchased position like TQQQ. Would the HasOpenPosition function pick that up if the Live Position is check-marked?
The script must have created at least one position previously. You can exit that hypothetical Position(s) - or not - and the script will pick up the Live Position on the last bar.
Remove this statement - that disables Signals!
If you load at least 2 weeks of data, then it should work (as long as at least one position was created in the past).
CODE:
if (idx >= bars.Count - 1) return;
If you load at least 2 weeks of data, then it should work (as long as at least one position was created in the past).
Thank you very much for all your help.
I’ll try this change on Monday.
Mike
I’ll try this change on Monday.
Mike
By the way, just to be sure, you know that the script above was modified from the "One Percent Per Week" intraday strategy to:
1. exit at any loss beginning on the second day of the week, and,
2. only enter a position on the first day of the week.
Both of these will cause the performance of the system to suffer.
1. exit at any loss beginning on the second day of the week, and,
2. only enter a position on the first day of the week.
Both of these will cause the performance of the system to suffer.
Your Response
Post
Edit Post
Login is required