Someone on X/Twitter has shared a good strategy, I am trying to implement it but there are errors generated.
I’ve asked AI to convert it to code/txt from the image they shared. The strategy looks quite good, they’ve documented it in the post.
https://x.com/AlgomaticTrade/status/1893249375758893545
can anyone help / figure out why it’s not working?
Here’s the converted code:
defparam preLoadBars = 10000
defparam cumulateOrders = false
atrPeriods = 39 // ATR calculation period
// Variables
trueRange = Max(High - Low, Max(Abs(High - Close[1]), Abs(Low - Close[1])))
atr = Average[atrPeriods](trueRange) // ATR based on True Range
// Dynamic Position Sizing
// User Inputs
riskPercent = 1 // Risk percentage for position sizing
portfolioSize = 20000 // Default portfolio size
// ATR calculation period
contractsToTrade = 1 // Number of contracts to trade
// Position Sizing Calculation
positionEquity = Max(PortfolioEquity, portfolioSize) // Use real equity or default portfolio size
contractsToTrade = (riskPercent / 100 * positionEquity) / (atr * PointValue)
// Indicators
bodyRatio = abs(close-open) / (high-low)
// Conditions
cl = summation[3](close-open)=3
cl = cl and highest[3](bodyRatio) >= 0.7
clk = summation[3](close-open)=-3
// Action
if cl then
buy contractsToTrade contract at market
endif
if clk then
sell contractsToTrade contract at market
endif
JSParticipant
Senior
• I have adjusted the conditions, can you see if you mean this…
• // Checks if the closing price has been higher than the opening price for the last 3 candles
• C1 = SUMMATION[3](close > open) = 3
• // Checks if the highest BodyRatio in the last 3 candles is at least 0.7
• C2 = HIGHEST[3](ABS(close – open) / (high – low)) >= 0.7
• // Long signal when both conditions are met
• LongSignal = C1 AND C2
• // Sell signal when the closing price has been lower than the opening price for the last 3 candles
• SellSignal = SUMMATION[3](close < open) = 3
• // Execution of buy and sell orders
• IF LongSignal THEN
• BUY contractsToTrade CONTRACT AT MARKET
• ENDIF
• IF SellSignal THEN
• SELL contractsToTrade CONTRACT AT MARKET
• ENDIF