-
-
Save femtotrader/8fe99b5283106b4644cfceb75eff632d to your computer and use it in GitHub Desktop.
Exit trade first profitable price over entry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################### | |
# exit first profitable price over entry | |
# exit after fixed bars (max_hold) if no profitable trade | |
############################################################### | |
signal = [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0] | |
close = [10,10,8,3,14,5,6,2,4,22,2,1,1,1,10,3,3,3,3,3,3,30] | |
first_prof = zero(signal) | |
max_hold = 6 | |
for i = 1:length(signal) # 1:length(signal)-1 if buying next bar at open | |
if signal[i] == 1 | |
first_prof[i] == 1 | |
entry_price = close[i] # note if buy next bar open, change to open[i+1] to represent buying 1x bar ahead at open | |
j=i | |
n_travel = 0 | |
while(close[j] <= entry_price) | |
n_travel = n_travel + 1 | |
if n_travel == max_hold # set maximum bars to hold without a profitable trade | |
break | |
end | |
if j >= length(signal) # Check if n is bigger than the length of signal, if true break | |
break | |
end | |
first_prof[j] = 1 | |
j = j + 1 | |
if close[j] >= entry_price # include the bar that is higher than entry price | |
first_prof[j] = 1 | |
end | |
end | |
j=1 | |
end | |
end | |
############################################################### | |
# Do not carry trades over last bar of trade ie into evening or next day session | |
# exit first profitable price over entry | |
# exit after fixed bars (max_hold) if no profitable trade | |
############################################################### | |
signal = [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0] | |
close = [10,10,8,3,14,5,6,2,4,22,2,1,1,1,10,3,3,3,3,3,3,30] | |
time = ["09:00","09:30","10:00","10:30","11:00","11:30","12:00","12:30","13:00","13:30","14:00","14:30","15:00","09:00","09:30","10:00","10:30","11:00","11:30","15:00","12:30","13:00"] | |
first_prof = zero(signal) | |
max_hold = 6 | |
for i = 1:length(signal) # 1:length(signal)-1 if buying next bar at open | |
if signal[i] == 1 | |
first_prof[i] == 1 | |
entry_price = close[i] # note if buy next bar open, change to open[i+1] to represent buying 1x bar ahead at open | |
j=i | |
n_travel = 0 | |
while(close[j] <= entry_price) | |
n_travel = n_travel + 1 | |
if n_travel == max_hold # set maximum bars to hold without a profitable trade | |
break | |
end | |
if j >= length(signal) # Check if n is bigger than the length of signal, if true break | |
break | |
end | |
if time[j] == "15:00" | |
break | |
end | |
first_prof[j] = 1 | |
j = j + 1 | |
if close[j] >= entry_price # include the bar that is higher than entry price | |
first_prof[j] = 1 | |
end | |
end | |
j=1 | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment