Having an issue with Incorrect code for count

greco26

Active member
Hi All,

Was wondering if someone can take a look at my code and tell me why my 'movediff' line isn't counting the difference between the exit and entries (the chart bubbles). My goal is to combine the historical difference in the entry and exits then divide by the number of times the trade triggered so that I can get the average move per trade. I've been at this all day and im stumped....

Code:
def h = high;
def l = low;
def o = open;
def c = close;

def insidebar =  (H <= H[1] and L >= L[1]);
def twodown  = H <= H[1] and L < L[1];
def Cross_Up =  H > H[1];
def Mag_UP = H >= H[2];

# Strat Combos

def Two_One_UP =  twodown[2] and insidebar[1] and Cross_Up ;
def bn = barnumber();
def trigger =  Two_One_UP;
def triggercount = if trigger then triggercount[1] + 1 else triggercount[1];
def on = if trigger then on[1] * 0 + 1 else if on[1] ==1 and low <low[1] then on[1]-1 else on[1];
def entry = if on == 1 and on[1] == 0 then high[1] else double.nan;
def exit = if on[1] == 1 and on == 0 then low[1] else double.nan;
def entryprice = if trigger and !exit then high[1] else entryprice[1];
def exitprice = if exit then low[1] else exitprice[1];

def movediff = if on[1] ==1 and on ==0 then movediff[1] + exitprice-entryprice else movediff[1];

def avgmovediff = movediff/triggercount;

addlabel(yes, movediff, color.white);
addchartbubble(entry,high[1], high[1], color.cyan);
addchartbubble(exit,low[1], low[1], color.magenta);
 
Solution
Thanks for the response. I think the issue though is not the labels but rather the line of code that should be counting the difference between entry and exit. I have the labels just to visualize.

Ok, here is a revision to show the movediff based upon entrys and exits only when there is an exit after an entry. Any entry without an exit will not be counted until there is an exit. A bubble is now above each exit to show the running total difference of exitprice - entryprice. Triggercount was revised to only count exits.

Capture.jpg
Ruby:
def h = high;
def l = low;
def o = open;
def c = close;

def insidebar =  (h <= h[1] and l >= l[1]);
def twodown  = h <= h[1] and l < l[1];
def Cross_Up =  h > h[1];
def Mag_UP = h >= h[2];

#...
Hi All,

Was wondering if someone can take a look at my code and tell me why my 'movediff' line isn't counting the difference between the exit and entries (the chart bubbles). My goal is to combine the historical difference in the entry and exits then divide by the number of times the trade triggered so that I can get the average move per trade. I've been at this all day and im stumped....

Code:
def h = high;
def l = low;
def o = open;
def c = close;

def insidebar =  (H <= H[1] and L >= L[1]);
def twodown  = H <= H[1] and L < L[1];
def Cross_Up =  H > H[1];
def Mag_UP = H >= H[2];

# Strat Combos

def Two_One_UP =  twodown[2] and insidebar[1] and Cross_Up ;
def bn = barnumber();
def trigger =  Two_One_UP;
def triggercount = if trigger then triggercount[1] + 1 else triggercount[1];
def on = if trigger then on[1] * 0 + 1 else if on[1] ==1 and low <low[1] then on[1]-1 else on[1];
def entry = if on == 1 and on[1] == 0 then high[1] else double.nan;
def exit = if on[1] == 1 and on == 0 then low[1] else double.nan;
def entryprice = if trigger and !exit then high[1] else entryprice[1];
def exitprice = if exit then low[1] else exitprice[1];

def movediff = if on[1] ==1 and on ==0 then movediff[1] + exitprice-entryprice else movediff[1];

def avgmovediff = movediff/triggercount;

addlabel(yes, movediff, color.white);
addchartbubble(entry,high[1], high[1], color.cyan);
addchartbubble(exit,low[1], low[1], color.magenta);

These were values rather than true/false (1,0)

def entry = if on == 1 and on[1] == 0 then 1 else 0;
def exit = if on[1] == 1 and on == 0 then 1 else 0;

