Step 2a: Goal: simulate actual margin and/or reinvestment
Yes rolling WFO; Yes oversample; Yes maxtrades; Yes all param optimizing; Yes modifier(s); Yes emode; Yes reinvest

In this step, I will use Zorro's OptimalF feature to tell me mathematically how great this tradebot has become (or perhaps how much it sucks!). Either way, OptimalF will slap a numeric rating on the performance, and I can use that as a guide for determining how much of my trading stake I want to risk.

So before anything else is done, I need to let Zorro calculate the OptF for this strategy by adding the FACTORS flag like below. I'll be training initially withOUT any reinvest or margin settings:
Code:
set(PARAMETERS+FACTORS);


The purpose of this step is to see how Zorro would perform if it had access to a specific sum of risk capital in my account. Up until this point, we have designed the tradebot using only flat lots (Lots = 1). This gave us an objective view of how well the trading logic itself could perform. Also, it did not answer the question of "how much" should be risked on each trade or asset, which is specifically what OptimalF gives you (the optimal fraction to place at risk).

I should note that in this section, I use the terminology "REinvest" but in fact I am actually not currently applying any reinvestment compounding. That is a somewhat complicated proposition which has been discussed elsewhere in this forum. The code section below that uses the flag "reinvestprofits" has a switch to enable it, but the formula does not provide any compounding effect (but this is the section where such a formula could live). My decision not to apply a compounding effect (at least for now) is based on some discussions in the forum where we've basically determined that the purpose of compounding is for long-running tradebots. The rate of compounding would be somewhat negligible on a small account. The growth of the account should come as a result of the tradebot performance, not as a result of compounding.

The following code section lives just above the "edge trading logic" section:
Code:
//reinvest a portion of profits
	int reinvestprofits = 0; //invoke margin setting during trade logic
	Margin = 0; //default
	var MarginLong = 0; //default
	var MarginShort = 0; //default
	if (reinvestprofits)
	{
		Capital = 1000; //simulated account balance
		var riskCapital = 900; //basis to trade with
		if (OptimalF>.001) //profitable as compared to other assets
		{
			MarginLong = OptimalFLong * riskCapital;
			MarginShort = OptimalFShort * riskCapital;
		}
	}



And then just before our trade entry commands, I place this code:
Code:
if (reinvestprofits && MarginLong>0) Margin = MarginLong; else if(is(TRADEMODE)) Lots = -1;


Code:
if (reinvestprofits && MarginShort>0) Margin = MarginShort; else if(is(TRADEMODE)) Lots = -1;



A couple comments before proceeding further:
Capital above is used in the Zorro simulation only. It allows me to see the "what if" of how my real account would perform, had I turned Zorro loose on it. In a live trading account, your live balance would be the equivalent. See picture for further explanation on this.

riskCapital is the amount of funds that Zorro has to work with. This figure needs to be adjusted to be mindful of Zorro's "Capital required" figure, which is given in the Performance Report. Therefore, you can provide a "cushion" for drawdown by setting the riskCapital less than the Capital. However, for now just play around with different figures so you can see how changes affect the end result. A final adjustment for live trading is made in the next Step (Step 3).

You'll see in my code that I always refer to OptimalF using a comparison such as >.001. The reason I do this is because of precision truncation behind the scenes. For example, even though Zorro's Performance Report may show a component with a value of .000 it could actually be .0000046 and I want to ensure that is treated as a zero.

In the code before trade entry commands... the reason for the "else if(is(TRADEMODE))" section is that-- I found that in live trading, if OptimalF was .000, then Zorro would use the default Margin setting of 0, which results in a 1 Lot actual trade. So for live trading purposes, I added this to force those trades to be phantom only.

So now that we've trained with the FACTORS flag, we can now set reinvestprofits = 1; and then press Test. This will produce a simulation of how the tradebot performs in live trading, taking into account the balance, and utilizing OptimalF to weight trades (short or long) that historically perform best. Can you see where this is going? Imagine using this power on an entire basket of trading instruments? Each instrument, each trade direction, each trade algorithm -- has an optimal fraction calculation. It's way too much math for a human discretionary trader to keep up with, but really a snap for Zorro...

Look at the before & after differences... in the "before" picture, we are seeing the raw trade logic, without any enhancements that OptimalF brings to that table (namely, the weightings... the better an asset+direction+algo performs, the more equity it should be allowed to trade with). By treating all trades the same, you get an overall much sloppier performance.

BEFORE:


In the "after" picture, we can see significant improvements in the way this bot trades. The equity curve is smoother, even when flat. Spikes and other sloppy trades are reduced. Overall drawdown severity is reduced. And known good performance is rewarded by OptimalF.

AFTER:


Coming next... Step 3: Goal: identify best assets