Fixed. I changed the code from strategy.exit to strategy.close and it compiled without errors.
Trying to create a pine script to back test DeFi borrowing strategy.
I not really sure where to start much appreciated if anyone wants to help.
Using Maker to generate DAI during uptrend each time the loan becomes 450% collateralize it borrows till the loan is 400% collateralize. In a down trend when collateral reaches 250% it gets upped to 300% by buying more ETH with the DAI. Liquidation requires over 70% retrenchment.
//changed the close to another entry strategy to just short instead. Interesting results! Thank you!
strategy(title=âMAStrategyâ)
m=sma(close, 10)
buy=close > m
sell=close < m
strategy.entry(âbuyâ, true, 5, when=buy)
strategy.entry(âbuyâ, false, 5, when=sell)
Hi guys, I have a question for you pinescript geniuses, I am trying to program a simple alertcondition which will fire an alert whenever a condition is true.
However, when testing the alertcondition function I am finding a problem. My alertcondition should never fire with this code, but it does. It fires immediately and i can not understand why. (My Alert option are âOnly Onceâ)
Any ideas?
//@version=4
study("Example of alertcondition")
src = input(close)
ma_1 = sma(src, 20)
ma_2 = sma(src, 10)
//c = cross(ma_1, ma_2)
c = 2>3
alertcondition(c, title='Red crosses blue', message='Red and blue have crossed!')
plot(ma_1, color=color.red)
plot(ma_2, color=color.blue)
I am trying to figure out how to test my strategy within just a given period of time. Say i want to test using the 2017 btc/usd chart only. How do i do that?
Hi @Tradesmart, hope you are well.
you will have to create a time range variables that define your custom period of time to be test it.
Here is the code that filip explain in further videos to backtesting your strategy based on a time range:
//DATE & TIME
fromMonth = input(defval=1, title="From month", minval=1)
fromDay = input(defval=1, title="From day", minval=1)
fromYear = input(defval=2019, title="From year", minval=2014)
toMonth = input(defval=1, title="To month", minval=1)
toDay = input(defval=1, title="To day", minval=1)
toYear = input(defval=2025, title="To year", minval=2014)
If you have any more questions, please let us know so we can help you!
Carlos Z.
I have a question , how do you add the arrows that show where you enter and exit a trade when you apply your strategy to the chart? I currently donât have those arrows.
Hi @iceman12276, hope you are ok.
Now arrows can be set with the plotshapes
function, it will add a shape in your strategy to help you get a better view on it, here you can read more about it: https://www.tradingview.com/pine-script-docs/en/v4/annotations/Plotting_shapes_chars_and_arrows.html
If you have any more questions, please let us know so we can help you!
Carlos Z.
Hey @filip ,
I hope you are well. Iâm doing the algorithmic trading course. Iâm at the part where you build out a strategy. When I type the code in pine editor it gives me this error message:
Add to Chart operation failed, reason: line 8: mismatched character â\nâ expecting '"
I donât think the code is wrong;
//@version=4
strategy(title=âMAStrategyâ)
m=sma(close, 10)
buy=close > m
sell=close< m
strategy.entry (buy", true, 1, when=buy)
strategy.close(buy", when=sell)
Let me know.
Thanks
Mark
I think you need another " .
strategy.entry (âbuyâ, true, 1, when=buy) and
strategy.close(âbuyâ, when=sell)
Thanks Berglund, that was the issue. If you donât mind, iâm also stuck with the next section.
When i add this to chart, it does not come up with any data in strategy tester.
//@version=4
strategy(title=âMoving Average Crossingâ, overlay=true)
//DEFINITIONS
shortMa = sma(close, 20)
longMa = sma(close, 50)
//LOGIC
longSignal = crossover(shortMa, longMa)
shortSignal = crossover(longMa, shortMa)
//POSITIONS
strategy.entry(id=âlongPositionâ, long=true, when=longSignal)
strategy.entry(id=âshortPositionâ, long=true, when=shortSignal)
Thanks again!
The last (//positions) syntax is wrong.
strategy.entry(âPositionâ, true, when = longSignal)
id is the name of the strategy, this must be the same (id is always the first argument, just the string)
long is the always the second argument, both are required and you must not add the variable name.
when is an optional parameter, and all of those must have the variable name.
strategy.close(âPositionâ, when = shortSignal)
I do not know if it matters, Filip is using ver3.
I was on the previous section when replying to youâŠ
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © aberglund
//@version=4
strategy(âMy MA2-Strategyâ,
overlay=true,
initial_capital = 100,
default_qty_type = strategy.percent_of_equity,
default_qty_value = 30,
commission_type=strategy.commission.percent,
commission_value=0.1
)
m1=sma(close, 7)
m2=sma(close, 21)
longSignal = crossover(m1, m2)
shortSignal = crossunder(m1, m2)
plot(m1)
plot(m2)
strategy.entry(âKöpâ, true, 100, when=longSignal, comment=âKâ)
strategy.close(âKöpâ, when=shortSignal, comment=âSâ)
If you want to check out the code of the existing indicators click on the curly brackets and the code will appear in the Pine Editor.
Yes Berglund, youâre a star.
So that all loaded up fine, so as im continuing to follow the instructions I have no problems until this;
//LOGIC
timeInRange = (time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time < timestamp(toYear, toMonth, toDay, 23, 59))
longSignal = crossover(shortMa, longMa) and timeInRange
shortSignal = crossover(longMa, shortMa) and timeInRange
when i add this 1st line timeInRange, it still loads no problem, when i add (timeInRange) at the end of longSignal and shortSignal, it does not load any data.
Full code below;
//@version=4
strategy(title=âMoving Average Crossingâ, overlay=true)
//DATE AND TIME
fromMonth = input(defval=1, title = âfrom monthâ, minval=1)
fromDay = input(defval=1, title = âfrom dayâ, minval=1)
fromyear = input(defval=2021, title = âfrom yearâ, minval=2014)
toMonth = input(defval=1, title = âto monthâ, minval=1)
toDay = input(defval=1, title = âto dayâ, minval=1)
toYear = input(defval=2025, title = âto yearâ, minval=2014)
//DEFINITIONS
shortMa = sma(close, 20)
longMa = sma(close, 50)
//LOGIC
timeInRange = (time > timestamp(fromyear, fromMonth, fromDay, 00, 00)) and (time < timestamp(toYear, toMonth, toDay, 23, 59))
longSignal = crossover(shortMa, longMa) and timeInRange
shortSignal = crossover(longMa, shortMa) and timeInRange
//POSITIONS
strategy.entry(id=âlongPositionâ, long=true, when=longSignal)
strategy.entry(id=âshortPositionâ, long=false, when=shortSignal)
One issue is (perhaps) the predefined variables month, year and to but I am not sure why they are causing a problem within the quotationsâŠ
My updated versionâŠ
NB Pine is case sensitive.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © aberglund
//@version=4
strategy(âMy MA-Strategyâ,
overlay=true,
initial_capital = 1000,
default_qty_type = strategy.percent_of_equity,
default_qty_value = 10,
commission_type=strategy.commission.percent,
commission_value=0.1
)
FromDay = input(defval=1, title = âFrom Dayâ, minval=1)
FromMonth = input(defval=2, title = âFrom Monthâ, minval=1)
FromYear = input(defval=2020, title = âFrom Yearâ, minval=2014)
ToDay = input(defval=1, title = âTo Dayâ, minval=1)
ToMonth = input(defval=5, title = âTo Monthâ, minval=1)
ToYear = input(defval=2020, title = âTo Yearâ, minval=2014)
timeInRange = (time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59))
fsma = input(defval=7, title = âFastMAâ, minval=1)
ssma = input(defval=21, title = âSlowMAâ, minval=1)
m1=sma(close, fsma)
m2=sma(close, ssma)
longSignal = crossover(m1, m2) and timeInRange
shortSignal = crossunder(m1, m2) and timeInRange
plot(m1, color = color.red, linewidth = 3)
plot(m2, color = color.blue, linewidth = 3)
strategy.entry(âLongâ, true, when=longSignal, comment=âLongâ)
strategy.entry(âShortâ, false, when=shortSignal, comment=âShortâ)
Perfect, yes it was the year that needed adjusting.
Do you know why, i dont see any data on my chart, they do in the video example
ignore that last question. and thanks again.
Hi there,
I wrote the same script in the lecture
strategy(title="MAStrategy")
m=sma(close, 10)
buy=close > m
sell=close < m
strategy.entry("entry", true , 1 , when = buy)
strategy.close("entry" , when = sell)
and I add in chart but no result in chart.
On the console appear âThe study of the script was added to chartâ.
I apreciate if anyone can help. @filip