15 minute chart Brent Crude
DEFPARAM PreLoadBars = 0
//No multiple entries in same direction
DEFPARAM FlatAfter = 213000
// The market analysis looks at this bar and the one before to set the high low levels JH
StartTime = 141500
// Variables which can be adapted based on your preferences
PositionSize = 1
AmplitudeMax = 120 //used to set max allowable range in 1st 2 bars
AmplitudeMin = 30 //used to stop entry if range of 1st two are too small
OrderDistance = -4 // used to set buy sell confirm trigger below or above 1st two bar highs and lows
MinDistance = 6 //used to stop entry if bars after 1st two are too near
// We initialize this variable once at the beginning of the trading system.
ONCE StartTradingDay = -1
// The variables which can change during the day are initialized
// at the beginning of each new trading day.
IF (Time <= StartTime AND StartTradingDay <> 0) OR IntradayBarIndex = 0 THEN
BuyLevel = 0
SellLevel = 0
BuyPosition = 0
SellPosition = 0
StartTradingDay = 0
ELSIF Time >= StartTime AND StartTradingDay = 0 THEN
// We store the index of the first bar of the trading day
StartTradingDay = 1
ELSIF StartTradingDay = 1 then
// For each trading day, the highest and lowest price of the instrument
// are recorded every 15 minutes since StartTime
// 4 bars
IF BuyLevel = 0 OR SellLevel = 0 THEN
UpperLevel = Highest[4](High)
LowerLevel = Lowest [4](Low)
// Calculation of the difference between the highest
// and lowest price of the instrument since StartTime
DayDistance = UpperLevel - LowerLevel
// Calculation of the buy and sell levels for the day if the conditions are met
IF DayDistance <= AmplitudeMax THEN
IF SellLevel = 0 AND (Close - LowerLevel) >= MinDistance THEN
SellLevel = LowerLevel + OrderDistance
Else
SellLevel=1
ENDIF
IF BuyLevel = 0 AND (UpperLevel - Close) >= MinDistance THEN
BuyLevel = UpperLevel - OrderDistance
Else
BuyLevel=1
ENDIF
ENDIF
ENDIF
// Creation of buy and sell short orders for the day if the conditions are met
IF SellLevel > 0 AND BuyLevel > 0 and (BuyLevel - SellLevel) >= AmplitudeMin THEN
IF BuyPosition = 0 THEN
IF LongOnMarket THEN
BuyPosition = 1
ELSe
BUY PositionSize CONTRACT AT BuyLevel STOP
Set target pprofit 25
ENDIF
ENDIF
IF SellPosition = 0 THEN
IF ShortOnMarket THEN
SellPosition = 1
ELSe
SELLSHORT PositionSize CONTRACT AT SellLevel STOP
Set target pprofit 25
ENDIF
ENDIF
ENDIF
ENDIF
Brent Crude 15 minute chart
I’m having difficulty with the “Buy xxxx Contract At xxxxx Stop” function. Buying price is set at a fixed Buylevel parameter.
Basically the system is set up so that it buys a position using this function but it also sets a “target pprofit” for the first few bars in case theres a rapid increase to a certain level. On the occasions where this target is met it sells at the profit target but then opens another position on the next bar despite it being higher than the Buylevel parameter, which I do not want it to do.
I’m assuming this is because the Buy at Stop is being renewed every bar in the code but it still executes a trade even the the price is above the buylevel stop that is set.
Is there anyway to stop it doing this? I would like it to take the profit and not enter again until the next signal condition.
An example of this occuring is on 7th April 2020
If the price is above the pending stop order price, the order should open. You can add a condition to test if the actual price is below the buy stop level to prevent an order to open.
Thanks I’ll give that a go.