PINE SCRIPT V5 STRATEGY

Job ID: 36245216

Budget: €8 – €30 EUR

I need implement a long short Dollar Cost Averaging (DCA) condition in a Pine Script v5 strategy. The idea is create a function that captures the number of contracts of the last closed order and another function that captures the profit of the last order and also define a variable, let's call it qty, which is equal to 1% of the capital.

The condition for the DCA would be as follows: if the last trade closed with a loss, then the amount of the next order should be qty multiplied by a multiplier factor. qty * 1.5 for example.

To capture the number of contracts of the last closed order, we can use the strategy.closedtrades function, which returns an array of closed trades. You can then access the number of contracts for the most recent trade using the entry_size attribute of the last element of the array.

To capture the profit of the last order, we can use the strategy.closedtrades function along with the strategy.currentholdings function. You can calculate the profit by subtracting the entry price from the exit price, and multiplying it by the number of contracts.

Once you have captured the number of contracts and profit of the last order, we can use them to calculate the next order size using the qty variable and the multiplier factor. Finally, we can add la variable qty in long and short orders.

Attached is the strategy code where these conditions need to be added.
Related categories: Pine Script Trading