NexusFi: Find Your Edge


Home Menu

 





Subgraphs


Discussion in Sierra Chart

Updated
      Top Posters
    1. looks_one tomlryde with 4 posts (0 thanks)
    2. looks_two Nicolas11 with 3 posts (3 thanks)
    3. looks_3 Big Mike with 1 posts (0 thanks)
    4. looks_4 vegasfoster with 1 posts (1 thanks)
    1. trending_up 2,555 views
    2. thumb_up 4 thanks given
    3. group 3 followers
    1. forum 9 posts
    2. attach_file 0 attachments




 
Search this Thread

Subgraphs

  #1 (permalink)
tomlryde
south bend, indiana
 
Posts: 7 since Aug 2013
Thanks Given: 8
Thanks Received: 1

I am struggling a little to understand how sub graphs are used and work. Can anyone point me to a link on the Sierra web site or give me a quick explanation that will help me program and better utilize the language
Thanks
Tomlryde

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
ZombieSqueeze
Platforms and Indicators
NexusFi Journal Challenge - April 2024
Feedback and Announcements
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
Request for MACD with option to use different MAs for fa …
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Retail Trading As An Industry
61 thanks
NexusFi site changelog and issues/problem reporting
46 thanks
Battlestations: Show us your trading desks!
38 thanks
GFIs1 1 DAX trade per day journal
32 thanks
What percentage per day is possible? [Poll]
25 thanks

  #3 (permalink)
 vegasfoster 
las vegas
 
Experience: Intermediate
Platform: Sierra Chart
Broker: Velocity/IB
Trading: 6E
Posts: 1,145 since Feb 2010
Thanks Given: 304
Thanks Received: 844


You use a subgraph for any calculation you want to plot on the screen or any calculation where you want to be able to access prior bar(s) data. In the example below, for the high and low I didn't need to access prior bars calculations, so I simply defined them as floats. But for the Plot calculation, I wanted to be able to take the difference between the high and low for the current bar and high and low for the prior bar. To do that, I needed the PriorDifference calculation to be a subgraph so I could reference sc.Index-1.

 
Code
#include "sierrachart.h"

SCDLLName("Example") 
/*==========================================================================*/
SCSFExport scsf_Example(SCStudyInterfaceRef sc)
{
	SCSubgraphRef Plot	 	= sc.Subgraph[0];
	SCSubgraphRef PriorDifference    =  sc.Subgraph[1];

	// Set configuration variables
	if (sc.SetDefaults)
	{
		// Set the configuration and defaults
		sc.GraphName = "Example";
		sc.FreeDLL = 0;
		sc.AutoLoop = 1;
		
		Plot.Name = "Plot";
		Plot.DrawStyle = DRAWSTYLE_LINE;
		return;
	}
	
	// Do data processing
	
	float high = sc.High[sc.Index];
	float low = sc.Low[sc.Index];
	
	PriorDifference[sc.Index] = high-low;
	Plot[sc.Index] = PriorDifference[sc.Index] - PriorDiffference[sc.Index-1];

}

Reply With Quote
The following user says Thank You to vegasfoster for this post:
  #4 (permalink)
 
Nicolas11's Avatar
 Nicolas11 
near Paris, France
 
Experience: Beginner
Platform: -
Trading: -
Posts: 1,071 since Aug 2011
Thanks Given: 2,232
Thanks Received: 1,769

Hi,

Since PriorDifference is not plotted, I would personally use a https://www.sierrachart.com/index.php?l=doc/doc_ACS_ArraysAndLooping.htmlsc.Subgraph[0].Arrays[], perhaps more suited to this kind of background calculation. But everybody has his/her own programming style!

Nicolas

Sent from my mobile phone with Tapatalk

Visit my NexusFi Trade Journal Reply With Quote
The following user says Thank You to Nicolas11 for this post:
  #5 (permalink)
tomlryde
south bend, indiana
 
Posts: 7 since Aug 2013
Thanks Given: 8
Thanks Received: 1

Thank you very much, So a Subgraph is an array and every loop of the program puts the result in the subgraph relative the the Index value? I understand but do not quite understand how to best use them, or why some things need subraphs and some do not. Is there any sort of programmers manual available besides the Information on the Sierra web site? How many subgraphs does Sierra allow?

Thanks again

Tom

Reply With Quote
  #6 (permalink)
 
Nicolas11's Avatar
 Nicolas11 
