public static int TradingDaysBetweenDates(this WealthScript obj, DateTime dt1, DateTime dt2)public int TradingDaysBetweenDates(DateTime dt1, DateTime dt2)
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;namespace WealthLab.Strategies { public class MyStrategy : WealthScript { protected override void Execute() { ClearDebug(); FundamentalItem fi; int barsToNextEvent = -1; for(int bar = GetTradingLoopStartBar(1); bar < Bars.Count; bar++) { // Determine the number of bars until the next event try { fi = GetNextFundamentalItem(bar, Bars.Symbol, "dividend"); barsToNextEvent = fi.Bar - bar; // if fi.Bar isn't on the chart yet calculate the number of trading days if (fi.Bar == -1) { barsToNextEvent = this.TradingDaysBetweenDates(Datebar, fi.Date); if (bar == Bars.Count - 1) DrawLabel(PricePane, "Calculated trading days to next event: " + barsToNextEvent, Color.Red); } } catch { barsToNextEvent = -1; // no future fundamental item } if (IsLastPositionActive) { Position p = LastPosition; if (barsToNextEvent == 1) // Exit on day before ex-div { SellAtClose(bar, p, "Ex-div"); } else { double Stop = p.EntryPrice * (1 - 15.00 / 100.0d); SellAtStop(bar + 1, p, Stop, "Stop Loss"); } } else { if (barsToNextEvent > - 1 && barsToNextEvent <= 8) { BuyAtMarket(bar + 1); } } } } } }
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; using Community.Components; /*** Requires installation of Community.Components Extension from www.wealth-lab.com > Extensions ***/namespace WealthLab.Strategies { public class MyStrategy : WealthScript { protected override void Execute() { DateTimeFunctions dtf = new DateTimeFunctions(this); ClearDebug(); FundamentalItem fi; int barsToNextEvent = -1; for(int bar = GetTradingLoopStartBar(1); bar < Bars.Count; bar++) { // Determine the number of bars until the next event try { fi = GetNextFundamentalItem(bar, Bars.Symbol, "dividend"); barsToNextEvent = fi.Bar - bar; // if fi.Bar isn't on the chart yet calculate the number of trading days if (fi.Bar == -1) { barsToNextEvent = dtf.TradingDaysBetweenDates(Datebar, fi.Date); if (bar == Bars.Count - 1) DrawLabel(PricePane, "Calculated trading days to next event: " + barsToNextEvent, Color.Red); } } catch { barsToNextEvent = -1; // no future fundamental item } if (IsLastPositionActive) { Position p = LastPosition; if (barsToNextEvent == 1) // Exit on day before ex-div { SellAtClose(bar, p, "Ex-div"); } else { double Stop = p.EntryPrice * (1 - 15.00 / 100.0d); SellAtStop(bar + 1, p, Stop, "Stop Loss"); } } else { if (barsToNextEvent > - 1 && barsToNextEvent <= 8) { BuyAtMarket(bar + 1); } } } } } }