Hi All,
I would be very grateful for any help with this small sell side code.
The idea is for shorter time frames which I use for day trading but struggling to automate it.
Principles as below. Sell side only in this case.
- Two longer than average bars become the new ‘range’
- The middle of this range is the ‘mean’ for buying back.
- Once the range is set every new bar triggers a sell unless its low crosses below the mean
- At the ‘mean’ all shorts are bought back and the trade ends
Questions are one
- Is this code ok as I cannot backtest it because I dont know how to (2)
- How do I set a MAX position
DEFPARAM CumulateOrders = True //
Once Upper = 1
Once Lower = 1
c1 = abs(High[1] - Low[1]) > 1.5*averagetruerange[7]
c2 = abs(High[0] - Low[0]) > 0.8*abs(High[1] - Low[1])
c3 = abs(High[0] - Low[0]) < abs(High[1] - Low[1])
c4 = High[1] > High[0]
If c1 and c2 and c3 and c4 then
Upper = High[1]
Lower = Lowest[2](low)
Rangebar=1
Endif
If Rangebar=1 and Low > (Upper+Lower)/2 then
sellshort 0.1 lots at Upper + averagetruerange[7] limit
Endif
If Low crosses under (Upper+Lower)/2 then
Rangebar=0
Exitshort at (Upper+Lower)/2 limit
endif
Set stop $loss 500
Many thanks in advance
Is there anyone out there? 🙂
Your code looks OK.
To back test just press the ‘ProBacktest my system’ button.
[attachment file=83375]
If by MAX position you mean that you want to set a limit to the number of positions opened then you can do this with a condition based on COUNTOFPOSITION or by adding 1 to a variable IF ONMARKET and then resetting that variable IF NOT ONMARKET and then use that variable quantity in your conditions as to whether to put a sellshort order on the market.
Hi Vonasi,
Many thanks for your reply.
I have tried the back test but the problem I have is the position becomes to large.
I have 2 questions
- Is there a way to code maximum position size ie. Sell every bar until position reaches 10 lots.
- Also If I want to set a buy level at say the low[1] before the first lots is sold, how would I code this, as currently each new sell creates a new buy back level!
Once again this beginner really appreciates your time!
If Rangebar=1 and Low > (Upper+Lower)/2 and countofposition < 10 then
sellshort 0.1 lots at Upper + averagetruerange[7] limit
Endif
I’m not sure I fully understand question 2. Maybe this works as it fixes the levels until you have no trades on the market:
If not onmarket and c1 and c2 and c3 and c4 then
Upper = High[1]
Lower = Lowest[2](low)
Rangebar=1
Perfect. Problem solved. If it works I’ll post the full code. Thanks again