if close crosses over average[200,0](close) and not Onmarket then
buy at market
endif
//*********************************************************************************
// Trailing Stop by Nicolas
// https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/
// (lines 17- 56)
// modified to use Percentages
//
//trailing stop function
//
StartPerCent = 0.5 //Trailing Stop will activate after 0.5% gain
StepPerCent = 0.1 //Trailing Steps will occur at any further 0.1% gain
//
trailingstart = PositionPrice * StartPerCent / 100 / PipSize
trailingstep = PositionPrice * StepPerCent / 100 / PipSize
//
//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-PositionPrice>=trailingstart*pipsize THEN
newSL = PositionPrice+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = max(newSL,newSL+trailingstep*pipsize)
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND PositionPrice-close>=trailingstart*pipsize THEN
newSL = PositionPrice-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = min(newSL,newSL-trailingstep*pipsize)
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//*********************************************************************************
//graphonprice PositionPrice coloured(0,0,255,255)
//graphonprice newSL coloured(255,0,0,255)
//graph TrailingStart
//graph TrailingStep