Long/Short automation script for moving average

R

rinrin

New member
I have the following code:

Code:
input maline = 9;

def ma = SimpleMovingAvg(Length = maline);
def longsize = 100;
def shortsize = 100;
def buy = close > ma;
def sell = close < ma;

# long when above MA
AddOrder(OrderType.BUY_TO_OPEN, buy, tickColor = Color.LIGHT_GREEN, arrowColor = Color.LIGHT_GREEN, name = "buy", tradeSize = longsize, price = OPEN()[1]);
AddOrder(OrderType.SELL_TO_CLOSE, sell, tickColor = Color.RED, arrowColor = Color.RED, name = "sell", tradeSize = longsize, price = open()[1]);

I'm trying to get to a automated script that buys 100 shares when the daily 9 SMA is crossed (signaling bullish trend). After it buys 100, I'd like it to hold until the daily 9 SMA is crossed again, at which point it should sell my long position and short 100 shares. Then when the daily 9 is crossed again, cover the 100 and go long 100 etc.

Is this possible with thinkscript?

Cheers,
Ri
 
BenTen

BenTen

Administrative
Staff
VIP
ThinkorSwim currently do not support auto trading.
 
R

rogtrader

New member
Interesting.. seems to be simple but effective.. We need to create conditional orders with order rules.. but keep recreating these rules on continuous basis is tricky.. let me test this a bit
 
R

rinrin

New member
Interesting.. seems to be simple but effective.. We need to create conditional orders with order rules.. but keep recreating these rules on continuous basis is tricky.. let me test this a bit

Love to see what you came up with!
 
R

rinrin

New member
This is what I have so far.
Code:
def ma = Average(close, 50);
def size = 10000/close;
def buy = close crosses above ma;
def sell = close crosses below ma;
def position = GetQuantity(getSymbol());
plot SMA = ma;;

# long when above MA
AddOrder(OrderType.BUY_TO_OPEN, buy, tickcolor = Color.LIGHT_GREEN, arrowcolor = Color.LIGHT_GREEN, name = "buy", tradeSize = size, price = OPEN()[-1]);
# sell long position
AddOrder(OrderType.SELL_TO_CLOSE, sell, tickColor = Color.LIGHT_GREEN, arrowColor = Color.LIGHT_GREEN, name = "buy", tradeSize = position, price = OPEN()[-1]);

Don't know how to get the short orders in there, can't use an "and" statement. "If" statements seem to be kinda tricky with this too.
 
A

adii800

Member
This is what I have so far.
Code:
def ma = Average(close, 50);
def size = 10000/close;
def buy = close crosses above ma;
def sell = close crosses below ma;
def position = GetQuantity(getSymbol());
plot SMA = ma;;

# long when above MA
AddOrder(OrderType.BUY_TO_OPEN, buy, tickcolor = Color.LIGHT_GREEN, arrowcolor = Color.LIGHT_GREEN, name = "buy", tradeSize = size, price = OPEN()[-1]);
# sell long position
AddOrder(OrderType.SELL_TO_CLOSE, sell, tickColor = Color.LIGHT_GREEN, arrowColor = Color.LIGHT_GREEN, name = "buy", tradeSize = position, price = OPEN()[-1]);

Don't know how to get the short orders in there, can't use an "and" statement. "If" statements seem to be kinda tricky with this too.
you can just use "auto" for both the buy and sell orders.
 
A

adii800

Member
I think you are looking for something like this?:
Code:
#SMA

input price = close;
input length = 9;
input displace = 0;
input showBreakoutSignals = no;

plot SMA = Average(price[-displace], length);
plot UpSignal = price crosses above SMA;
plot DownSignal = price crosses below SMA;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

SMA.SetDefaultColor(GetColor(1));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

AddOrder(condition = close crosses above SMA, tradeSize = 100, name = "Long", tickColor = Color.CYAN, arrowColor = Color.CYAN, price = CLOSE);
AddOrder(type = OrderType.SELL_AUTO, condition = close crosses below SMA, tradeSize = 100, name = "Short", tickColor = Color.LIGHT_RED, arrowColor = Color.LIGHT_RED, price = CLOSE);

also, yes I didn't need to paste in the code for the sma but I got lazy lol :p
 
YungTraderFromMontana

YungTraderFromMontana

Well-known member
@rinrin and @rogtrader, the reason this makes money is because you are calculating profits using the open not the close inflating the p/l. This wouldn't work if used live.
 

Similar threads

Top