I have some basic MA cross strategy indicator and I would like to implement better strategy, to sell only when price is higher than was bought before, but I am not sure how to do it in PINE syntax, any idea pls ?
Here is simple code, working fine this one, it open LONG or close LONG depends on cross MA :
// Strategy functions
if (crossover(outShort,outLong))
strategy.entry(id="Long", long=strategy.long)
if (crossunder(outShort,outLong))
strategy.close(id="Long")
And here is my problematic code, I would like to implement to sell only when price is higher than was bought before, so I added condition to check new price price < close
, but not sure how to write in Pine syntax
// Strategy implemented - but with errors
var entryprice = 0
if (crossover(outShort,outLong))
entryprice := close
strategy.entry(id="Long", long=strategy.long)
if (crossunder(outShort,outLong)) and ( entryprice < close )
strategy.close(id="Long")
Here is screenshot, logic still not works as expected, if condition entryprice < close
not works
Excelent, By the way do you know why my initial_capital value =1000 defined in strategy not correspond to the profit ? even if I change the capital number, profit show still same results.
//@version=4
strategy(title="My_MA_strategy", shorttitle="MyMA Strategy_0.04", format=format.price, initial_capital=1000, overlay=true)
E.g. 1st trade show profit $154, even there is only 0,26% profit ( from 1000 capital not possible) Percentage shows OK, but profit not.
Tried to change Market tpe , but no progress here, still show same profit
Use:
var float price = 0.0
or
var float price = 0
or
var price = 0.
so that your variable is declared as type "float". The first, more explicit form is preferable because it declares your intent more clearly.
[EDIT 2021.05.06 10:34 — LucF]
The if
block for entries should be entered only once, when there is no position, otherwise its condition can be true multiple times during a trade, and entryprice
will change each time, which you do not want.
Use:
if (crossover(outShort,outLong)) and strategy.position_size == 0