Hello !
I want to sell a long position if at any time after the buy, the stock reaches a price 2% below the low of the candle of entry. I thought about using (0,98*Tradeindex(low)) but that doesn’t work.
Can anyone tell me how to code this ?
Thank you very much !
if not onmarket and (your conditions) then
buy 1 contract at market
sl = low * 0.98
endif
sell at sl stop
Tradeindex is the bar when the trade was opened. Tradeprice is the keyword you should use, instead.
But first of all you have to save the price of the LOW when the trade is opened, like:
IF My_Conditions AND Not Onmarket THEN
BUY 1 ONTRACT AT MARKET
MyLow = LOW * 0.98
ENDIF
Then you can test the exit conditions:
A) comparing the current price each bar
IF LongOnMarket AND Close <= MyLow THEN
SELL AT MARKET
ENDIF
or
B) using a pending order each bar
IF LongOnMarket THEN
SELL AT MyLow STOP
ENDIF
You don’t need to use TRADEPRICE either.
I think we have both got it slightly wrong Robert where the pending orders are concerned as mine only lasts one bar and yours it not placed on the first bar!
These two options might work better:
if not onmarket and (your conditions) then
buy 1 contract at market
sl = low * 0.98
sell at sl stop
endif
if longonmarket then
sell at sl stop
endif
or
if not onmarket and (your conditions) then
buy 1 contract at market
sl = low * 0.98
set stop ploss close - sl
endif
If it is a market with lots of gaps then the last option might not work so well.
Ok it works. Thank you !!