It is saying syntax error
this variable is not used in the code: maxbars
the following variable is undefined: maxdays
maxbars = 7
if onmarket and barindex - tradeindex >= maxdays then
sell at market
endif
I got it to work.
On the time code you gave me this suggests to only enter trades between 8am and 1pm, is this correct? If so how do I get it to work to only place trades between 11pm and 7am which is the NYE market for me in Australia?
starttime = 080000
endtime = 130000
timeok = opentime >= starttime and opentime <= endtime
if timeok and (your conditions) then
buy 1 contract at market
endif
starttime = 230000
endtime = 070000
It is saying syntax error
Sorry – my fault. I put MaxDays instead of MaxBars (I usually trade daily charts so use the MaxDays as a name but changed it to MaxBars for you but I missed one of them! I edited my post moments later but you were obviously too quick!
If so how do I get it to work to only place trades between 11pm and 7am which is the NYE market for me in Australia?
I just used those times as an example as you did not say what times you actually wanted. Time is in HHMMSS. So 130000 is 13Hr 0Min 0Sec and 144859 is 14Hr 48Min 59Sec. But you should be aware that the latter time does not actually exist in bar form on anything but second time frame charts.
starttime = 230000
endtime = 070000
timeok = opentime >= starttime and opentime <= endtime
Ok so when I use the above code I get no trades. I think it has to do with the start and end times?
starttime = 080000
endtime = 130000
timeok = opentime >= starttime and opentime <= endtime
But when I use the above code I get lots of entries?
starttime = 230000 endtime = 070000 timeok = opentime >= starttime and opentime <= endtime Ok so when I use the above code I get no trades. I think it has to do with the start and end times?
For me in the UK on GMT, starttime = 230000 endtime = 070000 is out of hours for USA Market.
You are GMT + 10 in Australia soooo, 🙂 oh it’s too early and the coffee has not woke my brain up yet, but you can see where I am coming from???
Use the link below to work out you have the correct times in your code for USA Market open in relation to the time standard you have set on your Platform etc?
https://www.worldtimezone.com/markets24.php
It is because you have your start time as a higher number than your endtime. If you want to straddle midnight then you have to change the AND to an OR
starttime = 230000
endtime = 070000
timeok = opentime >= starttime or opentime <= endtime
Ok will make these changes.
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = exponentialAverage[5](close)
indicator2 = exponentialAverage[10](close)
c1 = (indicator1 crosses over indicator2)
indicator3 = exponentialAverage[200](close)
c2 = (indicator2 > indicator3)
IF c1 AND c2 THEN
BUY 250/myatr contracts at market
ENDIF
// Conditions to enter short positions
indicator4 = exponentialAverage[5](close)
indicator5 = exponentialAverage[10](close)
c3 = (indicator4 crosses under indicator5)
indicator6 = exponentialAverage[100](close)
c4 = (indicator5 < indicator6)
IF c3 AND c4 THEN
SELLSHORT 250/myatr contracts at market
ENDIF
// Stops and targets
SET STOP $LOSS 250
SET TARGET $PROFIT 250
myATR = 4* (averagetruerange[24](medianprice))
//trailing stop function
trailingstart = 1*myatr //trailing will start @trailinstart points profit
trailingstep = 1*myatr //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
The above code wipes out my account balance and incurs losses of say $45k. So why if I reverse the buy and sell entries it does not produce the same level of profit being $45k?
I have not tested it but I would guess it is because bad entries coupled with bad exits do not change just because you are selling instead of buying.
I notice that you have the calculation for ‘myatr’ after you first use the variable myatr in the code so for the first bar myatr will equal zero until line 30 is first read. You should move it to higher to above line 12 at least.
I also notice that you are not testing with level stakes. Take out the money management until you know that you have a strategy that can win at a level stake of 1 and then see if you can improve it with the ATR based position sizing once you know your idea works. You are just muddying the water of your analysis of whether the basic idea works by including money management from the start.
It might also be useful for others to know what market and what time frame you are testing on.
I had no idea the line in which you entered the code had any effect on the calculation – THANK YOU, I will bring up to above line 12.
I will also remove the atr based stop for now.
I think this is why I am pulling my hair out. I am trying to develop a strategy at first with too many conditions. Do you suggest keep it basic and then add conditions?
I keep it very basic. If I can’t describe my strategy theory in one sentence then the idea is too complicated.
My best strategies might start off with just ten lines of code (maybe more if I am including the code from a home made indicator within the strategy). I analyse the output then optimize one thing at a time and re-analyse, then I start taking things out to see how important they are to the decision making process – the more conditions the more likely it is to be curve fitted. Every fixed variable is a potential curve fit – every single one must be analysed to compare with values either side of it. It is not until I am happy that my basic idea has been analysed to within an inch of its life that I will start thinking about money management or variable position sizing. Then I will forward test it in demo for a few months to decide if it really is something I want to risk actual money on.
The final strategy might be 100 lines of code but 90 of that will be money management options and 10 lines the actual strategy!
Others might do it differently but that is my method and philosophy on strategy coding.