Hi guys!
This code looks really interesting and I’m following your work and progress since I think proper money management is a key to profitable trading.
Just a thought on the 1% stoploss. As it is coded now newSL = positionprice – (positionprice*0.99) doesn’t it just calculate the difference between the positionprice and the new sl? Shouldn’t it just be newSL = positionprice*0.99?
This code states:
make the New Stop Loss = POSITIONPRICE (being the average of the Positions Price(s) – 1% of the Average Position Price(s))
Lets say the Position Prices are:
95 + 100 +105 = 300
300/3 = 100
newSL = 100 – (100*0.99)
newSL = 100 – 99
newSL = 1 (which is 1% of 100)
If understand it correctly.
@Brage @dwgfx
Thanks for your help.
Yes It were that I thought at the first glance, but since we want to exit at 1% loss of the account balance, it’s not it 🙂
So now tests are good, I coded the sell side version and made a small fixed for forex pairs. Here is the full code (also attached in itf file to this post):
defparam preloadbars = 0
//parameters
accountbalance = 10000 //account balance in money at strategy start
riskpercent = 1 //whole account risk in percent%
gridstep = 20 //grid step in point
amount = 1 //lot amount to open each trade
//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))
onepointvaluebasket = pointvalue*countofposition
mindistancetoclose =(moneyrisk/onepointvaluebasket)*pipsize
//GRAPH mindistancetoclose as "d"
//stoploss trigger
if onmarket then
SELL AT positionprice-mindistancetoclose STOP
EXITSHORT AT positionprice-mindistancetoclose STOP
endif
Please don’t leech it only. Instead, please add comments or any ideas that would improve a strategy or create a new one with this code. There are many brainful traders here and i’m pretty sure that we can make something cool all together with this nice money management idea 🙂
cftaParticipant
Senior
Amazing Nicolas and thanks a lot! I will start testing it tonight and I’ll come back with an update tomorrow night…
cftaParticipant
Senior
I’m afraid I have already come across some trouble, when I try to run the code in real time market I get this error message and the system is stopped;
“This trading system was stopped due to a division by zero during the evaluation of the last candlestick. Please add protections to your code to prevent divisions by zero.”
This is the code I was using, tried both long and short and with or without the graph line;
//-------------------------------------------------------------------------
// Main code : Grid Short Nicolas
//-------------------------------------------------------------------------
defparam preloadbars = 0
//parameters
accountbalance = 10000 //account balance in money at strategy start
riskpercent = 1 //whole account risk in percent%
gridstep = 20 //grid step in point
amount = 1 //lot amount to open each trade
//first trade whatever condition
if NOT ONMARKET AND close<close[1] AND STRATEGYPROFIT=0 then
SELLSHORT 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))
onepointvaluebasket = pointvalue*countofposition
mindistancetoclose =(moneyrisk/onepointvaluebasket)*pipsize
//stoploss trigger
if onmarket then
EXITSHORT AT positionprice-mindistancetoclose STOP
endif
Any idea what might be the problem?
Ahahah
“division by zero” is something that ProBacktest don’t know apparently 🙂
Well, if you have set correctly the line 7 to 10 (no zero values), the problem is in line 26 (this is the only code line with a division).
A quick fix would be, move the line 26 into the “//stoploss trigger” function, like this:
//stoploss trigger
if onmarket then
mindistancetoclose =(moneyrisk/onepointvaluebasket)*pipsize
EXITSHORT AT positionprice-mindistancetoclose STOP
endif
That would make the trick.
Great work! This is fun! 🙂
Question: Is it possibile to add a breakeven function, and a stop loss that moves up when the orders are in profit?
I thought the code:
liveaccountbalance = accountbalance+strategyprofit
would do that, but it seems like the stop loss don’t take the profit (from the orders) into calculation? But only start account balace?
So maybe a breakeven function, and a stop loss that moves up when the orders are in profit based on equity curve?
I have tried this but can’t make it right.
Or will this behave differently in real market?
All the best!
Yes, everything’s possible.
Actually, the code don’t calculate the floating profit. The code do actually exactly what cfta wanted and there’s no any ‘takeprofit’ system in it. I already have some ideas for that based on the live equity curve as you mentioned it.
Firstly I would like to find a mechanical way to enter the market for this system, that would be fun 🙂 Any idea?
Nice Nicolas!
Yes, I have thought and tried a few things 🙂
My conclustion was that maybe it isn’t nessesarily because of the 1% account stop loss.
Otherwise my best suggestions is:
a) Entry based on pricemove:
// Conditions to enter long and short positions
gridstep1=x
//first trade whatever condition
if NOT ONMARKET AND close-tradeprice(1)>=gridstep1*pipsize then
BUY amount LOT AT MARKET
endif
if NOT ONMARKET AND tradeprice(1)-close>=gridstep1*pipsize then
SELLSHORT amount LOT AT MARKET
endif
b) “Good old turtle fashion” (on 5min TF):
// Conditions to enter long positions
c1 = (close=Highest[600](close))
// Conditions to enter short positions
c2 = (close=Lowest[600](close))
//first trade whatever condition
if NOT ONMARKET AND c1 then
BUY amount LOT AT MARKET
endif
if NOT ONMARKET AND c2 then
SELLSHORT amount LOT AT MARKET
endif
c) I have also tried MA, but don’t like it:
// Conditions to enter long positions
indicator1 = WilderAverage[40](close)
indicator2 = WilderAverage[40](close)
c1 = (indicator1 > indicator2[1])
// Conditions to enter shortpositions
c2 = (indicator1 < indicator2[1])
//first trade whatever condition
if NOT ONMARKET AND c1 then
BUY amount LOT AT MARKET
endif
if NOT ONMARKET AND c2 then
SELLSHORT amount LOT AT MARKET
endif
But as said, maybe this is not nessisarily, or maybe a “mini-turtle” is enough :
c1 = (close=Highest[200](close))
I also think its important that I don’t “cut” this thread from cfta, since he started it 🙂
You are right. I’ll code floating profit of the whole orders basket and then we’ll can move stoploss accordingly to x% profit.
About entries, breakout of recent highs or lows is clever. However, I think that we should introduce another trigger like recent volatility.
Hi Nicolas and cfta!
Nice work so far. Really interesting.
Can this grid order code be combined with any of the 15-min Breakout-systems in the library for a daytrading system? With gridsteps and riskpercent adjusted to the market traded? And perhaps flat after market close?
Yes of course. It only needs some copy/paste experience 🙂
I need to finish some other things right now, maybe someone can have a look in the mean time.
cftaParticipant
Senior
Good morning fellows,
I had a very long day at my regular old school job yesterday so I didn’t have time for posting but I did some testing remotely. I’m thrilled to see all the suggestions for further developement, a stop loss triggered by trend change, by MA cross, stochastics, ADX or whatever is the most effective is of course a great way to maximize profits.
For the real market testing everything looks well, entries where taken as intended and the SL worked perfectly. Great job Nicolas!
I’ll stay in the mix for discussing further improvements…