Hi i am using Chat GPT to build a strategy in think or swim, but i want to use this startegy in the WLD program as it provides auto trading and other features i like. I am not a coder,that is why i use Chat GPT. What coding language do i convert the think or swim code to WLD ? Here is what i pasted in the C+ and it is not working . any help is appreciated.
CODE:
using System; using System.Collections.Generic; namespace WeeklyPivotPoints { class Program { // Data structure to hold weekly data public class WeeklyData { public double High { get; set; } public double Low { get; set; } public double Close { get; set; } } // Main function static void Main(string[] args) { // Example weekly data (replace with real data from your trading API) List<WeeklyData> historicalData = new List<WeeklyData> { new WeeklyData { High = 150.0, Low = 140.0, Close = 145.0 }, new WeeklyData { High = 155.0, Low = 145.0, Close = 150.0 } }; // Current day's opening price (replace with real-time data) double openToday = 147.0; // Calculate pivot points and signals for the most recent weekly data var lastWeek = historicalData[historicalData.Count - 1]; CalculatePivotPoints(lastWeek, openToday); } // Function to calculate pivot points and generate buy/sell signals static void CalculatePivotPoints(WeeklyData weeklyData, double openToday) { // Calculate pivot point and levels double pivotPoint = (weeklyData.High + weeklyData.Low + weeklyData.Close) / 3; double R1 = (2 * pivotPoint) - weeklyData.Low; double S1 = (2 * pivotPoint) - weeklyData.High; double R2 = pivotPoint + (weeklyData.High - weeklyData.Low); double S2 = pivotPoint - (weeklyData.High - weeklyData.Low); // Output pivot levels Console.WriteLine($"Weekly Pivot Point: {pivotPoint:F2}"); Console.WriteLine($"Resistance 1 (R1): {R1:F2}"); Console.WriteLine($"Support 1 (S1): {S1:F2}"); Console.WriteLine($"Resistance 2 (R2): {R2:F2}"); Console.WriteLine($"Support 2 (S2): {S2:F2}"); // Generate buy/sell signals if (openToday > pivotPoint) { Console.WriteLine("Buy Signal: Price opened above the pivot point."); } else if (openToday < pivotPoint) { Console.WriteLine("Sell Signal: Price opened below the pivot point."); } // Breakout conditions if (openToday > R1) { Console.WriteLine("Breakout above R1 detected."); } else if (openToday < S1) { Console.WriteLine("Breakdown below S1 detected."); } } } }
Rename
If you use WealthLab's compiler, it's C# and you need to make sure GPT is training on the Anatomy of a Wealth-Lab Strategy.
Anyway, if I knew if I knew how often the pivot points should change, I could whip it up quickly. Do they change only at the end of the week for the next week? Or do they take into account the daily changes to the Weekly data to change the pivot daily?
Also, it looks like you want to trade based on the opening price, so that requires the NextSessionOpen indicator or the ExecuteSessionOpen() method.
Anyway, if I knew if I knew how often the pivot points should change, I could whip it up quickly. Do they change only at the end of the week for the next week? Or do they take into account the daily changes to the Weekly data to change the pivot daily?
Also, it looks like you want to trade based on the opening price, so that requires the NextSessionOpen indicator or the ExecuteSessionOpen() method.
Hi Cone
The weekly pivots would be based upon last week (high, low, close), but run on a daily basis on the opening price. So in essence using weekly pivots to trade on the daily time frame but with the weekly pivots showing. Using the daily opening price to determine based upon the rules where it is a buy or sell.
I would also like to use this theory for scaning stocks to find that meet these criteria than trading them.
Thanks Cone.
The weekly pivots would be based upon last week (high, low, close), but run on a daily basis on the opening price. So in essence using weekly pivots to trade on the daily time frame but with the weekly pivots showing. Using the daily opening price to determine based upon the rules where it is a buy or sell.
I would also like to use this theory for scaning stocks to find that meet these criteria than trading them.
Thanks Cone.
Here's a good start for you. It uses the NextSessionOpen indicator to trade the opening price. To actually trade it, you need a Historical Provider that will return a partial bar (most do) and you'd have to schedule to run it after the open, when the opening price is available. If you have a large DataSet, there will be a delay to get each opening price.
The strategy only buys or sells or sells when the open is above or below the pivot. The R/S levels aren't used, but you can add logic for them.
Note that the blue dots are the "Next Session's Open" prices. They're plotted a day earlier because that the bar's data used to compare prices for a trade on the next open - which can't be realized until the open actually occurs.
The strategy only buys or sells or sells when the open is above or below the pivot. The R/S levels aren't used, but you can add logic for them.
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; using WealthLab.PowerPack; namespace WealthScript2 { public class WeeklyPivots : UserStrategyBase { public WeeklyPivots() { _showValues = AddParameter("Show Values", ParameterType.Int32, 1, 0, 1); _plotOpen = AddParameter("Plot Next Open", ParameterType.Int32, 1, 0, 1); } Parameter _showValues; Parameter _plotOpen; IndicatorBase _nextOpen; TimeSeries _pivot; TimeSeries _R1; TimeSeries _S1; TimeSeries _R2; TimeSeries _S2; int _idxLast = 0; public override void Initialize(BarHistory bars) { StartIndex = 1; //weekly bars BarHistory _wbars = BarHistoryCompressor.ToWeekly(bars); //synchronize to base scale _wbars = BarHistorySynchronizer.Synchronize(_wbars, bars); //PlotTimeSeriesLine(_wbars.Close, "WeeklyClose", "Price", WLColor.AntiqueWhite, 1); //calculate pivot and levels _pivot = (_wbars.High + _wbars.Low + _wbars.Close) / 3; _R1 = (2 * _pivot) - _wbars.Low; _S1 = (2 * _pivot) - _wbars.High; _R2 = _pivot + (_wbars.High - _wbars.Low); _S2 = _pivot - (_wbars.High - _wbars.Low); //delay the series 1 bar _pivot = _pivot >> 1; _R1 = _R1 >> 1; _S1 = _S1 >> 1; _R2 = _R2 >> 1; _S2 = _S2 >> 1; // plot the series (line connecting weeks is ugly) // PlotTimeSeriesLine(_pivot, "PivotPoint", "Price", WLColor.NeonYellow, 1); // PlotTimeSeriesLine(_R1, "R1", "Price", WLColor.Red, 1); // PlotTimeSeriesLine(_S1, "S1", "Price", WLColor.Blue, 1); // PlotTimeSeriesLine(_R2, "R2", "Price", WLColor.NeonFuschia, 1); // PlotTimeSeriesLine(_S2, "S2", "Price", WLColor.NeonGreen, 1); // Next Day's open indicator _nextOpen = new NextSessionOpen(bars); if (_plotOpen.AsInt == 1) PlotIndicator(_nextOpen); } public override void Execute(BarHistory bars, int idx) { if (idx == bars.Count - 1 || bars.TomorrowIsLastTradingDayOfWeek(idx - 1)) { //draw the levels for the previous week int n = idx; DrawLine(_idxLast, _pivot[n], idx, _pivot[n], WLColor.NeonYellow, 1); DrawLine(_idxLast, _R1[n], idx, _R1[n], WLColor.Red, 1); DrawLine(_idxLast, _S1[n], idx, _S1[n], WLColor.Blue, 1); DrawLine(_idxLast, _R2[n], idx, _R2[n], WLColor.NeonFuschia, 1); DrawLine(_idxLast, _S2[n], idx, _S2[n], WLColor.NeonGreen, 1); if (_showValues.AsInt == 1) { DrawTextVAlign($"P: {_pivot[n]:N2}", _idxLast, _pivot[n], VerticalAlignment.Top, WLColor.NeonYellow, 10, 3); DrawTextVAlign($"R1: {_R1[n]:N2}", _idxLast, _R1[n], VerticalAlignment.Top, WLColor.Red, 10, 3); DrawTextVAlign($"S1: {_S1[n]:N2}", _idxLast, _S1[n], VerticalAlignment.Top, WLColor.Blue, 10, 3); DrawTextVAlign($"R2: {_R2[n]:N2}", _idxLast, _R2[n], VerticalAlignment.Top, WLColor.NeonFuschia, 10, 3); DrawTextVAlign($"S2: {_S2[n]:N2}", _idxLast, _S2[n], VerticalAlignment.Top, WLColor.NeonGreen, 10, 3); } // show the last levels if (idx == bars.Count - 1) { DrawHeaderText($"P: {_pivot[n]:N2}", WLColor.NeonYellow, 12); DrawHeaderText($"R1: {_R1[n]:N2}", WLColor.Red, 12); DrawHeaderText($"S1: {_S1[n]:N2}", WLColor.Blue, 12); DrawHeaderText($"R2: {_R2[n]:N2}", WLColor.NeonFuschia, 12); DrawHeaderText($"S2: {_S2[n]:N2}", WLColor.NeonGreen, 12); } _idxLast = idx + 1; } if (!HasOpenPosition(bars, PositionType.Long)) { if (_nextOpen[idx] > _pivot[idx]) { Transaction t = PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } } else { Position p = LastPosition; if (_nextOpen[idx] < _pivot[idx]) ClosePosition(p, OrderType.Market); } } } }
Note that the blue dots are the "Next Session's Open" prices. They're plotted a day earlier because that the bar's data used to compare prices for a trade on the next open - which can't be realized until the open actually occurs.
Thank you Cone.
Just a quick modification to make this work well with intraday data too.
We can probably add Pivots as another PowerPack condition block.
We can probably add Pivots as another PowerPack condition block.
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; using WealthLab.PowerPack; using WealthLab8; namespace WealthScript1 { public class WeeklyPivots : UserStrategyBase { public WeeklyPivots() { _showValues = AddParameter("Show Values", ParameterType.Int32, 1, 0, 1); _plotOpen = AddParameter("Plot Next Open", ParameterType.Int32, 1, 0, 1); } Parameter _showValues; Parameter _plotOpen; IndicatorBase _nextOpen; TimeSeries _pivot; TimeSeries _R1; TimeSeries _S1; TimeSeries _R2; TimeSeries _S2; int _idxLast = 0; public override void Initialize(BarHistory bars) { StartIndex = 1; //weekly bars BarHistory cbars = bars; if (bars.IsIntraday) // create pivots from Daily data to be consistent cbars = GetHistoryUnsynched(bars.Symbol, HistoryScale.Daily, null); BarHistory _wbars = BarHistoryCompressor.ToWeekly(cbars); //synchronize to base scale _wbars = BarHistorySynchronizer.Synchronize(_wbars, bars); //PlotTimeSeriesLine(_wbars.Close, "WeeklyClose", "Price", WLColor.AntiqueWhite, 1); //calculate pivot and levels _pivot = _wbars.AveragePriceHLC; _R1 = (2 * _pivot) - _wbars.Low; _S1 = (2 * _pivot) - _wbars.High; _R2 = _pivot + (_wbars.High - _wbars.Low); _S2 = _pivot - (_wbars.High - _wbars.Low); //delay the series 1 bar _pivot = _pivot >> 1; _R1 = _R1 >> 1; _S1 = _S1 >> 1; _R2 = _R2 >> 1; _S2 = _S2 >> 1; int n = bars.Count - 1; DrawHeaderText($"Current Levels", WLColor.White, 12); DrawHeaderText($"R2: {_R2[n]:N2}", WLColor.NeonFuschia, 12); DrawHeaderText($"R1: {_R1[n]:N2}", WLColor.NeonFuschia, 12); DrawHeaderText($"P: {_pivot[n]:N2}", WLColor.NeonYellow, 12); DrawHeaderText($"S1: {_S1[n]:N2}", WLColor.NeonGreen, 12); DrawHeaderText($"S2: {_S2[n]:N2}", WLColor.NeonGreen, 12); // Next Day's open indicator _nextOpen = new NextSessionOpen(bars); if (_plotOpen.AsInt == 1) PlotIndicator(_nextOpen); } public override void Execute(BarHistory bars, int idx) { if (idx == bars.Count - 1 || bars.DateTimes[idx + 1].DayOfWeek < bars.DateTimes[idx].DayOfWeek) { //draw the levels for the previous week int n = idx; DrawLine(_idxLast, _pivot[n], idx, _pivot[n], WLColor.NeonYellow, 1); DrawLine(_idxLast, _R1[n], idx, _R1[n], WLColor.NeonFuschia, 1); DrawLine(_idxLast, _S1[n], idx, _S1[n], WLColor.NeonGreen, 1); DrawLine(_idxLast, _R2[n], idx, _R2[n], WLColor.NeonFuschia, 1); DrawLine(_idxLast, _S2[n], idx, _S2[n], WLColor.NeonGreen, 1); if (_showValues.AsInt == 1) { DrawTextVAlign($"P: {_pivot[n]:N2}", _idxLast, _pivot[n], VerticalAlignment.Top, WLColor.NeonYellow, 10, 3); DrawTextVAlign($"R1: {_R1[n]:N2}", _idxLast, _R1[n], VerticalAlignment.Top, WLColor.NeonFuschia, 10, 3); DrawTextVAlign($"S1: {_S1[n]:N2}", _idxLast, _S1[n], VerticalAlignment.Top, WLColor.NeonGreen, 10, 3); DrawTextVAlign($"R2: {_R2[n]:N2}", _idxLast, _R2[n], VerticalAlignment.Top, WLColor.NeonFuschia, 10, 3); DrawTextVAlign($"S2: {_S2[n]:N2}", _idxLast, _S2[n], VerticalAlignment.Top, WLColor.NeonGreen, 10, 3); } _idxLast = idx + 1; } if (!HasOpenPosition(bars, PositionType.Long)) { if (_nextOpen[idx] > _pivot[idx]) { Transaction t = PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } } else { Position p = LastPosition; if (_nextOpen[idx] < _pivot[idx]) ClosePosition(p, OrderType.Market); } } } }
Your Response
Post
Edit Post
Login is required