hi,
can someone help me out with this? i want to close half my position when the market has reach have my limit and then move stoploss to breakeven.
ONCE PerCent = 0.50 //0.50 = close half positions
IF positionperf = 0.5 AND LongOnMarket THEN
SELL (abs(CountOfPosition) * PerCent) AT Market
set stop ploss 0
ELSIF positionperf = 0.5 AND ShortOnMarket THEN
EXITSHORT (abs(CountOfPosition) * PerCent) AT Market
set stop ploss 0
ENDIF
here’s what i got so far
thank you
You cannot expect POSITIONPERF to equal 0.5% exactly. Change it to >=
Also SET STOP pLOSS 0 does not set any stop loss – it simply cancels any stop loss on the market. Use SELL or EXITSHORT AT POSITIONPRICE STOP and place this order at every bar.
Something like: (not tested)
ONCE PerCent = 0.50 //0.50 = close half positions
if not onmarket and (your long entry conditions) then
buy 1 contract at market
flag = 0
endif
if not onmarket and (your short entry conditions) then
sellshort 1 contract at market
flag = 0
endif
IF positionperf >= 0.5 AND LongOnMarket THEN
SELL (abs(CountOfPosition) * PerCent) AT Market
flag = 1
endif
IF positionperf >= 0.5 AND ShortOnMarket THEN
EXITSHORT (abs(CountOfPosition) * PerCent) AT Market
flag = 1
ENDIF
if flag then
sell at positionprice stop
exitshort at positionprice stop
endif
im getting an error for line
SELL (abs(CountOfPosition) * PerCent) AT Market
at the “AT”
Sorry – I cut and pasted your code! Add CONTRACTS before AT.
does not seem to work. i got picture. if it worked then it should sell half and then breakeven with this example.
Yes it needs a little tweeking:
ONCE PerCent = 0.50 //0.50 = close half positions
if not onmarket and (your long entry conditions) then
buy 1 contract at market
flag = 0
endif
if not onmarket and (your short entry conditions) then
sellshort 1 contract at market
flag = 0
endif
IF positionperf >= 0.5 AND LongOnMarket and not flag THEN
SELL (abs(CountOfPosition)) * PerCent contracts AT Market
flag = 1
endif
IF positionperf >= 0.5 AND ShortOnMarket and not flag THEN
EXITSHORT (abs(CountOfPosition) * PerCent) contracts AT Market
flag = 1
ENDIF
if flag then
sell at positionprice stop
exitshort at positionprice stop
endif
still the same result.
i tried changing the codes but the results are still the same.
if not onmarket and longconditions and buyentry then
buy 1 contracts at market
flag = 0
endif
ONCE PerCent = 0.50 //0.50 = close half positions
IF ((close - positionprice) >= 0.5*longstoploss) AND LongOnMarket and flag = 0 THEN
SELL (abs(CountOfPosition) * PerCent) contracts AT Market
flag = 1
ENDIF
if flag = 1 then
sell at positionprice stop
endif
think i got it
if not onmarket and longconditions and buyentry then
buy 1 contracts at market
flag = 0
endif
ONCE PerCent = 0.50 //0.50 = close half positions
IF ((close - positionprice)/pipsize >= 0.5*longstoploss) AND LongOnMarket and flag=0 THEN
SELL (abs(CountOfPosition) * PerCent) contracts AT Market
flag = 1
ENDIF
if flag=1 then
sell at positionprice stop
endif
My last code worked just fine when I tested it. You seem to have added a variable called longstoploss which is not declared at any value in your code!
my codes seems to work half the time. half being it close half and move stoploss to breakeven and the other one is when profit reaches half the limi, it closes half 2 times resulting closing the full trade. anyone got any ideas why?
As I said – I tested my code and it worked just fine.
You changed the exit price calculation so that it is no longer a percentage using POSITIONPERF – why?
It is highly likely that your problem is being created by the code that you are choosing not to show us – do you have any other stop orders in the code? Without all the information such as the value of longstoploss the instrument and time frame and the rest of the code it is very difficult for anyone to help you or see what you are saying is happening.
A screenshot is worth a thousand words.
heres the screen shot of what my current codes are doing on prorealtime.
defparam CUMULATEORDERS = false
timeframe(15mn)
ema37 = ExponentialAverage[37](close)
ema50 = ExponentialAverage[50](close)
bar15mn = barindex
//conditions to go long
longc1 = ema37 - ema50 > 2*pipsize
longc2 = high[1] < high[2] and high[2] < high[3]
longc3 = (low[1] crosses under ema37[1]) or (low[1] crosses under ema50[1])
longc4 = (close[1] > ema50[1]) and (close[2] > ema50[2]) and (close[3] > ema50[3])
longconditions = longc1 and longc2 and longc3 and longc4
if not onmarket and (longconditions ) then
prehigh15mn = high[1]
prelow15mn = low[1]
endif
timeframe(default)
timeframe(default)
once tradeON = 1
mybar = bar15mn
if mybar <>mybar[1] then
tradeon = 1
endif
//long entry trigger
buyentry = (close >= prehigh15mn)
spreadave = 2.5
minstop = 5
if not onmarket and longconditions and buyentry and tradeon then
buy 1 contracts at market
tradeon = 0
flag = 0
if (close - prelow15mn+spreadave*pipsize)/pipsize > minstop and (close - prelow15mn+spreadave*pipsize)/pipsize < 50 then
longstoploss = (close - prelow15mn+spreadave*pipsize)/pipsize
elsif (close - prelow15mn+spreadave*pipsize)/pipsize <= minstop then
longstoploss = minstop
elsif (close - prelow15mn+spreadave*pipsize)/pipsize >= 50 then
longstoploss = 50
endif
endif
longtarget = longstoploss
set stop ploss longstoploss
set target pprofit longtarget
ONCE PerCent = 0.50 //0.50 = close half positions
IF ((close - positionprice)/pipsize >= 0.5*longstoploss) AND LongOnMarket and not flag THEN
SELL (abs(CountOfPosition) * PerCent) contracts AT Market
flag = 1
ENDIF
if flag then
sell at positionprice stop
endif
this is only for going long.
however, on ig trading the results r different. when it reached half my limit, it close half and another half. resulting in closing full.
this is the screenshot with the codes u suggested. line 52 to line 55 replaced with these
IF positionperf >= 0.5 AND LongOnMarket and not flag THEN
SELL (abs(CountOfPosition)) * PerCent contracts AT Market
flag = 1
endif