Click here to Login





                                                   Adjust the Entry and exit prices of trades in AMM script

  0

0
Dave Walton
2014-03-21 20:51:38


Hi QS,

I am interested in dynamically adjusting the prices at which trades are executed (both open and close prices) in an AMM script. I'm seeing unexpected behavior.

To do this, here are some code snippets I'm using
OnNewPosition:
double OpenPrice = NewPosition.Price;
double ModPrice = OpenPrice +{x}; // where x is my adjustment factor
Functions.UpdateOrder(Orders.SimulatedOrder(ModPrice));

This does not seem to work at all. Even if I set {x} to 0, the realized entry price changes by some amount in every case. Why is the NewPosition.Price not always the same as what is shown in the trade log?

OnClosePosition:
double ClosePrice = Functions.GetPositionDetails().LastPrice;
doubel ModPrice = ClosePrice + {x}; // where x is my adjustment factor
Functions.UpdateOrder(Orders.SimulatedOrder(ModPrice));

This works for normal exits but does not work for stop loss orders. When a position is closed via a stop loss, the Functions.GetPositionDetails().LastPrice seems to be not even close to what is shown in the trade log.

Please help.



QuantShare
2014-03-22 13:00:04

  0

Hi Dave,

In "OnNewPosition", you probably have set slippage or commissions and in that case the adjusted open price will be different from the "NewPosition.Price".

I have just tested your script with a basic trading system and it does work as expected.

Regarding stop loss, note that in order for a stop order to pass the "OnClosePosition" event, you must put the following line in "OnStartSimulation":
TradingSystemSettings.IsExecuteOnClosePositionOnStop = true;


"Functions.GetPositionDetails().LastPrice" gets the last close price while your position is probably exited on the open of the next bar.




Dave Walton
2014-03-24 22:43:52

  0

Hi QS,

I verified that slippage and commissions are set to zero. I also added your suggested "OnStartSimulation" statement to enable running OnClosePosition for a stop loss.

Two problems are still there:
1) NewPosition.Price is not returning actual entry price,
2) Functions.GetPositionDetails().LastPrice does not return the actual stop price. I'm not sure if that is the intent or not. I want to modify the actual stop price, not the close price of the bar. How can I do that?

I have sent you an email with files that show these problems.



QuantShare
2014-03-25 11:31:43

  0

Hi Dave,

1) "NewPosition.Price" is not the entry price. It is the last price (close price).
On "NewPosition" event the trade is not yet executed so at that time we don't know the execution or entry price.

2) "Functions.GetPositionDetails().LastPrice" returns also the last price (close price).
You cannot modify that of course.

You want to update the stop price in the "OnClosePosition" event?
In most cases, changing the stop price on the "OnClosePosition" event doesn't change anything. The position is about to be closed.




Dave Walton
2014-03-25 17:57:49

  0

Hi QS, it just occured to me that I haven't explained what I'm trying to do. I'm trying to programmatically update realized slippage in the AMM. I got the idea from reading your post here: http://www.quantshare.com/sa-110-transaction-costs.

I was thinking that the easy way is to update the execution price both on a new position and on closing a position. If this is not how, kindly give me a pointer on this. Thanks much.



QuantShare
2014-03-26 12:24:15

  0

Updating the execution price will update the total position settings (P&L, exit/entry price, max drawdown...)
There is no way to update the slippage per symbol as it is calculated automatically based on the slippage setting of your trading system.



Torsten
2015-06-24 12:56:28

  0

Hi QS,

is there a way to update the pos.Entryprice in the OnEndPeriod event?

I also want to add a slippage logic.

Thanks,
Torsten



QuantShare
2015-06-24 17:57:58

  0

You cannot update the "Entryprice".
You can add a slippage logic either by updating the slippage setting or programmatically in the "OnNewPosition"

Example:
_TradingOrder order = Orders.SimulatedOrder(price); // Specify execution price here
Functions.UpdateOrder(order);



Torsten
2015-06-26 05:09:04

  0

Hi QS,

Thanks, it works. What brings me to the next problem :)

I use the script for future trading. To calculate the slippage I have to use two time series:
1. Backadjusted futures (for backtesting)
2. Nearest futures (the actual prices in the past for slippage calculation)

For the slippage calculation I look up the price of the given period in the neares future time series. That works fine.
But as I use the script in the "OnNewPosition" event, the returned price is the price of the signal day.
To get the price of the execution day, I would have to snoop one day in the future, but unfortunately that doesn't seem to work:

double OpenN = Data.GetPriceSeries(NSymbol,"open")[-1]; // snoop one day in the future: [0] signal day, [-1] execution day

