ProRealCode - Trading & Coding with ProRealTime™
Thanks for your post, but in this code you totally deleted the money management system of cfta. This code only add orders on the same trend and then exit them when the supertrend change direction. I’m sorry, but what is the best of the system here is the 1% loss despite of how many trades are on the run, so while deleting this part, it’s like get back to the beginning 🙂
I’ll plan to work on dynamic step asap. But I’d like to find a good way to enter market first, so do anyone have worked already on that? I know a lot of people are reading this thread, don’t be shy! 🙂
@Nicolas and everybody
I might have found a system which might work as an entry. It is called State Space and I have started a new thread here to discuss it:
I have there also posted some combinations together with the grind system, starting in post 4 I think. But I think its good to start reading from post 1.
@Nicolas could you please help with the first goal in the thread?
Please use all systems there carefully. I have not used them live yet.
I am sorry if I offended you or cfta. It is not that the idea and the coding aren’t brilliant. I was merely trying to demonstrate an alternative as I was finding the BB exit system showing mostly losses on back tests. I did not intend it as complete code but I don’t know how to code the money management system to integrate with a SuperTrend stop.
Has anyone else back tested the grid system extensively with the BB exit switched on? Are you finding the same issues?
I will share my actual start point in my trading.
I wait till the markets moves at least 45 pips in any direction above or under Exponentialaverage[8](close). When this occurs a limit order starts at this Exponentialaverage[8](close) buy or sell. I got a fixed profit and stop for 25-30 pips. I tryed BB exit with worst results than taking a fixed profit (maybe beacuse backtest limits).
See attached images.
This is normal since you don’t use add orders in your strategy. Adding the bollinger bands deviation over the floating profit curve give us some space for the whole basket orders to close while it can continue to make profit along the way. It’s just a way of let the profit run than to exit properly with a fixed % trailing stop.
I haven’t code anything else with BB exit for the moment. But I think its powerfulness resides in a trending market condition with a simple entry based on things not too complicated, I do believe that a couple of moving average could make the trick.
I’m not offended at all! Let’s share your ideas! Sorry if it looked different in my previous replies.
Thanks for give back your feedback. I tested my strategy adding orders (see attached image). It gives very good trades sometimes and the most important thing is it didn’t crash the whole account over the time, so it’s not so bad! ( with BB EXIT ). but sometimes i don’t like it, hehehe, like image2.
Keep following for news, thanks all!
Ok but please keep in mind that the BB exit calculated on the floating profit is exactly the same thing as a standard bollinger bands on price. It is more convenient to recalculate it on floating profit because it is the same deviation for the buy and sell orders, that’s all.
Floating profit is nothing more than the difference of the actual price and the average price of orders, multiplied by the number of them. So its curve, is the same as the price itself.
The BB exist operand only if the Risk Reward ratio is reached so it would never exit positions in loss (if everything is in the code though! 🙂 )
Hi all, very very intesresting… but… something is missing!!! I really like that you have developped a Trading system from the end. Usually we develop TS from the enrty conditions 🙂 not from the exit.
So… Now What is missing is the entry condition. I am working on it and will come back soon.
What I have changed to the TS?
I am working now to find the entry condition… what we have to look for is a paramenter that tell us when a direction is choosen… one question… on which TF you would like to run it?
It is importan that this system will run on index with fixed spread if we want to use it on bigger TF than 15 minutes. During night we have too big spreads on normal index. Maybe we should use this on commodites?
Nicholas, one question… when we run a backtest all the loosing trades should have the same loss. (1% of the account). Why does not work in this way? pleas elook the image attached.
In the picture one example on dax 1 hour with spread 1.
Last thing that it seems that we forget always 🙁
The code must be roboust. This mean that if I run it on different index it should have similar performance in all. (I think that this is the reason why most of the code I found on this forum are not good for real money trading)
Here my code modifed:
//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.25 // decide with which interval the new entries are done
accountbalance = 10000 //account balance in money at strategy start
riskpercent = 1 //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 = 3//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
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)*ticksize //index points calculated to keep the stoploss fixed
endif
//floating profit
floatingprofit = ((close-positionprice)*pointvalue)*countofposition //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
endif
//resetting the risk reward reached variable
//if not onmarket then
//RRreached = 0
//endif
//GRAPH average[10](strategyprofit) coloured(0,0,255) as "profit curve"
//HOW TO MAKE IT BETTER: About ATR step instead of fixed one is clever. But, since we want to quickly make huge profit, I suggest to add more orders when the ATR is high and less when it is low. So when the ATR rise a lot in short period, it means that actual volatility can guarantee that a lot of grid orders can be launched to grow the floating profit. Then BB exit would take care of the rest. Of course, we are talking of backtest, live environment should not the exact same thing as market slippage can take its part into making trouble.
Hi, while working on that code I realize there were a mistake in the floating profit formula.
The good one is:
//floating profit
floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains
While the old one were sufficient for indices for example, it were not good for forex pairs.
Maybe that is where is the trouble with your tests David.
@David-1984 thank you for the comments.
I would like to discuss “Robustness” some more as a general concept because I believe the notion of Robustness across different instruments/pairs is unrealistic. You probably have much more experience with writing algorithms than I do, so I would really like to openly chat about it some more. I think it’s best if we discuss this as a New Post?
I look forward to learning from your experiences.
This is a really good point @dwgfx. One that has concerned me for some time. Thanks for bringing it up. I am very interested to hear what others think.
It harks back to Nicholas’s post re curve fitting. In my limited experience with PRT, I don’t think there is a way to find the optimal settings for a portfolio of instruments. However, Amibroker does have this ability. Has anyone had any experience coding Amibroker? It could be used to optimise a system so the settings could then be used in PRT.
Robustness is the one of the major thing an algorithm trader/coder should focus on. There are many aspects about what we call Robustness in quantitative and algorithmic trading and this is by far a very large subject that of course deserve a new topic.
FYI, Walk-Forward and Monte Carlo analysis are on the to-do list to the things that could be added to the optimization module of prorealtime.
Hi all, I said a stupid thing in my previus post… I said that the Loss should be always the same value. This is not true because our account is not static and it is updated with older trades. This mean that if we have 10000 euro and 1% risk we will lose 100 euro only in the first trade. After that we make (or lose) money we will lose 1% of a bigger value. Sorry again for the mistake.
While checking this I found another small problem…
In this formula the below ticksize could be wrong. It return to us the value divided by 100. This happen in all index except the commodities. For them it works.
I make an example in the picture. Nicholas, could it be a problem witgh IG market PRT version? Can somone check if also in theyr system this problem appear?
mindistancetoclose =(moneyrisk/onepointvaluebasket)*ticksize
OT: let’s discuss here about robustness
http://www.prorealcode.com/topic/robustness-in-automated-algorithmic-trading-systems/
Nicolas, I’m afraid I have not managed to make the system with BB exit work as intended yet in real time markets, whenever I start the system it takes one trade and then gets stopped out after a 10 pip / 0.1 % loss. I tried the fix you mentioned above and the other fix posted on page 6;
Grid orders with one combined stop loss and limit, can it be done?
It would be great if we can make the BB exit work. Here is the code I used for longs;
//-------------------------------------------------------------------------
// Main code : LONG GRID BB Exit
//-------------------------------------------------------------------------
defparam preloadbars = 100
once RRreached = 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
rr = 3 //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
// case BUY - add orders on the same trend
if longonmarket and close-tradeprice(1)>=gridstep*pipsize then
BUY amount LOT AT MARKET
endif
//money management
liveaccountbalance = accountbalance+strategyprofit
moneyrisk = (liveaccountbalance*(riskpercent/100))
if onmarket then
onepointvaluebasket = pointvalue*countofposition
mindistancetoclose =(moneyrisk/onepointvaluebasket)*ticksize
endif
//floating profit
floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains
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
//stoploss trigger when risk reward ratio is not met already
if onmarket and RRreached=0 then
SELL 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
endif
endif
//resetting the risk reward reached variable
if not onmarket then
RRreached = 0
endif
Please have a look!
Grid orders with one combined stop loss and limit, can it be done?
This topic contains 307 replies,
has 1 voice, and was last updated by OtherAttorney
1 year, 10 months ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 04/14/2016 |
| Status: | Active |
| Attachments: | 106 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.