It is clear that it is difficult to have a trading code that could take place for a long time exactly as intended by its designers. On the other hand, this code could well be profitable on certain financial instruments (Crypto, Forex, Shares or Indices) while checking it beforehand on backtest at different timeframes. Example: BTC / USD at TimeFrame = 20min and 2 minutes; EUR / USD at TimeFrame = 100 ticks or 15 30 45 min or 1 hour; DAX, etc. NB: There is the possibility of being able to improve it by the professionals that you are and to share …
DEFPARAM CumulateOrders=False
//CCI strategy with limit threshold for input or output signal
Once Quantite = 1 //(1*PipValue)
Once Interval = 55
Once SeuilCCI = 170 //(OK at: 100,150,'170',180)
indicoteNegatif = CCI[Interval](Close) < -SeuilCCI
indicotePositif = CCI[Interval](Close) > SeuilCCI
indicator1 = CCI[Interval](Close)
indicator2 = Highest[7](CCI[Interval](Close[1]))
indicator3 = Highest[6](CCI[Interval](Close[3]))
c1 = (indicator3 < indicator2) AND (indicator2 < indicator1)
c2 = (indicator3 > indicator2) AND (indicator2 > indicator1)
REM BUY
IF indicoteNegatif AND c1 THEN
BUY Quantite SHARES AT MARKET
ELSIF LongOnMarket AND indicotePositif THEN
EXITSHORT AT MARKET
ENDIF
REM SELL
IF indicotePositif AND c2 THEN
SELLSHORT Quantite SHARES AT MARKET
ELSIF Not LongOnMarket AND indicoteNegatif THEN
EXITSHORT AT MARKET
ENDIF
Thanks…
I modified it by using MTF (1h TF for the setup & entry + 5m TF for the trailing stop):
DEFPARAM CumulateOrders=False
//CCI strategy with limit threshold for input or output signal
timeframe(1h,UpdateOnClose)
//
Once Quantite = 1 //(1*PipValue)
Once Interval = 55
Once SeuilCCI = 170 //(OK at: 100,150,'170',180)
indicoteNegatif = CCI[Interval](Close) < -SeuilCCI
indicotePositif = CCI[Interval](Close) > SeuilCCI
indicator1 = CCI[Interval](Close)
indicator2 = Highest[7](CCI[Interval](Close[1]))
indicator3 = Highest[6](CCI[Interval](Close[3]))
c1 = (indicator3 < indicator2) AND (indicator2 < indicator1)
c2 = (indicator3 > indicator2) AND (indicator2 > indicator1)
REM BUY
IF indicoteNegatif AND c1 THEN
BUY Quantite SHARES AT MARKET
ELSIF LongOnMarket AND indicotePositif THEN
EXITSHORT AT MARKET
ENDIF
REM SELL
IF indicotePositif AND c2 THEN
SELLSHORT Quantite SHARES AT MARKET
ELSIF Not LongOnMarket AND indicoteNegatif THEN
EXITSHORT AT MARKET
ENDIF
//
Timeframe(default)
//
// Nicolas' trailing stop code, slightly modified to:
//
// - acount for minimum distance required by the broker for pending orders
// - start trailing not only from the first bar, but from any later bar
//
IF (BarIndex - TradeIndex) >= 0 THEN //start from the first bar or a later one
trailingstart = 20 //20 trailing will start @trailinstart points profit
trailingstep = 10 //10 trailing step to move the "stoploss"
distance = 10 //10 pips distance from caurrent price (if required by the broker)
//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*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
IF LongOnMarket THEN
IF (close + distance) > newSL THEN
SELL AT newSL STOP
ELSIF (close - distance) < newSL THEN
SELL AT newSL LIMIT
ELSE
SELL AT Market
ENDIF
ELSIF ShortOnmarket THEN
IF (close + distance) < newSL THEN
EXITSHORT AT newSL STOP
ELSIF (close - distance) > newSL THEN
EXITSHORT AT newSL LIMIT
ELSE
EXITSHORT AT Market
ENDIF
ENDIF
ENDIF
ENDIF
//*********************************************************************************
Thank you very much RobertoGozzi
The code worked well with the gains and loss limitation.
NB: The CCI Threshold and TimeFrame parameters should be adjusted for each financial asset.
Here are the best results obtained with the optimal values of the 2 adjustable parameters:
– For BTC/USD ==>
CCI threshold = 170
TimeFrame (1 minutes, UpdateOnClose)
– For EUR/USD ==>
CCI threshold = 140
TimeFrame (3 minutes, UpdateOnClose)
– For GBP/USD ==>
CCI threshold = 170
TimeFrame (10 minutes, UpdateOnClose)
– For USD/CHF ==>
CCI threshold = 100 and 120
TimeFrame (3 minutes, UpdateOnClose)
Even more, it would be necessary to define the values of Stop and follower, as for example:
trailingstart = 150 // 20 trailing will start @trailinstart points profit
trailingstep = 15 // 10 trailing step to move the “stoploss”
distance = 15 // 10 pips distance from caurrent price (if required by the broker)
Thanks…