My intention in the code below was to increase the profit target on subsequent trades up to a maximum of 3 trades
but the TradeCount doesn’t update the profit target correctly.
I would be very grateful if someone could point out what I’ve done wrong after many hours of head scratching.
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// The system will cancel all pending orders and close all positions at 0:00. No new ones will be allowed until after the "FLATBEFORE" time.
DEFPARAM FLATBEFORE = 081500
// Cancel all pending orders and close all positions at the "FLATAFTER" time
DEFPARAM FLATAFTER = 161500
TIMEFRAME (5 minutes)
once TradeCount = 0
// Conditions to enter long positions
indicator1 = ExponentialAverage[10](close)
indicator2 = ExponentialAverage[21](close)
indicator3 = ExponentialAverage[50](close)
c1 = (indicator1 > indicator2) AND (indicator2 > indicator3)
c2 = (indicator1[5] > indicator2[5]) AND (indicator2[5] > indicator3[5])
c3 = (indicator1[10] > indicator2[10]) AND (indicator2[10] > indicator3[10])
indicator16 = LinearRegressionSlope[18](close)
c12 = (indicator16 > longLRSmin)
indicator17 = LinearRegressionSlope[5](close)
c13 = (indicator17 > longLRSmax)
IF c1 AND c2 AND c3 AND c12 AND c13 then
BUY 1 PERPOINT AT MARKET
TradeCount = TradeCount+1
ENDIF
// Stops and targets
SET STOP LOSS 50
IF TradeCount = 1 then
SET TARGET PROFIT 10
Endif
IF TradeCount = 2 then
SET TARGET PROFIT 20
Endif
IF TradeCount = 3 then
SET TARGET PROFIT 30
Endif
IF TradeCount = 4 then
TradeCount = 0
Endif
Try replacing line 20 with this one:
IF c1 AND c2 AND c3 AND c12 AND c13 AND Not OnMarket then
because as it is in your code, the line TradeCount = TradeCount+1 is always executed when the conditions are met, no matter whether OnMarket or Not, the sole difference is that when OnMarket no trade is entered owing to DEFPARAM CumulateOrders = False.