Giter VIP home page Giter VIP logo

Comments (4)

TeldridgeLDN avatar TeldridgeLDN commented on May 26, 2024 1

That's super helpful. I adapted the revised script to show the sma line as well on the chart. Many thanks for helping me on this

from backtrading-python-binance.

lth-elm avatar lth-elm commented on May 26, 2024

Firstly, congratulations on creating such a fantastic script. It has been really helpful.

Can I clarify the SMA strategy in backtrader you have applied as part of your code? For example when I run your script for BTC, I can see that a SMA strategy with a period of 10 would be best.

I would like to replicate this SMA strategy via trading view pine script so that when it crosses over or under an alert is generated.

However there are many interpretations of SMA in tradingview and on backtrader. For example, you can have fast and slow MA that cross over as in this example

Or is the SMA strategy taking the closing price of candle and comparing it with the 10 previous candles?

Curious to know the logic applied for the SMA in your script.

Thank you @TeldridgeLDN glad this script came in handy to some people :)

To answer your question the only different interpretations you can have for a Simple Moving Average (SMA) is if the period or the taken value (by default at closing price of a candle but can be changed to lower/higher value...) used is different, otherwise the calculation is the same : sum of n last closing price divided by the period n. Investopedia has good explanation, here is the one for the SMA.

When you mention fast and slow MA as a different interpretations of SMA, if you notice well the 'fast MA' is actually an SMA with a period of 10, thus fast since it will be more sensitive to the last 10 candles, and the 'slow MA' is an SMA with a period of 30 (less sensitive to the very last candles thus "slow").

# ...
params = dict(
        pfast=10,  # period for the fast moving average
        pslow=30   # period for the slow moving average
    )

    def __init__(self):
        sma1 = bt.ind.SMA(period=self.p.pfast)  # fast moving average /// pfast = period = 10 and is applied to the SMA indicator
        sma2 = bt.ind.SMA(period=self.p.pslow)  # slow moving average
# ...

Here you have the same indicator but with different values since one only take into account the last 10 candles while the other one take the last 30.

For example in this chart you have two SMA, one with a period of 60 and the other 4 (faster to react to any change and closer to the price).

Therefore you have nothing to change between tradingview and backtrader, you can keep using the same SMA indicator since the calculation is the same, you only have to set the parameters as you want : take the closing values by default and setting the period.

However in TradingView make sure to not mistake the Smooth Moving Average (SMMA is calculated in a different way) and the Simple Moving Average called only "Moving Average" in the indicator tab (however for pinescript use sma()).

Hope I made it clear to you ! 😄

from backtrading-python-binance.

TeldridgeLDN avatar TeldridgeLDN commented on May 26, 2024

Thank you for responding @lth-elm . I really appreciate the effort you took to respond.

I think I was getting myself confused with combining slow and fast MA's. When all I wanted was one Moving Average that maps to the results from your back testing script.

I've only been programming for a 2 weeks but I think I managed to come up with a pine script that may work.

`//@Version=4
strategy("SMA PTL", overlay=true, initial_capital=100, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

start_date = input(title="Start Date", type=input.integer, defval=4, minval=1, maxval=31)
start_month = input(title="Start Month", type=input.integer, defval=9, minval=1, maxval=12)
start_year = input(title="Start Year", type=input.integer, defval=2020)
end_date = input(title="End Date", type=input.integer, defval=6, minval=1, maxval=31)
end_month = input(title="End Month", type=input.integer, defval=9, minval=1, maxval=12)
end_year = input(title="End Year", type=input.integer, defval=2020)

between_dates = (time >= timestamp(start_year, start_month, start_date, 7, 0)) and (time < timestamp(end_year, end_month, end_date, 23, 59))

len = input(9, minval=1, title="Length")
src = input(close, title="Source")
offset = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)

a = sma(src, len)
b = close
c = crossover(a, b)
d = crossunder(a, b)
plot(a, color=color.blue)
plot(b, color=color.green)
plotshape(c, color=color.red)
plotshape(d, color=color.green)

buy_condition = c
sell_condition = d

if between_dates
strategy.entry("doge door", strategy.long, when=buy_condition)

strategy.close("doge door", when=sell_condition)`

