# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.from AlgorithmImports import *### <summary>### his algorithm sends a list of portfolio targets to custom endpoint### </summary>### <meta name="tag" content="using data" />### <meta name="tag" content="using quantconnect" />### <meta name="tag" content="securities and portfolio" />class CustomSignalExportDemonstrationAlgorithm(QCAlgorithm):def initialize(self) -> None:''' Initialize the date and add all equity symbols present in list _symbols '''self.set_start_date(2013, 10, 7) #Set Start Dateself.set_end_date(2013, 10, 11) #Set End Dateself.set_cash(100000) #Set Strategy Cash# Our custom signal export accepts all asset typesself.add_equity("SPY", Resolution.SECOND)self.add_crypto("BTCUSD", Resolution.SECOND)self.add_forex("EURUSD", Resolution.SECOND)self.add_future_contract(Symbol.create_future("ES", Market.CME, datetime(2023, 12, 15)))self.add_option_contract(Symbol.create_option("SPY", Market.USA, OptionStyle.AMERICAN, OptionRight.CALL, 130, datetime(2023, 9, 1)))# Set CustomSignalExport signal export provider.self.signal_export.add_signal_export_provider(CustomSignalExport())def on_data(self, data: Slice) -> None:'''Buy and hold EURUSD and SPY'''for ticker in [ "SPY", "EURUSD", "BTCUSD" ]:if not self.portfolio[ticker].invested and self.securities[ticker].has_data:self.set_holdings(ticker, 0.5)from requests import postclass CustomSignalExport:def send(self, parameters: SignalExportTargetParameters) -> bool:targets = [PortfolioTarget.percent(parameters.algorithm, x.symbol, x.quantity)for x in parameters.targets]data = [ {'symbol' : x.symbol.value, 'quantity': x.quantity} for x in targets ]response = post("http://localhost:5000/", json = data)result = response.json()success = result.get('success', False)parameters.algorithm.log(f"Send #{len(parameters.targets)} targets. Success: {success}")return successdef dispose(self):pass'''# To test the algorithm, you can create a simple Python Flask application (app.py) and run flask# $ flask --app app run# app.py:from flask import Flask, request, jsonifyfrom json import loadsapp = Flask(__name__)@app.post('/')def handle_positions():result = loads(request.data)print(result)return jsonify({'success': True,'message': f'{len(result)} positions received'})if __name__ == '__main__':app.run(debug=True)'''
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。