# 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.

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.

@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?

@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.

87k+ Posts
431 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?