Trading off a Spread Indicator

Questions about MultiCharts and user contributed studies.
CharlieChalk
Posts: 7
Joined: 09 Feb 2015

Trading off a Spread Indicator

Postby CharlieChalk » 24 Mar 2015

Hello guys,

i am having major problems trying to get some code of mine working. I have used the print statment to try and find what the cause of this is but to no avail.

I am looking to buy a contract when the spread indicator(which i have turned into a dollar value) moves above or below a 30 period MA. the only issue is that the system keeps buying and selling every bar. I have applied this to daily data with Corn on data1, wheat data2 and corn data3.

I'd massively apprecaite some help in trying to get to the bottom of this. I've attached the code and snip of the code applied to chart .

Regards,

Charles

inputs:
AntiNegativeNum(100000),
BigPOintValue.SeriesA(100),
BigPOintValue.SeriesB(100),
NumContracta.SeriesA(1),
NumContractsSeriesB(1),
MaxADX( 20 ),
TriggerLength(30);


variables:

MATrigger(0),
SpreadATR(0),
spread.h(0),
spread.l(0),
spread.o(0),
spread.c(0);



MATrigger = Average(spread.c,TriggerLength);

spread.h = AntiNegativeNum + ((high data2 * NumContracta.SeriesA)* BigPOintValue.SeriesA) - ((high data3 * NumContractsSeriesB) * BigPOintValue.SeriesB);
spread.l = AntiNegativeNum + ((low data2 * NumContracta.SeriesA) * BigPOintValue.SeriesA) - ((low data3 * NumContractsSeriesB) * BigPOintValue.SeriesB);
spread.o = AntiNegativeNum + ((open data2 * NumContracta.SeriesA) * BigPOintValue.SeriesA) - ((open data3 * NumContractsSeriesB) * BigPOintValue.SeriesB);
spread.c = AntiNegativeNum + ((close data2 * NumContracta.SeriesA) * BigPOintValue.SeriesA) - ((close data3 * NumContractsSeriesB) * BigPOintValue.SeriesB);



{Long Entry}

if spread.c > MATrigger then
begin
Buy NumContracta.SeriesA Contracts this bar at close ;

end ;


{Long Exit}


if MarketPosition = 1 and spread.c < MATrigger then sell this bar at Close;


{Short Entry}

if spread.c > MATrigger then
begin
SellShort NumContracta.SeriesA Contracts this bar at close ;

end;


{Short Exit}


if MarketPosition = -1 and spread.c > MATrigger then BuyToCover this bar at close;


print (Date, "Date", spread.c, "Spread.c", EntryPrice, "EntryPrice", MATrigger, "MATrigger");
Attachments
MC Spread Snip.png
(146.59 KiB) Downloaded 409 times
MC Spread.pla
(5.03 KiB) Downloaded 465 times

User avatar
TJ
Posts: 7743
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: Trading off a Spread Indicator

Postby TJ » 24 Mar 2015

Hello guys,
i am having major problems trying to get some code of mine working. I have used the print statment to try and find what the cause of this is but to no avail.
I am looking to buy a contract when the spread indicator(which i have turned into a dollar value) moves above or below a 30 period MA. the only issue is that the system keeps buying and selling every bar. I have applied this to daily data with Corn on data1, wheat data2 and corn data3.
I'd massively apprecaite some help in trying to get to the bottom of this. I've attached the code and snip of the code applied to chart .
Regards,
Charles
inputs:
AntiNegativeNum(100000),
BigPOintValue.SeriesA(100),
BigPOintValue.SeriesB(100),
NumContracta.SeriesA(1),
NumContractsSeriesB(1),
MaxADX( 20 ),
TriggerLength(30);
variables:
MATrigger(0),
SpreadATR(0),
spread.h(0),
spread.l(0),
spread.o(0),
spread.c(0);
MATrigger = Average(spread.c,TriggerLength);
spread.h = AntiNegativeNum + ((high data2 * NumContracta.SeriesA)* BigPOintValue.SeriesA) - ((high data3 * NumContractsSeriesB) * BigPOintValue.SeriesB);
spread.l = AntiNegativeNum + ((low data2 * NumContracta.SeriesA) * BigPOintValue.SeriesA) - ((low data3 * NumContractsSeriesB) * BigPOintValue.SeriesB);
spread.o = AntiNegativeNum + ((open data2 * NumContracta.SeriesA) * BigPOintValue.SeriesA) - ((open data3 * NumContractsSeriesB) * BigPOintValue.SeriesB);
spread.c = AntiNegativeNum + ((close data2 * NumContracta.SeriesA) * BigPOintValue.SeriesA) - ((close data3 * NumContractsSeriesB) * BigPOintValue.SeriesB);
{Long Entry}
if spread.c > MATrigger then
begin
Buy NumContracta.SeriesA Contracts this bar at close ;
end ;
{Long Exit}
if MarketPosition = 1 and spread.c < MATrigger then sell this bar at Close;
{Short Entry}
if spread.c > MATrigger then
begin
SellShort NumContracta.SeriesA Contracts this bar at close ;
end;
{Short Exit}
if MarketPosition = -1 and spread.c > MATrigger then BuyToCover this bar at close;
print (Date, "Date", spread.c, "Spread.c", EntryPrice, "EntryPrice", MATrigger, "MATrigger");
[FAQ] How to Post Codes (that people can read)
viewtopic.php?f=16&t=11713

User avatar
TJ
Posts: 7743
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: Trading off a Spread Indicator

Postby TJ » 24 Mar 2015

Might not be directly related to your problem,
posting this just in case you are not aware...

see post #7
[FAQ] This Bar on Close
viewtopic.php?f=16&t=10811


Return to “MultiCharts”