1414*/
1515
1616using Newtonsoft . Json ;
17+ using QuantConnect . Algorithm . Framework . Portfolio ;
1718using QuantConnect . Algorithm . Framework . Portfolio . SignalExports ;
1819using QuantConnect . Api ;
1920using QuantConnect . Data ;
2021using QuantConnect . Interfaces ;
2122using System ;
23+ using System . Linq ;
2224using System . Net . Http ;
2325using System . Net . Http . Json ;
2426using System . Text ;
@@ -43,6 +45,7 @@ public override void Initialize()
4345
4446 /// Our custom signal export accepts all asset types
4547 AddEquity ( "SPY" , Resolution . Second ) ;
48+ AddCrypto ( "BTCUSD" , Resolution . Second ) ;
4649 AddForex ( "EURUSD" , Resolution . Second ) ;
4750 AddFutureContract ( QuantConnect . Symbol . CreateFuture ( "ES" , Market . CME , new DateTime ( 2023 , 12 , 15 ) , null ) ) ;
4851 AddOptionContract ( QuantConnect . Symbol . CreateOption ( "SPY" , Market . USA , OptionStyle . American , OptionRight . Call , 130 , new DateTime ( 2023 , 9 , 1 ) ) ) ;
@@ -57,7 +60,7 @@ public override void Initialize()
5760 /// <param name="slice"></param>
5861 public override void OnData ( Slice slice )
5962 {
60- foreach ( var ticker in new [ ] { "SPY" , "EURUSD" } )
63+ foreach ( var ticker in new [ ] { "SPY" , "EURUSD" , "BTCUSD" } )
6164 {
6265 if ( ! Portfolio [ ticker ] . Invested && Securities [ ticker ] . HasData )
6366 {
@@ -74,10 +77,17 @@ internal class CustomSignalExport : ISignalExportTarget
7477
7578 public bool Send ( SignalExportTargetParameters parameters )
7679 {
77- var message = JsonConvert . SerializeObject ( parameters . Targets ) ;
80+ object SimplePayload ( PortfolioTarget target )
81+ {
82+ var newTarget = PortfolioTarget . Percent ( parameters . Algorithm , target . Symbol , target . Quantity ) ;
83+ return new { symbol = newTarget . Symbol . Value , quantity = newTarget . Quantity } ;
84+ } ;
85+
86+ var message = JsonConvert . SerializeObject ( parameters . Targets . Select ( SimplePayload ) ) ;
7887 using var httpMessage = new StringContent ( message , Encoding . UTF8 , "application/json" ) ;
7988 using HttpResponseMessage response = _httpClient . PostAsync ( _requestUri , httpMessage ) . Result ;
8089 var result = response . Content . ReadFromJsonAsync < RestResponse > ( ) . Result ;
90+ parameters . Algorithm . Log ( $ "Send #{ parameters . Targets . Count } targets. Success: { result . Success } ") ;
8191 return result . Success ;
8292 }
8393
@@ -95,6 +105,7 @@ from json import loads
95105@app.post('/')
96106def handle_positions():
97107 result = loads(request.data)
108+ print(result)
98109 return jsonify({'success': True,'message': f'{len(result)} positions received'})
99110if __name__ == '__main__':
100111 app.run(debug=True)
0 commit comments