As an example, when I get a Buy signal (say caused by 2 moving averages crossing), I would like to enter the trade if the next bar’s price goes above the high of the signal bar (and enter the trade at that price not the open of the following bar)
The profit target would be lets say 2 * ATR(14) above the high of the signal bar.
The exit would be if the price crosses below the high of the signal bar – the ATR(14) at the time of the signal bar.
Lastly, I have borrowed part of this code from MattyJ (thank you). Using the money management code here, how do I code this so the trade size (I am spread betting) is 5% of the account size at the time of the trade?
defparam cumulateorders=false
barquantity=5 //how many bars to wait after a signal to open a new order
REM Money Management
Capital = 1000 // initial capital at launch of the strategy
Risk = 0.05 // risk in percent
REM Calculate contracts
equity = Capital + StrategyProfit
maxrisk = round(equity*(Risk/100))
indicator1 = Average[25](close)
indicator2 = Average[250](close)
indicator3 = Average[15](close)
c1 = (indicator1 CROSSES OVER indicator2)
c2 = (indicator1 CROSSES UNDER indicator3)
if c1 then
buybar=barindex
buyprice=close
endif
if c2 then
sellbar=barindex
sellprice=close
endif
stillbullish = buybar>sellbar
stillbearish = sellbar>buybar
if stillbullish and barindex-buybar>=barquantity and gobuy<>buybar and close>buyprice then
buy maxrisk -0.5 perpoint at market
gobuy=buybar
endif
if stillbearish and barindex-sellbar>=barquantity and gosell<>sellbar and close<sellprice then
sellshort maxrisk-0.5 perpoint at market
gosell=sellbar
endif
I recognize the code we made recently in the Mattyj’s topic.
I understand your request about the changes to made to this code, but you did not mention if the elapsed bars after the MA cross conditions should be left or not?
5% of the account size at the time of the trade
It is already the case in the strategy, despite the fact that Mattyj reduced the calculated lots by 0.5 at lines 36 and 41 (don’t know why).
Thank you for the very quick reply Nicolas. I’m not sure what you mean about the elapsed bars being left or not. Sorry if I wasn’t very clear. What I meant was the bar at which time the MAs cross is what I call the signal bar. This should be the reference point for the entry . So in an up trade the high of the signal bar would be the entry level and the take profit would be 2 ATRs above the high and the stop loss would be 1 ATR below the high.
After each trade the account size would be adjusted and the amount per point on the next trade would change based on the risk percentage stated.
ok I understand your strategy, but the code provided embed a lot more than a simple MA cross: it has condition about bars to wait before opening a trade after a cross and it has also 3 MA, not only 2. I’d rather start from scratch 🙂
So, to be sure: only 2 moving average and no need to wait X bars before launching orders? (apart from the fact that it is necessary to wait for a break of the High of the signal bar).
Yes that’s right Nicolas.
Fine, let’s try with this code, do your own tests cause I didn’t.
defparam cumulateorders=false
REM Money Management
Capital = 1000 // initial capital at launch of the strategy
Risk = 5 // risk in percent
REM Calculate contracts
equity = Capital + StrategyProfit
maxrisk = round(equity*(Risk/100))
MAfast = Average[7](close)
MAslow = Average[21](close)
atr = averagetruerange[14]
c1 = (MAfast CROSSES OVER MAslow)
c2 = (MAfast CROSSES UNDER MAslow)
//The profit target would be lets say 2 * ATR(14) above the high of the signal bar.
//The exit would be if the price crosses below the high of the signal bar – the ATR(14) at the time of the signal bar.
if c1 then
trigger = high
tp = atr*2
sl = atr
elsif c2 then
trigger = low
tp = atr*2
sl = atr
endif
if MAfast>MAslow and trigger>0 and close<trigger then
BUY maxrisk CONTRACTS AT trigger stop
endif
if MAfast<MAslow and trigger>0 and close>trigger then
SELLSHORT maxrisk CONTRACTS AT trigger stop
endif
set target profit tp
set stop loss sl