Import signal from CSV file?
Author: janu314
Creation Date: 2/14/2017 8:30 PM
profile picture

janu314

#1
New to this forum so forgive any mistakes.

1. I have a strategy coded in Excel which outputs a signal around 3:45 PM everyday, in either Excel or CSV format.


2. All we need to do is have wealthLab read this file and input this trade into the system, everyday.


3. Is this possible to do in WLPro and can you please tell us the easiest way to do this in WealthLabPro 6.9?


4. Perhaps I can use one of the strategy codes available here as starting points to do this?

Thanks a lot,

Janu
profile picture

Eugene

#2
Hello Janu,

Yes, this is possible.
profile picture

Eugene

#3
1. How and when do you plan to execute that signal - "tomorrow" or "today"?
2. If "today", is it important to execute it in WLP precisely or anytime between 3:45pm and 3:59pm should do?
3. Do you need an Alert or just to see it on the chart as a historical trade?
4. Assuming you're free to control how the output CSV file will look like?

Question is if our ImportHistoricalTrades method to import a preformatted CSV file with trades could be used, or it'd take to create a customized Strategy example.

P.S. As Wealth-Lab is very powerful in both backtesting and charting, have you considered porting your algo to our platform?
profile picture

janu314

#4
Thanks Eugene for detailed comments. I am sorry, I was not clear about the distinction between support ticket and the forum.

To answer your questions:

1. The trade is to be executed today (and everyday) before the close.

2. I have to double check, but I believe we want to send it at 3:50 PM precisely.

3. I do need a trade alert and then the order is to be routed live to the market.

4. I will look at ImportHistoricalTrades method, but I think I will need to create a custom method for our purpose.
Is the source code available ? Perhaps, I can use it as a starting point.

5. I started coding (see code snippet below) this up by looking at times of the last few bars and checking for time = 3:50.

Not sure if this is right approach. Also, this is only producing a historical trade, not a trade alert or live trade ?

Code Snippet:

CODE:
Please log in to see this code.



6. At the moment coding everything in WealthLab is not an option as the XL-sheet (belongs to someone else) is not available.

profile picture

Eugene

#5
Janu, thanks for clarifying. Here's rough plan:

1. Use Fidelity's intraday data (1-minute)
2. Make sure the signal is generated
3. Parse the CSV output from your XL sheet
4. Using GetTime (found in Community Components), check if the bar's time is 3:49pm
5. If true, send alert for the next bar for WLP to execute it at 3:50pm

The source code for ImportHistoricalTrades is available after separate registration in the Wiki and "greenlisting" your new account, but I have a feeling that your task does not require it and after finding out the signal file format, could be coded from scratch. Do you have an example of the CSV file already?
profile picture

janu314

#6
Thanks Eugene,


1. I will use 1 min and GetTime as suggested by you.


2. I am able to generate backtest trades but not trade alerts/trades. When I do the code as below: I get trades in my chart but no trade alerts, or Orders in my orders tool bar.

I do not understand how to generate alerts or to send orders to market/paper account. Please help.


CODE:
Please log in to see this code.




3. . I am attaching a sample CSV file below. Perhaps I can code the parser from scratch.
profile picture

Eugene

#7
2 - The code snippet is not enough.

3 - Attachment did not work out.
profile picture

janu314

#8
Eugene,

here is the CSV file, was unable to attach it first time!

here I think is the relevant code for buy order. I can attach the whole file if you like ?

CODE:
Please log in to see this code.



thanks a lot for your help!

Janu
profile picture

Eugene

#9
Thanks, that's better. Not seeing any obvious faults. The whole file would let me evaluate the composition and if the trading loop is used correctly. To attach the CSV, just zip it or rename to TXT.
profile picture

janu314

#10
Eugene,


Here is the whole file. Hope you can tell me my mistake. Or perhaps, the issue is one of my settings on WLPro?

thanks a lot!

PS: there is a lot of debugging (print) code which you can ignore.
janu

CODE:
Please log in to see this code.


