kurtsmock

[FN] Strategy - Store Level on Condition

kurtsmock Telah dikemas kini   
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.
Nota Keluaran:
- fixed notes

Skrip sumber terbuka

Dalam semangat TradingView yang sebenar, penulis skrip ini telah menerbitkannya dengan menggunakan sumber terbuka supaya pedagang-pedagang dapat memahami dan mengesahkannya. Sorakan kepada penulis! Anda dapat menggunakannya secara percuma tetapi penggunaan semula kod ini dalam penerbitan adalah dikawalselia oleh Peraturan Dalaman. Anda boleh menyukainya untuk menggunakannya pada carta.

Penafian

Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.

Ingin menggunakan skrip ini pada carta?