declare a strategy and draw the averages
A strategy declares itself with Two lines are drawn over the price bars. No orders are placed yet.
strategy(...) instead of indicator(...). A script has exactly one of the two. It may plot like an
indicator.enter when the fast average crosses above
talib.crossover(fast, slow) is true on the bar where fast moves from at or below slow to above it. strategy.entry(id, direction)
places an order to open a position: id names it, and direction is strategy.long or strategy.short.strategy.entry in the same direction is ignored, because pyramiding defaults to 1.close when it crosses back
strategy.close(id) closes the position opened under that id. talib.crossunder(fast, slow) is true where fast moves from at or
above slow to below it.strategy.entry("Short", strategy.short) there: an entry against an open position closes it and opens the new one.set up the account
The account is set in The compiler says: Namespace
strategy(...). Every argument is a fixed value, not a calculation. The ones used most:strategy.fixed is a constant. There is no strategy.cash or strategy.percent_of_equity:strategy has no variable percent_of_equity. To size by equity, pass the quantity to the order: strategy.entry("Long", strategy.long, qty=strategy.equity * 0.1 / close).show the position and the result
strategy.position_size is the signed size of the open position (positive long, negative short, zero flat). We shade the background
while long and give each order a comment. The trade list, equity and totals appear in the strategy’s results (see
Reading a strategy’s results); strategy.netprofit, strategy.equity and the other
strategy.* variables are readable in the script too.What we built
A long-only crossover strategy with inputs for both lengths, a 10,000 account, a 0.05 percent commission on each fill, and a green background while a position is open. The orders are simulated; to be told when a signal appears, use an alert (see Alert on your own condition).Next
Strategies and backtesting
how a strategy runs and what it can and cannot do.
Orders and positions
limit and stop orders,
strategy.exit, strategy.order.Reading a strategy's results
the trade list and the
strategy.* variables.