I attempted to convert the TASC 2016-04 | Trading Gap Reversals (Calhoun) WL6 code to WL8 as shown at http://www2.wealth-lab.com/WL5WIKI/TASCApr2016.ashx but I'm not getting any trades. Perhaps someone can correct the mistakes to get it running.
CODE:By the way, the above URL is not formatting the strategy code right. Perhaps someone can fix that.
using WealthLab.Backtest; using WealthLab.Core; namespace WealthScript3 { public class TradingGapReversals : UserStrategyBase { //TASC 2016-04 | Trading Gap Reversals (Calhoun) conversion from WL6 // www2.wealth-lab.com/WL5WIKI/TASCApr2016.ashx by Eugene Parameter gapPct, buyStp, sellStp; TimeSeries trigger; public TradingGapReversals() { gapPct = AddParameter("Gap %", ParameterType.Double, 10.0, 1.0, 30.0, 1.0); buyStp = AddParameter("Buy stop", ParameterType.Double, 0.50, 0.10, 1.0, 0.1); sellStp = AddParameter("Sell stop", ParameterType.Double, 0.40, 0.10, 1.0, 0.1); } public override void Initialize(BarHistory bars) { trigger = bars.Close * (1.0 - gapPct.AsDouble/100.0) >> 1; StartIndex = 1; } public override void Execute(BarHistory bars, int idx) { Position p = LastPosition; if (HasOpenPosition(bars, PositionType.Long)) { //sell conditions below double amount = p.MFEAsOf(idx)/p.Quantity + p.EntryPrice - sellStp.AsDouble; CloseAtTrailingStop(p, TrailingStopType.PointC, amount, "Trailing stop Sell"); PlaceTrade(bars,TransactionType.Sell,OrderType.Stop,p.EntryPrice-sellStp.AsDouble,"Initial stop Sell"); } else if ( p == null || !p.IsOpen && p.EntryBar < (idx-bars.IntradayBarNumber(idx)) ) { //buy conditions below int firstBarToday = idx-bars.IntradayBarNumber(idx); int lastBarYesterday = firstBarToday-1; bool priceFilter = (bars.Close[firstBarToday] >= 20) && (bars.Close[firstBarToday] <= 70); bool gap = (bars.Open[firstBarToday] <= trigger[lastBarYesterday]); if (priceFilter && gap && bars.IntradayBarNumber(idx)>0) { Transaction t = PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, bars.Low[firstBarToday] + buyStp.AsDouble, "Initial stop Buy"); t.AutoProfitTargetPrice = bars.Close[lastBarYesterday]; //set gap Close price } } } //same bar exits: 3% profit or loss from execution price public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice) { t.AutoProfitTargetPrice = executionPrice * 1.03; t.AutoStopLossPrice = executionPrice * 0.97; } } }
Rename
Your gap percent defaults to 10% which is pretty large for an intraday trading gap overnight. I ran with 10% for a number of different symbols and didn't get any trades. I set the gap to 2% and 1% and got a bunch of trades.
QUOTE:
Your gap percent defaults to 10% which is pretty large for an intraday trading gap overnight... I set the gap to 2% and 1% and got a bunch of trades.
I agree 10% is pretty large. I tried running Daily bars at 1% and still got nothing. I'm not experienced with gap trading, so I don't fully understand the approach here. Perhaps if the stock misses it's earning estimate, there will be a big gap down, the strategy will buy it, then it will recover slowly over time. But I'm just guessing.
But there are more things about this strategy that bother me. Take the line below:
CODE:If you're running Daily bars, you now have
int firstBarToday = idx-bars.IntradayBarNumber(idx);
CODE:That's not the first bar of the day, or am I missing something? Does firstBarToday mean tomorrow's bar since that's when the buying bar is placed? If so, then they should say entryBar instead (or proposedEntryBar).
int firstBarToday = idx-(-1); //or ... int firstBarToday = idx+1;
My other concern is that I've never use the Transaction.AutoProfitTargetPrice property before. Nor have I used the AssignAutoStopTargetPrices() method before. I don't know if I'm using them correctly.
Don't run it on Daily data. Clearly it's programmed for intraday charts. It would be pretty easy to modify for daily though.
How could it be modified to run with either Daily or Intraday data?
CODE:
if (bars.IsIntraday) { // the strategy as written } else { // do something else }
Ha, ha. I thought you were going to post a solution. I know nothing about gap trading. Check my attempt below and make any corrections.
CODE:Also, in the above code, is the following line below always "true" and what is it's purpose? Could this strategy be trying to Buy and Sell multiple times on the same bar?
else { int firstBarToday = bars.IsIntraday ? idx-bars.IntradayBarNumber(idx) : idx; if (p == null || !p.IsOpen && p.EntryBar < firstBarToday) { //buy conditions below int lastBarYesterday = firstBarToday-1; bool priceFilter = (bars.Close[firstBarToday] >= 20) && (bars.Close[firstBarToday] <= 70); bool gap = (bars.Open[firstBarToday] <= trigger[lastBarYesterday]); if (priceFilter && gap && bars.IsIntraday ? bars.IntradayBarNumber(idx) > 0 : true) { Transaction t = PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, bars.Low[firstBarToday] + buyStp.AsDouble, "Initial stop Buy"); t.AutoProfitTargetPrice = bars.Close[lastBarYesterday]; //set gap Close price } } }
CODE:Is there any other code that needs to be modified to make this strategy trade with Daily bars?
!p.IsOpen && p.EntryBar < firstBarToday
Your Response
Post
Edit Post
Login is required