var float start_cash = 1000
var int Percen_count = 0 // loss Count
// Check the last closed trade for loss and update funds
if (strategy.closedtrades > 0)
last_trade_exit_price = strategy.closedtrades.exit_price(strategy.closedtrades-1)
last_trade_entry_price = strategy.closedtrades.entry_price(strategy.closedtrades-1)
// Get the size of the last transaction
last_trade_size = strategy.closedtrades.size(strategy.closedtrades-1)
// Calculate actual profit or loss
trade_profit_loss = (last_trade_exit_price - last_trade_entry_price) * last_trade_size
// Check if there is a loss
is_loss = trade_profit_loss < 0
if is_loss
Percen_count := Percen_count + 1
// If lose 5 times in a row, it will be cleared next time.
if Percen_count == 4
Percen_count := 0
start_cash := 1000
else
start_cash := start_cash * (1 + (0.2 * Percen_count))
else
Percen_count := 0 // If profitable, reset counter
start_cash := 1000 // Restore initial funds
The code(problem fragment) snippet I have in question is as above, TVC index uses MovingAvg2LineCross(10, 5), first backtest started on 2019-08-13.
I need to invest $1,000 at the closing price when a signal appears (eg: long signal), close the long position of $1,000 and sell $1,000 short at the closing price when the signal appears (eg: short signal). If I lose the first time, I will lose the second time. Invest 20% more, such as: (start_cash :=start_cash * (1 + (0.2 * Percen_count))). If you win a certain time or lose 5 times in a row, you will return to 1,000 US dollars and the Percen_count will be cleared.
After I ran this script, I found that the amount invested in the first 11 times of , the direction of the signal, and the profit and loss situation were as follows:
[998 Long win
999 Short win
998 Long win
998 Short win
998 Long loss
998 Short loss
999 Long loss
1679 Short Loss
999 Long loss
1199 Short win
1679 Long win
]
Each loss does not perform the operation as expected start_cash := start_cash * (1 + (0.2 * Percen_count)), such as (the fifth transaction to the ninth transaction), and the 10th transaction (having lost 5 times in a row before) to The 11th transaction (the 10th transaction that was profitable) should have initial funds of 1,000 US dollars, but they were not set to 1,000 US dollars.
jacky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.