Running a script with Live Trading
Author: dr_kin_cheung
Creation Date: 1/13/2011 10:46 AM
profile picture

dr_kin_cheung

#1
Hi All,

I have created a very simple script to buy at the 5th bar of the intraday (bar=minute), and sell after the 10th bar (or sell if the close is 10% less than the entry price, or sell if bar >= 388). It worked as intended when I do backtest. However, when I run the same script during live trading, the script did not execute the sell order. I am wondering if the function "IsLastPositionActive" is only for back test and not live trading since it did not get into the loop.
Below is my code. Any input from you would be very much appreciated. Thank you!


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

Cone

#2
You won't get an order if the condition doesn't exist - maybe there were never 388 bars in your chart. (Many Providers, like Fidelity, don't add zero-volume "bars" to the chart.) And, Alerts occur for future trading actions (not past ones). In other words, if you loaded up the chart and it hadd 390 bars, then you're too late to get the Alert.

Another problem is that you're making comparisons for absolute bar numbers. The script assumes that you start with fewer than 5 bars in a streaming chart.

See the following WealthScript methods of the Bars object: IntradayBarNumber and IsLastBarOfDay.

Also, in the Community.Components: Intraday Support functions.
profile picture

dr_kin_cheung

#3
Hi Cone,

Do you know what function I need to use to get the sell loop executed? I really would like to try out my code in live trading using real money before the market close today.

Thank you very much!
Kin
profile picture

dr_kin_cheung

#4
Hi,

I just need the script to sell the positions that I have. The above script can buy at the bar that I specified but it won't sell. What is the proper way to generate a sell order? If you have any codes that you used in the past that actually sold the positions in live trading that you can share with me would be very much appreciate. Again, thank you!

Kin
profile picture

Cone

#5
If you're code already bought, then it already sold too because it's supposed to sell 10 bars after the entry bar. Assuming you use a 1-minute chart, you'll never get an Alert to sell at bar 388.

Is the idea to manage a sale for a Position that you own?
profile picture

dr_kin_cheung

#6
Hi Cone,

No, the code did not sell my position. It bought but never sold the position. Any clues on what I should do to get it sold from the script?

The whole point of this excercise is to get an idea of how to buy and sell in live trading. It worked in back testing as intended but not in living trading.

Yes, I am aware that it will never get to bar 388.

Kin
profile picture

Cone

#7
The strategy doesn't make any sense for live trading because of the way you programmed it to work. If you run that code on any chart with more than 18 bars, it will buy on bar number 6 and sell on bar number 18. That's what it does. It will create those theoretical trades. It will do it every time, no doubt.

Since I don't understand what you're trying to do, I can't really help. What do you really want to happen?
profile picture

dr_kin_cheung

#8
Hi Cone,

I just want a code that will sell at the market intraday with bar=minute. Any script that you have with a sell order would be fine. I just want to see a sell order through the scripting in live trading. I know the buy code in my script works but not the sell code.

Thank you!!
Kin
profile picture

dr_kin_cheung

#9
Hi Cone,

Let me just quickly clarify. The code that I have does the buy order for me and I am comfortable that it will buy at the specified bar number.

However, the code above does not sell the position that I just bought. I would like the script to sell the position at the market price. I just can't get it to sell. Maybe it never get into the sell loop. Please let me know if this is not clear.

Thank you.
profile picture

Cone

#10
Paste this in, compile, and run it in a streaming window.

CODE:
Please log in to see this code.

This will give you an Alert to exit a Position at 15:58. When the 15:59 bar completes, you'll see that exit in the chart. Make sure to look at the full Trades log, double click on a trade to see it in the chart.
profile picture

dr_kin_cheung

#11
Hi Cone,

Thank you very much for the code. I will execute your code above tomorrow morning in live trading since the market already closed.

Also, can you please show me another condition to sell at 10 bars after I bought it. The code above waits until the end of the day to sell or a loss to sell.

I want to see it will actually sell the position that I just bought in 10 minutes. Many thanks!

Kin
profile picture

Cone

#12
You can add your condition back for that. You did that correctly - well you were off by a couple bars, it should be: if ((bar + 1 - p.EntryBar) >= 10). The point I was making before was that your code will only buy on bar 6 and sell several bars after that - only once. For you to see the buy, you have to load a streaming chart with 5 or fewer bars to see the Alert for the buy, and then wait another 10 (or 12 bars with your code) for the sell.

Because you hard coded bar numbers, they only relate to the number of bars in the chart. If you refreshed the chart midday to load 5 or fewer bars, then the process would have repeated.

This quite different than what I've given you. In my version of the code, I assumed that you wanted to buy on the 6th intraday bar, i.e., Alert at 09:35 (on a 1-minute chart) to buy the open of the 09:36 bar. Then, I assumed you wanted to sell on the open of the 15:59 bar. It does this for every 390-minute day you have loaded.

If you add the sell for 10 bars, which will effectively exit at 09:46, the close of market exit will never occur because a Position will never exist for that logic to ever execute. Clear?
profile picture

dr_kin_cheung

#13
Hi Cone,

Thanks for reponse!
Let me start all over. I am trying to write a script to do automatic DAY trading with real money (in and out of the stock market in 10 minutes). The code that I pasted above works for back testing but not during live trading. The code above automatically buy the stocks at the 6th bar as I specified. However, it does not sell the position that I just bought at 6th bar regardless of whether it's 16th, 17th, ... etc bar. I need the script to sell the stocks at the market but it is not do the selling.

I do not need the script to send me an alert to buy and then manually hand place the trade. I want the script to automatically buy and sell the stocks that I have in the data tree.

Within WL, the process that I had to go through to accomplish this task is by activating the strategy and then click on
'auto-trade'.

Then I see the order for buying at the 6th bar kicked in. After some minutes past by, the script did not sell the stocks. I want the script to sell at the market price.

To make things short, I woulk to buy at 9:36 am and sell at 9:46 am (or 9:47 or 9:48, does not matter) using 'auto trade' when the stock market opens. So far, I have been able to get the script to automatically buy the stocks that are in my datatree but the script does not execute the SellAtMarket code. There is no strategy in the code. The whole point of this execercise is to see how the script automatically buy and sell my stocks. All the questions that I asked are for DAY trading.

I hope this is clear. Again, thank you very much for your time and being so patient!

Kin
profile picture

Cone

#14
QUOTE:
The code that I pasted above works for back testing but not during live trading.
Sure it does, but it only works for 1 trade.

QUOTE:
However, it does not sell the position that I just bought at 6th bar regardless of whether it's 16th, 17th, ... etc bar.
It must sell it, just look at the Trades log. If you set it up as I said for a streaming chart, your code will Alert for the sell on the 12th bar after the entry. You have to go to the Alerts view to see that Alert. If you want the order to automatically be staged or placed in the Orders tool, then you have to enable the appropriate options. (See User Guide: Orders)

QUOTE:
...After some minutes past by, the script did not sell the stocks. I want the script to sell at the market price.
Okay, let's get more information:
1. Are you doing paper trading or live accounts?
2. If live accounts, did you allow the script to actually purchase the shares for your account? If you didn't, the sell order won't make it to the Orders tool because the Position doesn't exist in your account. (See User Guide: Orders)
profile picture

dr_kin_cheung

#15
Hi Cone,

Have you or do you know anyone that has actually used Wealth Lab to do live DAY trade using real money? If so, can you please show me the code? Can Wealth Lab really do Day trades (in and out of the market in minutes)? I am getting kind of skeptical that WL can actually do Day trade. It does very well for back testing but not live trading.

As for a test of that, I modified the script to buy at 6th bar and then sell at the market the next bar. I just want to see the script actually does the selling for me. The script did not buy nor sell.

Why is it not doing the buying nor selling (if there was no buy, obviously there will be no sell)?

Previously, I was able to buy using the script but the script did not sell. I am using live account and I see the stocks that I purchased using the script in my account. The position does exist in my account.

The script just won't sell at the market even without any conditions.

Thank you!




CODE:
Please log in to see this code.

profile picture

Cone

#16
The only thing that I can assume is that there's an error condition that you're not resolving, or something of the sort. Since the market is closed, let's start with a simple exercise. Maybe in doing it, you'll find a step you've missed along the way. Let's use this script:

CODE:
Please log in to see this code.


1. Log in to Fidelity
2. Open Preferences (F12), select Trading, and select your live account as the Default Account if it's not already there.
3. Open the Orders Tool (Ctrl+R) so that it's in view. (For intraday trading, you'd set up Auto-Trading: Live Accounts, but doesn't matter when the markets are closed since orders will only be Staged and not Placed.)
4. Copy, paste, and save the script. Call it "Always Alert at Market".
4a. Click the Compile button and ensure that it compiles successfully. (You don't have to do this if you open a strategy from the Strategy Explorer. It's only when you make a change in the Editor.)
5. Select any symbol, 1-minute chart, Data Range: 200 bars, Position Size: Raw Profit Mode: 10 Shares.
6. Turn on Streaming.
7. On the Alerts tab, enable "Auto-Stage". (This will change to "Auto-Place" during market hours with Auto-Trading enabled.)

At this point, we'd just wait for a new bar to form and be added to the chart, but since that's not going to happen until next week, just hit "Go" or F5 to refresh the chart. You should see the Alert Auto-Stage in the Orders tool. Did you get it?

Incidentally, that might seem like a lot of work to set up, but you only have to set up things once and save the Workspace. After loading a Workspace, the entire configuration will be loaded with one exception - you always have to turn on Auto-Trading in the Orders tool each day. This is one action that we want you to be fully aware that you're doing.
profile picture

dr_kin_cheung

#17
Hi Cone,

Thanks!

I will try out your code above next week when the market opens.

Kin
profile picture

dr_kin_cheung

#18
Hi Cone,

The steps that you outlined above are very similar to what I did to get the script running with live trading but not exactly what I did. I don't think it was the steps that I had issues with but rather it was the main loop in the script. Below are the steps that I did.

1. login to Fidelity
2. click on "Orders" and the order window pops out.
3. From the orders window, select the account to do live trading then at the "Auto-Trading" drop down menu select "live account".
4. Click on "Strategy Monitor" and it pops up another window.
5. At the "Strategy Monitor" window, click on "Add Strategy" then select the script that I wanted to run.
6. At the "Strategy Activation Settings", I select the appropriate account; Data range - I selected today's date as start date and end date with a future date(The system does not allow the start date and end date to be the same).

I noticed above you had "Number of Bars = 200", do I need to enter 200 bars instead of selecting the date range? Why 200 bars? There are 390 (or 391) minute intraday bars?

7. At the same window (Source Data), I selected the dataset that the script will run on.

8. At the "Strategy Monitor" window, I clicked on "Activate Strategy" and "Auto-Trade".

9. I see a red arrow next to the strategy indicating that the strategy is running. I am comfortable to say that the script is executing.

Please let me know if any of the steps that I did are not correct.

Also, please answer my question above on "Number of Bars = 200".

Thank you!!

profile picture

Cone

#19
QUOTE:
do I need to enter 200 bars instead of selecting the date range?

No, many selections are possible. It's just a suggestion in order to have a common starting point, and, the fastest option to load data is to specify a number of bars.

QUOTE:
Why 200 bars? There are 390 (or 391) minute intraday bars?
The script above needs only 1 bar to work. Even your strategy can start with just 1 bar. It's just a starting point. In a Streaming Window, bars are continually added to the chart. So, if you start with 1, you'll have 2 bars at 9:31, 3 at 9:32, etc. Anyway, you can start with 390 or 20,390 depending on the initial lookback required by your script.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).