near Paris, France
 
Experience: Beginner
Platform: -
Trading: -
Posts: 1,071 since Aug 2011
Thanks Given: 2,232
Thanks Received: 1,769

1. Yes, the Subgraph is an array. Its length is the same as the number of (price) bars on the screen.

The Subgraph basically corresponds to a one-line indicator. It stores the values of the said indicator, to be drawn on chart. For instance, if you want to display a stochastics, the subgraph will contain the value of the stochastics for each bar. If you need an average of the stochastics (to see stochastics crossover with signal line), you will use a second subgraph (in the same study) to store the value of this average.

Within the study, there are as many subgraphes as indicator "lines" to be displayed.

2. We could define as many as 60 subgraphes ( documentation) within the same study. It means that a given study (indicator) can display as many as 60 lines (or other types of display). If, for any reason, it is not enough, we can always create a second study.

3. The procedure scsf_xxx of the studies is called once per bar for past bars, then at each update interval for real-time bars ( documentation).

So, when scsf_xxx is called, it means that there is, either a new bar, or an update of the price of the current bar. So we have to modify the [sc.Index] value of the subgraph, since sc.Index is the index of the last bar.
We should not modify the values of the previous indexes of the subgraph (0, 1, ..., sc.Index-1), since they were calculated at previous calls of the function (except if we want an indicator which... repaints).

4. Have you looked at the " Step-By-Step Instructions to Create an Advanced Custom Study Function"? It contains a template of study.

You can also have a look within CustomStudies.cpp and Studies#.cpp which contains numerous examples of studies (more on that page).

5. I am not aware of the existence of a Sierra Chart "tutorial" or "manual" different from the on-line documentation.

6. If you are not very comfortable with programming, you can start with Sierra Chart's spreadsheets, which allow creating indicators on an Excel-like spreadsheets. Quite convenient!

Nicolas

Visit my NexusFi Trade Journal Reply With Quote
The following user says Thank You to Nicolas11 for this post:
  #7 (permalink)
tomlryde
south bend, indiana
 
Posts: 7 since Aug 2013
Thanks Given: 8
Thanks Received: 1

Thank you very much Nicholas,

I am a fairly proficient C Programer but C++ is a bit different and some of the C++ syntax gives me a hard time but I seem to muddle through and get things done. Thank you for the subgraph explanation. I have combined all the examples in the Sierra Library into one file so I can easily search for examples. I am heading out of town for a week to visit my daughter in California. I will post a few examples of what I am trying to do. Well, I am trying to write a routine that compares previews bars MACD lines with current bars MACD. It is Different from corss over. Cross over is an event. Comparing MACD previos bar with current would be to examine if MACD lines crosses and then re-crossed on next bar giving you a false entry. I am struggling to understand the MACD arrays and why I can not access previews bars

if (MovingAverageType.GetMovAvgType()==MOVAVGTYPE_EXPONENTIAL)
sc.DataStartIndex = 2;
else
sc.DataStartIndex = MACDLength.GetInt() + max(FastLength.GetInt(), SlowLength.GetInt());

int i = sc.Index;
sc.MACD(sc.BaseDataIn[InputData.GetInputDataIndex()], MACD, i, FastLength.GetInt(), SlowLength.GetInt(), MACDLength.GetInt(), MovingAverageType.GetInt());

MovAvgOfMACD[i] = MACD.Arrays[2][i];
MACDDiff[i] = MACD.Arrays[3][i];
RefLine[i] = 0;
sc.Subgraph[7][i] = MACD.Arrays[2][i-1];
sc.Subgraph[8][i] = MACD.Arrays[3][i-1];


Thanks so much for your help. Hope your trades are profitable

Tom

Reply With Quote
  #8 (permalink)
 
Nicolas11's Avatar
 Nicolas11 
near Paris, France
 
Experience: Beginner
Platform: -
Trading: -
Posts: 1,071 since Aug 2011
Thanks Given: 2,232
Thanks Received: 1,769

I am not sure if there is a question in your last message.

If you need help on the code, it would be better than you post a fully compilable code, and explain more explicitly what "does not work" (expected result vs actual result).

Nicolas

Visit my NexusFi Trade Journal Reply With Quote
The following user says Thank You to Nicolas11 for this post:
  #9 (permalink)
tomlryde
south bend, indiana
 
