AddOrder Logic - Simulate BUY AUTO and SELL AUTO

D

diazlaz

Well-known member
2019 Donor
VIP
Hi Everyone,

Inspired by @RobertPayne and this post, for a while, I been trying to replicate the auto functionality in the AddOrder. Seems the behavior is different when from Auto and Manual on how AddOrder is executed.

I'm trying to simulate the buy and sell and simulate the reversal from going long to short or vice-versa in the same bar. Auto it seems the entry is taken at the same bar while manual is on the next bar.

Let's use this as an example:



Ruby:
def EMA = ExpAverage(close, 8);
def STATE = if close > ema then 100 else -100;
plot EMA8 = EMA;

AssignPriceColor (
if STATE == 100 then COLOR.GREEN else COLOR.RED);

AddOrder(OrderType.BUY_AUTO,no);
In this example, if close > ema then go long else go short, if you're in a long trade and trend changes, reverse and go short, vice-versa.

1) Using Auto:

Ruby:
def EMA = ExpAverage(close, 8);
def STATE = if close > ema then 100 else -100;
plot EMA8 = EMA;

AssignPriceColor (
if STATE == 100 then COLOR.GREEN else COLOR.RED);

AddOrder(OrderType.BUY_AUTO,no);

##Automatic
AddOrder(OrderType.BUY_AUTO, condition = (STATE == 100),  name = "BAUTO: " + BarNumber());
AddOrder(OrderType.SELL_AUTO, condition = (STATE == -100),  name = "SAUTO: " + BarNumber());


This works as expected, notice that is opening and closing the position at the same bar with a buy to open and a sell to close, vice-versa.

2) Using Manual

Ruby:
def EMA = ExpAverage(close, 8);
def STATE = if close > ema then 100 else -100;
plot EMA8 = EMA;

AssignPriceColor (
if STATE == 100 then COLOR.GREEN else COLOR.RED);

##Long
AddOrder(OrderType.BUY_TO_OPEN, condition = (STATE == 100),  name = "BTO: " + BarNumber());
AddOrder(OrderType.SELL_TO_CLOSE, condition = (STATE == -100),  name = "STC: " + BarNumber());

##Short
AddOrder(OrderType.SELL_TO_OPEN, condition = (STATE == -100),  name = "STO: " + BarNumber());
AddOrder(OrderType.BUY_TO_CLOSE, condition = (STATE == 100),  name = "BTC: " + BarNumber());
The behavior is different, it executes in the next bar as such:



The reason, is i'm trying to build a strategy that will reverse on trend, but at times, I will pause getting into new positions, but want to close the position when trend change occurs BUT entire in the direction of the trend when that condition is cleared.

Any suggestions or ideas would be greatly appreciated.
 
Last edited:
YungTraderFromMontana

YungTraderFromMontana

Well-known member
I have the credentials for what I want to trigger the orders in the strategy but am a novice at coding and can't figure out how to create the automatic orders. All I need is code where I can quickly plug in my credentials for when a order takes place. (ex buy put when , sell put when).
 
YungTraderFromMontana

YungTraderFromMontana

Well-known member
I finally have the signals and orders on my strategy finished but now want to backtest it. Ideally it would allow me to fine tune my strategy by showing me an updating P/L. When I would make a change in the strategy, there would also be a change in the results. This could allow me to perfect my strategy and squeze every possible dollar or cut every preventable loss. Any feedback will be helpful in passing this final step so thanks everyone.
 
D

diazlaz

Well-known member
2019 Donor
VIP
I finally have the signals and orders on my strategy finished but now want to backtest it. Ideally it would allow me to fine tune my strategy by showing me an updating P/L. When I would make a change in the strategy, there would also be a change in the results. This could allow me to perfect my strategy and squeze every possible dollar or cut every preventable loss. Any feedback will be helpful in passing this final step so thanks everyone.
sure share your script and we will provide our observations.
 
YungTraderFromMontana

YungTraderFromMontana

Well-known member
For the strategy I'm backtesting I don't want it to get caught holding postions overnight, how could I make a sell order triggered by a time?
 
K

kaflesu

New member
Hi All,
I am trying to backtest the strategy using the thinkorswim. Whenever I add the strategy and add the FloatingPL, after pressing apply it disappear from the Lower. Does anyone know the reason?

