- ago
Hi.
WL6 have a interesant utility in code like “FirstActualBar” to make sure that don't enter trades on the symbol before its actual history began.
I’ve search in WL quickRef equivalences and don’t exist the equivalence to WL7.
How Can i do the same funtion that FirstActual bar does in WL6 on WL7?
Thanks in advance
0
1,069
9 Replies

Reply

Bookmark

Sort
- ago
#1
Hi,

If you post an example of a real world task which required FirstActualBar, we could show how to translate it to WL7?
0
- ago
#2
When you go through a group of symbols in a dataset, the idea is not to get into a symbol that does not have enough bars.

WL6 Code:

CODE:
for (int bar = 10; bar < Bars.Count; bar++)          {             foreach (string symbol in DataSetSymbols)             {                SetContext(symbol, true);                if (Bars.FirstActualBar < bar)                {                }                RestoreContext();             }          }


WL7 Code (Something like this)

CODE:
for (int bar = 10; bar < bars.Count; bar++)          {             foreach (BarHistory symbol in BacktestData)             {                ------->    if (" the first real bar of the secondary series(Equivalent WL6 FirstActualBar) " < bar) <-------                            {              //do something..........                            }                             }          }

0
- ago
#3
That's a textbook sample. Let's start by saying that WL7 doesn't have the bars loop anymore. Do you have a real world case?
0
- ago
#4
Yes, this rotational from:
https://indexswingtrader.blogspot.com/2016/04/introducing-protective-asset-allocation.html
Line 45 of code:

CODE:
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; namespace WealthLab.Strategies {       public class FSF_RotationJuan : WealthScript, IComparer<MOMHolder>          {           StrategyParameter numberOfSymbols;       StrategyParameter SMA_Period;       StrategyParameter a1;       public FSF_RotationJuan()       {          numberOfSymbols = CreateParameter("n Symbols", 6, 1, 12, 1);          SMA_Period = CreateParameter("SMA Period", 10, 3, 18, 1);          a1 = CreateParameter("ProtectionFactor", 0, 0, 2, 1);       }                    protected override void Execute()       {          ClearDebug();          ClearExternalSymbols();          int number = numberOfSymbols.ValueInt;          int a = a1.ValueInt;          int Period = SMA_Period.ValueInt;                int N =12; //Numero activos sin IEF              //Execute rotation strategy          List<MOMHolder> list = new List<MOMHolder>();          for(int bar = Period; bar < Bars.Count; bar++)          {             //Identify the symbols with the highest MOM             list.Clear();             int SMApos=0;             foreach(string symbol in DataSetSymbols)             { //HACE UNA LISTA MOMHolder CON LOS ETF "NO IEF"                if (symbol !="IEF")                {                   SetContext(symbol, true);                   if (Bars.FirstActualBar < bar)                   {                      MOMHolder holder = new MOMHolder();                      holder.symbol = symbol;                      holder.mom = Close[bar]/SMA.Series(Bars.Close, Period)[bar]-1;                      if (holder.mom>0) SMApos++;                      list.Add(holder);                      PrintDebug (Date[bar]+" "+holder.symbol +" "+holder.mom );                   }                   RestoreContext();                }             }             list.Sort(this);             list.Reverse(); // Reverse sorting order to select highest MOM symbols                          double BF =100*(N-SMApos)/(N-a*N/4);             BF = Math.Min(100,BF); // PORCENTAJE DE COMPRA IEF             double porcent_stocks = 100-BF;                          PrintDebug (Date[bar]+" SMApos = "+SMApos +" BF = "+BF);             //keep top number (1 is initial default) only             int c = list.Count;             int Top = Math.Min(SMApos,number); // COMO MAXIMO COMPRA 6 ETFS "NO IEF"             double porcent_stock = porcent_stocks/Top; //PORCENTAJE DE COMPRA DE CADA ETF "NO IEF"             //Close ALL positions             for(int pos = ActivePositions.Count - 1; pos >= 0; pos--)             {                Position p = ActivePositions[pos];                   SellAtMarket(bar+1, p);                          }                 // QUITA LAS POSICIONES NO TOP DE LA LISTA             for(int i = c - 1; i >= Top; i--)                list.RemoveAt(i);                          //Buy new positions             foreach(MOMHolder holder in list)             {                   SetContext(holder.symbol, true);                             // COMPRA LOS ETF NO IEF SI porcent_stock>0                if ((porcent_stock>0)&& ( BuyAtMarket(bar + 1) != null))                   {                      LastPosition.Tag =porcent_stock ;                      PrintDebug (Date[bar]+" "+holder.symbol +" porcent_stock = "+porcent_stock );                      }                   RestoreContext();                             }             // SI EL PORCENTAJE DE BF ES MAYOR DEL 5% ENTRA EN IEF (LA COMPRA MAS PEQUEÑA DE IEF ES 8% -> PARA a=0 y n=11             if (BF>5)             {                SetContext("IEF", true);                if ( BuyAtMarket(bar + 1) != null)                {                   LastPosition.Tag =BF ;                   PrintDebug (Date[bar]+" IEF porcent_stock = "+BF );                }                RestoreContext();             }                       }                 }       public int Compare(MOMHolder item1, MOMHolder item2)       {          return item1.mom.CompareTo(item2.mom);       }    }    public class MOMHolder    {       internal string symbol;       internal double mom;    } }
0
- ago
#5
If you're looking for a C# rotation strategy sample for WL7 and want to apply some customizations, check out the built-in "Tactical Asset Rotation" strategy (code-based).
0
- ago
#6
Thanks Eugene, I cant find c# code(WL7) for rotational, and this is not a rotational you can create with WL7 rotation settings.
0
- ago
#7
You cannot find it by entering part of the name in the Name Filter?
0
- ago
#8
I find the strategy but not the C# code

0
- ago
#9
That's very odd. Anyway here's the code:
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript1 { public class TacticalAssetRotation : UserStrategyBase {       //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)       {          avgROC = (ROC.Series(bars.Close, 60) + ROC.Series(bars.Close, 120) + ROC.Series(bars.Close, 200)) / 3;          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];             bh.UserData = rocVal; //save the current AvgROC value along with the BarHistory instance          }          //sort the participants by AvgROC 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 < 3; n++)          {             if (n >= participants.Count)                break;             buys.Add(participants[n]);          }       }       //execute the strategy rules here, this is executed once for each bar in the backtest history       public override void Execute(BarHistory bars, int 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);          } } } }
0

Reply

Bookmark

Sort