Hey guys. I’m keen to understand how to reset or add new trailing stops to all positions that are running after they hit a combined profit margin. Here is a some code in working with;
//Max profit variable
MaxProfit=3500 //Max profit allowed (in money)
// Test the current floating profit and close orders if needed
FloatingProfit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains
if FloatingProfit>=MaxProfit then
SET STOP $TRAILING 500
endif
Will the Set Stop apply a $500 Trailing Stop all positions?
No. Because the stop will only be in place when FloatingProfit equals 3500 exactly. A very rare thing I imagine. You need to set a flag when price >= 3500 and then only have the trailing stop in operation if that flag is set. Otherwise the trailing stop only works if price goes from >= all the way down to your stop price in one bar. Reset the flag to zero if price falls below your stop level.
Thanks @Vonasi,
Here’s a real snippet I’m testing at the moment across a few pairs and it seems to work sometimes and not others – what am I getting wrong?
// test the current floating profit and close orders if needed
FloatingProfit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains
if FloatingProfit > 1500 and floatingprofit <2500 then
SET STOP PTRAILING 10
endif
if FloatingProfit > 2501 and floatingprofit <5000 then
SET STOP PTRAILING 5
endif
if FloatingProfit > 5001 and floatingprofit <8000 then
SET STOP PTRAILING 4
endif
if FloatingProfit > 8001 then
SET STOP PTRAILING 3
endif
I’ve been monitoring it on the AUD/USD and it triggered fine. However, miss fires on the USD/CAD. Could it not be converting to the AUD for the total gain for all running positions? I’m expecting the FloatingProfit to be calculated in AUD for all open position for that individual system …
Keen to hear your thoughts guys.
The pointvalue instruction, use to calculate the floating profit, is in the currency of the current instrument, not in the currency of your account, because there is no way to get it through code. You’ll have to make a conversion yourself in the code for that.
Thanks again,
So the code looks correct?
The instrument I’ve been testing on is the USD/CAD where the system calculates the gain for open positions in CAD. Do you mean I’ll have to code a conversation back into AUD which what I believe you’re saying?
As for the terminology for the system ‘latent gain’ and ‘today gain’ I’m not looking to calculate the total latent gain for the system for all time.
All I’m looking to test is:
My system opens short and long position, and if the combined open and running positions hit a certain value a trailing stop is activated for all running positions. If the positions are then stopped-out because the market reversed — next time the system opens a new set of positions the floatingprofit will start from zero.
Apologies if I’m not explaining myself properly.
Thoughts?
Hey @Nicolas – I’ve been testing the above and recently found a flaw in my code;
// test the current floating profit and close orders if needed
FloatingProfit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains
if FloatingProfit > 50 and floatingprofit <150 then
SET STOP PTRAILING 4
endif
if FloatingProfit > 151 and floatingprofit <250 then
SET STOP PTRAILING 2
endif
if FloatingProfit > 251 then
SET STOP PTRAILING 1
endif
One of these functions is calculating the total gain value for the system rather than only the current open positions;
(close-positionprice)*pointvalue)*countofposition)/pipsize
The solution or advice I’m looking for is to find a way to reset the floatingprofit at each new position open. Is there a function to do this?
Would this work … subtracting the system profit using strategyprofit function from the total accumalative floatingprofit:
FloatingProfit = Strategyprofit – (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual open trade gains
Thanks in advance.
Bart
Sorry –
Would this work … subtracting the system profit using strategyprofit function from the total accumalative floatingprofit:
FloatingProfit = Strategyprofit – (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual open trade gains
Sorry – should be the otherway around;
FloatingProfit = ((((close-positionprice)*pointvalue)*countofposition)/pipsize) – Strategyprofit //actual open trade gains