I saw an interesting strategy. I need help coding it, looked really promising.
Base numbers: Stock ABC is at 100 (attached image for a clearer view)
- Buy 1 lot when rsi 3 crosses above 70 (follow momentum)
- Set SL: x points (example 10 points)
- Set TP y points (example 20 points)
- If TP is hit, we open lot #2 and move both SL to 5 points below lot #2 entry (we opened the buy in ABC at 100, moved to our TP at 120, we open lot #2 at 120, SL for both lots are at 115)
- If SL is hit, exit all positions
- If new TP at 140 is hit, open lot #3, set SL for all 3 lots at 5 points below lot #3 entry.
- Repeat until SL is hit.
- Reverse the rules for shorts.
hoping for Roberto to save the day 😀
Here’s some code to get started. Courtesy of chatpgt –
// Initial setup
DEFPARAM CumulateOrders = False // Each order is treated separately
DEFPARAM FlatAfter = 235900 // Close all positions at the end of the day
// Parameters
Lot1EntryPrice = 100
SLPoints = 10
TPPoints = 20
SLAdjustment = 5
LotSize = 1 // Size of each lot
// RSI calculation
RSI3 = RSI[3](close)
// Conditions
BuyCondition = RSI3 crosses over 70
SellCondition = RSI3 crosses under 30
// State variables to keep track of lots
NumLots = 0
TotalEntryPrice = 0
// Buying Logic
IF BuyCondition AND NumLots = 0 THEN
BUY LotSize CONTRACT AT MARKET
SetStop = Lot1EntryPrice - SLPoints
SetTarget = Lot1EntryPrice + TPPoints
NumLots = 1
TotalEntryPrice = Lot1EntryPrice
ENDIF
IF NumLots > 0 AND close >= TotalEntryPrice + (TPPoints * NumLots) THEN
BUY LotSize CONTRACT AT MARKET
NumLots = NumLots + 1
TotalEntryPrice = TotalEntryPrice + close
SetStop = close - SLAdjustment
ENDIF
// Selling Logic
IF SellCondition AND NumLots = 0 THEN
SELLSHORT LotSize CONTRACT AT MARKET
SetStop = Lot1EntryPrice + SLPoints
SetTarget = Lot1EntryPrice - TPPoints
NumLots = 1
TotalEntryPrice = Lot1EntryPrice
ENDIF
IF NumLots > 0 AND close <= TotalEntryPrice - (TPPoints * NumLots) THEN
SELLSHORT LotSize CONTRACT AT MARKET
NumLots = NumLots + 1
TotalEntryPrice = TotalEntryPrice + close
SetStop = close + SLAdjustment
ENDIF
// Exiting Logic
IF (LongOnMarket AND close <= SetStop) OR (ShortOnMarket AND close >= SetStop) THEN
SELL AT MARKET
exitSHORT AT MARKET
NumLots = 0
TotalEntryPrice = 0
ENDIF
My eye fell on this :
NumLots = 0
TotalEntryPrice = 0
And with this it may have a chance :
Once NumLots = 0
Once TotalEntryPrice = 0
🙂