Hi Guys,
I just wanted to ask for a little bit of help adding something to my SMA system.
Here is my current system –
REM Money Management
Capital = 2000 // 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[MA1](close)
indicator2 = Average[MA2](close)
indicator3 = Average[MA3](close)
c1 = (indicator1 CROSSES OVER indicator2)
c2 = (indicator1 CROSSES UNDER indicator3)
if c1 and ControlBuy =0 then
ControlBuy=1
ControlSell=0
BUY maxrisk-0.5 PERPOINT AT MARKET
elsif c2 and ControlSell=0 then
ControlSell=1
ControlBuy=0
SELLSHORT maxrisk-0.5 PERPOINT AT MARKET
Endif
SET STOP LOSS SL
I am hoping to add the following to this.
When c1 occurs then wait x number of bars and if the price is above the price at the SMA crossover then buy, if price is under then no trade. And likewise for C2.
Basically, waiting to see if a trend becomes established by waiting x number of bars and cutting down the number of losing trades.
I hope that makes sense.
Automatic trading support go to ProOrder support… and please add code within the <> code button please! 😐
Ok, so firstly we save the BARINDEX of the bar that saw the moving averages cross. Before buying or selling, we verify if the trend if still the right one (no contrarian cross over have occurred since) and if we have sufficient bar elapsed since the cross over/under. You can adapt this bar quantity with the ‘barquantity’ variable in the code.
barquantity=5 //how many bars to wait after a signal to open a new order
REM Money Management
Capital = 2000 // 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[MA1](close)
indicator2 = Average[MA2](close)
indicator3 = Average[MA3](close)
c1 = (indicator1 CROSSES OVER indicator2)
c2 = (indicator1 CROSSES UNDER indicator3)
if c1 then
buybar=barindex
endif
if c2 then
sellbar=barindex
endif
stillbullish = buybar>sellbar
stillbearish = sellbar>buybar
if stillbullish and barindex-buybar>=barquantity then
buy 1 contract at market
endif
if stillbearish and barindex-sellbar>=barquantity then
sellshort 1 contract at market
endif
SET STOP LOSS SL
I did not test the code, please make your own tests and make feedback here.
I did not test it, please make your own tests and give feedback.
Hi Nicolas,
Thanks so much for coming back to me, oh no sorry was this in the wrong place? and I am sorry I will add the code using the button next time. I am still very much a newbie.
I will do some testing on the code and let you know. Thanks.
Matt
barquantity=5 //how many bars to wait after a signal to open a new order
REM Money Management
Capital = 2000 // 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
endif
if c2 then
sellbar=barindex
endif
stillbullish = buybar>sellbar
stillbearish = sellbar>buybar
if stillbullish and barindex-buybar>=barquantity then
buy maxrisk -0.5 perpoint at market
endif
if stillbearish and barindex-sellbar>=barquantity then
sellshort maxrisk-0.5 perpoint at market
endif
SET STOP LOSS 30
Hi Nicolas,
Thanks again, I had a go with the above and it’s not quite right. It’s taking a trade on every single bar!?
Just add this line of code at the top of the program:
defparam cumulateorders=false
It will prevent to add orders while we are already on market.
Thanks again Nicolas.
Something still doesn’t seem right. It is taking more trades than without your additional code. I can’t figure why that would be
If you restrict the length of each trade it is not so strange that the number of trades increases. Sometimes the new version with restriction takes a new trade when the old version is still on market with the first trade. Think about it.
Because there are more than 1 order for a single MA cross: If the first order is stopped by its stoploss, the trading conditions are still true and another order will be launched at that time.
Ok thank again for the response.
On my original code there would only be one trade taken for any MA cross over. Either C1 or C2. Why is it now launching more than one trade?
Maybe i’ve not explained it correctly. I don’t want to restrict the length of the trade.
I’ll explain again more clearly. When a cross over occurs I want to wait x number of candles sticks to see if the price is above the price it was when the cross over occurred. If it is then the system should take the trade at that moment, if it isn’t then don’t take a trade and wait for the next cross over to occur. Other than that I don’t want anything else to change to the system, if a trade is taken it should then continue until either my stop loss is hit or the exit condition is met.
On my original code there would only be one trade taken for any MA cross over. Either C1 or C2. Why is it now launching more than one trade?
Because code has changed a bit to reflect your strategy..
So, in this new version, I added only one trade per cross and if the Close is above / below the cross price when all other conditions are ok.
defparam cumulateorders=false
barquantity=5 //how many bars to wait after a signal to open a new order
REM Money Management
Capital = 2000 // 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
SET STOP pLOSS 30
Please note that in this version, orders are still exited by contrarian condition (a SELLSHORT is exited if new BUY conditions are met).
Thanks very much for your help with this.