Skip to content

Instantly share code, notes, and snippets.

@flare9x
Last active August 19, 2018 01:04
Show Gist options
  • Save flare9x/c1e3b36811055910a511c315a82c3625 to your computer and use it in GitHub Desktop.
Save flare9x/c1e3b36811055910a511c315a82c3625 to your computer and use it in GitHub Desktop.
Exit trade first profitable price over entry
###############################################################
# 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_price = [10,10,20,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_price[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_price[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_price[j] >= entry_price # include the bar that is higher than entry price
first_prof[j] = 1
end
end
j=1
end
end
test = hcat(signal,close_price,first_prof)
###############################################################
# 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_price = [10,10,8,3,14,5,6,2,4,22,2,1,1,1,10,3,3,3,3,3,3,30]
times = ["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_price[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_price[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 times[j] == "15:00"
break
end
first_prof[j] = 1
j = j + 1
if close_price[j] >= entry_price # include the bar that is higher than entry price
first_prof[j] = 1
end
end
j=1
end
end
##########################
# Ignore Repeat signals whilst in trade
##########################
signal = [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0]
close_price = [10,10,20,3,14,5,6,2,4,22,2,1,1,1,10,3,7,3,3,5,5,30]
first_prof = zero(signal)
let max_hold = 6, condition_i = 0
for i = 1:length(signal) # 1:length(signal)-1 if buying next bar at open
if signal[i] == 1 && i > condition_i
first_prof[i] == 1
entry_price = close_price[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_price[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_price[j] >= entry_price # include the bar that is higher than entry price
first_prof[j] = 1
end
end
condition_i = j
end
end
end
test = hcat(signal,close_price,first_prof)
###############################################################
# 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_price = [10,10,8,3,14,5,6,2,4,22,2,1,1,1,10,3,3,3,3,3,3,30]
times = ["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)
let max_hold = 6, condition_i = 0
for i = 1:length(signal) # 1:length(signal)-1 if buying next bar at open
if signal[i] == 1 && i > condition_i
first_prof[i] == 1
entry_price = close_price[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_price[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 times[j] == "15:00"
break
end
first_prof[j] = 1
j = j + 1
if close_price[j] >= entry_price # include the bar that is higher than entry price
first_prof[j] = 1
end
end
condition_i = j
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment