Hello Can someone please assist me with my code..
I run it on South african 40.. But I want it to open a buy or sell when it breaks the high or low of the 8:55 candle on 5 min chart.
I dont know how to change the code so that when price moves past the high or past the low it opens a order in that direction. So far all i could put together is this code below ehich opens a trade once the candle closes but i want it to place a trade in the candle thats busy breaking high or low not after close.
Please help me change the code to be able to do it.
DEFPARAM CumulateOrders = false
DEFPARAM FLATBefore = 084500 //09:00
DEFPARAM FLATAfter = 210000 //21:00
DEFPARAM PreLoadBars = 2000
ONCE nLots = 15
ONCE MaxPrice = 999999
ONCE MinPrice = 0
starttime = 090000
SET STOP LOSS 150
IF time = starttime THEN
MaxPrice = highest[1](high) //Il massimo/minimo delle ultime 12 barre
MinPrice = lowest[1](low) // (ogni ora = 12 barre da 5 minuti)
ENDIF
//************************************************************************
// trailing stop function
trailingstart = 20 //5 trailing will start @trailinstart points profit
trailingstep = 60 //5 trailing step to move the "stoploss"
//
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//************************************************************************
// LONG
a1 = close > MaxPrice
IF a1 THEN
Buy nLots CONTRACT AT MARKET
MaxPrice = 999999
MinPrice = 0
ENDIF
// SHORT
b1 = close < MinPrice
IF b1 THEN
Sellshort nLots CONTRACT AT MARKET
MaxPrice = 999999
MinPrice = 0
ENDIF
if countofposition = 1 then
buy 1 contract at tradeprice + 50 stop
endif
if countofposition = -1 then
sellshort 1 contract at tradeprice - 50 stop
endif
Replace lines 58 and 66 with pending orders, instead of at-market orders:
Buy nLots CONTRACT AT MaxPrice STOP
Sellshort nLots CONTRACT AT MinPrice STOP
In this case you won’t even need lines 56-57, 59-61, 64-65 and 67-69.
Hi Roberto
Thanks for the help.. Now it buying at the right place but It keeps on placing orders through the day. I just want it to do on 1 trade per day.. Just the 9 O clock trade en when it closes it must do nothing for the rest of the day. Can you help me with this?
Thanks
At line 10 add:
IF OnMarket THEN
MaxPrice = -1
MinPrice = -1
ENDIF
This should do, since no trade will ever enter at a negative price. Next morning, AT STARTTIME new values will be set to start trading again.
You don’t say what time frame you are running this on. In one of your other similar topics you were asking for one pending order to be cancelled if the other one is filled. In the code you have posted here are you aware that if you run it on a say a 15 minute time frame then you will have both orders on the market at the same time and if one is filled the other one will still be on the market until the current fifteen minute bar closes? This means that at volatile times you could open a long and then have it close with a short order fill before the fifteen minute candle closes.
With that SL & TP it’s quite difficult that an order opens and closes on the same bar.
My comments weren’t really regarding TP and SL but I now see that I misread the code and it is actually only placing one pending order at a time and not the two that I thought it was. Totally my mistake for mixing up topics by Lombard974.
Thank you guys for the reply.. The code I put together here was a mix of a few forums.. I am running it on 5 min time frame.. I dont want it to place 2 pending orders anymore just open a trade when 5 min candle breaks the high or low at 8:55. The code above opens a trade but waits for the close of the first 5min candle 9:05. But thats sometimes a little late.. some trades on SAF40 I close manually 9:02 because price moved fast.. So this is what I am aiming for- High low break of 8:55 candle when market opens at 9:00. Then it must place no other trades- So far the code and trailing works well but if i can get it to activate a trade before the 9:05 candle closes the profit potential is allot higher.
I will try the above suggestions.
Thanks