Help with Code moving average crossover
Forums › ProRealTime English forum › ProOrder support › Help with Code moving average crossover
- This topic has 10 replies, 2 voices, and was last updated 8 years ago by Mark.
-
-
11/11/2016 at 11:15 PM #16401
Hello,
I was wondering if someone would be kind enough to help me with something, I’m sure it would be an easy job for someone who knows how to code. I am new to automated trading, i usually manual trade but I’m keen to play around with automating my systems.
What i would like is a code to do the following…
LONG
Entry – 5EMA crosses above 200EMA
Exit – 5EMA crosses below 100EMA
SHORT
Entry – 5EMA crosses below 200EMA
Exit – 5EMA crosses above 100EMA
The entry and exit signals are pretty straight forward but i struggle with the risk management, position size and stop loss part. Here is the criteria…
I want to risk 3% per trade
I want my stop loss to be positioned at the previous 15 bar high or low (depending on whether we are long or short)
This system may be a very simple bit of code for someone in the know, any help would be greatly appreciated to start me on my automated trading journey.
Mark
11/14/2016 at 11:43 AM #1647911/14/2016 at 1:33 PM #16495Thank you for your reply,
its the stop loss part I’m struggling with, I want my stop loss to be the previous 15 bar high low depending on whether I’m going long or short, I then want to stop loss to determine my position size by calculating 3% of my account size divided by my stop loss.
on a separate note..
why does pro real time only allow the pointsize to be round numbers and not pounds and pence?
Mark
11/14/2016 at 4:35 PM #16500I coded your EMA cross trading strategy accordingly to your description. Money management lot calculation are made upon the last 15 bars highest high and lowest low.
You’ll find the ITF file to import into your platform attached to this post. Please let us know if you have trouble importing it.
11/14/2016 at 5:36 PM #16523I have successfully imported the code, many thanks for you’re help.
Can i just add, i think the website is brilliant, and very helpful, i am gradually becoming converted to automated trading. If only i could find some simple systems that work well for the long haul.
Mark
11/14/2016 at 10:04 PM #16537Hi again Nicolas,
I wondered if you could give me your thoughts on my system and its results? It is the system you coded for me but i have altered some of the parameters as follows…
- Market – EUR/GBP
- Spread – 2
- EMA 25/100/600 crossover
- Starting Capital – 3000.00
- Risk – 3%
I have attached a screenshot of my results, is this the sort of results i need to expect before putting a system live?
Mark
1234567891011121314151617181920212223242526272829303132333435363738394041424344defparam cumulateorders = falseREM Money ManagementCapital = 3000 // initial capital at launch of the strategyRisk = 3 // risk in percentREM Calculate contractsequity = Capital + StrategyProfitmaxrisk = round(equity*(Risk/100))REM defining moving averagesEMA5 = exponentialaverage[25]EMA100 = exponentialaverage[100]EMA200 = exponentialaverage[600]hh = highest[10](high)ll = lowest[10](low)// -- case BUYif EMA5 crosses over EMA200 thenstoploss = (close-ll[1])/pointsizestoplevel = (close-ll[1])PositionSize = abs(round((maxrisk/StopLoss)/PointValue)*pipsize)BUY PositionSize SHARES AT MARKETendif// -- case SELLif EMA5 crosses under EMA200 thenstoploss = (hh[1]-close)/pointsizestoplevel = (hh[1]-close)PositionSize = abs(round((maxrisk/StopLoss)/PointValue)*pipsize)SELLSHORT PositionSize SHARES AT MARKETendif// -- trades exitif EMA5 crosses under EMA100 thenSELL AT MARKETendifif EMA5 crosses over EMA100 thenEXITSHORT AT MARKETendifSET STOP LOSS stoplevel1 user thanked author for this post.
11/15/2016 at 1:40 PM #16571Well, since you have optimised a bit the values, it is quite over-fit on past market data. But the strategy would have done the same thing as your backtest in real time (only stoploss and no takeprofit at all in the code).
But, what I see here just tell me what you can expect of any trend following method: lot of consecutive loss when market is in a tight range. The real question is: do you want to suffer 10 consecutive loss with a drawdown 5765GBP in real life? when your starting account were only 3000GBP.. Don’t tell, I already know the answer 🙂
11/15/2016 at 2:50 PM #16576Nicolas,
Trend following systems on average make 30% wining trades, the reason i have no take profit is because the big winning trades pay off all the small loosing trades, this is why strict risk management is neccessary.
But isn’t that what you should expect from a trend following system? The 5765GBP drawdown wasn’t on 3000GBP, that was just the starting balance, the 5765GBP drawdown was on a balance of 16,000GBP which is all in proportion to the 3% risk per trade. Compounding the profits will obviously increase the amount of drawdown but it is all relevant? Trend following systems on average make 30% wining trades, the reason i have no take profit is because the big winning trades pay off all the small loosing trades, this is why strict risk management is neccessary.
Would you advise any changed or additions for a trend following code? alternatively would you recommend trying a different system to make a steady profit? If so, what?
Also the system you coded for me comes up with an error message when i try to take it live, the code is as follows…
“this trading system had to be stopped because it tried to place a stop or limit at a negative price level. Please add protections to your code to prevent orders with negative price levels”
Mark
11/15/2016 at 3:02 PM #16578To prevent the negative calculation, please delete the [1] from the lines 22,23,30,31 and see what happen.
About the trend following method and my previous post, please don’t be offend. I understood perfectly why there were no takeprofit and of course why it is very important to embed the money management code in this strategy. Finding the best edge with mechanical trade entries is very difficult, especially with moving average crossover. Because you used tight stop and round moving average periods, I believe you are on the good way to find something nice to be traded in a live environment.
Maybe use of breakeven of even adding orders on the same trend (like Reiner is doing with Pathfinder or cfta with the grid system) would be a nice feature to add..
11/15/2016 at 3:12 PM #16580I remove the [1] and the error has gone away. thankyou
I am not offended by any of your comments, far from it, i take everything on board, i’m keen to learn. If i knew how to add breakeven and adding orders to trends i would, as i said in my original post, coding isn’t my strength haha
11/27/2016 at 10:09 PM #17326Nicolas,
I wonder if you can add something to the code for me,
In the code you wrote for me above it works out a stop loss of the previous 10 bar high or low…
Im looking to keep this but have a minimum stop loss of 10, or simply add 5 to the calculated stop loss, i would like to try both options.
Many thanks, mark
-
AuthorPosts
Find exclusive trading pro-tools on