Back

Migrating my first algo from quantopian to QC.

Hi all,

I am new to programming and algorithms in general, so please bear with me. 

I just started programming my first algo over at Quantopian (QP) this week when I realized that QC is probably the better place to be at the moment. I am now trying to simply migrate my basic algo over to QC, and was hoping to find some kind help on this forum. My algo is very simple for obvious reasons, but I still find it hard to figure out how to go about converting it to QC standards. 

The algo is based on two technical indicators (ADX and Parabolic SAR). The algo goes long when the ADX crosses a certain threshold and when the parabolic SAR is indicating an up-trend, It exists the long posistion and switches to a second asset at exit when similar conditions are met by the SAR and ADX indicator. The aglo from QP currently imports Talib for the mentioned indicators, and this is the difficult part to migrate. The code from QP currently looks like this: 

# ---------------------------------------------------------------------  

# ADX, PLUS_DI, MINUS_DI, SAR  
import talib  
# ---------------------------------------------------------------------  
stock, bond, period = symbol('dia'), symbol('tlt'), 14; bars = period*2  
# ---------------------------------------------------------------------  
def initialize(context):  
    schedule_function(trade , date_rules.every_day(), time_rules.market_open(minutes = 65))
    set_benchmark(symbol('dia'))
    set_slippage(slippage.FixedSlippage(spread=0.02))
    set_commission(commission.PerShare(cost=0.01, min_trade_cost=1))
    

def trade (context, data):  
    if get_open_orders(): return  

    H = data.history(stock,'high', bars, '1d').dropna()  
    L = data.history(stock,'low', bars, '1d').dropna()  
    C = data.history(stock,'close', bars, '1d').dropna()  

    adx = talib.ADX(H, L, C, period)[-1]  
    mdi = talib.MINUS_DI(H, L, C, period)[-1]  
    pdi = talib.PLUS_DI(H, L, C, period)[-1]  
    sar = talib.SAR(H, L, 0.02, 0.2)[-1]

    if (adx < 15 and mdi > pdi and sar > C[-1]):  
        wt_stk, wt_bnd = 0, 1
    elif (adx > 15 and mdi < pdi):  
        wt_stk, wt_bnd = 3, 0  
    else: return  

    if all(data.can_trade([stock, bond])):  
        order_target_percent(stock, wt_stk)  
        order_target_percent(bond, wt_bnd)  

def before_trading_start(context,data):  
    x = context.account.leverage
    y =  context.account.net_leverage  
    record(leverage = x,net_leverage = y)
# ---------------------------------------------------------------------  

I hope this community can help me get started with learning how to program on this platform. 

Thank you all in advance!

 

 

Update Backtest







0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi Martin,

Welcome to QC! I'd recommend going over the Bootcamp tutorials and also taking a look at the example algorithms.

Also, when including code, please use the Insert Code Snippet button. 37376_1566484341.jpg

Good luck!

1

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed