Initial Trade when strategy starts

If you need an Indicator or Signal developed or translated from other language, please post all Indicator development REQUESTS to this section here.

Moderator: admin

Initial Trade when strategy starts

Postby robbieyoung » Thu Nov 08, 2018 7:55 am

I have attached a strategy which will buy when the MA changes direction and moves up and sell when the MA changes direction and moves down.

To ensure that strategy only trades when the MA changes direction it is currently coded such that when the strategy is first started it ignores the first direction of the MA and waits for the direction to change.

This way it won't buy in the middle of a long MA up sequence, instead it waits for the start of the next MA down sequence.

Problems with this arise and trades can be missed when i restart TS and the strategy after each weekend.

I would like, if at all possible, to be able to specify the allowed side of the first trade to "Buy" or "Sell" and then once the first trade (or any trade) is open the allowed side is set to "Both".

Thanks in advance for any help you might give.
Last edited by robbieyoung on Fri Nov 09, 2018 6:47 pm, edited 1 time in total.
robbieyoung
 
Posts: 32
Joined: Tue Apr 21, 2015 3:12 am

Re: Initial Trade when strategy starts

Postby Apprentice » Thu Nov 08, 2018 4:22 pm

What if you use.

Code: Select all
MAUp =
(MovingAverage.DATA[getClosedPeriod(MovingAverage.DATA, MovingAverage.DATA:size() - 1)] > MovingAverage.DATA[getClosedPeriod(MovingAverage.DATA, MovingAverage.DATA:size() - 2)]
and
MovingAverage.DATA[getClosedPeriod(MovingAverage.DATA, MovingAverage.DATA:size() - 2)] <= MovingAverage.DATA[getClosedPeriod(MovingAverage.DATA, MovingAverage.DATA:size() - 3)]
);


and

Code: Select all
MADown =
( MovingAverage.DATA[getClosedPeriod(MovingAverage.DATA, MovingAverage.DATA:size() - 1)] < MovingAverage.DATA[getClosedPeriod(MovingAverage.DATA, MovingAverage.DATA:size() - 2)]
and
MovingAverage.DATA[getClosedPeriod(MovingAverage.DATA, MovingAverage.DATA:size() - 2)] >= MovingAverage.DATA[getClosedPeriod(MovingAverage.DATA, MovingAverage.DATA:size() - 4)]
);
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36435
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Initial Trade when strategy starts

Postby robbieyoung » Fri Nov 09, 2018 8:17 am

That may work for this particular strategy but I have many other more complicated strategies based on 2 MA's etc which suffer from the same issue.

I also wouldn't know how to properly incorporate this into the strategy and which pieces of code to remove to deactivate the previous functionality.

It would be simpler and more transferable to be able to specify, for example, the Allowed side as Buy or Sell if there are no trades currently open then Allowed side Both when a trade exists.

First Trade Allowed Side: Buy / Sell

If no trade exists then Allowed side = Buy / Sell

If trade exists then Allowed side = Both

Is it even possible to change the allowed side mid way through a strategy?
robbieyoung
 
Posts: 32
Joined: Tue Apr 21, 2015 3:12 am

Re: Initial Trade when strategy starts

Postby Apprentice » Fri Nov 09, 2018 4:17 pm

Is it even possible to change the allowed side mid way through a strategy?

Sure.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36435
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Initial Trade when strategy starts

Postby robbieyoung » Mon Nov 12, 2018 7:48 am

I altered the strategy to include:

--If trade exists then AllowedSide=Both
if countPositions(sInstrument) > 0 then
mAllowedSide = "Both"
end

But trades are created before they should be.

Any idea why?
Attachments
MA Up or Down.lua
(20.35 KiB) Downloaded 448 times
robbieyoung
 
Posts: 32
Joined: Tue Apr 21, 2015 3:12 am

Re: Initial Trade when strategy starts

Postby Apprentice » Mon Nov 12, 2018 9:59 am

Try to set MAUp and MADown on ExtUpdate start,
or define them as local within ExtUpdate.

You will need some logic to prevent multiple positions for the same signal.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36435
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Initial Trade when strategy starts

Postby robbieyoung » Mon Nov 12, 2018 10:18 am

Thanks for your help but I don't have enough knowledge of lua to make these changes.
I am happy to pay for these changes to be made, how would I go about this?
robbieyoung
 
Posts: 32
Joined: Tue Apr 21, 2015 3:12 am

Re: Initial Trade when strategy starts

Postby Apprentice » Mon Nov 12, 2018 5:23 pm

If requested via forum you can have this service for free.
Simple describe your idea and post your request here.
Will write this for you and post it back on the forum.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36435
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia


Return to Indicator and Signal Requests

Who is online

Users browsing this forum: Bing [Bot] and 14 guests