简体   繁体   中英

can anyone explain to me why the strategy.entry function is not executed?

can anyone explain to me why the strategy.entry function is not executed? This is intended to be a pine script that buys an asset every other week and tracks the accumulation of that asset. I have the following script that doesn't produce any errors, but it doesn't enter any trades or actually save the accumulation variable. The accumulation variable switches between 0 and 1, it doesn't incrementally increase.

// This strategy buys the asset displayed on the active chart every 
other week.
strategy("Dollar Cost Averaging")

// Set the amount of USD to buy the asset.
investment_amount = 75

// Variable to track accumulation.
float accumulation = 0

// Buy the asset every other week.
if (weekofyear(time) % 2 == 1.0) and (dayofweek(time) == 2.0) and 
(hour(time) == 12.0) and (minute(time) == 0.0)

    // Buy.
    strategy.entry(id = "Buy", direction = strategy.long, 
    qty=float(investment_amount/close))

    // Alternative way of keeping track of the accumulation.
    accumulation += float(investment_amount/close)

I tried looking up simple DCA pine scripts, but I only found scripts that bought based on indicators. I just want to buy every 2 weeks.

You should use the version 5 of pinescript. The to declare a float, you must use the value = 0.0, not = 0. Then you must use 'var' to declare and give a value the first time only. In your code you use:

float accumulation = 0

which reset the value of accumulation to 0 each new bar

Here is the code:

//@version=5
strategy("Dollar Cost Averaging")

// Set the amount of USD to buy the asset.
investment_amount = 75.0

// Variable to track accumulation.
var accumulation = 0.0

// Buy the asset every other week.
weektoinvest = (weekofyear(time) % 2 == 1) and (dayofweek(time) == 2) and (hour(time) == 12) and (minute(time) == 0)
if weektoinvest
    // Buy.
    strategy.entry(id = "Buy", direction = strategy.long, qty= investment_amount/close)

    // Alternative way of keeping track of the accumulation.
    accumulation += investment_amount/close

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM