Back to Community
Coding help please.

Hello There!
I'm new at coding but have experience on technical analysis. If you can help me to create a algo for my logic it would be great!

There will be two stocks and all money will be invested in these two stocks at all time. Either %1 to %99 or %50 to %50.

If SPY 500 index's macd is over zero line and fast ma crossed above slow than portfolio should be %70 stock A and %30 Stock B
If SPY 500 index's macd is over zero line and fast ma crossed below slow than portfolio should be %50 stock A and %50 Stock B
If SPY 500 index's macd is crossed zero line than portfolio should be %40 stock A and %60 Stock B
If SPY 500 index's macd is crossed zero line and fast moving average is below slow moving average than portfolio should be %30 stock A and %70 Stock B

No re balancing and time frame should be 5 min.

Thank you for your help in advance.

3 responses

Ehh??

def initialize(context):  
   context.spy = sid(8554)  
   context.stockA = sid(1751)  
   context.stockB = sid(24)

   total_minutes_trading = 6*60 + 5  
   for i in range(1, total_minutes_trading):  
       if i % 5 == 0:  
           schedule_function(five_minute_fun,  
                             date_rules.every_day(),  
                             time_rules.market_open(minutes=i),  
                             True  
                             )  
def five_minute_fun(context, data):  
    f_days = 12  
    s_days = 26  
    f_avg = data[context.spy].mavg(f_days)  
    s_avg = data[context.spy].mavg(s_days)  
    macd = f_avg - s_avg  
    qavg = [macd,9]  
    if (qavg > 0) and (f_avg > s_avg):  
        order_target_percent(context.stockA, 0.70)  
        order_target_percent(context.stockB, 0.30)  
    if (qavg > 0) and (f_avg < s_avg):  
        order_target_percent(context.stockA, 0.50)  
        order_target_percent(context.stockB, 0.50)  
    if (qavg < 0):  
        order_target_percent(context.stockA, 0.40)  
        order_target_percent(context.stockB, 0.60)  
    if (qavg < 0) and (f_avg < s_avg):  
        order_target_percent(context.stockA, 0.30)  
        order_target_percent(context.stockB, 0.70)  

To be honest, I wanted to do this with concurrent functions, but there was a wall of debugging and it is past my bedtime.

Thank you for your help. This helps me to understanding the logic of coding. I was able to understand if parts very well however I did not understand the time period part. How do I change the time period? And also when we change target percentage what algo does? Sells everything and buys accordingly or buy sell to balance it the target level.

A "for" loop is an iterative process. I told it to iterate 365 times (60*5+5, which represents minutes in a trading day) and every fifth minute do a trade. I told it to do it a trade every fifth minute by looking at the remainder of the current iteration divided by 5. When the remainder is zero, we trade.

Here the API for order_target_percent: https://www.quantopian.com/help#api-order-target-percent