2

Being a beginner, I'm still confused; too many questions, to many options, it's a labyrinth for me yet, sorry for perhaps blunt questions…

Here's the header:

strategy(title="Testing Strategy Testing", initial_capital = 100000, default_qty_type = strategy.percent_of_equity, default_qty_value = 1, pyramiding = 20, currency = 'USD', overlay=true)

And here's the strategy entry code part (it could be a long-type one):

if ShortItPlease and in_date_range and enable_short_strategy == true
    strategy.entry("ShortItPlease", strategy.short, qty=1, when=ShortItPlease, alert_message="Open Short Position")
    strategy.exit( "ShortItPlease", qty=1, from_entry="Short", loss=short_stoploss_percentage, profit=short_takeprofit_percentage, alert_message="Your Short SL/TP Limit As Been Triggered.")
    strategy.close("ShortItPlease", when=when_specified, alert_message="Close Short Position")

So as you may see, default_qty_type = strategy.percent_of_equity is specified in the header, so is the default_qty_value = 1 but

  • in the strategy.entry() or strategy.exit() function (above), it seems we can only specify quantity, independently from the default_qty_value variable (if I don't specify qty then it results an error message),
  • and there's not even such variables as default_qty_type or default_qty_value, to re-use. These are not variables to be used in calculations, for example.

What to do different, to make these parameters affect the strategy's behavior? Perhaps I should use the following variables in the entry functions, adjusted properly? Goal is: set PT/SL based on percentage, order qty based on account balance percentage.

long_stoploss_price = strategy.position_avg_price * (1 - long_stoploss_value/100)
long_takeprofit_price = strategy.position_avg_price * (1 + long_takeprofit_value/100)
short_stoploss_price = strategy.position_avg_price * (1 + short_stoploss_value/100)
short_takeprofit_price = strategy.position_avg_price * (1 - short_takeprofit_value/100)
Vendrel
  • 865
  • 9
  • 19

2 Answers2

2

In general to answer "How to set up a Strategy properly?" I'd suggest the following:

  1. Keep your strategy settings like concerning initial_capital to a minimum. Fancy stuff but first your strategy logic should work at all
  2. Check if your trigger condition works. Place eg. just a label.new(bar_index, close, "works")line in there to make sure your strategy could be triggered there.
  3. Then place a simple order. Just strategy.entry('str1', strategy.long) or strategy.order(...) S/L/P etc. fancy stuff but first your strategy logic should work at all
  4. If everything is fine you can refine it step-by-step and add all the extra features.

In the strategy declaration you can define the default logic by that your positions and bids should work. Explanation Parameter list If you place your orders in your script you actually don't have to take care about these anymore.

To your specific example: strategy.entry() and strategy.order() are more or less interchangeable depending what you want to achieve just like strategy.exit()and strategy.close() (great examples in the link that you can learn from). My favorite one is strategy.order().

strategy.entry()in the opposite direction will close all other positions. (All long positions are going to be closed and "qty" short positions opened instead.) If that's a problem you have to go with strategy.order() instead.
strategy.close()does not have a when parameter. Your balance is strategy.equity that you can use to dynamically set the qty parameter on each order. You cannot define your SL in percentage. Like the description says, it has to be either in price or tick.
That's all you need to get back on track and work with your strategy.

elod008
  • 1,227
  • 1
  • 5
  • 15
0

Pick

LMK if this helps idk why I haven't encountered this problem before.. In the picture, all I did was typify it with float

FFriZz
  • 26
  • 2
  • I'm not sure it's relevant, it may not belong here (the code is useful though, I like it, I saved it). Perhaps you should remove it before an admin comes and do admin things. If you have a question, post a question. – Vendrel May 04 '22 at 17:19
  • were you responding to me? – FFriZz May 05 '22 at 21:45
  • I was suggesting typifying your exit conditions in the picture you can see that I got 2 different returns by having one typified and one not.. could be nothing tho – FFriZz May 05 '22 at 21:50
  • Oh, I see! It's useful but not for this problem. In my question, parameters don't take affect, typified or not. :) – Vendrel May 05 '22 at 22:05
  • I guess I should have read it better lol.. what error is it giving you is it a runtime error (the red exclamation point on the chart) or in the console of pine.. and your 'if ShortItPlease' I'm assuming this isn't a bool condition (T/F) since you have the enable_short_strategy.. your problem probably lies there you should take the exit and close out of the (if ShortItPlease) scope and give them their own.. and on the exit, you should subtract or add the % of the $ and instead of using profit= and loss= I use limit= and stop= at the price you want it to exit.. at least that's how i do it.. lmk – FFriZz May 06 '22 at 05:43