admin管理员组文章数量:1122832
Context: I tried to use the money from selling the stock to re-invest in the next buy.
Implementation: I want to use the variable cash and shares to remember the historical information. But seems my code doesn't work and cash and shares value are always 0.
How should I fix it?
ThinkScript Code:
input SMA_Length = 120; # Length of the SMA
input initialCash = 10000;
# Calculate the Simple Moving Average
def SMA = Average(close, SMA_Length);
# Detect buy and sell signals based on slope transitions
def BuySignal = close > SMA;
def SellSignal = close < SMA;
def cash;
def shares;
shares = if BuySignal and IsNaN(shares[1]) then floor(initialCash/close) # first trade
else if BuySignal then floor(cash[1]/close)
else if SellSignal then 0
else shares[1];
cash = if IsNaN(cash[1]) then initialCash # before first trade
else if BuySignal then 0
else if SellSignal then shares[1]*close
else cash[1];
# Add buy and sell orders
addOrder(OrderType.BUY_TO_OPEN, BuySignal, price = close, tradeSize = shares);
addOrder(OrderType.SELL_TO_CLOSE, SellSignal, price = close, tradeSize = shares);
addchartbubble(1, 10, "c:" + cash + "\n" + "s:" + shares + "\n" + "i:" + initialCash);
本文标签: recursioninitialized the recursive variable in thinkscriptStack Overflow
版权声明:本文标题:recursion - initialized the recursive variable in thinkscript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736282597a1926693.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论