I cam across that same error of money management % loss this week end. I have worked a lot on different set of codes since then, but I think the one attached here is the good and only one that work across various instruments.
Please have a look at the example of trades on NIKKEI with BB exit, it works good.
Here is the complete code:
defparam preloadbars = 100
once RRreached = 0
//parameters
accountbalance = 10000 //account balance in money at strategy start
riskpercent = 2 //whole account risk in percent%
gridstep = 10 //grid step in point
amount = 1 //lot amount to open each trade
rr = 2 //risk reward ratio (set to 0 disable this function)
//first trade whatever condition
if NOT ONMARKET AND close>close[1] AND STRATEGYPROFIT=0 then
BUY amount LOT AT MARKET
endif
if NOT ONMARKET AND close<close[1] AND STRATEGYPROFIT=0 then
SELLSHORT amount LOT AT MARKET
endif
// case BUY - add orders on the same trend
if longonmarket and close-tradeprice(1)>=gridstep*pipsize then
BUY amount LOT AT MARKET
endif
// case SELL - add orders on the same trend
if shortonmarket and tradeprice(1)-close>=gridstep*pipsize then
SELLSHORT amount LOT AT MARKET
endif
//money management
liveaccountbalance = accountbalance+strategyprofit
moneyrisk = (liveaccountbalance*(riskpercent/100))
if onmarket then
onepointvaluebasket = pointvalue*countofposition
mindistancetoclose =(moneyrisk/onepointvaluebasket)*pipsize
endif
//floating profit
floatingprofit = ((close-positionprice)*pointvalue)*countofposition
MAfloatingprofit = average[20](floatingprofit)
BBfloatingprofit = MAfloatingprofit - std[20](MAfloatingprofit)*2
//floating profit risk reward check
if rr>0 and floatingprofit>moneyrisk*rr then
RRreached=1
endif
//GRAPH floatingprofit as "float"
//GRAPH RRreached as "rr"
//GRAPH floatingprofit as "floating profit"
//GRAPH BBfloatingprofit as "deviation of floating profit"
//stoploss trigger when risk reward ratio is not met already
if onmarket and RRreached=0 then
SELL AT positionprice-mindistancetoclose STOP
EXITSHORT AT positionprice-mindistancetoclose STOP
endif
//stoploss trigger when risj reward ratio has been reached
if onmarket and RRreached=1 then
if floatingprofit crosses under BBfloatingprofit then
SELL AT MARKET
EXITSHORT AT MARKET
endif
endif
//resetting the risk reward reached variable
if not onmarket then
RRreached = 0
endif
I haven’t set it on real time market already, but I plan to with simple price breakout breach to see how it goes.
@cfta The problem that the system exit ate 0.1% is due to the thing that I explain in my post of 22/05. Please have a look into it.
@Nicholas, have you seen the problem that I mentioned? also cfta has it… If you don’t have it it means that it is due to IG market.
My suggestion it is to (depending on which index we choose) multiply the ticksize in the formula below x100 but this must be checked index by index and case by case.
mindistancetoclose =(moneyrisk/onepointvaluebasket)*ticksize
Second thing, you are specking to put it in real market time. what TF and index you suggest? 15 minutes? In my opinion we should choose some index with good volatility and strong movements during the day.
Have you tried to remove the money management and see if all the trades close with the same loss?
I have changed this formula at line 35 to pipsize, which is in accordance to the pip value obviously.
Since this is a “no-brain” system (understand that there is no initial trading decision made by the code), timeframe is not important. Of course, because we need it to add orders quickly and with the minimal step requirement we want, we should put it on a minimal timeframe possible, such as seconds eventually… as long as this strategy don’t have needs of any indicator.
I said that I’ll put it on real live environment for testing purpose. First trade will be placed with breakout of a price level (Close crosses under/over …)
I think that the first idea of making decision of when and how this system should operate is a good one. As I said, ProScreener can help us a lot for that, and what we need here is to fetch trend continuation or breakout on different timeframe horizon.
Problem with pipsize as suggested by Nicolas it works on all indexes. Great (as usual) job Nicolas!!
One other comment… maybe (I’m testing it now) it doesn’t work properly the exit when RRreached=1. In some cases we lose more than the risk decided. A good trade turns into the worste one.
Please change this function to see if it happens again:
//stoploss trigger when risj reward ratio has been reached
if onmarket and RRreached=1 then
if floatingprofit crosses under BBfloatingprofit then
SELL AT MARKET
EXITSHORT AT MARKET
endif
SELL AT positionprice-mindistancetoclose STOP
EXITSHORT AT positionprice-mindistancetoclose STOP
endif
Thanks.
Nicolas… you are too fast 🙂
Your code solved the problem…
Here my proposal. Have a look in the picture. If we are making good money and the account is bigger than the beginning we could reach this bad situation.
2 Proposals:
Stop loss should be fixed and not equal to the account? Maybe a stop loss linked to the volatility AND the account balance?
Stoploss if after the first enrty we don’t reach a second entry in a certain time or number of candels.
(Please tell me if I’m going out of topic, I know that you want to run it only once.)
cftaParticipant
Senior
Nicolas it works! First day of real time trading everything looks good, I was short on time today and only set it up for the Nikkei with 2 % SL and 10 grid step on a 1 min, most entries were taken with a step of about 15-20 pips but of course this is due to the fast moving price. On a highly volatile index like the Nikkei it probably needs to be run on a 1 second chart.
@David great input, a mi piace! As for your above trade in oil, when we know that it is likely that the market will move and keep moving but we are uncertain of the direction run two sets of systems simultaneously, one long and one short. That’s what I did on the Nikkei today, the longs gave me a profit of 5.45 % and know I have a floating loss of 1.8 % which will either retrace or stop out at -2 %.
When trying in realtime do you keep the entry like close<close[1]? Does it mean that you enter pratically in a random moment??
Or how did you decided to enter at 16.00?
David
All this has been one of the best reads I have done in a long time. Thanks all for the input. In the spirit of sharing here is a system I trade, which could maybe help with an entry. It is a system that looks for a signal candle of a certain size and then trades into that same direction. It also has an addition to it where it enters long if an unexpected big red candle appears. This is done on the 1HR TF. Have a look and maybe this can be incorporated. Im adding X’s into the code as these numbers would differ from instrument to instrument like DAX vs SP500. p.s. my coding skills aren’t that great so if anyone can suggestion changes pls feel free.
DEFPARAM CumulateOrders = True
CONCOUNT = 2
// Conditions to enter on BIG RED candle
c1 = (OPEN - CLOSE) //
c1a = (c1/CLOSE)*100
c2 = (CLOSE - OPEN) //= -ve value
c3 = (HIGH - LOW) //
TP = (c3 * 0.62) //
GoLong = c1a > 0.77 AND c2 < 0 //
IF GoLong THEN
BUY CONCOUNT CONTRACT AT (HIGH - X) STOP//
SET STOP pLOSS X// H-L c3
SET TARGET pPROFIT TP//
ENDIF
///////////////////////////////////////////////////////////////////////////
// Conditions to enter long on BREAK-OUT
c1s = (CLOSE - OPEN) //
c1as = (c1s/OPEN)*100
c2s = (CLOSE - OPEN) //= +ve value
c3s = (HIGH - LOW) //
TPs = (c3s * 0.86) //
ADX1 = ADX[X]
GoShort = c1as > 0.5 AND c1as < 1.2 AND c2s > 0 AND ADX1 > X//
IF GoShort THEN
BUY CONTCOUNT CONTRACT AT (HIGH - X) STOP//high - 100
SET STOP pLOSS X//
SET TARGET pPROFIT TPs//
ENDIF
///////////////////////////////////////////////////////////////////////////
@cfta
Since there´s been a lot of changes and new inputs to the original code, it would be great to know which version you use now and that you mentioned above that you tested real time today?
And again thanks to everyone for all the input in this thread. Really interesting and I’ve learned a lot so far. Looking forward to when this code is combined with a fantastic entry strategy. It will be amazing:)
I agree with @Brage, could you @cfta please post the code you are using? Thanks!
All the best!
In my opinion there are 2 codes that you should look for:
Code number 1 (the last one posted by Nicholas if you want to do only 1 entry)
Code number 2 (if you want to do more than 1 entry. posted below by me)
//after the enrty (long or short) this TS is meant to do more entry until the Stoploss or the Takeprofit will be reached. The stop loss is not static but it is decided at the beginning with variable riskpercent. When the system makes the second, third ,fourth, etc entries the SL is moved to guarantee that the risk is always the same (it means that at every new buy signal it is moved more near to the avarage price.
//The Take profit is done when the atual gain is under the avarage of the last trades and when the risk reward ratio is reached
defparam preloadbars = 100
once RRreached = 0
//parameters
indexvalue = 10000 //dax=10000 MIB=18000 WTI=4800 etc...
Entrysteps = 0.1 // decide with which interval the new entries are done
accountbalance = 10000 //account balance in money at strategy start
riskpercent = 2 //whole account risk in percent%
gridstep = indexvalue*(riskpercent/100*Entrysteps) //grid step in point, this should change depending on the volatility
amount = 1 //lot amount to open each trade
rr = 2//risk reward ratio (set to 0 disable this function)
//first trade whatever condition. Here you should put the entry of long trades. this one below is only an example.
if NOT ONMARKET AND close>close[1] then
BUY amount LOT AT MARKET
endif
//first trade whatever condition. Here you should put the entry of short trades. this one below is only an example.
if NOT ONMARKET AND close<close[1] then
SELLSHORT amount LOT AT MARKET
endif
// case BUY - add orders on the same trend. Here is when next buy are made
if longonmarket and close-tradeprice(1)>=gridstep*pipsize then
BUY amount LOT AT MARKET
endif
// case SELL - add orders on the same trend. Here is when next sell are made
if shortonmarket and tradeprice(1)-close>=gridstep*pipsize then
SELLSHORT amount LOT AT MARKET
endif
//money management
liveaccountbalance = accountbalance+strategyprofit //balance account considering the past trades
//liveaccountbalance = accountbalance
moneyrisk = (liveaccountbalance*(riskpercent/100)) //value in euro that we risk on each trade.
if onmarket then
onepointvaluebasket = pointvalue*countofposition //value of each point of the index. It increase when we have multiples contracts
mindistancetoclose =(moneyrisk/onepointvaluebasket)*pipsize //index points calculated to keep the stoploss fixed
endif
//floating profit
floatingprofit = (((close-positionprice)*pointvalue)*countofposition) // /pipsize??? //actual trade gains
MAfloatingprofit = average[20](floatingprofit)//avarage of the last 20 trades
BBfloatingprofit = MAfloatingprofit - std[20](MAfloatingprofit)*2 //standard deviation of the avarage of last 20 trades
//floating profit risk reward check. If the actual gain is bigger than the riskreward the variable RRreached is set to 1. It means that the system is ready to exit at the take profit signal.
if rr>0 and floatingprofit>moneyrisk*rr then
RRreached=1
endif
//Here is where we set the stoploss. it means that the reward ratio is not reached yet.
if onmarket and RRreached=0 then
SELL AT positionprice-mindistancetoclose STOP
EXITSHORT AT positionprice-mindistancetoclose STOP
endif
//stoploss trigger when risk reward ratio has been reached.
//If the risk reward is reached the take profit can be done. The Take profit is done when the atual gain is under the avarage of the last trades
if onmarket and RRreached=1 then
if floatingprofit crosses under BBfloatingprofit then
SELL AT MARKET
EXITSHORT AT MARKET
RRreached = 0 //resetting the risk reward reached variable
endif
SELL AT positionprice-mindistancetoclose STOP
EXITSHORT AT positionprice-mindistancetoclose STOP
RRreached = 0 //resetting the risk reward reached variable
endif
//resetting the risk reward reached variable
//if not onmarket then
//RRreached = 0
//endif
//GRAPH mindistancetoclose coloured(0,0,255) as "mindistancetoclose"
//GRAPH floatingprofit coloured(0,0,0) as "ticksize"
//GRAPH RRreached coloured(0,0,255) as "RRreached"
World indices made big performances yesterday with almost no retracement. That’s where this money management / add orders strategy is the most powerful. You are right about execution cfta, this implied that it needs to be launch on 1 seconds timeframe, as long that our entry don’t rely on indicator. We are all awaiting the multitimeframe support, which I hope will be soon.