So, I have this global variable named accountBalance is declared and initialized to 0.0. This variable will be used to store the dynamic account balance:
double accountBalance = 0.0;
Then I have the OnExecutionUpdate method that gets called whenever there is an execution (a filled order). It updates the accountBalance variable by retrieving the current account balance in USD using Account.Get method with AccountItem.CashValue as the parameter:
protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
// Update the dynamic account balance on each execution
accountBalance = Account.Get(AccountItem.CashValue, Currency.UsDollar);
}
And finally, the OnBarUpdate method that gets called on each incoming bar. It checks if the script is running on the primary instrument (BarsInProgress != 0) and if there is at least one bar of historical data (CurrentBars[0] < 1). It calculates the remaining account balance by subtracting the total position size from the dynamic account balance. It calculates the dollar amount to risk per trade based on the remaining account balance and the desired risk percentage. It calculates the position size in contracts based on the calculated risk and the instrument's point value. It then defines conditions (longCondition and shortCondition) based on a simple trading logic using the Alligator indicator and the current bar's close price. If the conditions are met, it enters a long or short position with the calculated position size:
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set your desired risk percentage
double riskPerTradePercentage = 2.0;
// Get all open positions
Position[] openPositions = Positions.ToArray();
// Get the total position size (sum of all open positions)
double totalPositionSize = openPositions.Sum(position => position.Quantity * position.Instrument.MasterInstrument.PointValue);
// Calculate the remaining account balance after considering open positions
double remainingAccountBalance = accountBalance - totalPositionSize;
// Calculate the dollar amount to risk per trade
double riskPerTrade = remainingAccountBalance * (riskPerTradePercentage / 100.0);
// Calculate the position size in contracts based on risk and Point Value
double positionSize = riskPerTrade / Instrument.MasterInstrument.PointValue;
// Set 1 - Long condition
bool longCondition = WisemanAlligator1.Lips[0] >= WisemanAlligator1.Teeth[0] &&
WisemanAlligator1.Teeth[0] >= WisemanAlligator1.Jaw[0] &&
Close[0] >= WisemanAlligator1.Jaw[0];
// Set 2 - Short condition
bool shortCondition = WisemanAlligator1.Lips[0] <= WisemanAlligator1.Teeth[0] &&
WisemanAlligator1.Teeth[0] <= WisemanAlligator1.Jaw[0] &&
Close[0] <= WisemanAlligator1.Jaw[0];
// Enter Long
if (longCondition)
{
EnterLong((int)positionSize, "Long");
}
// Enter Short
else if (shortCondition)
{
EnterShort((int)positionSize, "Short");
}
}
My question is then, why when I run a backtest it always position sizes a different amount:

Comment