#define, #if #else pragma's supported?

Aug 13 at 8:31 PM

I try to work with Python. C# is much easier :-). I have a project which I need to port from offline testing to online production (zipline) to (www.quantopian.com) . Both environments have unfortunately different function calls and some features are even not present in the offline version which runs behind the online version. In C# I would solve this by coding 2 ways: 1 for Quantopian and 1 for offline usage. Is there a way in Python to use #define #if #elseif so that the compiler does ignore some parts? Or is that much too advanced for Python and PTVS?

I for instance sid(3934) on Quantopian which is not supported offline. Offline is is symbol['AAPL'].

Aug 13 at 8:37 PM
You can simply use regular if statements with Python. As long as the code is valid syntax, it doesn't matter if the names are missing until the code actually runs.

If you have different modules available in online/offline you can use code like this:
    import online_functions as functions
except ImportError:
    import offline_functions as functions

If there is an environment variable set then you can use this with normal if statements:
import os

is_offline = os.getenv('OFFLINE', '0') == '1'

if is_offline:
    v = symbol['AAPL']
    v = sid(3934)
If you can tell me more about the differences between your online and offline environments then I can point you towards some specific techniques that may be helpful.
Aug 13 at 10:10 PM
Thank you. I got it working with the try-except.
    import os
    import pandas as pd
    import zipline
    from zipline import TradingAlgorithm
    from zipline.data.loader import load_bars_from_yahoo
    from zipline.api import order_target, order_target_percent,  record, symbol, history, add_history, get_datetime
    from datetime import datetime
    #import offline_functions

    is_offline = True;
except ImportError:
    is_offline = False;

import numpy as np
import talib
from pytz import timezone
from zipline.utils import tradingcalendar

def initialize(context):
    context.leverage = 2
Aug 13 at 10:14 PM
This looks excessively pessimistic. os and datetime are standard modules - I don't think you want those in try..catch. Probably not pandas, either. It should probably be just the zipline stuff (and then the tradingcalendar import outside of try also looks suspicious).