- ago
Hi, Guys! Really need your help, I'm trying to implement a simple rotation strategy. I looked at a few forum posts and tried to make my own:
A rotation strategy based on return for a certain interval, rebalancing by the day of the week, I also interested in whether it is more profitable to enter at closing prices or using market orders.
I'm confused by the gaps in the trades...I will be glad to receive any criticism, it is quite possible that since this is one of the first strategies on WL8, I did not take something into account...

CODE:
public class Rotate : UserStrategyBase { public Rotate() : base() { AddParameter("ROC Period", ParameterType.Int32, 1, 1, 100, 1); AddParameter("Close Open", ParameterType.Int32, 0, 0, 1, 1); AddParameter("Day of Week", ParameterType.Int32, 1, 1, 5, 1); } //declare private variables below private TimeSeries avgROC; private string seriesKey = "Average ROC"; //the list of symbols that we should buy each bar private static List<BarHistory> buys = new List<BarHistory>(); //create the weight indicator and stash it into the BarHistory object for reference in PreExecute public override void Initialize(BarHistory bars) { StartIndex = Parameters[0].AsInt; avgROC = ROC.Series(bars.Close, Parameters[0].AsInt); bars.Cache[seriesKey] = avgROC; } //this is called prior to the Execute loop, determine which symbols have the lowest average ROC public override void PreExecute(DateTime dt, List<BarHistory> participants) { //store the symbols' AvgROC value in their BarHistory instances foreach (BarHistory bh in participants) { TimeSeries symbolRoc = (TimeSeries)bh.Cache[seriesKey]; int idx = GetCurrentIndex(bh); //this returns the index of the BarHistory for the bar currently being processed double rocVal = symbolRoc[idx]; //if the indicator isn't valid set a high value to avoid selection in rotation if (idx < symbolRoc.FirstValidIndex) rocVal = 1.0e6; bh.UserData = rocVal; //save the current AvgROC value along with the BarHistory instance } //sort the participants by ROC value (lowest to highest) participants.Sort((a, b) => a.UserDataAsDouble.CompareTo(b.UserDataAsDouble)); //keep the top 1 symbol buys.Clear(); buys.Add(participants[0]); } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { // rebalance in certain days if ((int)bars.DateTimes[idx].DayOfWeek == Parameters[2].AsInt) { bool inBuyList = buys.Contains(bars); // first check if any position if (HasOpenPosition(bars, PositionType.Long)) { if (!inBuyList) { if (Parameters[1].AsInt == 0) { PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose); } else { PlaceTrade(bars, TransactionType.Sell, OrderType.Market); } } } // if (!HasOpenPosition(bars, PositionType.Long)) { if (inBuyList) { if (Parameters[1].AsInt == 0) { PlaceTrade(bars, TransactionType.Buy, OrderType.MarketClose); } else { PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } } } } } }

1
321
Solved
1 Replies

Reply

Bookmark

Sort
Cone8
 ( 4.98% )
- ago
#1
You're probably using 100% equity sizing without any margin?
Bump up the margin to 1.1 or reduce your sizing to 90% and try again. (And then look for information about Basis price in the Help F1 > Strategy > Strategy Settings > Basis Price).
0
Best Answer

Reply

Bookmark

Sort