- ago
I'm just trying to produce a simple strategy that buys 5 days before earnings and sells right before earnings. "Earnings Run-Up"

I can do it in WL6 but I can't seem to figure it out in WL7
0
1,273
Solved
10 Replies

Reply

Bookmark

Sort
- ago
#1
Here is my code - it only works for the last fundamental item


CODE:
using WealthLab.Backtest; using WealthLab.Core; using System.Drawing; namespace WealthScript8 {    public class MyStrategy1 : UserStrategyBase    {       //create indicators and other objects here, this is executed prior to the main trading loop       public override void Initialize(BarHistory bars)       {          if (bars.EventDataPoints.Count > 0)          {             EventDataPoint fdp = bars.EventDataPoints[bars.EventDataPoints.Count - 1];             DrawHeaderText("The most recent event item is a " + fdp.ItemName + " reported on " + fdp.Date.ToShortDateString(), Color.Black, 18);          }       }       //execute the strategy rules here, this is executed once for each bar in the backtest history       public override void Execute(BarHistory bars, int idx)       {          if (!HasOpenPosition(bars, PositionType.Long))          {             var today = bars.DateTimes[idx].Date;             EventDataPoint fdp = bars.EventDataPoints[bars.EventDataPoints.Count - 1];             if(fdp.Date == today)                PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0);          }          else          {             int days = 5;             Position p = LastPosition;             if (idx + 1 - p.EntryBar >= days)                ClosePosition(p, OrderType.Market);          }                              }    } }
0
Glitch8
 ( 12.10% )
- ago
#2
Here's how you could do this in WL7. It hold the position for 5 calendar days, as opposed to 5 daily bars.

CODE:
using WealthLab.Backtest; using WealthLab.Core; using System.Collections.Generic; using System; namespace WealthScript1 { public class MyStrategy : UserStrategyBase { //Initialize public override void Initialize(BarHistory bars) {          earnings = bars.GetEventDataPoints("Earnings"); } //Execute public override void Execute(BarHistory bars, int idx) {          if (HasOpenPosition(bars, PositionType.Long))          {             //sell at the close the day of the earnings             if (idx < bars.Count - 1 && bars.DateTimes[idx + 1] == nextEarning.Date)                PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose);          }          else          {             //get the next future earnings             nextEarning = null;             DateTime dt = bars.DateTimes[idx];             foreach(EventDataPoint earning in earnings)                if (earning.Date > dt)                {                   nextEarning = earning;                   break;                }             if (nextEarning != null)             {                //is it 5 or less days away?                TimeSpan span = nextEarning.Date - dt;                if (span.TotalDays <= 6)                   PlaceTrade(bars, TransactionType.Buy, OrderType.Market);             }          } }       //private members       List<EventDataPoint> earnings;       EventDataPoint nextEarning; } }


1
- ago
#3
QUOTE:
Here is my code - it only works for the last fundamental item

On a related note, most Extensions install some sample Strategies. So after installing the Fundamental extension they would appear under the Strategies tree. If you look into the Fundamental folder there you'd find there two such examples: "Rising EPS Trend" and "EPS Surprises v2". Both illustrate accessing the Earnings items, querying their properties such as Date and Details (e.g. Percent of earnings surprise).
0
- ago
#4
Cool man - much better than the one I was attempting to make. You must have been the kid I copied off of in school ;)

Thanks!

So how would I modify it to close out the day before earnings (some earnings report are before the open). Normally I would just decrement bars - 1. But I can't do that here......


0
- ago
#5
Am I retarded or is there something more to this line?

CODE:
if (idx < bars.Count - 1 && bars.DateTimes[idx + 1] == nextEarning.Date)


I'm trying to exit the trade the day before earnings (some stocks have am vs pm reports). I thought I could just increment/decrement idx +/- 1, but it just goes nutso. If I just use idx it's the day after earnings.

What am I not getting here????
0
Glitch8
 ( 12.10% )
- ago
#6
Based on that line of code alone I don't know what to tell you. I mean, that line in isolation looks fine. What exactly is happening? It would be so much better if you pasted the whole strategy code here.
0
- ago
#7
Also don't forget to indicate the symbol you're having problem with.
0
- ago
#8
QUOTE:
I'm trying to exit the trade the day before earnings (some stocks have am vs pm reports). I thought I could just increment/decrement idx +/- 1, but it just goes nutso. If I just use idx it's the day after earnings.

With regard to Zacks Adjusted Earnings, let's leverage Dion's code above to show how you could accomplish this:

CODE:
using WealthLab.Backtest; using WealthLab.Core; using System.Collections.Generic; using System; using System.Drawing; using System.Linq; namespace WealthScript3 {    public class MyStrategy : UserStrategyBase    {       //Initialize       public override void Initialize(BarHistory bars)       {          earnings = bars.GetEventDataPoints("Earnings");          //don't process a symbol with no earnings or surprises (data not loaded/available)          if (earnings.Count == 0 || !bars.EventDataPoints.Any(i => i.Details["Time"] != null))             return;          if (bars.EventDataPoints.Any(i => i.Details["Time"] == null))          {             DrawHeaderText("This symbol has empty earnings release Time fields. Have you updated Zacks events data before running the Strategy?", Color.Red, 14);             return;          }       }       //Execute       public override void Execute(BarHistory bars, int idx)       {          if (HasOpenPosition(bars, PositionType.Long))          {             //if earnings are reported "Before Open" (e.g. BBY), exit them at close             //for "After Close" (e.g. AAPL), exit at market             String _time = nextEarning.Details["Time"].ToLower();             int exitAtBar = idx +1;             if ( _time.Contains("before") || _time.Contains("-"))             {                exitAtBar = idx + 1;             }             if (_time.Contains("after"))             {                exitAtBar = idx;             }                          //sell at the close / at market the day of the earnings             if (idx < bars.Count - 1 && bars.DateTimes[exitAtBar] == nextEarning.Date)                PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose, 0, _time);          }          else          {             //get the next future earnings             nextEarning = null;             DateTime dt = bars.DateTimes[idx];             foreach(EventDataPoint earning in earnings)                if (earning.Date > dt)                {                   nextEarning = earning;                   break;                }             if (nextEarning != null)             {                //is it 5 or less days away?                TimeSpan span = nextEarning.Date - dt;                if (span.TotalDays <= 6)                   PlaceTrade(bars, TransactionType.Buy, OrderType.Market);             }          }       }       //private members       List<EventDataPoint> earnings;       EventDataPoint nextEarning;    } }
0
Best Answer
- ago
#9
I get

CODE:
Initialize Exception (AAPL) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (PFE) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (AXP) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (HD) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (HON) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (JNJ) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (UNH) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (DIS) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (MMM) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (CAT) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (DOW) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (V) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (BA) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (PG) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (CRM) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (JPM) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (MSFT) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (GE) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (AMGN) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (CVX) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (RTX) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (NKE) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (GS) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (WMT) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (XOM) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (MRK) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (IBM) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (VZ) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (TRV) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (CSCO) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (DD) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (WBA) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (MCD) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (KO) Line 18 - The given key 'Time' was not present in the dictionary. Initialize Exception (INTC) Line 18 - The given key 'Time' was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at WealthScript3.MyStrategy.<>c.<Initialize>b__0_0(EventDataPoint i) in :line 18 at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate) at WealthScript3.MyStrategy.Initialize(BarHistory bars) in :line 18 at WealthLab.Backtest.UserStrategyExecutor.FillStub(List`1 symbols)


when I attempt to run it.....
0
- ago
#10
Cannot reproduce. You haven't updated Zacks data? Are "Zacks" and "Earnings" both checked on Event Providers tab?
0

Reply

Bookmark

Sort