- ago
Hi,

First of all, im very impressed with WL8 so far. I've ported two strategies over from my homegrown python system and am attempting to test them live. I haven't succeeded yet, but that's only because i'm still new to the platform and its very different from what I'm used to. But i'm close! I have some questions about a simple rebalancing strategy...

For simplicity, let's say I want to rebalance two assets to a specific allocation every month. I am using daily data. I have several questions...

1. Im not using margin in my account, so I had to allocate less than 100% because i'm using market orders and some days, the market may have gapped up. Allocating 99% gives me a little slippage. I suppose I could use limit orders and bake in the slippage so i can allocate 100%. But then i'm just coding everything myself and not taking advantage of the Rebalance function. Does anyone do this?

2. Let's say I use the Rebalance function and it has to sell asset X and buy asset Y. How does it do that? Does it place both orders at the same time? What happens if it can't buy until there's cash from the sell?

3. Specifically, in my code, I only call the rebalance function on the first trading day of the new month. If the buy doesn't happen because the sell diddn't happen, or because the asset to be bought gapped up, is the buy order still on the books or is it cancelled?

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace MyWealthScripts {        public class MyGlobalEightyTwentyReplica : UserStrategyBase    {       // Define symbols and target allocations       Dictionary<string, double> weights = new Dictionary<string, double>();              public override void Initialize(BarHistory bars)       {          StartIndex = 1;          BacktestSettings.RetainNSFPositions = true;          // This adds up to 99% so there is 1% left over to handle gap ups overnight.          // <a href="https://www.wealth-lab.com/Discussion/Missing-trades-from-backtest-when-margin-is-not-used-11300" target="_blank">https://www.wealth-lab.com/Discussion/Missing-trades-from-backtest-when-margin-is-not-used-11300</a>          weights.Add("VT", 79.0);          weights.Add("BNDW", 20.0);       }       public override void Execute(BarHistory bars, int idx)       {          // rebalance if nothing open or at the start of every month          if (LastPosition == null || bars.DateTimes[idx].Month != bars.DateTimes[idx - 1].Month)          {                          try             {                double pct = 0.0;                pct = weights[bars.Symbol];                Rebalance(bars, pct);             }             catch (KeyNotFoundException)             {                WriteToDebugLog($"Error occurred at index {idx}: No allocation for symbol: {bars.Symbol}");             }          }       }    } }


Thanks!
0
79
Solved
1 Replies

Reply

Bookmark

Sort
Cone8
 ( 6.74% )
- ago
#1
Re: 3. Specifically...
Rebalance calculates the share Quantity required for an asset based on Backtester.CurrentEquity and the asset's Closing price. Then it places Buy/Sell Market orders to match that calculated quantity.

If there's a significant gap, you're right, a Market entry order can fail if there is no account margin to provide leeway for the trade. For a backtest, it's a good idea to bump up the margin setting a small amount (e.g., 1.2) for this purpose.

However, as I mentioned in another thread of yours, Rebalance does not sync to a live account value.
0
Best Answer

Reply

Bookmark

Sort