Hi,
My code has been working as expected; yesterday trading the Dow on 10min candle, my conditions triggered and the system opened a trade on the 3pm candle open. However, the price spiked. The opening price of the candle was say 29,000 but in 1 second it spiked 50 points and opened a position for me at 29,050. Consequently it fell, stopped me out and I lost money.
IG have said I should use LIMIT orders and at the minute I am using at market.
So how do I introduce this LIMIT order into my code, to say, if the price is 5 points (as an example) away from the opening candle, do not open a trade. Same for the sell side.
Thanks
defparam cumulateorders=false
// --- settings
size = 1 //size of orders
maxDayOrder = 1 //max orders per day
startBreakeven = 11 //how much pips/points in gain to activate the breakeven function?
PointsToKeep = 6 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
TakeProfit = 30 //takeprofit in points/pips
StopLoss = 45 //stoploss in points/pips
// --- end of settings
// Prevents the system from creating new orders to enter the market or increase position size before the specified time
noEntryBeforeTime = 090000
timeEnterBefore = time >= noEntryBeforeTime
// Prevents the system from placing new orders to enter the market or increase position size after the specified time
noEntryAfterTime = 160000
timeEnterAfter = time < noEntryAfterTime
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
//max orders each day
if intradaybarindex=0 or day<>day[1] then
orderscount=0
endif
allowtrading=orderscount<maxDayOrder and timeenterbefore and timeenterafter and not daysforbiddenentry
//indicators
ema5=average[5,1](close)
ema17=average[17,1](close)
ema62=average[62,1](close)
ignored, ignored, ignored, ignored, BuySignal, SellSignal, ignored = CALL "WA Explosion COMBO"[150, 30, 15, 15]
irsi=rsi[7](close)
//orders launch
buysig = ema5>ema17 and ema62<ema5 and BuySignal>=260 and irsi>=83
sellsig = ema5<ema17 and ema62>ema5 and SellSignal>=260 and irsi<=22
if allowtrading then
if buysig then
buy size contract at market
orderscount=orderscount+1
endif
if sellsig then
sellshort size contract at market
orderscount=orderscount+1
endif
endif
set target pprofit takeprofit
set stop ploss stoploss
// --- breakeven function
//reset the breakevenLevel when no trade are on market
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
// --- BUY SIDE ---
//test if the price have moved favourably of "startBreakeven" points already
IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
SELL AT breakevenLevel STOP
ENDIF
// --- end of BUY SIDE ---
// --- SELL SIDE ---
//test if the price have moved favourably of "startBreakeven" points already
IF SHORTONMARKET AND tradeprice(1)-close>=startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)-PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
EXITSHORT AT breakevenLevel STOP
ENDIF
// --- end of SELL SIDE ---
All the answers to most simple coding questions can be found in the manual.
https://www.prorealcode.com/prorealtime-documentation/
LIMIT
Thanks. So to be clear, would this code for long positions:
buy size contract at market
change to:
buy size contract at open +5 LIMIT
for short positions would it change from:
sellshort size contract at market
change to:
sellshort size contract at open -5 LIMIT
Thanks
I’m not sure to perfectly understand what IG is talking about. But, bear in mind that the code is read at the close of the candle and if the condition are fullfilled, then the order is opened at next bar open.
Anyway, we could replace the AT MARKET with LIMIT orders to see how it goes, and to avoid gap, but I think that there will be some rejected orders that could lead to an automatic stop of the strategy, you should try this on demo platform first.
thanks Nicolas.
what does LIMIT actually do then that you think it would stop the strategy and can I specify a “LIMIT distance” ie open a trade if within 5 points of candle open or reject?
I tried replacing AT MARKET with LIMIT but it comes up with an error. Any advice on what I should do here?
In your examples above you use OPEN. OPEN refers to the opening price of the just closed candle so it is likely that your order is sometimes the wrong side of current price for a pending order so it is rejected. Change OPEN to CLOSE.
ok thanks. will give it a try. See if I can avoid that happening again.
I assume for short positions I should put ” at close -2 LIMIT”?
sellshort at close -2 limit
I think will be rejected because limit orders have to be at a better/more favourable price than current price.
sellshort at close limit
Will mean that if the next open is the same as the current close or the market gaps up then your order will be filled at the opening price.
There is the possibility that this order could also be rejected if the broker sets a minimum order distance from price. They do for STOP orders but I’m not certain if they do for LIMIT orders. However as Nicolas suggested just putting the idea live in demo should tell you if this is the case or not.
Thank you both. Will give it a whirl. The previous code worked fine 6 times but on Monday price shot up and filled my order 50 points away…very odd.
Thanks