Posts: 7 since Aug 2013
Thanks Given: 8
Thanks Received: 1

I am at my wits end, simple MACD Cross over strategy
Below is the complete code I wrote for a simple MACD Cross over strategy (long) I then reversed the logic on the Buy sell entry trigger to have a short Strategy. It does not work and I am at my wits end ready to pull my hair our. I need your help to look it over and tell me where I went wrong on the short strategy. I hear how robust the Sierra Language is but it seems extremely temperamental. I have added one line of code to a strategy-- just on line and it completely changes the functionality of the code. It almost seems illogical. Someone please help me and clue me in.

Thanking you in advance

Tom

#include "sierrachart.h"



SCDLLName("9513help")

/********************************************************************
* *
* Tom MACD Long Combo *
* *
********************************************************************/

SCSFExport scsf_TomComboStrategyMACD(SCStudyInterfaceRef sc)
{
SCSubgraphRef BuyEntrySubgraph = sc.Subgraph[0];
SCSubgraphRef BuyExitSubgraph = sc.Subgraph[1];
SCSubgraphRef MACDData = sc.Subgraph[2];
SCSubgraphRef MovAvgOfMACD = sc.Subgraph[3];
SCSubgraphRef MACD = sc.Subgraph[4];
SCSubgraphRef MACDDiff = sc.Subgraph[5];




SCInputRef InputData = sc.Input[0];
SCInputRef FastLen = sc.Input[1];
SCInputRef SlowLen = sc.Input[2];
SCInputRef MACDLen = sc.Input[3];
SCInputRef MAType = sc.Input[4];
SCInputRef OffsetPercentInput = sc.Input[5];
SCInputRef Enabled = sc.Input[6];
SCInputRef TargetValue = sc.Input[9];
SCInputRef StopValue = sc.Input[10];
SCInputRef EntryLevelNum = sc.Input[11];




if (sc.SetDefaults)
{
// Set the configuration and defaults


sc.GraphName = "Tom MACD Strategy Long~";
sc.GraphRegion = 0; //Main chart region
sc.AutoLoop = 1;
sc.FreeDLL = 1;



BuyEntrySubgraph.Name = "Long Entry";
BuyEntrySubgraph.DrawStyle = DRAWSTYLE_ARROWUP;
BuyEntrySubgraph.PrimaryColor = RGB(19, 101, 236);
BuyEntrySubgraph.LineWidth = 2;
BuyEntrySubgraph.DrawZeros = false;


BuyExitSubgraph.Name = "Long Exit";
BuyExitSubgraph.DrawStyle = DRAWSTYLE_ARROWDOWN;
BuyExitSubgraph.PrimaryColor = RGB(140, 209, 242);
BuyExitSubgraph.LineWidth = 2;
BuyExitSubgraph.DrawZeros = false;

Enabled.Name = "Enabled";
Enabled.SetYesNo(0);

TargetValue.Name = "Target Value - Points - 1/4 point incriments";
TargetValue.SetFloat(20.0f);

StopValue.Name = "Stop Value - Points - 1/4 point incriments";
StopValue.SetFloat(1.25f);

/************MACD*****************/

InputData.Name = "Input Data";
InputData.SetInputDataIndex(SC_LAST);

FastLen.Name ="Fast Moving Average Length";
FastLen.SetInt(5);
FastLen.SetIntLimits(1,MAX_STUDY_LENGTH);

SlowLen.Name = "Slow Moving Average Length";
SlowLen.SetInt(30);
SlowLen.SetIntLimits(1,MAX_STUDY_LENGTH);

MACDLen.Name = "MACD Moving Average Length";
MACDLen.SetInt(9);
MACDLen.SetIntLimits(1,MAX_STUDY_LENGTH);

MAType.Name = "Moving Average Type";
MAType.SetMovAvgType(MOVAVGTYPE_EXPONENTIAL);

EntryLevelNum.Name = "Entry Level Below -0- Line"; //inactive in this code
EntryLevelNum.SetFloat(-1.0f);



sc.Input[15].Name = "Contracts to Buy";
sc.Input[15].SetInt(1);
sc.Input[15].SetIntLimits(1, 100);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// order stuff
sc.AllowMultipleEntriesInSameDirection = false;
sc.SupportReversals = false;
sc.SendOrdersToTradeService = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
sc.SupportAttachedOrdersForTrading = false;
sc.CancelAllOrdersOnEntriesAndReversals= false;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = false;
sc.AllowOnlyOneTradePerBar = true;
sc.MaintainTradeStatisticsAndTradesData = true;

return;
}

int Order_qty = sc.Input[15].GetInt();
sc.MaximumPositionAllowed = Order_qty;

//Do Study calculations ~~~~~~~~~~~~~~~

// MACD

sc.MACD(sc.BaseDataIn[InputData.GetInputDataIndex()], MACDData, sc.Index, FastLen.GetInt(), SlowLen.GetInt(), MACDLen.GetInt(), MAType.GetInt());
MovAvgOfMACD[sc.Index] = MACD.Arrays[2][sc.Index];
MACDDiff[sc.Index] = MACD.Arrays[3][sc.Index];
float LowerLine = sc.Input[11].GetFloat();




if (!Enabled.GetYesNo())
return;

SCFloatArrayRef Last = sc.Close;

// Get the Internal Position data to be used for position exit processing.
s_SCPositionData InternalPositionData;
sc.GetInternalPosition(InternalPositionData) ;
float LastTradePrice = sc.Close[sc.Index];
// Create an s_SCNewOrder object.
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = Order_qty; //Total contracts
NewOrder.OrderType = SCT_MARKET;



int cnt=0;
int i = sc.Index;
int Result;
int status = sc.GetBarHasClosedStatus(sc.UpdateStartIndex);



if ((sc.CrossOver( MACDData, MACDData.Arrays[2]) == CROSS_FROM_BOTTOM) && (status == BHCS_BAR_HAS_CLOSED))
{Result = sc.BuyEntry(NewOrder); // Buy Long
if (Result > 0)
BuyEntrySubgraph[sc.Index] = sc.Low[sc.Index];
}
else { // exit routine
if (InternalPositionData.PositionQuantity > 0 &&
((LastTradePrice <= InternalPositionData.AveragePrice - StopValue.GetFloat()) ||
(sc.CrossOver( MACDData, MACDData.Arrays[2]) == CROSS_FROM_TOP)
))

{Result = sc.BuyExit(NewOrder); //Sell Long
if(Result>0)
BuyExitSubgraph[sc.Index] = sc.High[sc.Index];

}
}

/*********************************** End Long Order Entry/Exit ****************/

}