Here is revised code with a 3moD chart of AAL with one set of bubbles that now match the labels shown.

Capture.jpg
Ruby:
def h = high;
def l = low;
def o = open;
def c = close;

def insidebar =  (H <= H[1] and L >= L[1]);
def twodown  = H <= H[1] and L < L[1];
def Cross_Up =  H > H[1];
def Mag_UP = H >= H[2];

# Strat Combos

def Two_One_UP =  twodown[2] and insidebar[1] and Cross_Up ;
def bn = barnumber();
def trigger =  Two_One_UP;
def triggercount = if trigger then triggercount[1] + 1 else triggercount[1];
def on = if trigger then on[1] * 0 + 1 else if on[1] ==1 and low <low[1] then on[1]-1 else on[1];
def entry = if on == 1 and on[1] == 0 then 1 else 0;
def exit = if on[1] == 1 and on == 0 then 1 else 0;
def entryprice = if trigger and !exit then high[1] else entryprice[1];
def exitprice = if exit then low[1] else exitprice[1];
addlabel(1, entryprice + " "+ exitprice, color.white);
def movediff = if on[1] ==1 and on ==0 then movediff[1] + exitprice-entryprice else movediff[1];

def avgmovediff = movediff/triggercount;

addlabel(yes, movediff, color.white);
addchartbubble(entry,high[1], high[1], color.cyan);
addchartbubble(exit,low[1], low[1], color.magenta);
 
These were values rather than true/false (1,0)

def entry = if on == 1 and on[1] == 0 then 1 else 0;
def exit = if on[1] == 1 and on == 0 then 1 else 0;

Here is revised code with a 3moD chart of AAL with one set of bubbles that now match the labels shown.
Thanks for the response. I think the issue though is not the labels but rather the line of code that should be counting the difference between entry and exit. I have the labels just to visualize.
 
Thanks for the response. I think the issue though is not the labels but rather the line of code that should be counting the difference between entry and exit. I have the labels just to visualize.

Ok, here is a revision to show the movediff based upon entrys and exits only when there is an exit after an entry. Any entry without an exit will not be counted until there is an exit. A bubble is now above each exit to show the running total difference of exitprice - entryprice. Triggercount was revised to only count exits.

Capture.jpg
Ruby:
def h = high;
def l = low;
def o = open;
def c = close;

def insidebar =  (h <= h[1] and l >= l[1]);
def twodown  = h <= h[1] and l < l[1];
def Cross_Up =  h > h[1];
def Mag_UP = h >= h[2];

# Strat Combos

def Two_One_UP =  twodown[2] and insidebar[1] and Cross_Up ;
def bn = BarNumber();
def trigger =  Two_One_UP;

def on = if trigger then on[1] * 0 + 1 else if on[1] == 1 and low < low[1] then on[1] - 1 else on[1];
def entry = if on == 1 and on[1] == 0 then 1 else 0;
def exit = if on[1] == 1 and on == 0 then 1 else 0;
def entryprice = if trigger and !exit then high[1] else entryprice[1];
def exitprice = if exit then low[1] else exitprice[1];
def moveentry = if entry then moveentry[1] + entryprice else moveentry[1];
def moveexit = if exit then moveexit[1] + exitprice else moveexit[1];
def movediff = if  exit then movediff[1] + (exitprice - entryprice) else movediff[1];
def triggercount = if exit then triggercount[1] + 1 else triggercount[1];
def avgmovediff = movediff / triggercount;

AddLabel(yes, "Difference of Entrys with Exits = " + movediff, Color.WHITE);
AddLabel(1, "AvgMove = " + avgmovediff + " (" + movediff + "/" + triggercount + ")", Color.WHITE);
AddChartBubble(exit, high, movediff);
AddChartBubble(entry, high[1], high[1], Color.CYAN);
AddChartBubble(exit, low[1], low[1], Color.MAGENTA);
 
Solution

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
293 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