IBridgePy和导入at函数

2024-10-02 02:38:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我花了三个月左右的时间来学习python,我从这个网站上的回复中学到了很多东西。我面临着一个关于进口的问题,以及IBridgePy提供的与IB进行贸易的代码

我相信我的问题是我想把它们的模块级配置代码作为一个函数来运行。我读过import在第一次调用时设置正确。问题是,我需要在一个循环中运行他们的配置代码,因为我的程序是用来扫描股票的,当一个股票是理想的时候,它就会买入。在

既然我不能在函数中导入,那么在每次遍历循环时,如何替换循环中原始配置文件中模块的初始化?在

我的错误是,如果没有上述解决方案,第一个订单之后的每个订单只会打印第一个订单的信息,这表明order函数inst接受我更新的变量。在

你可以在这里找到整个库,尽管他们希望你登录下载。 http://www.ibridgepy.com/wp-content/uploads/2017/11/IBridgePy_Python3_64bit.zip

这是他们提供的学习本模块的方法。它要么超出我的技能水平,要么与目的无关。不管怎样,我都觉得没用。 https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwia5M6hnqrXAhUJ4yYKHSSpBd4QFggoMAA&url=https%3A%2F%2Fwww.interactivebrokers.com%2Fwebinars%2F2016-WB-2578_QuantInsti-ImplementAlgoTradingCodedinPythonNotes.pdf&usg=AOvVaw0ePQpNlEGDa7UoFgeDGRL9

下面是他们提供的三个文件的代码,我正在尝试,但未能循环。在我的代码中,我将“ifname==”部分中看到的大部分内容作为函数调用。我递给它一个股票,它试图买它。它在第一个滴答器上成功,之后每一个都失败。在

from IBridgePy.quantopian import LimitOrder, StopOrder, MarketOrder
from IBridgePy.quantopian import LimitOnOpenOrder, LimitOnCloseOrder
from BasicPyLib.handle_calendar import *
from IBridgePy.quantopian import calendars,date_rules, time_rules
from IBridgePy.TradingRules import TradingRules, Indicator, IndicatorDetails
import os
from sys import exit
import time

accountCode='DUXXXXXX'

def initialize(context):
    context.flag=False
    context.security=symbol('STK,'+ticker+',USD') 

def handle_data(context, data):
    if context.flag==False:               
        orderId=order(context.security, 100)
        order_status_monitor(orderId, target_status='Filled')
        context.flag=True

    else:
        time.sleep(10)
        display_all()
        end()

if __name__ == '__main__':
    global ticker
    for i in range(4):
        if i==0:
            ticker='ABEO'
        if i==1:
            ticker='ABIO'
        if i==2:
            ticker='APPL'
        if i==3:
            ticker='DOWJ'
        if i==4:
            ticker='AMD'





        try:
            id(runMode)
        except:
            runMode='run'

        if type(accountCode)==str:
            if runMode=='test_run_daily':
                from IBridgePy.TEST_Trader_single_account import Trader
                from IBridgePy.backtester_daily import MarketManager
                interval = 86400     
            elif runMode == 'test_run':
                from IBridgePy.TEST_Trader_single_account import Trader
                from IBridgePy.backtester_minute import MarketManager
                interval = 60
            else:
                from IBridgePy.Trader_single_account import Trader
                from IBridgePy.MarketManagerBase import MarketManager

        elif type(accountCode)==tuple:
            if runMode=='test_run':
                from IBridgePy.TEST_Trader_multi_account import Trader
            else:
                from IBridgePy.Trader_multi_account import Trader
                from IBridgePy.MarketManagerBase import MarketManager


        try:
            id(logLevel)
        except:
            logLevel='INFO'
        try:
            id(showTimeZone)
        except:
            showTimeZone='US/Eastern' 
        try:
            id(clientId) 
        except:
            clientId=9 


        repBarFreq=1

        try:
            id(before_trading_start)
        except:
            before_trading_start=None

        try:
            id(handle_data)
        except:
            handle_data=None

        try:
            id(trader)
            time.sleep(1)
            trader.disconnect()
            time.sleep(1)
        except:
            time.sleep(1)

        trader=Trader()
        trader.setup_trader(accountCode=accountCode,
                            logLevel=logLevel,
                            showTimeZone=showTimeZone,
                            repBarFreq=repBarFreq, 
                            initialize_quantopian=initialize,
                            handle_data_quantopian=handle_data,
                            before_trading_start_quantopian=before_trading_start)

        log = trader.log
        record = trader.userLog.record
        get_datetime=trader.get_datetime
        order=trader.order
        order_target=trader.order_target
        order_percent=trader.order_percent
        order_value = trader.order_value
        order_target_percent = trader.order_target_percent
        order_target_value = trader.order_target_value
        cancel_order=trader.cancel_order
        cancel_all_orders=trader.cancel_all_orders
        roundToMinTick=trader.roundToMinTick
        request_data=trader.request_data
        order_status_monitor=trader.order_status_monitor
        display_positions=trader.display_positions
        display_orderStatusBook=trader.display_orderStatusBook
        display_all=trader.display_all
        close_all_positions=trader.close_all_positions
        close_all_positions_except=trader.close_all_positions_except
        symbol=trader.symbol
        symbols=trader.symbols
        superSymbol=trader.superSymbol
        show_real_time_price=trader.show_real_time_price
        count_open_orders=trader.count_open_orders
        count_positions=trader.count_positions
        show_account_info=trader.show_account_info
        calculate_profit=trader.calculate_profit
        create_order=trader.create_order
        place_order_with_TP_SL=trader.place_order_with_TP_SL
        place_combination_orders=trader.place_combination_orders
        show_nextId=trader.show_nextId
        hold_any_position=trader.hold_any_position
        schedule_function=trader.schedule_function
        request_historical_data=trader.request_historical_data
        get_order=trader.get_order
        get_open_orders=trader.get_open_orders
        get_option_greeks=trader.get_option_greeks
        get_contract_details=trader.get_contract_details
        end=trader.end

        try:
            id(remoteHostIP)
        except:
            remoteHostIP=''
        c=MarketManager(trader, host=remoteHostIP, port=7496, clientId=clientId)

        if runMode=='run':
            c.run()
        elif runMode=='run_like_quantopian':
            c.run_like_quantopian()
        elif runMode=='run_auto_connection':
            c.run_auto_connection()
        elif runMode=='realTimeBars':
            c.runOnEvent()
        elif runMode in ['test_run_daily', 'test_run']:
            try:
                id(dataSource)
            except:
                dataSource='IB'
            c.test_run(startTime, endTime, dataSource, interval=interval)
        else:
            print ('realMode::EXIT, cannot handle runMode=%s'%(runMode,))

理想情况下,我只想订购(symbol('SPY'),100)并完成它,但这似乎不是我的选择。所以我在和他们给我的东西搏斗,对我来说,这似乎是无稽之谈。当我在http://www.ibridgepy.com/community/或通过电子邮件与他们联系时,每次我无缘无故地运行它时,程序都会输出不同的错误,要么我得不到任何回复,要么是一个无用的问答链接

我已经做了大约一个星期了,试着做我能想到的每一项工作,但这显然超出了我的能力范围。我哪里出错了?在


Tags: runfromimportiddatagetiforder

热门问题