2

I have made a strategy that works well except the important bit which is buying and selling.

This is the relevant snippets of the code

// Variables
var float varEma20      = 0.0 //Signal Candle's 20 EMA which is gonna be used as the exit loss
var float varBuyPrice   = 0.0 //Signal Candle's high + 0.01% just above candle high
var float varTakeProfit = 0.0 //Risk/Reward ratio: 2

// Buy/Sell
if (buyCondition1 and buyCondition2 and buyCondition3... == true)
    strategy.order("buy", strategy.long, limit= varBuyPrice)

strategy.cancel("buy", when= close < varEma20) //cancel the buy order when a candle closes under the Signal Candle's 20 EMA
strategy.exit("buy", limit= varTakeProfit, comment="profit") //two times the risk
strategy.exit("buy", loss= varEma20, comment="loss") //loss when the price hits the Signal Candle's 20 EMA

// Drawings
plotshape(SignalCandleConfirmed ? varTakeProfit : na, title='take profit', style=shape.labeldown, location=location.absolute, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), size=size.tiny)
plotshape(SignalCandleConfirmed ? varBuyPrice   : na, title='buy price'  , style=shape.labeldown, location=location.absolute, color=color.new(color.gray, 0),  textcolor=color.new(color.white, 0), size=size.tiny)
plotshape(SignalCandleConfirmed ? varEma20      : na, title='take profit', style=shape.labeldown, location=location.absolute, color=color.new(color.red, 0),   textcolor=color.new(color.white, 0), size=size.tiny)
  • varTakeProfit is plotted as green dots
  • varBuyPrice⠀⠀ is plotted as grey dots
  • varEma20 ⠀⠀⠀⠀is plotted as red dots

The dots are plotted on the candle as long as the buy order is active:

enter image description here

As you can see in the picture, each variable is plotted correctly, but the buy/exit orders are not fulfilled correctly.

It seems to buys at the candle right after the Signal Candle.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
omar
  • 293
  • 3
  • 13
  • 1
    `strategy.exit("buy", loss= varEma20, comment="loss")` looks wrong. If varEma20 is a price, this line must looks like `strategy.exit("buy", stop = varEma20, comment="loss")` – Andrey D May 12 '22 at 10:19

0 Answers0