Need some help as this is my first try to work with fundamental data in WL7
if I don't want to trade the next 3 days after a dividend what would be the condition I would add to the following simple example
if I don't want to trade the next 3 days after a dividend what would be the condition I would add to the following simple example
CODE:
if ((sma[bar] < sma[bar - 1]) PlaceTrade(bars, TransactionType.Short, OrderType.Market, 0, "Short 10");
Rename
Here's a Strategy that colors the background light red if it's within 3 days of the most recent dividend, hope this helps!
CODE:
using WealthLab.Backtest; using System; using WealthLab.Data; using WealthLab.Core; using System.Drawing; using System.Collections.Generic; namespace WealthScript1 { public class MyStrategy : UserStrategyBase { //Initialize public override void Initialize(BarHistory bars) { } //Execute public override void Execute(BarHistory bars, int idx) { //color bars that are within 3 days of last dividend List<EventDataPoint> divs = bars.GetEventDataPoints("dividend", idx); if (divs.Count > 0) lastDivDate = bars.DateTimes[idx]; if (lastDivDate != DateTime.MinValue) { TimeSpan span = bars.DateTimes[idx] - lastDivDate; if (span.TotalDays <= 3) { SetBackgroundColor(bars, idx, WLColor.FromArgb(32, 255, 0, 0)); } } } //private members private DateTime lastDivDate = DateTime.MinValue; } }
As usual, Glitch's example is elegant, but since I started on it, here's another way..
CODE:
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Data; using System.Drawing; using System.Collections.Generic; using WealthLab.Indicators; namespace WealthScript123 { public class EventIndexExample : UserStrategyBase { List<EventDataPoint> _events; BarHistory _bars; SMA sma; public override void Initialize(BarHistory bars) { _bars = bars; _events = bars.GetEventDataPoints("Dividend"); sma = SMA.Series(bars.Close, 10); } public override void Execute(BarHistory bars, int bar) { int eventIdx = IndexOfMostRecentEvent(bar); if (bar - eventIdx > 2) { if (sma[bar] < sma[bar - 1]) PlaceTrade(bars, TransactionType.Short, OrderType.Market, 0, "Short 10"); } else // no trading here SetBackgroundColor(bars, bar, WLColor.FromArgb(40, Color.Red)); } int IndexOfMostRecentEvent(int bar) { if (_events == null) return -1; // return the most recent event in the list that occurred before the current bar for (int n = _events.Count - 1; n >= 0; n--) { EventDataPoint edp = _events[n]; if (edp.Date <= _bars.DateTimes[bar]) return _bars.IndexOf(edp.Date, false); } return -1; } } }
Thanks for your help
Your Response
Post
Edit Post
Login is required