Skip to content

Instantly share code, notes, and snippets.

@flare9x
Last active August 18, 2018 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flare9x/64a41ec112261e790eab85d0f3616595 to your computer and use it in GitHub Desktop.
Save flare9x/64a41ec112261e790eab85d0f3616595 to your computer and use it in GitHub Desktop.
Trailing Point Exit
# Built on Julia 1.0
# Trailing point stop
# dummy h l c
c = [2648,2649.75,2654.5,2725.5,2732.75,2729.5,2728,2720.25,2726.75,(2763.25-50),2790.5,2765.25,2721.75,2665,2684.75,2740.25,2711,2742.25,2763.5,2798.75,2805.25,2784,(2763.75-70),2760,2748.75
,2724,2723.75,2692.25,2653.75,2633.5]
l = [2643.0, 2644.75, 2649.5, 2720.5, 2727.75, 2724.5, 2723.0, 2715.25, 2721.75, 2763.25, 2785.5, 2760.25, 2716.75, 2660.0, 2679.75, 2735.25, 2706.0, 2737.25, 2758.5, 2793.75, 2800.25, 2779.0, 2758.75, 2755.0, 2743.75, 2719.0, 2718.75, 2687.25, 2648.75, 2628.5]
h = [2656.0, 2657.75, 2662.5, 2733.5, 2740.75, 2737.5, 2736.0, 2728.25, 2734.75, 2776.25, 2798.5, 2773.25, 2729.75, 2673.0, 2692.75, 2748.25, 2719.0, 2750.25, 2771.5, 2806.75, 2813.25, 2792.0, 2771.75, 2968.0, 2756.75, 2732.0, 2731.75, 2700.25, 2661.75, 2641.5]
some_entry_signal = [0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0]
time_index = ["09:00","09:30","10:00","10:30","11:00","11:30","12:00","15:00","13:00","13:30","15:00","15:00","15:00","09:00","09:30","10:00","10:30","11:00","11:30","12:00","12:30","15:00","09:30","10:00",
"10:30","11:00","11:30","12:00","12:30","13:00"]
######################
# Trailing point stop
######################
# Set Trailing Stop Value
point_trail = 5
# Initialize output arrays
entry_out = zeros(size(c,1))
exit_out = zeros(size(c,1))
let p = point_trail,trade_state = "off"
for i = 1:size(l,1)
if some_entry_signal[i] == 1
trade_state = "on"
entry_out[i] = 1.0
else
entry_out[i] = 0.0
end
if trade_state == "on"
# Check if the close is below the low - the point trailing offset, if true, exit trade
if c[i] < l[i]-p
exit_out[i] = 2
trade_state == "off"
end
end
end
end
# Validate results
test = hcat(c,l,entry_out,exit_out)
###############################################
# Do not carry positions overnight
###############################################
######################
# Trailing point stop
######################
# Set Trailing Stop Value
point_trail = 5
# Initialize output arrays
entry_out = zeros(size(c,1))
exit_out = zeros(size(c,1))
let p = point_trail,trade_state = "off"
for i = 1:size(l,1)
if some_entry_signal[i] == 1
trade_state = "on"
entry_out[i] = 1.0
else
entry_out[i] = 0.0
end
if trade_state == "on"
# Check if the close is below the low - the point trailing offset, if true, exit trade
if c[i] < l[i]-p && time_index[i] != "15:00"
exit_out[i] = 2
trade_state == "off"
elseif trade_state == "on" && time_index[i] == "15:00"
exit_out[i] = 2.0
trade_state = "off"
end
end
end
end
# Validate results
test = hcat(c,l,entry_out,exit_out,time_index)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment