If you SET your SL when placing orders, the second (short) will override the first one (long). Now think what happens if your long trade is entered, what kind of SL will it cope with?
I suggest that you set you SL the next bar, when you are on market so you know what kind of calculation you have to do (but you must have saved your values before).
Hi
This part of the code is not to add 60 points to my entry but 60 points to my sl. I want it to buy as close as possible to the high of the previous 6 bars (5 min) and sell as close to the low of the last 6 bars. I want the sl to be 60 points below the high for a buy trade and 60 points above the low of the last 6 bars.
if not onmarket and time >= starttime and flag = 0 then
buy Nlots contract at MaxPrice STOP
set stop loss close- Minprice - 60 * pointsize
endif
if not onmarket and time >= starttime and flag = 0 then
sellshort Nlots contract at MinPrice STOP
set stop loss MaxPrice - close + 60 * pointsize
The problem i have is that its not placing trades in real time.. in backtest the code works but in real time it doesnt place any trades.. how can i change the code so that it does place a trade even if price is volatile and already moved and orders are too close to price?
Another thing is it also doesnt keep orders open till its filled,, if not filled in first 5 min it cancels all pending orders.
Please run a backtest and see some days it doesnt place any trades. This is the code i have now.
//-------------------------------------------------------------------------
// Main code : UltimateSAF
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
// Main code : MySystem(8)
//-------------------------------------------------------------------------
DEFPARAM CumulateOrders = false
DEFPARAM FLATBefore = 084500 //09:00
DEFPARAM FLATAfter = 210000 //21:00
DEFPARAM PreLoadBars = 2000
ONCE nLots = 5
ONCE MaxPrice = 999999
ONCE MinPrice = 0
starttime = 090000
SET TARGET PROFIT 120
IF time = starttime THEN
MaxPrice = max(close, highest[6](high)) //Il massimo/minimo delle ultime 12 barre
MinPrice = min(close, lowest[6](low)) // (ogni ora = 12 barre da 5 minuti)
flag=0
ENDIF
if onmarket then
flag = 1
endif
//************************************************************************
// LONG
if not onmarket and time >= starttime and flag = 0 then
buy Nlots contract at MaxPrice STOP
set stop loss close- Minprice - 60 * pointsize
endif
if not onmarket and time >= starttime and flag = 0 then
sellshort Nlots contract at MinPrice STOP
set stop loss MaxPrice - close + 60 * pointsize
MaxPrice = 999999
MinPrice = 0
endif
If you can please adjust code.
Thank you
The problem i have is that its not placing trades in real time.. in backtest the code works but in real time it doesnt place any trades.. how can i change the code so that it does place a trade even if price is volatile and already moved and orders are too close to price?
You can’t!
PRT is just a trading platform. When you place an order it is sent by PRT to your broker. If it is below the minimum distance from the current price that your broker allows for an order on the instrument that you are trading then that order will be rejected by the broker. There is no fix and there is no work around. You have tried to place an order closer than your broker will allow and that is that and the order is rejected.
I suggest that you create a new constant to store the minimum distance required by your broker, then place a BUY/SELLSHORT pending order at a price that at least is equal to, or exceeds, such requirement (my example is 10 pips):
DEFPARAM CumulateOrders = false
DEFPARAM FLATBefore = 084500 //09:00
DEFPARAM FLATAfter = 210000 //21:00
DEFPARAM PreLoadBars = 2000
ONCE nLots = 5
ONCE MaxPrice = 999999
ONCE MinPrice = 0
ONCE starttime = 090000
ONCE Distance = 10 * pipsize
SET TARGET PROFIT 120
IF time = starttime THEN
MaxPrice = max(close, highest[6](high)) //Il massimo/minimo delle ultime 12 barre
MinPrice = min(close, lowest[6](low)) // (ogni ora = 12 barre da 5 minuti)
flag=0
ENDIF
if onmarket then
flag = 1
endif
//************************************************************************
// LONG
if not onmarket and time >= starttime and flag = 0 then
buy Nlots contract at max(close + Distance,MaxPrice) STOP
set stop loss close- Minprice - 60 * pointsize
endif
//************************************************************************
// SHORT
if not onmarket and time >= starttime and flag = 0 then
sellshort Nlots contract at min(close - Distance,MinPrice) STOP
set stop loss MaxPrice - close + 60 * pointsize
endif
Roberto’s suggestion is the same as mine mentioned in this post:
Adjustment in code because combined stops can not be used with pro order
It does however mean that you will sometimes not be entering at the price you think you should be. Their is also the additional problem that on faster time frames – for example in a 1 second time frame that price will likely creep up to your desired buy price and then when it is too close your minimum distance code will start moving your order level further away to ensure orders are not rejected. If you use a slower time frame then you don’t have this issue so much but you increase the risk of both your long and short orders being hit within one bar.