//-------------------------------------------------------------------------
// Main code : H1 Buy and Sell on SA 40
//-------------------------------------------------------------------------
// Definition of code parameters
DEFPARAM CumulateOrders = false // Cumulating positions deactivated
//daysForbiddenEntry = OpenDayOfWeek = 1 //OR OpenDayOfWeek = 0
// Money Management
atr = AverageTrueRange[14](close)
accountbalance = 10000 //Starting acc balance
liveaccountbalance = accountbalance + strategyprofit
lotsize = max(2,round(((liveaccountbalance * 0.1) / (atr * 1.2)) /4))
//====================================================================================//
// Conditions to enter long positions
c1 = (close[3] <= high[4])
c2 = (close[3] >= low[4])
c3 = (close[2] <= high[3])
c4 = (close[2] >= low[3])
c5 = (close[1] <= high[2])
c6 = (close[1] >= low[2])
c2HLong = close >= trend2H
// Work Out the Highs
if high[4] > high[3] then
ath = high[4]
else
ath = high[3]
endif
if high[2] > ath then
ath = high[2]
else
ath = ath
endif
if high[1] > ath then
ath = high[1]
else
ath = ath
endif
c7 = (close > ath)
buycondition = c1 AND c2 AND c3 AND c4 AND c5 AND c6 and c7 and c2hlong //and c10 //and c9
IF buycondition then//and not daysForbiddenEntry THEN
buy lotsize CONTRACT AT MARKET
set stop ploss 270
ENDIF
//====================================================================================//
// Conditions to enter short positions
ARDOWN = AroonDown[14]
ARUP = AroonUp[14]
c1 = (ardown CROSSES OVER arup)
timeframe (2H)
trend2H = supertrend[7.4,17]
c2HShort = close <= trend2H
timeframe (default)
IF c1 and c2HShort then //and not daysForbiddenEntry then //and c2 and c3 and c4 THEN
SELLSHORT lotsize CONTRACT AT MARKET
SET STOP pLOSS 225
ENDIF
//====================================================================================//
//trailing stop function
trailingstart = 70 //trailing will start @trailinstart points profit
trailingstep = 60 //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*4 THEN
newSL = tradeprice(1)+trailingstep*4
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*4 THEN
newSL = newSL+trailingstep*4
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*4 THEN
newSL = tradeprice(1)-trailingstep*4
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*4 THEN
newSL = newSL-trailingstep*4
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//************************************************************************
GRAPH newSL as "trailing"