Variable TradeSize with AddOrder?

R

RickAns

Member
VIP
Hi All,

What a great site this is. I am really learning a lot about thinkscript.

Is it possible to use different tradeSize values for multiple AddOrder commands within a Strategy?

For example, if I want to open a trade with 20 shares and when a trailing stop is hit to sell 5 shares. Leaving me still holding 15 shares. No matter what I try with "tradeSize = " it insists on selling the entire 20 from the first buy and closing out the position. What am I not doing correctly, or is this hard coded into thinkorswim?

Can provide more examples if needed. I have discovered how for thinkorswim to let me do multiple buys in a row - thanks to BenTen for a picture of that in someone else's post. Just currently trying to figure out how to do a partial sell (or buy, on a pullback) with AddOrder. I am working on this to use with the Floating Profit / Loss function.

Thanks in advance,
Rick
 
Dublin_Capital

Dublin_Capital

Member
You can use something like this:

Code:
input tradeSize = 20;
input partialExit = 5;

AddOrder(OrderType.BUY_TO_CLOSE, condition = closePartial, price = close, tradesize = partialExit, tickcolor = Color.WHITE, arrowcolor = Color.White, name = "Partial Exit");
"closePartial" would be a condition you define to trigger the partial close.

You may be having issues if you are using the preloaded thinkscript "TrailingStopLX" or "TrailingStopSX" strategies. If you are using those separate from your custom strategy, they will default to the Default Trade Size under the Global Strategy Settings.

To get around this, you need to write the trailing stop code into your custom strategy and reference tradeSize from within the strategy. Some cutting and pasting should get you there.
 
Last edited:
R

RickAns

Member
VIP
Appreciate the quick reply DBSully. I am trying to adapt this idea to another forum members strategy that they posted here. Actually it is J007RMC's Pivot Confirmation with Trading Levels - https://usethinkscript.com/threads/pivot-confirmation-with-trading-levels.1843/#post-16985 . I am liking what they have done with that.

Their coding level and yours are above my skill level. I am trying to get the AddOrders to follow his coding and make the full and partial trades at the pivot levels he is using. Thought it would be a great way to learn by trying to deconstruct how things work :). I am plodding along and am sure I will need further help in the future.

Cheers,
Rick
 
Last edited:
Dublin_Capital

Dublin_Capital

Member
@RickAns The best way to learn is to keep doing what you are doing. Break apart the different lines of code in other people's studies and strategies, and figure out how each part works. Thinkscript is an odd language, but you'll be surprised at how far you can get if yo stick with it. This site is amazing - lots of help here.
 
C

cmohn

New member
@Dublin_Capital Thank you for your input. I am trying to do the same thing as the OP but I am unable to get it to work.

I don't think the code you show above accomplishes a partial close of the open position. wouldnt the tradesize = partialExit?

Thanks in advanced.
 
Dublin_Capital

Dublin_Capital

Member
@Dublin_Capital Thank you for your input. I am trying to do the same thing as the OP but I am unable to get it to work.

I don't think the code you show above accomplishes a partial close of the open position. wouldnt the tradesize = partialExit?

Thanks in advanced.
You are correct. I edited the above code to remove the error. It should be accurate now. Thanks for catching that.
 
madeinnyc

madeinnyc

Member
@Dublin_Capital , I'm trying to limit my contract size to 1...and how do I get it to calculate profit only during the cash market? This is for the /NQ. Thanks in advance!

input tradeSize = 1; (IS THIS CORRECT?)
input PandL_Label = Yes;

def orderDir = CompoundValue(1, if s2 < s3 then 1 else if s2 > s3 then -1 else orderDir[1], 0);
def isOrder = orderDir crosses 0;

def orderCount = CompoundValue(1, if IsNaN(isOrder) then 0 else if isOrder then orderCount[1] + 1 else orderCount[1], 0);

def noBar = IsNaN(open[-1]);

def orderPrice = if isOrder then if noBar then close else open[-1] else orderPrice[1];
def profitLoss = if !isOrder or orderCount == 1
then 0
else if orderDir < 0 then orderPrice[1] - orderPrice
else if orderDir > 0 then orderPrice - orderPrice[1] else 0;
#else if orderDir > 0 then orderPrice[1] - orderPrice
#else if orderDir < 0 then orderPrice - orderPrice[1] else 0;
#def profitLoss = if !isOrder or orderCount == 1 then 0 else orderPrice - orderPrice[1];
def profitLossSum = CompoundValue(1, if IsNaN(isOrder) then 0 else if isOrder then profitLossSum[1] + profitLoss else profitLossSum[1], 0);

def orderWinners = CompoundValue(1, if IsNaN(isOrder) then orderWinners[1] else if orderCount > 1 and profitLoss > 0 then orderWinners[1] + 1 else orderWinners[1], 0);

AddLabel(PandL_Label, orderCount + " orders (" + AsPercent(orderWinners / orderCount) + ") | P/L " + AsDollars((profitLossSum / TickSize()) * TickValue()), if profitLossSum > 0 then Color.GREEN else if profitLossSum < 0 then Color.RED else Color.GRAY);
 
