Example of a One-Time Buy-and-Hold Forex Position
Summary
This code example shows a scheduled strategy that checks for its first trading iteration, creates a forex asset using a configurable currency symbol, and submits a buy-to-open order for a fixed quantity. Its daily sleep interval means the strategy is evaluated on a daily schedule, while the first-iteration condition prevents repeated entries. The script includes a live execution path and a short backtesting path with a benchmark asset.
Despite the description's reference to holding a future to expiry, the implementation creates a forex asset and contains no expiry handling, exit order, or position monitoring. It therefore illustrates a one-time order submission more than a complete hold-to-expiry method. The example also leaves price checks commented out and provides no results, risk controls, or validation. Its backtesting setup depends on an external data provider and an API credential, so it does not itself supply evidence about the strategy's performance or suitability.
Key ideas
- A first-iteration condition can limit a scheduled strategy to a single entry.
- The example creates a forex asset from a configurable symbol and submits a buy-to-open order.
- The strategy uses a daily schedule and includes both live and backtesting execution paths.
- No expiry logic, exit order, position monitoring, or performance evidence is provided.
Tags
Full text
# forex_hold_to_expiry.py
```py
from datetime import datetime
from lumibot.entities import Asset
from lumibot.strategies.strategy import Strategy
"""
Strategy Description
An example strategy for buying a future and holding it to expiry.
"""
class FuturesHoldToExpiry(Strategy):
parameters = {
"buy_symbol": "GBP",
}
# =====Overloading lifecycle methods=============
def initialize(self):
# Set the initial variables or constants
# Built in Variables
self.sleeptime = "1D"
def on_trading_iteration(self):
"""Buys the self.buy_symbol once, then never again"""
buy_symbol = self.parameters["buy_symbol"]
# What to do each iteration
#underlying_price = self.get_last_price(underlying_asset)
#self.log_message(f"The value of {buy_symbol} is {underlying_price}")
if self.first_iteration:
# Calculate the strike price (round to nearest 1)
# Create futures asset
asset = Asset(
symbol=buy_symbol,
asset_type="forex",
)
# Create order
order = self.create_order(
asset,
10,
"buy_to_open",
)
# Submit order
self.submit_order(order)
# Log a message
self.log_message(f"Bought {order.quantity} of {asset}")
if __name__ == "__main__":
is_live = True
if is_live:
strategy = FuturesHoldToExpiry()
strategy.run_live()
else:
from lumibot.backtesting import PolygonDataBacktesting
# Backtest this strategy
backtesting_start = datetime(2023, 10, 19)
backtesting_end = datetime(2023, 10, 24)
results = FuturesHoldToExpiry.backtest(
PolygonDataBacktesting,
backtesting_start,
backtesting_end,
benchmark_asset="SPY",
polygon_api_key="YOUR_POLYGON_API_KEY_HERE", # Add your polygon API key here
)
```Shown in full with attribution under the source's licence. Licence: GPL-3.0
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.