ProRealCode - Trading & Coding with ProRealTime™
I leave the sale on the Marketplace to the Swedes… 😉 Well, a little more than 1 trade a day, short ends every day and long on Friday at the latest. There are actually no long-term trades.
Good idea – think I’ll wait to pay for the Nordic version of this code posted here too :)!
I have modified my previous version (v4) in order to reduce the DD. In this version (v5) the longterm average (480 weekly) has been replaced with two weighted averages and has been added a filter to avoid operations in a market with low volatility (image below : 100k)
//TS MULTIAVERAGE SP500 v5 - CFD 1 euro - Spread 0.6
defparam CUMULATEORDERS = false
positionSize = 5
//---------------------------------------------------------------------------------------
timeFrame(15 minute, updateOnClose) //TF 15 minutes
c1L = close > average[500,2](close)
c1S = close < average[450,2](close)
avg1 = average[15,0](close)
avg2 = average[55,0](close)
avg3 = average[14,1](close)
avg4 = average[20,1](close)
c2L = avg1 > avg2
c2S = avg3 < avg4
avgTrigger = average[5,0](close)
c3L = avgTrigger crosses over avg1
c3S = avgTrigger crosses under avg1
//---------------------------------------------------------------------------------------
timeFrame(default) //TF 5 minutes = DEFAULT
//------------------------------------------------
period = 110 //low volatility filter (MauroPro)
minLevel = 10 *pointsize
amplitudeRange = highest[period](high)-lowest[period](low)
cMinRange = amplitudeRange > minLevel
//-------------------------------------------------
cLong = c1L and c2L and c3L
cShort = c1S and c2S and c3S
cLongExit = c1S
cShortExit = c1L
//--------------------------------------------------------------------------------------
ONCE buyTime = 110000
ONCE sellTime = 213000
ONCE buyTimeShort = 150000
ONCE sellTimeShort = 213000
//--------------------------------------------------------------------------------------
if time >= buyTime and time <= sellTime then //LONG POSITION: TECHNICAL ENTRY
if cLong and cMinRange then
buy positionSize contract at market
endif
endif
if longOnMarket and cLongExit then //LONG POSITION: TECHNICAL EXIT
sell positionSize contract at market
endif
//----------------------------
if time >= buyTimeShort and time <= sellTimeShort then //SHORT POSITION: TECHNICAL ENTRY
if cShort and cMinRange then
sellshort positionSize contract at market
endif
endif
if shortOnMarket and cShortExit then //SHORT POSITION: TECHNICAL EXIT
exitShort positionSize contract at market
endif
//----------------------------------------------------------------------------------------
if longOnMarket then //SL & TP Exit
set stop %loss 1.7
elsif shortOnMarket then
set stop %loss 0.5
endif
if longOnMarket then
set target %profit 1
elsif shortOnMarket then
set target %profit 1
endif
//------------------------------------------------------------------------------------
if time = 223000 then //time Exit
//sell at market
exitShort at market
endif
if time = 225500 and dayOfWeek=5 then
sell at market
exitShort at market
endif
//--------------------------------------------------------------------------------------------
DirectionSwitch = (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket) //TrP Exit (Gozzi
IF Not OnMarket OR DirectionSwitch THEN
TrailStart = 40 // Start trailing profits
PointToKeep = 0.2 // 20% Profit percentage to keep when setting BreakEven
StepSize = 5 // Point to increase Percentage
PerCentInc = 0.2 // 20% PerCent increment after each StepSize Chunk
RoundTO = -0.5 //-0.5 rounds always to Lower integer, 0 defaults PRT behaviour
PriceDistance = 6* pipsize //minimun distance from current price
maxProfitL = 0
maxProfitS = 0
ProfitPerCent = PointToKeep //reset to desired default value
SellPriceX = 0
SellPrice = 0
ExitPriceX = 9999999
ExitPrice = 9999999
ELSE
IF PositionPrice <> PositionPrice[1] AND (ExitPrice + SellPrice) <> 9999999 THEN //go on only if Trailing Stop had already started trailing
IF LongOnMarket THEN
newSlL = PositionPrice + ((close - PositionPrice) * ProfitPerCent) //calculate new SL
SellPriceX = max(max(SellPriceX,SellPrice),newSlL)
SellPrice = max(max(SellPriceX,SellPrice),PositionPrice + (maxProfitL * pipsize)) //set exit price to whatever grants greater profits, comopared to the previous one
ELSIF ShortOnMarket THEN
newSlS = PositionPrice - ((PositionPrice - close) * ProfitPerCent)
ExitPriceX = min(min(ExitPriceX,ExitPrice),newSlS)
ExitPrice = min(min(ExitPriceX,ExitPrice),PositionPrice - (maxProfitS * pipsize))
ENDIF
ENDIF
ENDIF
//---------------------------------------------------------------------------------------------------------------------------------------------------
IF LongOnMarket AND close > (PositionPrice + (maxProfitL * pipsize)) THEN //LONG positions
// compute the value of the Percentage of profits, if any, to lock in for LONG trades
profitL = (close - PositionPrice) / pipsize //convert price to pips
IF profitL >= TrailStart THEN // go ahead only if N+ pips
Diff1 = abs(TrailStart - profitL) //difference from current profit and TrailStart
Chunks1 = max(0,round((Diff1 / StepSize) + RoundTO)) //number of STEPSIZE chunks
ProfitPerCent = PointToKeep + (PointToKeep * (Chunks1 * PerCentInc)) //compute new size of ProfitPerCent
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%
maxProfitL = max(profitL * ProfitPerCent, maxProfitL)
ENDIF
ELSIF ShortOnMarket AND close < (PositionPrice - (maxProfitS * pipsize)) THEN //SHORT positions
profitS = (PositionPrice - close) / pipsize
IF profitS >= TrailStart THEN
Diff2 = abs(TrailStart - profitS)
Chunks2 = max(0,round((Diff2 / StepSize) + RoundTO))
ProfitPerCent = PointToKeep + (PointToKeep * (Chunks2 * PerCentInc))
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent))
maxProfitS = max(profitS * ProfitPerCent, maxProfitL)
ENDIF
ENDIF
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
IF maxProfitL THEN //LONG positions - Place pending STOP order when maxProftiL > 0 (LONG positions)
SellPrice = max(SellPrice,PositionPrice + (maxProfitL * pipsize)) //convert pips to price
IF abs(close - SellPrice) > PriceDistance THEN
IF close >= SellPrice THEN
SELL AT SellPrice STOP
ELSE
SELL AT SellPrice LIMIT
ENDIF
ELSE
SELL AT Market
ENDIF
ENDIF
IF maxProfitS THEN
ExitPrice = min(ExitPrice,PositionPrice - (maxProfitS * pipsize)) //SHORT positions
IF abs(close - ExitPrice) > PriceDistance THEN
IF close <= ExitPrice THEN
EXITSHORT AT ExitPrice STOP
ELSE
EXITSHORT AT ExitPrice LIMIT
ENDIF
ELSE
EXITSHORT AT Market
ENDIF
ENDIF
//------------------------------------------------------------------------------------------------------------------------
Thank you. This is also a good option. I can see that this strategy structure seems to offer a lot of possibilities. The main filter is a bit slower, is that better? I’m not at the computer right now… could you please post the long/short split?
Hi Phoentzs, the test was done with 4 contracts and not 5 as the original (I had put four to better split the operations and I forgot to change the positionSize).
I also have noticed that the system remains short for a very little time.
I had the feeling with my tests too. I think it’s because a fast trailing is responsible in the short. And not to forget, every evening shorts will be closed. I found that the index recovered overnight very often and then an open short position is not good.
Hello,
Sorry I ‘m new, and I try to back test this bot NAS -5- MA Cross-V3, and after the 15th of February 2022 none position, the Bot stop until today. Maybe somebody have a answer or maybe somebody can help me. In advance thank you.
Regards
Chris
It would be worth you trying one of the other Systems off the List – View All Attachments – at the top left of this page … see if you get the same limitations re dates etc?
Oh, which one? 😉
The one CRISRJ refers to is Attachment 5, so I’m suggestig he tries, maybe Attachment 15?
Dear GraHal,
In first thanks a lot for your quick reply.
To be honest I try all the systems but this bot is very good and he never took position (now more of one year) and I don’t understand why, I try to check the code and with my level I see nothing is the reason why I ask for the experts.
Regards
Cris
Have you tried another bot? If yes, which one? The bot’s filters are designed for two things. Set the trend once and then keep the bot out of trouble and sideways phases. Maybe it’s because? There haven’t been many longer trends this year. But I can’t imagine that there was no trade at all. Maybe trading time? The bots are set to German time. Could you please post the relevant code?
Here’s the code …
CRISRJ is correct … no trades at all after 16 Feb 22!
//================================================
// Code: NAS 5m MACross v3
// Version 3
// Index: NASDAQ
// TF: 5 min
// Spread: 1
// Date: 27/01/2022
// Notes: added Tradetime
// changed SL, TP and Trailstart to percentage
// 3 MTF levels, 4h, 15m, 5m
// added max no. bars for any trade (EZT)
//
//================================================
DEFPARAM CUMULATEORDERS = FALSE
DEFPARAM preloadbars = 10000
//MONEY MANAGEMENT II
MM = 0 // = 0 for optimization
if MM = 0 then
positionsize = 0.5
ENDIF
if MM then
MinSize = 0.5 // IG minimum position size allowed
MaxSize = 2000 // IG tier 2 margin limit
ProfitAccrued = 0 // when restarting strategy, enter profit or loss to date in instrument currency
DD = 486 //MinSize drawdown in instrument currency
Multiplier = 3 //drawdown multiplier
Capital = DD * Multiplier
Equity = Capital + ProfitAccrued + StrategyProfit
PositionSize = Max(MinSize, Equity * (MinSize/Capital))
if positionsize > MaxSize then
positionsize = MaxSize
endif
PositionSize = Round(PositionSize*100)
PositionSize = PositionSize/100
ENDIF
//Tradetime
//adjustment for American Daylight Savings time
ADLS =1
if ADLS then
DLS =(Date >= 20100314 and date <=20100328) or (Date >= 20101031 and date <=20101107) or (Date >= 20110313 and date <=20110327) or (Date >= 20111030 and date <=20111106) or (Date >= 20120311 and date <=20120325) or (Date >= 20121028 and date <=20121104) or (Date >= 20130310 and date <=20130331) or (Date >= 20131027 and date <=20131103) or (Date >= 20140309 and date <=20140330) or (Date >= 20141026 and date <=20141102) or (Date >= 20150308 and date <=20150329) or (Date >= 20151025 and date <=20151101) or (Date >= 20160313 and date <=20160327) or (Date >= 20161030 and date <=20161106) or (Date >= 20170312 and date <=20170326) or (Date >= 20171030 and date <=20171105) or (Date >= 20180311 and date <=20180325) or (Date >= 20181028 and date <=20181104) or (Date >= 20190310 and date <=20190331) or (Date >= 20191027 and date <=20191103) or (Date >= 20200308 and date <=20200329) or (Date >= 20201025 and date <=20201101) or (Date >= 20210314 and date <=20210328) or (Date >= 20211031 and date <=20211107) or (Date >= 20220313 and date <=20220327) or (Date >= 20221030 and date <=20221106) or (Date >= 20230312 and date <=20230326) or (Date >= 20231029 and date <=20231105) or (Date >= 20240310 and date <=20240331) or (Date >= 20241027 and date <=20241103)
If DLS then
Tradetime = time >=133000 and time <200000
elsif not DLS then
Tradetime = time >=143000 and time <210000
endif
endif
if not ADLS then
Tradetime = time >=143000 and time <210000
endif
//Long Entry Filter
Timeframe(4 hours)
FMA1 = average[p,t](typicalprice)
FMA2 = average[p1,t](typicalprice)
cb1 = FMA1 > FMA2
//cs1 = FMA1 < FMA2
Timeframe(15 minutes)
cb2 = close>average[p2,t2](typicalprice)
//cs2 = close<average[p2,t2](typicalprice)
Timeframe(Default)
//Long Entry Criteria
MA1=average[p3,t3](typicalprice)
MA2=average[p4,t3](typicalprice)
cb3 = MA1 crosses over MA2
//cs3 = MA1 crosses under MA2
// Conditions to enter long positions
If Tradetime and cb1 and cb2 and cb3 Then
Buy PositionSize CONTRACTS AT MARKET
ENDIF
// Conditions to exit long positions
//if longonmarket and positionperf>0 and cs3 then
//sell at market
//endif
// Conditions to enter short positions
//If close <= Lowest[Sbars](OPEN) then
//Buy PositionSize CONTRACTS AT MARKET
//ENDIF
// Stops and targets
//slvalue=close[1]/153*1.6
//graph slvalue
//
//tpvalue=close[1]/88*1.6
//graph tpvalue
SET STOP %LOSS sl
SET TARGET %PROFIT tp
// Break even and trailing stop
// https://www.prorealcode.com/topic/breakeeven-trailing-profit/
IF Not OnMarket THEN
//
// when NOT OnMarket reset values to default values
ts = (tradeprice*pc)/100 // % trailing start
TrailStart = ts //30 Start trailing profits from this point
BasePerCent = base // 0.200 20.0% Profit percentage to keep when setting BerakEven
StepSize = ss //10 Pip chunks to increase Percentage
PerCentInc = pci // 0.100 10.0% PerCent increment after each StepSize chunk
BarNumber = bn //10 Add further % so that trades don't keep running too long
BarPerCent = bpc // 0.100 10% Add this additional percentage every BarNumber bars
RoundTO = -0.5 //-0.5 rounds always to Lower integer, +0.4 rounds always to Higher integer, 0 defaults PRT behaviour
PriceDistance = 5 * pipsize //IG minimun distance from current price
y1 = 0 //reset to 0
y2 = 0 //reset to 0
ProfitPerCent = BasePerCent //reset to desired default value
TradeBar = BarIndex
ELSIF LongOnMarket AND close > (TradePrice + (y1 * pipsize)) THEN
//LONG positions
//
// compute the value of the Percentage of profits, if any, to lock in for LONG trades
//
x1 = (close - tradeprice) / pipsize //convert price to pips
IF x1 >= TrailStart THEN // go ahead only if N+ pips
Diff1 = abs(TrailStart - x1) //difference from current profit and TrailStart
Chunks1 = max(0,round((Diff1 / StepSize) + RoundTO)) //number of STEPSIZE chunks
ProfitPerCent = BasePerCent + (BasePerCent * (Chunks1 * PerCentInc)) //compute new size of ProfitPerCent
// compute number of bars elapsed and add an additionl percentage
// (this percentage is different from PerCentInc, since it's a direct percentage, not a Percentage of BasePerCent)
// (if BasePerCent is 20% and this is 10%, the whole percentage will be 30%, not 22%)
BarCount = BarIndex - TradeBar
IF BarCount MOD BarNumber = 0 THEN
ProfitPerCent = ProfitPerCent + BarPerCent
ENDIF
//
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%
y1 = max(x1 * ProfitPerCent, y1) //y1 = % of max profit
ENDIF
ELSIF ShortOnMarket AND close < (TradePrice - (y2 * pipsize)) THEN
//SHORT positions
//
// compute the value of the Percentage of profits, if any, to lock in for SHORT trades
//
x2 = (tradeprice - close) / pipsize //convert price to pips
IF x2 >= TrailStart THEN // go ahead only if N+ pips
Diff2 = abs(TrailStart - x2) //difference from current profit and TrailStart
Chunks2 = max(0,round((Diff2 / StepSize) + RoundTO)) //number of STEPSIZE chunks
ProfitPerCent = BasePerCent + (BasePerCent * (Chunks2 * PerCentInc)) //compute new size of ProfitPerCent
// compute number of bars elapsed and add an additionl percentage
// (this percentage is different from PerCentInc, since it's a direct percentage, not a Percentage of BasePerCent)
// (if BasePerCent is 20% and this is 10%, the whole percentage will be 30%, not 22%)
BarCount = BarIndex - TradeBar
IF BarCount MOD BarNumber = 0 THEN
ProfitPerCent = ProfitPerCent + BarPerCent
ENDIF
//
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%
y2 = max(x2 * ProfitPerCent, y2) //y2 = % of max profit
ENDIF
ENDIF
IF y1 THEN //Place pending STOP order when y1 > 0 (LONG positions)
SellPrice = Tradeprice + (y1 * pipsize) //convert pips to price
//
// check the minimun distance between ExitPrice and current price
//
IF abs(close - SellPrice) > PriceDistance THEN
//
// place either a LIMIT or STOP pending order according to current price positioning
//
IF close >= SellPrice THEN
SELL AT SellPrice STOP
ELSE
SELL AT SellPrice LIMIT
ENDIF
ELSE
//
//sell AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price
//
SELL AT Market
ENDIF
ENDIF
IF y2 THEN //Place pending STOP order when y2 > 0 (SHORT positions)
ExitPrice = Tradeprice - (y2 * pipsize) //convert pips to price
//
// check the minimun distance between ExitPrice and current price
//
IF abs(close - ExitPrice) > PriceDistance THEN
//
// place either a LIMIT or STOP pending order according to current price positioning
//
IF close <= ExitPrice THEN
EXITSHORT AT ExitPrice STOP
ELSE
EXITSHORT AT ExitPrice LIMIT
ENDIF
ELSE
//
//ExitShort AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price
//
EXITSHORT AT Market
ENDIF
ENDIF
//EXIT ZOMBIE TRADE
EZT = 1
if EZT then
IF (longonmarket and barindex-tradeindex(1)>= b1 and positionperf>0) or (longonmarket and barindex-tradeindex(1)>= b2 and positionperf<0) then
sell at market
endif
IF (shortonmarket and barindex-tradeindex(1)>= 4000 and positionperf>0) or (shortonmarket and barindex-tradeindex(1)>= 4000 and positionperf<0) then
exitshort at market
endif
endif
Simple Moving Average Crossover Strategy
This topic contains 95 replies,
has 12 voices, and was last updated by CRISRJ
3 years, 1 month ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 01/26/2022 |
| Status: | Active |
| Attachments: | 39 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.