- ago
My first meager attempt and adding some functionality to an existing code. However I ran into an ERROR that I cannot solve
CODE:
// One Percent Per Week v3 Original version Glitch (Dion) // Mods By Guy Fleury. v5. May 2024 // Optimizable Risk Control added by Carova April 2025 using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript1 {    public class OnePercentOptimized : UserStrategyBase    {       public OnePercentOptimized() : base()       {          AddParameter("Profit Target Multiplier", ParameterType.Double, 1.01, 1.01, 1.10, 0.001);          AddParameter("Stop Loss Multiplier", ParameterType.Double, 2, 1, 3, 0.1);          AddParameter("Entry Limit Factor", ParameterType.Double, 1.0, 0.98, 1.02, 0.001);       }       public override void Initialize(BarHistory bars)       {          atr = new ATR(bars, 14);          PlotIndicator(atr, WLColor.Red);          ProfitTarget = Parameters[0].AsDouble;          StopLoss = Parameters[1].AsDouble;          EntryLimit = Parameters[2].AsDouble;       }       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;          }          // Risk Mitigation & Optimized Trade Execution          if (HasOpenPosition(bars, PositionType.Long))          {             double target = LastOpenPosition.EntryPrice * ProfitTarget;             double atrStop = LastOpenPosition.EntryPrice - (StopLoss * atr[idx]);             // Adjust profit targets based on performance             if (LastOpenPosition.ProfitPctAsOf(idx) > 0.3)                target *= 1.011;             // Apply optimized stop-loss             CloseAtTrailingStop(LastPosition, TrailingStopType.PercentHL, atrStop, "Sell at " + atrStop + "% trailing stop loss");             // Place sell limit order at optimized target             PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, target);             // End-of-week mandatory exit             if (NextBarIsLastDayOfWeek)                PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose);          }          else          {             // Volatility-filtered entry with optimized entry factor             if (!Double.IsNaN(mondayOpen) && !tradedThisWeek)             {                PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, mondayOpen * EntryLimit);                if (NextBarIsLastDayOfWeek)                   PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose);             }          }       }       //declare private variables below       private double mondayOpen = Double.NaN;       bool tradedThisWeek = false;       int _lastBarofWeek = -1;       double ProfitTarget;       double StopLoss;       double EntryLimit;       IndicatorBase atr;    } }


QUOTE:
12: 'OnePercentOptimized' does not implement inherited abstract member 'UserStrategyBase.Execute(BarHistory, int)'


What am I doing wrong? Thanks!
0
99
Solved
2 Replies

Reply

Bookmark

Sort
Cone8
 ( 2.85% )
- ago
#1
That code uses ExecuteSessionOpen(), whose implementation is optional, but Execute() is not optional. You just need to add an empty method -

CODE:
public override void Execute(BarHistory bars, int idx) {          }
0
Best Answer
- ago
#2
Thanks Cone! I must have erased that accidentally in the process.

Corrected code if anyone wants it
CODE:
// One Percent Per Week v3 Original version Glitch (Dion) // Mods By Guy Fleury. v5. May 2024 // Optimizable Risk Control added by Carova April 2025 using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript1 {    public class OnePercentOptimized : UserStrategyBase    {       public OnePercentOptimized() : base()       {          AddParameter("Profit Target Multiplier", ParameterType.Double, 1.01, 1.01, 1.10, 0.001);          AddParameter("Stop Loss Multiplier", ParameterType.Double, 2, 1, 3, 0.1);          AddParameter("Entry Limit Factor", ParameterType.Double, 1.0, 0.98, 1.02, 0.001);       }       public override void Initialize(BarHistory bars)       {          atr = new ATR(bars, 14);          PlotIndicator(atr, WLColor.Red);          ProfitTarget = Parameters[0].AsDouble;          StopLoss = Parameters[1].AsDouble;          EntryLimit = Parameters[2].AsDouble;       }       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;          }          // Risk Mitigation & Optimized Trade Execution          if (HasOpenPosition(bars, PositionType.Long))          {             double target = LastOpenPosition.EntryPrice * ProfitTarget;             double atrStop = LastOpenPosition.EntryPrice - (StopLoss * atr[idx]);             // Adjust profit targets based on performance             if (LastOpenPosition.ProfitPctAsOf(idx) > 0.3)                target *= 1.011;             // Apply optimized stop-loss             CloseAtTrailingStop(LastPosition, TrailingStopType.PercentHL, atrStop, "Sell at " + atrStop + "% trailing stop loss");             // Place sell limit order at optimized target             PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, target);             // End-of-week mandatory exit             if (NextBarIsLastDayOfWeek)                PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose);          }          else          {             // Volatility-filtered entry with optimized entry factor             if (!Double.IsNaN(mondayOpen) && !tradedThisWeek)             {                PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, mondayOpen * EntryLimit);                if (NextBarIsLastDayOfWeek)                   PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose);             }          }       }       //declare private variables below       private double mondayOpen = Double.NaN;       bool tradedThisWeek = false;       int _lastBarofWeek = -1;       double ProfitTarget;       double StopLoss;       double EntryLimit;       IndicatorBase atr;    } }
0

Reply

Bookmark

Sort