Hi, i’ve tried to sort this out myself based on setting up different trailing, so this is what i am doping (not sure if works?).
My reasoning is it takes TradePrice[1] to be the last trade price, if open or closed, so if a 2nd trade was opened then the first trade is TradePrice[2] if no closes occurred. But if say 1 position close did occur, then the open price of the first trade is then TradePrice[3] etc… thats why i have put in place the flags that keep track and reset when condition isnt so to null/stop one trailing and start the next.
Note i only ever open up to 2nd accumulative, and don’t reopen any until the entire position is closed. As such for the last (aka after one trade was closed, and thus one only open) then i can use positionprice as it would be taking the price of the remaining trade and thus trailing would be correctly following that still. Also if you want to say reopen a 2nd, etc.. after it was closed, then you could reset the flags after a position closed, aka if back to 1 then flag reset, then trailing restarts to the first (use positionprice), and then when 2nd trade reopened then that would be the last TradePrice[1] again.
If you want to do 3 or 4 or more accumulative, then you just keep repeating with more flags and more settings on more positions following the same approach (many lines and time consuming though).
This is my reasoning, i am using this myself, ? unsure if as such it is working as suggested, but it does close positions and often has 2 stop-lose orders in place on my IG account when i look which appear to match my conditions. I also do use this same reasoning with “Breakevens”, and i also do see them correctly placed at positions with each subsequent trade when i check my account. I have had some issues in past where it appeared my phone was notifying me non stop of changes to the stop-lose orders: as appeared close one bar it read one and then read the other, some bug loop occurring, but i think i have sorted those out after i added in the CountOfPositions and Flags ?
Let me know thoughts ?
//** Setup different trailing stop-loses and profits for accumulated positions, from 2nd +...
buyCond = a // a= whatever your buy conditions are
// --- Flag ---
// Setup a flag for when 2nd position open and reset when no position on market:
if not onmarket then
flagA = 1
ENDIF
//--- BUY EXECUTION ---
IF not longonmarket AND buyCond THEN
BUY 1 CONTRACT AT MARKET
elsif longonmarket and flagA =1 AND buyCond THEN
BUY 1 CONTRACT AT MARKET
flagA = 0 // reset the first flag to 0, to inform a 2nd position has been opened; use as many flags as you like for how many accumulated positions you have opened > use these below
// ..... Continue with FlagB etc.. based on 3rd, 4th etc accumulated positions open
//--- TRAILING PROFIT & STOPLOSES ---
// First trailing for when only 1 trade open
trailingstart = tradeprice(1)*(1/100)
trailingstep = tradeprice(1)*(0.5/100)
//reset the stop-loss value when no trades open, OR a second trade was opened then resets/nulls this
IF NOT ONMARKET OR (FlagA = 0) THEN
newSL=0
ENDIF
IF LONGONMARKET AND flagA = 1 THEN
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
IF newSL>0 THEN
SELL AT newSL STOP
ENDIF
//After a second trade open then need to keep the first trailing stop function
// using the flag and tradeprice[2] as price of 2 trades back
trailingstart2 = tradeprice[2]*(1/100)
trailingstep2 = tradeprice[2]*(0.5/100)
//reset the value for if no trade or only trade on market:
IF NOT ONMARKET OR flagA = 1 THEN
newSL2=0
ENDIF
IF LONGONMARKET AND flagA = 0 AND (abs(COUNTOFPOSITION) = 2) THEN //
IF newSL2=0 AND close-tradeprice[2]>=trailingstart2*pipsize THEN
newSL2 = tradeprice[2]+trailingstep2*pipsize
ENDIF
IF newSL2>0 AND close-newSL2>=trailingstep2*pipsize THEN
newSL2 = newSL2+trailingstep2*pipsize
ENDIF
ENDIF
IF newSL2>0 THEN
SELL AT newSL2 STOP
ENDIF
// -- The Second Open Trade (When Accumulated Trades) trailing to follow, use flag
trailingstart3 = positionprice*(1/100)
trailingstep3 = positionprice*(0.5/100)
//reset the value if not on market, or if only one trade open, then nulls this function.
IF NOT ONMARKET OR flagA = 1 OR AND (abs(COUNTOFPOSITION) < 2) THEN
newSL3=0
ENDIF
IF LONGONMARKET AND flagA = 0 AND (abs(COUNTOFPOSITION) = 2) THEN // If flag to 0 > a second trade opened, then start this
IF newSL3=0 and close-tradeprice[1]>=trailingstart3*pipsize THEN //trade price [1] as the last trade open was this 2nd trade!
newSL3 = tradeprice[1]+trailingstep3*pipsize
ENDIF
IF newSL3>0 and close-newSL3>=trailingstep3*pipsize THEN
newSL3 = newSL3+trailingstep3*pipsize
ENDIF
ENDIF
IF newSL3>0 THEN
SELL AT newSL3 STOP
ENDIF
// --- Add in the same for 3rd, 4th etc trades if your doing multiple accumulations here.
/// Do all the above works only if none of the prior (or subsequent) trades were closed (aka trade price of the close). So we need more functions below, to rest when this occurs
// -- BELOW - TRAILING IF ONE OF THE TWO TRADES WAS CLOSED:
trailingstart4 = positionprice*(1/100)
trailingstep4 = positionprice*(0.5/100)
// Use the Count of Position to inform this 2nd trade (2 positions) is still open, thus nulls this to 0, despite the flag informing 2nd trade opened:
IF NOT ONMARKET OR flagA = 1 OR (abs(COUNTOFPOSITION) = 2) THEN
newSL3=0
ENDIF
// Below 1. FlagA = 0 to inform that 2nd trade was opened, and then 2. CountOfPosition informs though that now less then 2 positions, so it was since closed:
IF LONGONMARKET AND flagA = 0 AND (abs(COUNTOFPOSITION) < 2) THEN
// flagA = 1 // Depending how many accumulated positions and your strategy, ? if you might want to open a 2nd trade again despite closing it, then you could rest the flag here, and use 'positionprice' in the first trailing instead of tradeprice. To reloop trailing back to start there!
IF newSL4=0 and close-positionprice>=trailingstart4*pipsize THEN
newSL4 = positionprice+trailingstep4*pipsize
ENDIF
IF newSL4>0 and close-newSL4>=trailingstep3*pipsize THEN
newSL4 = newSL4+trailingstep4*pipsize
ENDIF
ENDIF
IF newSL4>0 THEN
SELL AT newSL4 STOP
ENDIF
// you then consider redoing all the above on subsequent accumulative positions