NexusFi: Find Your Edge


Home Menu

 





I am at my witts end, simple MACD Cross over strategy


Discussion in Sierra Chart

Updated
      Top Posters
    1. looks_one vegasfoster with 4 posts (3 thanks)
    2. looks_two tomlryde with 3 posts (1 thanks)
    3. looks_3 wldman with 3 posts (3 thanks)
    4. looks_4 Cloudy with 1 posts (0 thanks)
      Best Posters
    1. looks_one sandptrader with 2 thanks per post
    2. looks_two wldman with 1 thanks per post
    3. looks_3 vegasfoster with 0.8 thanks per post
    4. looks_4 tomlryde with 0.3 thanks per post
    1. trending_up 4,437 views
    2. thumb_up 9 thanks given
    3. group 5 followers
    1. forum 11 posts
    2. attach_file 0 attachments




 
Search this Thread

I am at my witts end, simple MACD Cross over strategy

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

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 witts end ready to pull my hair our. I need your help to look it over and tell me whereI went wrong on the sgort strategy. I hear how robust the Sierra Language is but it seems extremely tempermental. 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

Can you help answer these questions
from other members on NexusFi?
NexusFi Journal Challenge - April 2024
Feedback and Announcements
ZombieSqueeze
Platforms and Indicators
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
58 thanks
Battlestations: Show us your trading desks!
55 thanks
NexusFi site changelog and issues/problem reporting
48 thanks
What percentage per day is possible? [Poll]
31 thanks
GFIs1 1 DAX trade per day journal
29 thanks

  #3 (permalink)
 
wldman's Avatar
 wldman 
Chicago Illinois USA
Legendary Market Wizard
 
Experience: Advanced
Broker: IB, ToS
Trading: /ES, US Equities/Options
Frequency: Several times daily
Duration: Hours
Posts: 3,502 since Aug 2011
Thanks Given: 2,040
Thanks Received: 9,475


I know exactly how you are feeling. But the answer is as simple as this. If the idiom wits' end, has just one t in it and you type two t's, a human knows exactly what you mean. However one character "off" in your script can ruin the entire script.

Get someone who uses that like @vegasfoster involved.

Second, if you have an editor on your platform, can you "find all, change all"? Go back and use that to retry the language you changed to create the sell side. Probably missing a single character or something real simple.

No worries though man, someone withe a fresh set of eyes will see it right away.

Visit my NexusFi Trade Journal Reply With Quote
The following 2 users say Thank You to wldman for this post:
  #4 (permalink)
 sandptrader 
Valdosta, GA. U.S.A
 
Experience: Advanced
Platform: Sierra , TOS
Trading: 6E, ES, CL, GC
Posts: 498 since Sep 2010
Thanks Given: 1,881
Thanks Received: 472

@ wildman.....withe
i could not help myself, seems this was intentional.
maybe one day i need to start learning how to program.
@ tomlryde.....i hope you can get this working like you want man.

Reply With Quote
The following 2 users say Thank You to sandptrader for this post:
  #5 (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

It looks like you have two different strategies in the same file, is that correct? Cuz I'm pretty sure you can't do that. For example, you can't define two different subgraph[0] etc. in the same file. Create separate sections for long and short entry/exit criteria, but create one contiguous set of subgraphs, inputs, and configuration defaults.

Just a note on long/short entries and exits, if you are reversing, then you don't need to both exit the open position and enter the new position, just enter the new position and the open position will be closed automatically.

Computers do exactly what you tell them to do, so even a single character can screw up any program, regardless of the platform. The easiest way to code is to start with existing code that that's similar to what you want to do and then change it one line or one section at a time. Edit, compile, test, edit, compile, test, etc. Then you will know exactly where any problems lye.

Lemme know.

Reply With Quote
The following 2 users say Thank You to vegasfoster for this post:
  #6 (permalink)
 
wldman's Avatar
 wldman 
Chicago Illinois USA
Legendary Market Wizard
 
Experience: Advanced
Broker: IB, ToS
Trading: /ES, US Equities/Options
Frequency: Several times daily
Duration: Hours
Posts: 3,502 since Aug 2011
Thanks Given: 2,040
Thanks Received: 9,475

@vegasfoster

I may need a place to stay in South Bend or some game tickets. Pays to help a brother out.

DB

Visit my NexusFi Trade Journal Reply With Quote
  #7 (permalink)
tomlryde
south bend, indiana
 
Posts: 7 since Aug 2013
Thanks Given: 8
Thanks Received: 1

Thanks for your reply. I failed to mention that I have two strategies posed above. the first is a long cross over which works just fine. the second is the exact same strategy only I reversed the logic on the cross over and it does not work. Listed is the complete code so anyone who wants to can load it and compile it to see what happens. So I am really frustrated.
Thanks again

Tom

Reply With Quote
The following user says Thank You to tomlryde for this post:
  #8 (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

Ok, I can take a look at it tomorrow.

Reply With Quote
  #9 (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


wldman View Post
@vegasfoster

I may need a place to stay in South Bend or some game tickets. Pays to help a brother out.

DB

Don't tell me, you dined and dashed again and have to lamb it. I can't protect you from the man anymore wildman. I just can't.

Reply With Quote
  #10 (permalink)
 
wldman's Avatar
 wldman 
Chicago Illinois USA
Legendary Market Wizard
 
Experience: Advanced
Broker: IB, ToS
Trading: /ES, US Equities/Options
Frequency: Several times daily
Duration: Hours
Posts: 3,502 since Aug 2011
Thanks Given: 2,040
Thanks Received: 9,475


@sandptrader

Way way back when the days of formal education where less of a distant fog, I had an English professor that inspired me to use items like the "withe" in my writing. I forgot the term, but doing that which I was using to make a point in context to reinforce and add levity, has a name.

You, being the first to notice or the first to point it out separate yourself from the crowd. You have a high level of acuity, that should be a great advantage in many things.

@vegasfoster is a crafty guy, although he does still live in his moms basement, I bet he can whip this issue in no time...come on vegas!

Visit my NexusFi Trade Journal Reply With Quote
The following user says Thank You to wldman for this post:





Last Updated on September 8, 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