kurtsmock

[FN] Strategy - Store Level on Condition

kurtsmock Diupdate   
This is a function that you can use in strategies. Not a strategy in and of itself.
Example thumbnail is showing the function applied to a strategy.

Oftentimes, I am asked a question regarding how to hold a variable at a specific, constant level over a conditional period of time. This question is always asked in a very long convoluted way like "I want the strategy to know what the high of the last pivot was while I'm in a long." or some other variation of wanting a script to remember something from prior bars.

This function is designed to store a price or some numeric level on the bar that your conditional (bool) statements determine that it should be stored. In this construct, you would set conditional statement(s) to flip the 'hold condition' to be true on the next bar, then hold that value until either the "hold condition" is no longer true or the initial conditions trigger again, causing an update to the level that you want to capture.

You still have to come up with the logic for the start condition and hold condition on your own, but I've provided an example that should give you an idea of how to accomplish this and customize/deploy the function for your purposes.

The function will return 'na' when neither the start condition nor hold condition are true. There's multiple ways to implement this and variations on how the level is chosen. I've written extensive notes in the script to guide you through the logic behind the function. My hope is that it will be useful to those trying to build strategies or anyone attempting to get their script to remember a level under given conditions.

In the thumbnail example, the take profit level is defined at the beginning of the trade and held until the take profit order executes. The order execution is a separate matter. However, storing the take-profit level at a static value is key to telling the strategy.exit() function what price to execute a limit exit order at.
Example: strategy.exit("Exit Long", from_entry = "long", qty_percent = 100, limit = stored_value)

Let me know how it works out for you and if I can be of any assistance.

Note: Strategy results are mainly derived from the fact that the strategy is long-only, the NQ only goes up, and there is no stop loss in place. So don't ask for the specific strategy, because unless you're trading a single contract with a $500,000 account, you'll probably get liquidated using this strategy as it is presented.
Catatan Rilis:
- fixed notes

Skrip open-source

Dalam semangat TradingView, penulis dari skrip ini telah mempublikasikannya ke sumber-terbuka, maka trader dapat mengerti dan memverifikasinya. Semangat untuk penulis! Anda dapat menggunakannya secara gratis, namun penggunaan kembali kode ini dalam publikasi diatur oleh Tata Tertib. Anda dapat memfavoritkannya untuk digunakan pada chart

Pernyataan Penyangkalan

Informasi dan publikasi tidak dimaksudkan untuk menjadi, dan bukan merupakan saran keuangan, investasi, perdagangan, atau rekomendasi lainnya yang diberikan atau didukung oleh TradingView. Baca selengkapnya di Persyaratan Penggunaan.

Inggin menggunakan skrip ini pada chart?