/********************************************************************
* *
* Tom MACD short Combo *
* *
********************************************************************/

SCSFExport scsf_TomComboShortStrategyMACD(SCStudyInterfaceRef sc)
{
SCSubgraphRef SellEntrySubgraph = sc.Subgraph[0];
SCSubgraphRef SellExitSubgraph = sc.Subgraph[1];
SCSubgraphRef MACDData = sc.Subgraph[2];
SCSubgraphRef MovAvgOfMACD = sc.Subgraph[3];
SCSubgraphRef MACD = sc.Subgraph[4];
SCSubgraphRef MACDDiff = sc.Subgraph[5];




SCInputRef InputData = sc.Input[0];
SCInputRef FastLen = sc.Input[1];
SCInputRef SlowLen = sc.Input[2];
SCInputRef MACDLen = sc.Input[3];
SCInputRef MAType = sc.Input[4];
SCInputRef OffsetPercentInput = sc.Input[5];
SCInputRef Enabled = sc.Input[6];
SCInputRef TargetValue = sc.Input[9];
SCInputRef StopValue = sc.Input[10];
SCInputRef EntryLevelNum = sc.Input[11];




if (sc.SetDefaults)
{
// Set the configuration and defaults


sc.GraphName = "Tom MACD Short Strategy ";
sc.GraphRegion = 0; //Main chart region
sc.AutoLoop = 1;
sc.FreeDLL = 1;



SellEntrySubgraph.Name = "Long Entry";
SellEntrySubgraph.DrawStyle = DRAWSTYLE_ARROWUP;
SellEntrySubgraph.PrimaryColor = RGB(19, 101, 236);
SellEntrySubgraph.LineWidth = 2;
SellEntrySubgraph.DrawZeros = false;


SellExitSubgraph.Name = "Long Exit";
SellExitSubgraph.DrawStyle = DRAWSTYLE_ARROWDOWN;
SellExitSubgraph.PrimaryColor = RGB(140, 209, 242);
SellExitSubgraph.LineWidth = 2;
SellExitSubgraph.DrawZeros = false;

Enabled.Name = "Enabled";
Enabled.SetYesNo(0);

TargetValue.Name = "Target Value - Points - 1/4 point incriments";
TargetValue.SetFloat(20.0f);

StopValue.Name = "Stop Value - Points - 1/4 point incriments";
StopValue.SetFloat(1.25f);

/************MACD*****************/

InputData.Name = "Input Data";
InputData.SetInputDataIndex(SC_LAST);

FastLen.Name ="Fast Moving Average Length";
FastLen.SetInt(5);
FastLen.SetIntLimits(1,MAX_STUDY_LENGTH);

SlowLen.Name = "Slow Moving Average Length";
SlowLen.SetInt(30);
SlowLen.SetIntLimits(1,MAX_STUDY_LENGTH);

MACDLen.Name = "MACD Moving Average Length";
MACDLen.SetInt(9);
MACDLen.SetIntLimits(1,MAX_STUDY_LENGTH);

MAType.Name = "Moving Average Type";
MAType.SetMovAvgType(MOVAVGTYPE_EXPONENTIAL);

EntryLevelNum.Name = "Entry Level Below -0- Line"; //inactive in this code
EntryLevelNum.SetFloat(-1.0f);



sc.Input[15].Name = "Contracts to Sell";
sc.Input[15].SetInt(1);
sc.Input[15].SetIntLimits(1, 100);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// order stuff
sc.AllowMultipleEntriesInSameDirection = false;
sc.SupportReversals = false;
sc.SendOrdersToTradeService = false;
sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
sc.SupportAttachedOrdersForTrading = false;
sc.CancelAllOrdersOnEntriesAndReversals= false;
sc.AllowEntryWithWorkingOrders = false;
sc.CancelAllWorkingOrdersOnExit = false;
sc.AllowOnlyOneTradePerBar = true;
sc.MaintainTradeStatisticsAndTradesData = true;

return;
}

int Order_qty = sc.Input[15].GetInt();
sc.MaximumPositionAllowed = Order_qty;

//Do Study calculations ~~~~~~~~~~~~~~~

// MACD

sc.MACD(sc.BaseDataIn[InputData.GetInputDataIndex()], MACDData, sc.Index, FastLen.GetInt(), SlowLen.GetInt(), MACDLen.GetInt(), MAType.GetInt());
MovAvgOfMACD[sc.Index] = MACD.Arrays[2][sc.Index];
MACDDiff[sc.Index] = MACD.Arrays[3][sc.Index];
float LowerLine = sc.Input[11].GetFloat();




if (!Enabled.GetYesNo())
return;

SCFloatArrayRef Last = sc.Close;

// Get the Internal Position data to be used for position exit processing.
s_SCPositionData InternalPositionData;
sc.GetInternalPosition(InternalPositionData) ;
float LastTradePrice = sc.Close[sc.Index];
// Create an s_SCNewOrder object.
s_SCNewOrder NewOrder;
NewOrder.OrderQuantity = Order_qty; //Total contracts
NewOrder.OrderType = SCT_MARKET;



int cnt=0;
int i = sc.Index;
int Result;
int status = sc.GetBarHasClosedStatus(sc.UpdateStartIndex);



if ((sc.CrossOver( MACDData, MACDData.Arrays[2]) == CROSS_FROM_TOP) && (status == BHCS_BAR_HAS_CLOSED))
{Result = sc.SellEntry(NewOrder); // Sell Long
if (Result > 0)
SellEntrySubgraph[sc.Index] = sc.High[sc.Index];
}
else { // exit routine
if (InternalPositionData.PositionQuantity > 0 &&
((LastTradePrice <= InternalPositionData.AveragePrice + StopValue.GetFloat()) ||
(sc.CrossOver( MACDData, MACDData.Arrays[2]) == CROSS_FROM_BOTTOM)
))

{Result = sc.SellExit(NewOrder); //Sell Long
if(Result>0)
SellExitSubgraph[sc.Index] = sc.Low[sc.Index];

}
}

/*********************************** End Short Order Entry/Exit ****************/

}

Reply With Quote
  #10 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,320 since Jun 2009
Thanks Given: 33,142
Thanks Received: 101,475


@tomlryde,

1) Wrap long blocks of code in the [code] bbcode so someone doesn't have to scroll forever in your post.
2) You already have an existing thread on this, please do not cross-post/duplicate.



Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote





Last Updated on September 5, 2013


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts