Hello everyone,
I am having problems implementing a Bollinger Strategy.
The following code should buy and sell with the shown condition. But we have trouble with the exits. They do not as they should
defparam cumulateorders = false
startTradingTime = 090500
stopTradingTime = 210000
positionSize = 1
// TRADING HOURS
timeCondition = 0
IF(time >= startTradingTime) AND (time <= stopTradingTime) THEN
timeCondition = 1
ENDIF
AmountOfSigmas = 2
BollingerAmountOfPeriods = 20
BollingerBandsMeanAtCurrentClose = average[BollingerAmountOfPeriods,0](close)
standardDeviation = STD[BollingerAmountOfPeriods](close)
upperBollingerBand = BollingerBandsMeanAtCurrentClose + (AmountOfSigmas * standardDeviation)
lowerBollingerBand = BollingerBandsMeanAtCurrentClose - (AmountOfSigmas * standardDeviation)
AmountOfSigmascloseMean = 0.1
UpperBollingerCloseMean = BollingerBandsMeanAtCurrentClose + AmountOfSigmascloseMean*standardDeviation
LowerBollingerCloseMean = BollingerBandsMeanAtCurrentClose - AmountOfSigmascloseMean*standardDeviation
// Onboard Condition
CurrentValueAboveUpperBollinger = close > upperBollingerBand // BollingerUp[BollingerAmountOfPeriods](close)//upperBollingerBand //BollingerUp[BollingerAmountOfPeriods](close)
CurrentValueBelowLowerBollinger = close < lowerBollingerBand //BollingerDown[BollingerAmountOfPeriods](close)//lowerBollingerBand// BollingerDown[BollingerAmountOfPeriods](close) //
BuyShortCondition = CurrentValueAboveUpperBollinger AND timeCondition
BuyLongCondition = CurrentValueBelowLowerBollinger AND timeCondition
// EXIT CONDITION
sellLongCondition = close >= LowerBollingerCloseMean
sellShortCondition = close <= UpperBollingerCloseMean
sellstoplong = 40
sellstopshort = 40
IF BuyLongCondition AND NOT LONGONMARKET THEN
BUY positionSize PERPOINT AT MARKET
//SET STOP PLOSS sellstoplong
SET TARGET PROFIT (BollingerBandsMeanAtCurrentClose +AmountOfSigmascloseMean*standardDeviation)*pipsize // POINT WHERE IT SHOULD SELL
ENDIF
IF BuyShortCondition AND NOT SHORTONMARKET THEN
SELLSHORT positionSize PERPOINT AT MARKET
//SET STOP PLOSS sellstopshort
SET TARGET PROFIT (BollingerBandsMeanAtCurrentClose-AmountOfSigmascloseMean*standardDeviation)*pipsize // POINT WHERE IT SHOULD SELL
ENDIF
JSParticipant
Senior
I don’t see exit orders, I do see “Exit Condition” but these are not used in the code…
You go Long with “Buy” but I don’t see “Sell” as “exit order”
You go Short with “SellShort” but I don’t see “ExitShort” as “exit order”
LONG: Buy vs Sell
SHORT: SellShort vs ExitShort
IF sellLongCondition THEN
sell at market
ENDIF
IF sellShortCondition THEN
exitshort at market
ENDIF
Yes you are right, but how would I use a SET TARGET PROFIT with UpperBollingerCloseMean or LowerBollingerCloseMean as example?
JSParticipant
Senior
DefParam CumulateOrders = False
Period = 20
xMean = Average[Period](Close)
xStd = Std[Period](Close)
BBUp = xMean + 2 * xStd
BBDown = xMean - 2 * xStd
If Close Crosses Over BBUp then
SellShort 1 contract at Market
EndIf
If Close Crosses Under BBDown then
Buy 1 contract at Market
EndIf
xMeanUp = xMean + 0.1 * xStd
xMeanDown = xMean - 0.1 * xStd
If LongOnMarket then
Sell at xMeanUp LIMIT
EndIf
If ShortOnMarket then
ExitShort at xMeanDown LIMIT
EndIf
Hi
@taklause
You can use a LIMIT order to exit your positions…