I used [1] and [-1], either way the script returns the price of the past period.

Any idea?

Thanks,
Torsten








QuantShare
2015-06-26 21:10:38

  0

You need to use "Data.ParseFormula" for this.

TimeSeries goog = Data.ParseFormula("a = ref(close, -1);").GetTimeSeries("GOOG", "a");



Torsten
2015-07-10 06:39:15

  0

Hi QS,

I added the slippage logic as described in the NewPosition Event:

_TradingOrder order = Orders.SimulatedOrder(price); // Specify execution price here

It works fine as long as the time series is traded next bar. It looks like QS executes the order even the time series has no prices in the next simulated bar.

In my case FCH (CAC40 future) :
28.10.94 - Signal Enter Short
29.10.94 - Weekend
30.10.94 - Weekend
31.10.94 - No prices -> simulated order with slippage logic gets executed here, even CAC40 future wasn't traded this day.
01.11.94 - No prices
02.11.94 - Execution Enter Short -> correct execution day, execution works if order isn't updated with the code above

Is it possible to ensure that the order gets executed at the next bar with price information?

Thanks,
Torsten



QuantShare
2015-07-10 12:10:35

  0

Are you sure there was not data on these days? How do you know that the order was executed exactly on that date?

Are you tracking orders using the "Details" tab? You should see there that weekends and "no price" days are not available.



Torsten
2015-07-10 14:57:19

  0

Hi QS,

Yes, I tracked the trade in the details tab. If I apply the trading order update the trade gets executed next bar (Monday) no matter if prices are available.

Without trading order update (correct)
28.10.94 - Opened
31.10.94 - There is no trading bar for the current date
01.11.94 - There is no trading bar for the current date
02.11.94 - Order Executed


With trading order update;
28.10.94 - Opened, Updated Order - MM
31.10.94 - Order Executed

Data:
03.11.1994 00:00 1538,5 1500 1539 1500 3499 20500
02.11.1994 00:00 1500 1505,5 1512,5 1496 22288 20417
28.10.1994 00:00 1537,5 1492 1538 1488 4657 20860
27.10.1994 00:00 1487 1477 1485,5 1466 15542 20836





QuantShare
2015-07-10 18:36:37

  0

Hi Torsten,

After checking again, I can confirm that the "SimulatedOrder" would execute any trade at the specified price no matter if there is a trading bar or not on the next day. Alternatively, you can use a limit or stop order but as you know there is no guarantee the order will be filled on next trading bar.



Torsten
2015-09-09 04:49:27

  0

Hi all,

I'm currently building a system completly in the AMM and add long and short orders in the OnEndPeriod event.
Since the OnNewPosition and OnClosePosition events are not triggered, I'm looking for a way to pass the execution price with the order or update the order after it was added.

I use the following statements to add long and short orders:

bool res = Functions.AddLongPosition(symbol, possize,Orders.OpenMarketOrder(),settings);
bool res = Functions.AddShortPosition(symbol, possize,Orders.OpenMarketOrder(),settings);

Does anyone has a hint how to pass the execution price with the order or update it with an execution price after it was added?

Thanks,
Torsten




QuantShare
2015-09-12 09:47:22

  0

Hi Torsten,

For this you need to use the SimulatedOrder class.

Example:
bool res = Functions.AddLongPosition(symbol, possize,Orders.SimulatedOrder(price_here),settings);



No more messages
0




Reply:

No html code. URLs turn into links automatically.

Type in the trading objects you want to include: - Add Objects
To add a trading object in your message, type in the object name, select it and then click on "Add Objects"










QuantShare

Trading Items
Staggered Entry and Exit Strategy
Entry Price and Exit Price in Trading System Report
Market Rule based on the Crossover Between Stock Close prices and...
Adaptive Trading Indicator based on the Return of Past Trades
Adaptive Trading System - Percent Winning Trades for the Last N-D...

How-to Lessons
How to get stocks for a particular index using the global script
Difference between the watchlist and the screener tools
How to scale and move the chart Y-axis
How to calculate the average of a time series using the money man...
How to add a metric in the trading system simulation report

Related Forum Threads
Money Management script and exit options in the wizard
How do you retrieve the entry and exit price for a position?
MM Staggered Entry and Exit
Can I update the sector and industry information in the stock dat...
Entry and Exit in Rule manager analyzer

Blog Posts
How to predict and trade the stock market using pivot points
How to Adjust the Time Displayed in your Intraday Charts
How to get buy and sell orders for a portfolio based on a trading...
6 ways to download free intraday and tick data for the U.S. stock...
Charting & Scripts - Manage stock charts using the global script









QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright 2024 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
Follow us on Google+
RSS Trading Items



Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.