Here is the original CSV(txt) file as you asked. I am using a simple one for the code above, as I don't have a parser for this one yet.


many thanks,

J--
profile picture

Eugene

#11
Here's a quick example that parses your file (roughly) and generates trades or alerts depending on file contents:

CODE:
Please log in to see this code.
profile picture

janu314

#12
Great! Thanks Eugene,


I hope that will generate trades/alerts. I will try it and let you know.

I hope there is a not a problem with my settings, not sure why I was not getting any trades before?


thanks again,

janu


profile picture

Eugene

#13
Slightly improved code to parse the "Trade Indication" field (buy/hold/sell).

Note that to get an alert the trade date in the file should be "today" and the script must be running in Streaming mode (or in the Strategy Monitor) before 3:45pm already.

CODE:
Please log in to see this code.
profile picture

janu314

#14
Thanks Eugene,



Hope you had a great holiday weekend.

I just got back and I see that the code seems to be working as planned.

***********
But,
I am still having some issues:

[1] I would like to first stage and manually place the orders
Then to Auto-place the orders in the live market. Does this need special permissions on the account from Fidelity ?
Trying to find the correct sequence of steps to accomplish this. If you have any pointers for me let me know.


[2] If I upload a Sell Signal from the .CSV file (does not have associated position).
On WLPro I will have to send this as a Short order ? Right ?

thanks again!

janu
profile picture

Eugene

#15
1 - I think yes but you're better off asking your Fidelity rep for the auto-trading requirements. The Wealth-Lab User Guide's Orders chapter is your go-to reference. Should you have general usage questions regarding staging/placing orders, you're welcome to ask them in new/other threads.

2 - It's up to you. A 'sell' does not necessarily means 'short'. You could wait until there's signal to short by your system and stay in cash. If I didn't understand you feel free to correct me.
profile picture

janu314

#16
Eugene,

Sorry I was unclear.

The problem is that I do not know how to retrieve the current Cash balance and the positions from the Accounts Window in the program?

Even though I have positions (overnight etc), the method Positions.Count returns zero, (as they were not part of backtest I think). So I am unable to sell them.

How do I retrieve Cash balance, buying power , positions for a particular account in the Code. Then I can do the orders.

hope that is clearer.

thanks

janu
profile picture

Eugene

#17
Janu,

Pulling a rabbit out of hat is not a good idea. You asked for importing signal from CSV file, and you got it more or less. Retrieving cash balances, buying power and external positions was never a requirement for your simple task. WealthScript is not meant for that and it does not include any methods to support it. That's a big topic of its own.

Technically, it may be possible to work around some limitations, like Community Components lets one import the history of trades from Wealth-Lab's Accounts tool. But the simplest and best solution might be to not have any external positions to sync up to. The signal file should not be telling you to sell a Position not owned by your system.
profile picture

janu314

#18
Thanks Eugene,


Sorry I got off topic!

Yes, thanks for all your help so far with everything.

I am just trying to execute a sell order and I can't because I don't have position object. Trying to see if there is a way around this ?


thanks

janu
profile picture

Eugene

#19
QUOTE:
I am just trying to execute a sell order and I can't because I don't have position object. Trying to see if there is a way around this ?

Before you're able to sell something, first you have to buy that something. So as a workaround you have to establish a historical Position. How? Simply create it on a preceding bar with BuyAt*/ShortAt* methods.

How I would do it? I'd insert a duplicate check for...
CODE:
Please log in to see this code.

...but this time before the main loop which starts at...
CODE:
Please log in to see this code.

The idea is that if you got a sell signal from CSV and there's no matching Position...
CODE:
Please log in to see this code.

...you create a dummy Position at whatever bar# you feel appropriate. Now, the regular processing of "if( indication == 3 )" will operate with an existing position.

But to stress, the correct way is to avoid this scenario. How come your Strategy is selling a Position that it doesn't own?
profile picture

janu314

#20
Great! Thanks Eugene,

That seems like a great workaround!

I will give it a shot.

janu