I am trying to define a variable only once when running the algorithm. Since the backtest acts as a for loop for every minute, I am having a problem being able to do that. Essentially, I am trying to create a complex stop-loss logic:
1) Sell only if shares are bought
2) Sell 50% when the price falls by this much
3) Sell the rest of the 50% when the price falls by this much
This code example explains what I am trying to do.
# initialize variable
var = "Buy"
for x in range(0, 6): #a for loop example to mimic the backtest every minute loop
print "Starting Operation Num %d" % (x)
if var == "Buy":
print "Buy 100%"
var = "Sell"
elif var == "Sell":
print "Sell 50%"
var = "Sell 50%"
if var == "Sell 50%":
print "Sell 100%"
var = "Buy"
If there's another method of doing this (complex or nested stop-loss logic), feel free to mention it.