Search code examples
pine-scripttrading

Pinescript / Tiered Strategy Exits not executing


var value = 0.0 
if bar_index > 1000 
    value := close[1000]
else    
    value := close[bar_index]


var runups = array.new_float(10, value * .01)
var drawdowns = array.new_float(10, value * .01)
avgrunup = array.avg(runups)
avgdrawdown = array.avg(drawdowns)
price_action = (avgrunup / 3) * .01
if strategy.opentrades == 0 and array.get(tracker,0) > 0 
    strategy.entry("Enter Long", strategy.long)
    strategy.exit("Exit Long 1%",from_entry = "Enter Long",limit = (strategy.opentrades.entry_price(0) * price_action) + strategy.opentrades.entry_price(0),qty_percent = 20)
    strategy.exit("Exit Long 1.25%",from_entry = "Enter Long",limit = (strategy.opentrades.entry_price(0) * (price_action * 2)) + strategy.opentrades.entry_price(0), qty_percent = 20)
    strategy.exit("Close Trade 1.5%",from_entry = "Enter Long",limit = (strategy.opentrades.entry_price(0) * (price_action * 3)) + strategy.opentrades.entry_price(0), qty_percent = 20)
    strategy.exit("Close Trade 2%",from_entry = "Enter Long",limit = (strategy.opentrades.entry_price(0) * (price_action * 4)) + strategy.opentrades.entry_price(0), qty_percent = 20)
    strategy.exit("Close Trade 3%",from_entry = "Enter Long",limit = (strategy.opentrades.entry_price(0) * (price_action * 5)) + strategy.opentrades.entry_price(0),qty_percent = 20)
    //strategy.exit("Exit Long 1%",from_entry = "Enter Long",profit = (strategy.opentrades.entry_price(0) * price_action) / syminfo.mintick,qty_percent = 20)
    //strategy.exit("Exit Long 1.25%",from_entry = "Enter Long",profit = (strategy.opentrades.entry_price(0) * (price_action * 2)) / syminfo.mintick, qty_percent = 20)
    //strategy.exit("Close Trade 1.5%",from_entry = "Enter Long",profit = (strategy.opentrades.entry_price(0) * (price_action * 3)) / syminfo.mintick, qty_percent = 20)  
    //strategy.exit("Close Trade 2%",from_entry = "Enter Long",profit = (strategy.opentrades.entry_price(0) * (price_action * 4)) / syminfo.mintick, qty_percent = 20)
    //strategy.exit("Close Trade 3%",from_entry = "Enter Long",profit = (strategy.opentrades.entry_price(0) * (price_action * 5)) / syminfo.mintick,qty_percent = 20)



plot((strategy.opentrades.entry_price(0) * price_action) + strategy.opentrades.entry_price(0))
plot((strategy.opentrades.entry_price(0) * (price_action * 2)) + strategy.opentrades.entry_price(0))
plot((strategy.opentrades.entry_price(0) * (price_action * 3)) + strategy.opentrades.entry_price(0))
plot((strategy.opentrades.entry_price(0) * (price_action * 4)) + strategy.opentrades.entry_price(0))
plot((strategy.opentrades.entry_price(0) * (price_action * 5)) + strategy.opentrades.entry_price(0))

var percent_up = 0.00
var percent_down = 0.00

if strategy.opentrades != 0 and array.get(tracker,0) < 0
    strategy.close("Enter Long")
    percent_up := (strategy.closedtrades.max_runup(strategy.closedtrades - 1) - (strategy.closedtrades.size(strategy.closedtrades - 1) * strategy.closedtrades.entry_price(strategy.closedtrades - 1)) / (((strategy.closedtrades.size(strategy.closedtrades - 1) * strategy.closedtrades.entry_price(strategy.closedtrades - 1)) + (strategy.closedtrades.max_runup(strategy.closedtrades - 1))) / 2)) * .01
    percent_down := (strategy.closedtrades.max_drawdown(strategy.closedtrades - 1) - (strategy.closedtrades.size(strategy.closedtrades - 1) * strategy.closedtrades.entry_price(strategy.closedtrades - 1)) / (((strategy.closedtrades.size(strategy.closedtrades - 1) * strategy.closedtrades.entry_price(strategy.closedtrades - 1)) + (strategy.closedtrades.max_runup(strategy.closedtrades - 1))) / 2)) * .01

    strategy.close_all()
    array.unshift(runups,percent_up)
    array.unshift(drawdowns,percent_down)
    

plot(percent_up, color = color.rgb(82, 255, 91), title = "Previous Max Runup")
plot(percent_down, color = color.rgb(250, 74, 74),title = "Previous Max DrawDown")
plot(avgrunup, color = color.green, title = "AvgAdjusted RunUp")
plot(avgdrawdown, color = color.rgb(255, 59, 59), title = "AvgAdjusted DrawDown")

Hello, hoping to get some help. Up until this point in the code I have created my own indicator using weighted instances of a bunch of other technicals put together and reduced down into an int "score" for buying and selling. The idea of the strategy is to use one main "buy / sell" that in the second if statement after closing tracks the run up and drawdown during the trade but to also set 5 limit sell orders that are tiered and set by the avg of the arrays that track the runup percentages and split into 1/3rds. The strategy opens perfectly so I know that the code is making it into the if statement. However, the following exits never work and the strategy never closes until it hits the second "if" statement with the strategy.close("Enter Long").

The plots below show that my price points are exactly where I want them to be when the trade opens so they should not be a problem.

If anybody can see what I've done wrong please help. Hopefully I have explained clear enough.

I have tried both using limit= and profit= and neither seem to want to work. The trades will show up and close if I put them outside of the if statement but the price points then are not correct and the trades all close immediately. As far as I can tell this is where you are supposed to put the strategy.exit() calls.


Solution

  • You have several problems here:

    1. Your strategy.exit will never be executed because they are called only when there are no open trades, i.e. when your condition strategy.opentrades == 0 and array.get(tracker,0) > 0 is fulfilled, you create a market order to enter, which will be executed on the next tick, and immediately create limit orders to exit, with the exit condition depending on the entry price of the first open trade , but at this point you have not yet filled your entry order, hence the entry price of the first trade is na, any calculations with na will be equal to na, so your exit order does not have an exit price. Since the strategies are calculated once at the time of closing, your created order will be executed at the opening of the next bar, it will be recalculated at its close, but your condition will be false, since strategy.opentrades is no longer equal to 0. To fix this, you need to remove the exits from the conditions, they can be called on each bar, because. if the ids are the same, they do not create new exit orders, but only modify the old ones, then with the same price_action value, it does not matter if they are executed once or on each bar.
    2. You use strategy.opentrades.entry_price(0), the first argument of this function is responsible for the trade number. 0 - this is the very first open trade, if you use pyramiding and open several trades, you can get into a situation when the exit price will be tied to the first trade, instead of the current one, a more reliable way would be to use strategy.opentrades.entry_price(strategy. opentrades-1)