R

RickAns

Member
VIP
Have you tried "tradeSize = 1" in your AddOrder line? (Without the quotes)

You can get fancier with an adjustable input if you like. Such as

input initialLots = 1;
....
AddOrder(blah blah, tradeSize = initialLots);
 
madeinnyc

madeinnyc

Member
@RickAns , ok, I tried: AddOrder(Ordertype.BUY_TO_OPEN, tradeSize = 1); but the AddOrder is highlighted in red....I must be missing a condition I can't figure out! :unsure:
 
R

RickAns

Member
VIP
Looks like you missed "type =" in front of Ordertype.


An example:
AddOrder(type = OrderType.BUY_TO_OPEN, price = close, tradeSize = 1, Color.Green, Color.Green, name = "Entry");
 
madeinnyc

madeinnyc

Member
Looks like you missed "type =" in front of Ordertype.


An example:
AddOrder(type = OrderType.BUY_TO_OPEN, price = close, tradeSize = 1, Color.Green, Color.Green, name = "Entry");
Ok, I put the code as shown here:

AddLabel(PandL_Label, orderCount + " orders (" + AsPercent(orderWinners / orderCount) + ") | P/L " + AsDollars((profitLossSum / TickSize()) * TickValue()), if profitLossSum > 0 then Color.GREEN else if profitLossSum < 0 then Color.RED else Color.GRAY);

AddOrder(type = OrderType.BUY_TO_OPEN, price = close, tradeSize = 1, Color.Green, name = "Entry");

However, I'm getting the following error:
Parameter already defined: tradesize
No default value for parameter 'condition' on addorder at 89:1
Expected double at 89:1


The Addorder is still highlighted red. Not sure if Addorder is conflicting with something else.
 
R

RickAns

Member
VIP
Ugh, you like things complex don't ya? I thought we were talking about AddOrder. I have not used AddLabel much.

Some of your quotes look off. Shouldn't " orders ( " be "orders" ( and ") | P/L" be ) | "P/L" ?

Maybe try to simplify it out a bit. Assuming the syntax is correct.
def condition1 = (" + AsPercent(orderWinners / orderCount) + ")
def condition2 = + AsDollars((profitLossSum / TickSize()) * TickValue())

AddLabel(PandL_Label, orderCount "orders" + condition1 + condition2, your if then statements


You may want to add the second Color.Green to the AddOrder if things look funny. If I remember right the first one is the color of an arrow and the second for the name. Could have been my user error but I have had it hijack other colors without it.



Maybe these AddLabel examples from someone smarter than me from a script I use will help.

AddLabel(trendText, concat("Vol: ", if volume > sAvgVolume[0] + 2.0 * sAvgVolumeSTD then "VH"

else if Volume[0] > (sAvgVolume[0] + 1.0 * sAvgVolumeSTD) then "H"

else if (Volume[0] > sAvgVolume[0]) then ">Avg"

else if (Volume[0] < sAvgVolume[0] && Volume[0] > (sAvgVolume[0] - 1.0 * sAvgVolumeSTD)) then "<Avg"

else if (Volume[0] < (sAvgVolume[0] - 1.0 * sAvgVolumeSTD)) then "L"

else ""), Color.white);

AddLabel(trendText, concat("Spr: ", if (spread > (avgSpread * 2.0)) then "W"

else if (spread > avgSpread) then ">Avg"

else "N"), Color.white);



# C_RP 20100809 added isVeryLowCloseBar and changed execution order so that both Verys and Mid execute first

AddLabel(trendText, concat("Cls: ", if (isVeryHighCloseBar) then "VH"

else if (isVeryLowCloseBar) then "VL"

else if (isMidCloseBar) then "M"

else if (isUpCloseBar) then "H"

else if (isDownCloseBar) then "D"

else "NC"), Color.white);



AddLabel(trendText, concat("Trd: ", concat("St-", if (ShortTermTrendSlope > 0) then "U"

else "D")), Color.white);



AddLabel(trendText, concat("Mt-", if (MiddleTermTrendSlope > 0) then "U" else "D"), Color.white);



AddLabel(trendText, concat("Lt-", if (LongTermTrendSlope > 0) then "U" else "D"), Color.white);
 
madeinnyc

madeinnyc

Member
I'm gonna work on it this week and figure it out. I'll then share the code once its complete.
 
Z

Zara2

New member
@Dublin_Capital I tried your method:

Code:
input tradeSize = 600;
input partialExit = 200;

def closePartial = close - EntryPrice() > 2;
AddOrder(OrderType.SELL_TO_CLOSE, condition = closePartial,
    price = close, tradesize = partialExit,
    tickcolor = Color.WHITE, arrowcolor = Color.White,
    name = "Partial Exit");
It's still selling all 600 shares, not the 200 I wanted. I don't think TOS lets you do partial exit.
 
rad14733

rad14733

Active member
VIP
@Dublin_Capital
It's still selling all 600 shares, not the 200 I wanted. I don't think TOS lets you do partial exit.
Correct... TOS only Closes what the Open order trade size was when using the AddOrder backest...
 

Similar threads

Top