Thanks
 
K

kaflesu

New member
It seems like I have created study, not the strategy.
Thanks
 
R

Rainmaker

New member
Hi everyone, I have been trying this out for hours and would appreciate some help.

I am trying to set up a simple conditional order such that when a stock on a 5-minute chart closes past a certain level, it triggers a market buy order.

I am using other timeframes for the chart, so I didn't use the default "<current period>" and used the AggregationPeriod.FIVE_MIN

This is the entire code on thinkScript editor:
close("period" = AggregationPeriod.FIVE_MIN) more than 100 within 60 bars

I got the error "secondary period cannot be less than primary"

Can someone tell me what went wrong?
 
B

Bobbydigital83

New member
Im looking all over the internet to for an idea but cannot find one. I have created these lines of code but results are not producing the results that I was expecting.

I have basic TOS coding skills but this one is really frustrating me on figuring out.

AddOrder(OrderType.SELL_TO_CLOSE, EntryPrice() + 2.5 or EntryPrice() - 1); #TP at EP +2.5 points or SL at EP -1 points

AddOrder(OrderType.buy_TO_CLOSE, EntryPrice() - 2.5 or EntryPrice() + 1); #TP at EP -2.5 points or SL at EP +1 points


Any help would be greatly appreciated!
 
D

dvorakm

New member
2019 Donor
VIP
I am trying to test out a strategy, but the sell orders are not behaving (selling at wrong time) and therefore jacking with the open orders.

Could someone please help me with a command to test out the buying only part of a strategy? That is, implement only the buying portion of the strategy so I can test and refine that portion.

when I comment out the "sell" potion, I only get one "buy" order, because it is expecting a sell to close. What I am trying to do is bypass that and have multiple buy orders triggered by the strategy, ignoring the "sell" part.

Then, I would be able to refine the selling part of the script so that it sells at the correct time.

thanks in advance.

mike
 
I

ianfaler

New member
I am looking for a program that can trade futures automatically.
As an example: Say I want to buy 1 /ES contract when the 10SMA crosses above the 20SMA on the 30 extended chart to go long. When that happens I need a conditional order to trigger that says we want to close and go short (sell 2 contracts) of /ES when the 10SMA crosses back below the 20SMA on the 30 extended chart. When that happens we need the long order to go back in (2 contracts again so that we sell the short and buy the long). This can be done in market orders but I dont know if there is a way to create a rolling conditional order in ToS. Any idea about this? Can this be created in ToS?
 
P

Piper2808t

Member
2019 Donor
VIP
hello i need help in a code i have dollar_amount in a backtst but want to replace it with tick amount
 
K

Ken_Adams

New member
So have been messing around with ToS scanner to try to predict if tomorrow will have a gap up based off the strength of the last 15 mins of a stock. My thought is I would buy in the last 15min, maybe a 30 delta call, and sell the next morning.

Scanning the 1m chart for things like Stochslow crossingover Fast within a certain range
RSI within a range
momentum over OBV
AvgVol increasing
Thinking about adding some kind of component like "percent below yesterdays high" to the scan

Now I want to be able to backtest it but having problems writing the strategy based off my limited knowledge. Could someone point me in the right direction to get started? Maybe other strategies I could pull from, or input on other indicators to use?
 
Last edited:
R

ryan5439

New member
What is the code for having 3 separate indicators making a buy/sell signal?

For example

This is the code for simple moving avg strat.

def crossAbove = price crosses above Avg;
def crossBelow = price crosses below Avg;
def buy;
def sell;
switch (mode) {
case "Trend Following":
buy = crossAbove;
sell = crossBelow;

I want to have "crossAbove" AND another indicator tripped AND another indicator tripped creating the buy signal.
 
O

Omar

New member
VIP
Is there anyway to add slippage on strategies?
 
Last edited by a moderator:
J

Jompatan

New member
I have the problem of not knowing how to see if this script is profitable.
It is as simple as buying when the short moving average crosses the long moving average and selling when the opposite occurs.
I don't know if I have set the AddOrder function well.

I know of the existence of the FLOATINGPL study but it shows nothing.
 
Top