Hi,
I’m coding a strategy that places two orders on the system based on the value of US crude on the first day of the month. Both can trigger but one is cancelled if the other hits target. The problem I’ve got is that I end up with multiple orders if a trade triggers and hits stop within the same candle. I know there is most probably a simple answer but I’m struggling to see it at the moment….
Lisa
// Definition of code parameters
DEFPARAM CumulateOrders = True // Cumulating positions deactivated
once cHitTarget = 1
// Check if this is the first trading day of the month
If OpenDay<=4 and OpenDayOfWeek>=1 and OpenDayOfWeek<=5 and opendate-firstdayofmonth>4 then
firstdayofmonth = opendate
//Calculate entry and exit points, stops and limits
cLongTrade = high + 5
cShortTrade = low + 5
CStop = (high-low)/2
cTarget = high - low
CHitTarget = 0
endif
if (close > cLongTrade + CTarget) OR (close < cShortTrade - cTarget) then
cHitTarget = 1
endif
// Need to stop it taking multiple trades
graph tradeprice(1)
graph cLongtrade
Graph cShortTrade
If not longonmarket AND NOT cHitTarget AND tradeprice(1) <> cLongTrade then
BUY 1 PERPOINT AT cLongTrade STOP
endif
If not shortonmarket AND NOT cHitTarget AND tradeprice(1) <> cShortTrade then
SellShort 1 PERPOINT AT cShortTrade STOP
endif
// Stops and targets
SET STOP pLOSS cStop
SET TARGET pPROFIT cTarget
//SET STOP pTRAILING LBSTOP
You have to save your STRATEGYPROFIT so that when not OnMarket you can check if it’s different from the previous bar. If is is because a trade was triggered and close in ust one bar.
ONCE MyProfit = 0
TradeTriggered = 0
IF Not OnMarket THEN
IF MyProfit <> STRATEGYPROFIT THEN
TradeTriggered = 1
MyProfit = STRATEGYPROFIT
ENDIF
ENDIF
You can then check TradeTriggered to take any action you need to take.
It could be foolished in the very unlikely case a trade is triggered and yields 0.
There is no simple answer. In fact, since the code is only read one time at Close, you cannot interact in the bar to “delete” the other pending order. Bear in mind, that pending orders can’t be canceled, they expire after 1 bar and we need to set them continuously if we want them to still be present.
So in order to not set them if an order has triggered within the current bar, the best way to do is to use a lower timeframe (with the ease of the TIMEFRAME instruction) to make a simple ‘IF ONMARKET’ test.
Thank you both; I’ve come up with this version that now checks to see if the last trade was long or short as well as using the strategy profit 🙂 The only times it doesn’t quite work is if the long and short trades are both triggered and hit stop in the same candle…..
What I think I’ll work on next is to run this on an hourly timeframe so I can check for the trades a bit more accurately and then use the timeframe(daily) to set the levels of the orders and check for the first trading day of the month. Last time I tried using the multiple timeframes I found it worked on my demo account but not on my live account (it wasn’t yet available for live accounts)
// Definition of code parameters
DEFPARAM CumulateOrders = True // Cumulating positions deactivated
once cHitTarget = 1
ONCE MyProfit = 0
graph LongTriggered
Graph ShortTriggered
// Check if this is the first trading day of the month
If OpenDay<=4 and OpenDayOfWeek>=1 and OpenDayOfWeek<=5 and opendate-firstdayofmonth>4 then
firstdayofmonth = opendate
//Calculate entry and exit points, stops and limits
cLongTrade = high + 5
cShortTrade = low + 5
CStop = (high-low)/2
cTarget = high - low
CHitTarget = 0
LongTriggered = 0
ShortTriggered = 0
endif
if (close > cLongTrade + CTarget) OR (close < cShortTrade - cTarget) then // Check to see if the target has been hit (long or short)
cHitTarget = 1
endif
IF Not OnMarket THEN
IF MyProfit < STRATEGYPROFIT THEN // Just had a winning trade
IF tradeprice(1) > tradeprice(2) then
LongTriggered = 1
else
ShortTriggered = 1
ENDIF
MyProfit = STRATEGYPROFIT
ENDIF
IF MyProfit > STRATEGYPROFIT THEN // Just had a losing trade
IF tradeprice(1) < tradeprice(2) then
LongTriggered = 1
else
ShortTriggered = 1
ENDIF
MyProfit = STRATEGYPROFIT
ENDIF
ENDIF
// Need to stop it taking multiple trades
If not longonmarket AND NOT cHitTarget AND NOT LongTriggered then
BUY 1 PERPOINT AT cLongTrade STOP
endif
If not shortonmarket AND NOT cHitTarget AND NOT ShortTriggered then
SellShort 1 PERPOINT AT cShortTrade STOP
endif
// Stops and targets
SET STOP pLOSS cStop
SET TARGET pPROFIT cTarget
//SET STOP pTRAILING LBSTOP
I’ve not looked closely at your code but why not split the code into two strategies – a long one and a short one?
I think if you request to have MTF turned on on your live account then it can be done – but do bear in mind that it is still in beta testing which is why everyone does not have it yet!
I did think of that but I need to know if either trade has hit target and then stop the order for the other trade being placed…. the basic rules are :
Oil Trades
Daily Chart
First trading day of the month (Mon-Fri)
Find the low and high of the day and calculate the daily range
Stop = ½ range
Target = range
Long order 5 pips above the high
Short order 5 pips below the low
Cancel 2ndorder when profit is hit on the first order.
I’ll continue with the code and when I’m happy that it’s working on demo I’ll ask to have MTF switched on 🙂