Just make sure in Trading view you select an end date of 2021.

With this script I can select the SMA period I want and overlay that to the chart timeframe easily. I also have the crossover and crossunder points indicated with a red or green x. I just need to test the alert function generated

I would love to get your thoughts. I'm sure there are some mistakes that I've missed

from backtrading-python-binance.

lth-elm avatar lth-elm commented on May 26, 2024

Thank you for responding @lth-elm . I really appreciate the effort you took to respond.

I think I was getting myself confused with combining slow and fast MA's. When all I wanted was one Moving Average that maps to the results from your back testing script.

I've only been programming for a 2 weeks but I think I managed to come up with a pine script that may work.

`//@Version=4
strategy("SMA PTL", overlay=true, initial_capital=100, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

start_date = input(title="Start Date", type=input.integer, defval=4, minval=1, maxval=31)
start_month = input(title="Start Month", type=input.integer, defval=9, minval=1, maxval=12)
start_year = input(title="Start Year", type=input.integer, defval=2020)
end_date = input(title="End Date", type=input.integer, defval=6, minval=1, maxval=31)
end_month = input(title="End Month", type=input.integer, defval=9, minval=1, maxval=12)
end_year = input(title="End Year", type=input.integer, defval=2020)

between_dates = (time >= timestamp(start_year, start_month, start_date, 7, 0)) and (time < timestamp(end_year, end_month, end_date, 23, 59))

len = input(9, minval=1, title="Length")
src = input(close, title="Source")
offset = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)

a = sma(src, len)
b = close
c = crossover(a, b)
d = crossunder(a, b)
plot(a, color=color.blue)
plot(b, color=color.green)
plotshape(c, color=color.red)
plotshape(d, color=color.green)

buy_condition = c
sell_condition = d

if between_dates
strategy.entry("doge door", strategy.long, when=buy_condition)

strategy.close("doge door", when=sell_condition)`

Just make sure in Trading view you select an end date of 2021.

With this script I can select the SMA period I want and overlay that to the chart timeframe easily. I also have the crossover and crossunder points indicated with a red or green x. I just need to test the alert function generated

I would love to get your thoughts. I'm sure there are some mistakes that I've missed

Hello I made a few changes, there was indeed a mistake in your code, instead of taking the crossover of price over the moving average you were taking the opposite : crossover of the SMA over the price therefore you would buy when price closes below the SMA. I've just inverted a and b and also removed the plotting of the closed price, it was a bit confusing I though for a second that it was a second moving average 😅. And finally I added the tabulation where it was needed after the if condition.

//@version=4
strategy("SMA PTL", overlay=true, initial_capital=100, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

start_date = input(title="Start Date", type=input.integer, defval=4, minval=1, maxval=31)
start_month = input(title="Start Month", type=input.integer, defval=9, minval=1, maxval=12)
start_year = input(title="Start Year", type=input.integer, defval=2020)
end_date = input(title="End Date", type=input.integer, defval=6, minval=1, maxval=31)
end_month = input(title="End Month", type=input.integer, defval=9, minval=1, maxval=12)
end_year = input(title="End Year", type=input.integer, defval=2021)

between_dates = (time >= timestamp(start_year, start_month, start_date, 7, 0)) and (time < timestamp(end_year, end_month, end_date, 23, 59))

len = input(10, minval=1, title="Length")
src = input(close, title="Source")
offset = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)

a = close
b = sma(src, len)
c = crossover(a, b)
d = crossunder(a, b)
plot(a, color=color.blue)
plotshape(c, color=color.red)
plotshape(d, color=color.green)

buy_condition = c
sell_condition = d

if between_dates
    strategy.entry("doge door", strategy.long, when=buy_condition)

    strategy.close("doge door", when=sell_condition)

Oh and for more realistic result you might consider adding some fees and slippage by adding this line commission_type=strategy.commission.percent, commission_value=0.07, slippage = 5 inside your strategy().

from backtrading-python-binance.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.