![]() ![]() When two bars close above the Exponential Moving Average (EMA), and that average is above the Simple Moving Average (SMA), we go long. The script below trades two moving averages. Let’s see how a full strategy can use the strategy.eventrades variable. When we do, Pine Script generates the ‘cannot use strategy functions in indicator script’ error. We can’t use strategy.eventrades in indicators.If we see break-even trades as losing trades, then the strategy’s total number of losses is: strategy.eventrades + strategy.losstrades.With the () function we find out which open trades have a break-even result. To know if the strategy’s current position has a break-even result, check if the strategy.openprofit variable equals ( =) zero. strategy.eventrades only measures closed trades.That gets us 9 break-even trades but only 1 break-even position. Say we scale into a position with 9 trades, and all close at break-even. These two differ, as one position can have several trades. strategy.eventrades measures break-even trades, not break-even positions.When the strategy closes 2 break-even trades on the same bar, the variable’s value jumps from 21 to 23. There’s no guarantee that the value of strategy.eventrades increases sequentially.With a lot of even trades, chances are we forgot to set those trading costs. The number of break-even trades is impacted by the strategy’s commission and slippage.On bar 250 the variable has a different value than on bar 15,700, if the strategy did break-even trades in the mean time. As the strategy processes the chart’s bars and generates trades, the value of strategy.eventrades changes.We can, for instance, calculate the Exponential Moving Average of the break-even trade count: // Calculate and plot the EMA of break-even trades plot( ta.ema( strategy.eventrades, 20), color = color.maroon , Because this variable has a value on every bar, functions can calculate with the data it holds. The third way to use strategy.eventrades is with a function. When we compare the current strategy.eventrades value with the previous bar value, we find out when the strategy closed a break-even trade: // Make the chart background orange when a new break-even trade happened bgcolor( strategy.eventrades > strategy.eventrades ? color.new( color.orange, 80) : na) Sometimes looking back just one bar helps too. In that case we code: // Go long when the 7-bar Relative Strength Index (RSI) crosses // 50 while the number of break-even trades is below 55 if ta.cross( ta.rsi( close, 7), 50) and strategy.eventrades 0 label.new( bar_index, high, text = "Did a break-even trade in the last 30 bars") Say we only want to trade a specific long entry when there are less than 55 break-even trades. The strategy can also use these comparisons with its order decisions. For example, to test if the break-even trade count is above 85 we do: // Verify if the number of break-even trades is above some threshold if strategy.eventrades > 85 label.new( bar_index, high, text = "Already more than 85 break-even trades!") To do so we insert strategy.eventrades at the code location where we want that info. The first option is to get the strategy’s current number of break-even trades. We can use the strategy.eventrades variable in several ways. If we use the variable on another bar than the last, it tells how many even trades the strategy did leading up to that bar. When the strategy processed all bars and gets to the chart’s end, strategy.eventrades returns the total number of break-even trades. It counts those trades from the chart’s first bar until the current bar. That variable returns an integer with how many trades closed with neither profit nor loss. In a Pine Script strategy, we get the number of break-even trades with the strategy.eventrades variable
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |