US Crude – 1st Day of month
Forums › ProRealTime English forum › ProOrder support › US Crude – 1st Day of month
- This topic has 5 replies, 4 voices, and was last updated 5 years ago by Lisa Beaney.
-
-
01/29/2019 at 7:22 PM #89977
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
US Crude daily chart123456789101112131415161718192021222324252627282930313233343536373839404142434445464748// Definition of code parametersDEFPARAM CumulateOrders = True // Cumulating positions deactivatedonce cHitTarget = 1// Check if this is the first trading day of the monthIf OpenDay<=4 and OpenDayOfWeek>=1 and OpenDayOfWeek<=5 and opendate-firstdayofmonth>4 thenfirstdayofmonth = opendate//Calculate entry and exit points, stops and limitscLongTrade = high + 5cShortTrade = low + 5CStop = (high-low)/2cTarget = high - lowCHitTarget = 0endifif (close > cLongTrade + CTarget) OR (close < cShortTrade - cTarget) thencHitTarget = 1endif// Need to stop it taking multiple tradesgraph tradeprice(1)graph cLongtradeGraph cShortTradeIf not longonmarket AND NOT cHitTarget AND tradeprice(1) <> cLongTrade thenBUY 1 PERPOINT AT cLongTrade STOPendifIf not shortonmarket AND NOT cHitTarget AND tradeprice(1) <> cShortTrade thenSellShort 1 PERPOINT AT cShortTrade STOPendif// Stops and targetsSET STOP pLOSS cStopSET TARGET pPROFIT cTarget//SET STOP pTRAILING LBSTOP01/29/2019 at 8:10 PM #89979You 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.
12345678ONCE MyProfit = 0TradeTriggered = 0IF Not OnMarket THENIF MyProfit <> STRATEGYPROFIT THENTradeTriggered = 1MyProfit = STRATEGYPROFITENDIFENDIFYou 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.
1 user thanked author for this post.
01/30/2019 at 9:18 AM #90004There 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.
1 user thanked author for this post.
01/30/2019 at 11:04 AM #90017Thank 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)
US Crude First Day of Month Trades12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970// Definition of code parametersDEFPARAM CumulateOrders = True // Cumulating positions deactivatedonce cHitTarget = 1ONCE MyProfit = 0graph LongTriggeredGraph ShortTriggered// Check if this is the first trading day of the monthIf OpenDay<=4 and OpenDayOfWeek>=1 and OpenDayOfWeek<=5 and opendate-firstdayofmonth>4 thenfirstdayofmonth = opendate//Calculate entry and exit points, stops and limitscLongTrade = high + 5cShortTrade = low + 5CStop = (high-low)/2cTarget = high - lowCHitTarget = 0LongTriggered = 0ShortTriggered = 0endifif (close > cLongTrade + CTarget) OR (close < cShortTrade - cTarget) then // Check to see if the target has been hit (long or short)cHitTarget = 1endifIF Not OnMarket THENIF MyProfit < STRATEGYPROFIT THEN // Just had a winning tradeIF tradeprice(1) > tradeprice(2) thenLongTriggered = 1elseShortTriggered = 1ENDIFMyProfit = STRATEGYPROFITENDIFIF MyProfit > STRATEGYPROFIT THEN // Just had a losing tradeIF tradeprice(1) < tradeprice(2) thenLongTriggered = 1elseShortTriggered = 1ENDIFMyProfit = STRATEGYPROFITENDIFENDIF// Need to stop it taking multiple tradesIf not longonmarket AND NOT cHitTarget AND NOT LongTriggered thenBUY 1 PERPOINT AT cLongTrade STOPendifIf not shortonmarket AND NOT cHitTarget AND NOT ShortTriggered thenSellShort 1 PERPOINT AT cShortTrade STOPendif// Stops and targetsSET STOP pLOSS cStopSET TARGET pPROFIT cTarget//SET STOP pTRAILING LBSTOP01/30/2019 at 11:32 AM #90020I’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!
01/30/2019 at 12:12 PM #90028I 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 rangeStop = ½ range
Target = rangeLong order 5 pips above the high
Short order 5 pips below the lowCancel 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 🙂
-
AuthorPosts
Find exclusive trading pro-tools on