If High > Highest(High,BreakoutPeriod)[1] then
Buy 1 Contract next bar at market;
If Low < Lowest(Low, BreakoutPeriod)[1] then
SellShort 1 Contract next bar at market;
If marketposition <> 1 then
MaxTradeLow = -999999;
If marketposition <> -1 then
MinTradeHigh = 999999;
If marketposition = 1 and L > MaxTradeLow[1] then
MaxTradeLow = L;
If marketposition = -1 and H < MinTradeHigh[1] then
MinTradeHigh = H;
Sell Next Bar at (MaxTradeLow -ATRFactor * AvgTrueRange(ATRPeriod)) stop;
BuyToCover Next Bar at (MinTradeHigh +ATRFactor * AvgTrueRange(ATRPeriod)) stop;
protected override void Initialize(){
CalculateOnBarClose = true;
}
protected override void OnBarUpdate(){
//Enter on a high or low breakout
if(High[0] > MAX(High, BreakoutPeriod)[1])
EnterLong();
if(Low[0] < MIN(Low, BreakoutPeriod)[1])
EnterShort();
//Clear stop data between trades
if(Position.MarketPosition != MarketPosition.Long)
MaxTradeLow = -99999;
if(Position.MarketPosition != MarketPosition.Short)
MinTradeHigh = 99999;
//Stop code for long trades
if(Position.MarketPosition == MarketPosition.Long){
//Find the highest Low since the trade started
if(Low[0] > MaxTradeLow)
MaxTradeLow = Low[0];
SetStopLoss(CalculationMode.Price, MaxTradeLow-ATRFactor * ATR(ATRPeriod)[0]);
//Stop code for short trades
}else if(Position.MarketPosition == MarketPosition.Short){
//Find the lowest High since the trade started
if(High[0] < MinTradeHigh)
MinTradeHigh = High[0];
SetStopLoss(CalculationMode.Price, MinTradeHigh+ATRFactor * ATR(ATRPeriod)[0]);
}
}

Comment