Automatically tying “fixed shares/contracts” or “cash per trade” position sizing to SetStopPosition or SetStopContract

Questions about MultiCharts and user contributed studies.
Unreal.2K7
Posts: 1
Joined: 27 Feb 2023

Automatically tying “fixed shares/contracts” or “cash per trade” position sizing to SetStopPosition or SetStopContract

Postby Unreal.2K7 » 27 Feb 2023

Hello everyone.

I am trying to codify a strategy which can be configured to work with a “fixed shares/contracts” or “cash per trade” position sizing basis.
This can easily be set in the strategy properties:

Image

Additionally, I want to enforce an association of the stop/take profit calculation method with the position sizing method. In other words:

  • If the strategy trade size is set to “Fixed Shares/Contracts” i wish to execute the SetStopContracts instruction in my code
  • If the strategy trade size is set to “Cash per Trade” i wish to execute the SetStopPosition instruction in my code


I initially envisioned there could be two ways to go about this:

  1. Forego using the setting in the strategy property and add a Boolean input to the strategy to manually specify the trade size and stop/tp calculation method (per contract or per whole position).

    Problem: the order generation commands only allow to manually specify the number of contracts/shares. As such, there is no way to programmatically specify a trade order of a fixed cash amount.
    The only way around this could be to manually calculate the trade size (in contracts) given a fixed cash amount and replicating the calculation that multicharts performs using the formula as indicated in this thread: viewtopic.php?f=1&t=51649&p=131139&hili ... de#p130701

    Code: Select all

    Number of Contracts = Cash per trade / (Big Point Value for the symbol * currency conversion exchange rate * order execution price)
    That is, if it’s even possible to get the currency conversion exchange rate programmatically.
    And even if it is, there could be problems where the number of contracts calculated by multicharts differs from the value calculated by the script due to a different order execution price in case when using market orders: the script would evaluate this in the previous bar, while multicharts would evalulate this at the opening of the next bar, during the order creation pipeline. Or so I suppose. Correct me if I’m wrong.


    In alternative:
  2. Leave the configuration at the strategy properties level and have some code in the strategy that somehow obtains an information about which option is enabled, and then executes SetStopContracts or SetStopPosition accordingly.
    Problem: in the powerlanguage manual, I couldn’t find an instruction to get this information.
    Besides, even if available, as indicated here ( viewtopic.php?f=16&t=10811&p=53386&hili ... ion#p53386 ), setting a global exit inside a condition is advised against.

Now, this does not completely prevent this strategy to be used with this functionality. Afterall, the user can still set the option he wants in the strategy properties and then manually set a boolean input to execute SetStopContracts or SetStopPosition accordingly, but I was looking for a way to have the code do this automatically, so there is no margin for user error.

By the way, I’m on MultiCharts64 Version 14.0 Release (Build 24407).

Thank you for your input on this.
Best regards.

Return to “MultiCharts”