- ago
Hello everyone,

I'm currently working on a strategy in WealthLab that involves using EMAs calculated on different timeframes. The idea is to trigger trades when a short-period EMA crosses a longer-period EMA. Additionally, I want the strategy to send an HTTP POST request to an external endpoint whenever such a cross occurs.

Below is a conceptual script I've developed in C# for this purpose. It uses the standard HttpClient for making the POST requests. However, I am not sure if this is the best approach or if WealthLab supports such functionality seamlessly.

CODE:
using System; using System.Net.Http; using System.Threading.Tasks; using WealthLab; namespace MyStrategies { public class EMACrossStrategy : UserStrategyBase { private HttpClient _httpClient; public override void Initialize(BarHistory bars) { _httpClient = new HttpClient(); int shortEmaPeriod = 10; int longEmaPeriod = 30; var shortEma = EMA.Series(bars.Close, shortEmaPeriod); var longEma = EMA.Series(bars.Close, longEmaPeriod); PlotSeries(bars.Date, shortEma, Color.Blue, LineStyle.Solid, 1); PlotSeries(bars.Date, longEma, Color.Red, LineStyle.Solid, 1); } public override void Execute(BarHistory bars, int idx) { if (idx < 1) return; if (CrossAbove(bars.Close, idx, shortEma, longEma, idx)) { BuyAtMarket(idx); SendPostRequestAsync("<a href="http://myendpoint.com/buy" target="_blank">http://myendpoint.com/buy</a>"); } else if (CrossBelow(bars.Close, idx, shortEma, longEma, idx)) { SellAtMarket(idx); SendPostRequestAsync("<a href="http://myendpoint.com/buy" target="_blank">http://myendpoint.com/buy</a>"); } } private async Task SendPostRequestAsync(string url) { try { var content = new StringContent("Data here...", Encoding.UTF8, "application/json"); var response = await _httpClient.PostAsync(url, content); if (response.IsSuccessStatusCode) { Console.WriteLine("Success"); } else { Console.WriteLine("Fail"); } } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } } } }


This script is intended to monitor EMA crosses and then send a trade signal via POST request. However, I'm uncertain about a few things:

Is using HttpClient in WealthLab for real-time strategy execution and making HTTP requests advisable? Are there any performance considerations or limitations I should be aware of?

Does WealthLab have built-in support or a recommended approach for such external data communication during strategy execution?

Are there any best practices for ensuring that the HTTP requests do not adversely affect the strategy's performance, especially in a real-time streaming context?

I'd greatly appreciate any insights, suggestions, or examples from those who have experience with similar implementations. Also, if there's a more efficient way to achieve this within WealthLab, I'd be eager to learn.

Thanks in advance for your help!

Best regards




0
204
Solved
4 Replies

Reply

Bookmark

Sort
- ago
#1
It's impressive to hear such question from a first-time trial user. As for adversely affecting the strategy's performance, aren't you making that call asynchronously to avoid exactly that?

Have you also checked the third-party finantic.Publisher extension? It's aimed to send trading signals to files, FTP, mail and Webhooks:

https://www.wealth-lab.com/extension/detail/finantic.Publisher
1
Cone8
 ( 24.06% )
- ago
#2
This code looks like a bad mix of WL versions 6 and 8.

In any case, if you the idea to send the SendPostRequestAsync in real time, you should test that you're processing only the last bar of the chart. Otherwise you'll be doing this for every one of those events in the chart history.

CODE:
if (idx == bars.Count - 1) SendPostRequestAsync(...
1
Best Answer
- ago
#4
Hey Eugene and Cone,

Thanks for the tips! Eugene, your point on asynchronous calls is spot-on – that should help with performance. And Cone, focusing on the last bar only is something I hadn't thought of. It'll definitely prevent unnecessary POST requests.

Also, the finantic.Publisher extension seems like a solid lead. I'm aiming to integrate WealthLab with cTrader, so this could be a key piece of the puzzle. Going to test out that 14-day trial and see how it fits.

Really appreciate your insights!
1

Reply

Bookmark

Sort