- ago
Hi,

[Suppose] 2018/5/29 ~ 2021/5/25

the code:

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript9 { public class MyStrategy : UserStrategyBase {       //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)       {          rsi = new RSI(bars.Close, 14);          bars.Cache["RSI"] = rsi;       }       //this is called prior to the Execute loop, determine which symbols have the lowest RSI       public override void PreExecute(DateTime dt, List<BarHistory> participants)       {          //store the symbols' RSI value in their BarHistory instances          foreach (BarHistory bh in participants)          {             RSI rsi = (RSI)bh.Cache["RSI"];             int idx = GetCurrentIndex(bh); //this returns the index of the BarHistory for the bar currently being processed             double rsiVal = rsi[idx];             bh.UserData = rsiVal; //save the current RSI value along with the BarHistory instance          }                    //sort the participants by RSI value (lowest to highest)          participants.Sort((a, b) => a.UserDataAsDouble.CompareTo(b.UserDataAsDouble));          //keep the top 3 symbols          buys.Clear();          for (int n = 0; n < 2; n++)          {             if (n >= participants.Count)                break;             buys.Add(participants[n]);          }          WriteToDebugLog("----------------PreExecute----------------");          WriteToDebugLog(dt.ToShortDateString());          WriteToDebugLog(buys[0].ToString());          WriteToDebugLog(buys[1].ToString());       }       //execute the strategy rules here, this is executed once for each bar in the backtest history       public override void Execute(BarHistory bars, int idx)       {          WriteToDebugLog("----------------Execute----------------");          WriteToDebugLog(bars.GetDate(idx));          bool inBuyList = buys.Contains(bars);          if (!HasOpenPosition(bars, PositionType.Long))          {             //buy logic - buy if it's in the buys list             if (inBuyList)                PlaceTrade(bars, TransactionType.Buy, OrderType.Market);          }          else          {             //sell logic, sell if it's not in the buys list             if (!inBuyList)                PlaceTrade(bars, TransactionType.Sell, OrderType.Market);          }       }       //declare private variables below       private RSI rsi; } }


When PreExecute is over, Execute always loops Why?

Look Deebug Log



Thx.
0
553
1 Replies

Reply

Bookmark

Sort
- ago
#1
QUOTE:
When PreExecute is over, Execute always loops Why?

By design, of course. Be sure to read the sticky post, it's explained there:
https://www.wealth-lab.com/Discussion/Quick-WL6-9-to-WL7-Translation-Guide-5548
0

Reply

Bookmark

Sort