• Get $40 off VIP by signing up for a free account! Sign Up

Strategy Help

Max71

New member
Here is the strategy ( see below pseudo code ) I want to develop and test. I can figure out most of the stuff but don't know how to

1 - track bought or sold- if addorder function succeeded or not
2 - if succeeded, how to extract the prices such as bought price or sold price.
3 - how to track the profit during testing of the strategy.

#buy at Market Open
if MarketOpen then
Buy 100 at next open price;
Purchase_Price = open price;
Bought = TRUE;
end if;


if ( close > Purchase_Price + 1) then
Sell 100 at next open price ;
Sold_price = open price;
Bought = False;
end if;

# Sell after Midday if not sold and price hasn't dropped more than 0.50
Midday = Noon Eastern;

If Midday and Bought = True then
if ( close > Purchase_price or close < Purchase_price -0.50 ) then
sell 100 at next open price;
Sold_price = open price;
Bought = False;
end if;
end if;

# Track total profit
total profit = total profit + ( Sold_Price - Purchase_Price ) *100;
 
Solution
@Max71
Use portfolio functions:
Ruby:
def Open_Position = GetQuantity() <> 0;

def Open_Price = If Open_Position then GetAveragePrice() else Double.NaN;

As far as 'sold price' you should define that in your strategy as a price target or some variant thereof.
@Max71
Use portfolio functions:
Ruby:
def Open_Position = GetQuantity() <> 0;

def Open_Price = If Open_Position then GetAveragePrice() else Double.NaN;

As far as 'sold price' you should define that in your strategy as a price target or some variant thereof.
 
Solution
@Max71
Use portfolio functions:
Ruby:
def Open_Position = GetQuantity() <> 0;

def Open_Price = If Open_Position then GetAveragePrice() else Double.NaN;

As far as 'sold price' you should define that in your strategy as a price target or some variant thereof.
Thank you @Svanoy !
Correct , I can do that .. but in following example - How do I capture actual price my position got sold for?
AddOrder(OrderType.SELL_TO_CLOSE, close > Open_price + 1, tradeSize = 1000);
 
@Max71
As far as I am aware there is not a function for sold price.
Best you can do is determine what it should be in the strategy.
Base on your code above, it sold price would be 1 tick higher than the value of 'Open_price' + 1.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
368 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top