// 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